Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/plot/plot3d/implicit_plot3d.py
4038 views
1
"""
2
Implicit Plots
3
"""
4
5
from implicit_surface import ImplicitSurface
6
7
def implicit_plot3d(f, xrange, yrange, zrange, **kwds):
8
r"""
9
Plots an isosurface of a function.
10
11
INPUT:
12
13
- ``f`` - function
14
15
- ``xrange`` - a 2-tuple (x_min, x_max) or a 3-tuple (x, x_min, x_max)
16
17
- ``yrange`` - a 2-tuple (y_min, y_may) or a 3-tuple (y, y_min, y_may)
18
19
- ``zrange`` - a 2-tuple (z_min, z_maz) or a 3-tuple (z, z_min, z_maz)
20
21
- ``plot_points`` - (default: "automatic", which is 50) the number of
22
function evaluations in each direction. (The number of cubes in the
23
marching cubes algorithm will be one less than this). Can be a triple of
24
integers, to specify a different resolution in each of x,y,z.
25
26
- ``contour`` - (default: 0) plot the isosurface f(x,y,z)==contour. Can be a
27
list, in which case multiple contours are plotted.
28
29
- ``region`` - (default: None) If region is given, it must be a Python
30
callable. Only segments of the surface where region(x,y,z) returns a
31
number >0 will be included in the plot. (Note that returning a Python
32
boolean is acceptable, since True == 1 and False == 0).
33
34
EXAMPLES::
35
36
sage: var('x,y,z')
37
(x, y, z)
38
39
A simple sphere::
40
41
sage: implicit_plot3d(x^2+y^2+z^2==4, (x, -3, 3), (y, -3,3), (z, -3,3))
42
43
A nested set of spheres with a hole cut out::
44
45
sage: implicit_plot3d((x^2 + y^2 + z^2), (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=60, contour=[1,3,5], \
46
... region=lambda x,y,z: x<=0.2 or y>=0.2 or z<=0.2).show(viewer='tachyon')
47
48
A very pretty example from http://iat.ubalt.edu/summers/math/platsol.htm::
49
50
sage: T = RDF(golden_ratio)
51
sage: p = 2 - (cos(x + T*y) + cos(x - T*y) + cos(y + T*z) + cos(y - T*z) + cos(z - T*x) + cos(z + T*x))
52
sage: r = 4.77
53
sage: implicit_plot3d(p, (x, -r, r), (y, -r, r), (z, -r, r), plot_points=40).show(viewer='tachyon')
54
55
As I write this (but probably not as you read it), it's almost Valentine's
56
day, so let's try a heart (from http://mathworld.wolfram.com/HeartSurface.html)
57
58
::
59
60
sage: p = (x^2+9/4*y^2+z^2-1)^3-x^2*z^3-9/(80)*y^2*z^3
61
sage: r = 1.5
62
sage: implicit_plot3d(p, (x, -r,r), (y, -r,r), (z, -r,r), plot_points=80, color='red', smooth=False).show(viewer='tachyon')
63
64
The same examples also work with the default Jmol viewer; for example::
65
66
sage: T = RDF(golden_ratio)
67
sage: p = 2 - (cos(x + T*y) + cos(x - T*y) + cos(y + T*z) + cos(y - T*z) + cos(z - T*x) + cos(z + T*x))
68
sage: r = 4.77
69
sage: implicit_plot3d(p, (x, -r, r), (y, -r, r), (z, -r, r), plot_points=40).show()
70
71
Here we use smooth=True with a Tachyon graph::
72
73
sage: implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), contour=4, smooth=True)
74
75
We explicitly specify a gradient function (in conjunction with smooth=True)
76
and invert the normals::
77
78
sage: gx = lambda x, y, z: -(2*x + y^2 + z^2)
79
sage: gy = lambda x, y, z: -(x^2 + 2*y + z^2)
80
sage: gz = lambda x, y, z: -(x^2 + y^2 + 2*z)
81
sage: implicit_plot3d(x^2+y^2+z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), contour=4, \
82
... plot_points=40, smooth=True, gradient=(gx, gy, gz)).show(viewer='tachyon')
83
84
A graph of two metaballs interacting with each other::
85
86
sage: def metaball(x0, y0, z0): return 1 / ((x-x0)^2 + (y-y0)^2 + (z-z0)^2)
87
sage: implicit_plot3d(metaball(-0.6, 0, 0) + metaball(0.6, 0, 0), (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=60, contour=2)
88
89
MANY MORE EXAMPLES:
90
91
A kind of saddle::
92
93
sage: implicit_plot3d(x^3 + y^2 - z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=60, contour=0)
94
95
A smooth surface with six radial openings::
96
97
sage: implicit_plot3d(-(cos(x) + cos(y) + cos(z)), (x, -4, 4), (y, -4, 4), (z, -4, 4))
98
99
A cube composed of eight conjoined blobs::
100
101
sage: implicit_plot3d(x^2 + y ^2 + z^2 +cos(4*x)+cos(4*y)+cos(4*z)-0.2, (x, -2, 2), (y, -2, 2), (z, -2, 2))
102
103
A variation of the blob cube featuring heterogeneously sized blobs::
104
105
sage: implicit_plot3d(x^2 + y ^2 + z^2 +sin(4*x) + sin(4*y) + sin(4*z) -1, (x, -2, 2), (y, -2, 2), (z, -2, 2))
106
107
A klein bottle::
108
109
sage: implicit_plot3d((x^2+y^2+z^2+2*y-1)*((x^2+y^2+z^2-2*y-1)^2-8*z^2)+16*x*z*(x^2+y^2+z^2-2*y-1), (x, -3, 3), (y, -3.1, 3.1), (z, -4, 4))
110
111
A lemniscate::
112
113
sage: implicit_plot3d(4*x^2*(x^2+y^2+z^2+z)+y^2*(y^2+z^2-1), (x, -0.5, 0.5), (y, -1, 1), (z, -1, 1))
114
115
Drope::
116
117
sage: implicit_plot3d(z - 4*x*exp(-x^2-y^2), (x, -2, 2), (y, -2, 2), (z, -1.7, 1.7))
118
119
A cube with a circular aperture on each face::
120
121
sage: implicit_plot3d(((1/2.3)^2 *(x^2 + y^2 + z^2))^-6 + ( (1/2)^8 * (x^8 + y^8 + z^8) )^6 -1, (x, -2, 2), (y, -2, 2), (z, -2, 2))
122
123
A simple hyperbolic surface::
124
125
sage: implicit_plot3d(x*x + y - z*z, (x, -1, 1), (y, -1, 1), (z, -1, 1))
126
127
A hyperboloid::
128
129
sage: implicit_plot3d(x^2 + y^2 - z^2 -0.3, (x, -2, 2), (y, -2, 2), (z, -1.8, 1.8))
130
131
Duplin cycloid::
132
133
sage: implicit_plot3d((2^2 - 0^2 - (2 + 2.1)^2) * (2^2 - 0^2 - (2 - 2.1)^2)*(x^4+y^4+z^4)+ 2*((2^2 - 0^2 - (2 + 2.1)^2 )*(2^2 - 0^2 - (2 - 2.1)^2)* (x^2 * y^2+x^2 * z^2+y^2 * z^2))+2* 2^2 *((-0^2-2^2+2^2+2.1^2)* (2 *x *2+2* y* 0-2^2)-4*0 *2.1^2 *y)*(x^2+y^2+z^2)+ 4 * 2^4 * (2 *x+0 *y)* (-2^2+0 * y+2 * x)+4* 2^4 * 2.1^2 * y^2+2^8, (x, -2, 2.2), (y, -2, 2), (z, -1.3, 1.3))
134
135
Sinus::
136
137
sage: implicit_plot3d(sin(pi*((x)^2+(y)^2))/2 +z, (x, -1, 1), (y, -1, 1), (z, -1, 1))
138
139
A torus::
140
141
sage: implicit_plot3d((sqrt(x*x+y*y)-3)^2 + z*z - 1, (x, -4, 4), (y, -4, 4), (z, -1, 1))
142
143
An octahedron::
144
145
sage: implicit_plot3d(abs(x)+abs(y)+abs(z) - 1, (x, -1, 1), (y, -1, 1), (z, -1, 1))
146
147
A cube::
148
149
sage: implicit_plot3d(x^100 + y^100 + z^100 -1, (x, -2, 2), (y, -2, 2), (z, -2, 2))
150
151
Toupie::
152
153
sage: implicit_plot3d((sqrt(x*x+y*y)-3)^3 + z*z - 1, (x, -4, 4), (y, -4, 4), (z, -6, 6))
154
155
A cube with rounded edges::
156
157
sage: implicit_plot3d(x^4 + y^4 + z^4 - (x^2 + y^2 + z^2), (x, -2, 2), (y, -2, 2), (z, -2, 2))
158
159
Chmutov::
160
161
sage: implicit_plot3d(x^4 + y^4 + z^4 - (x^2 + y^2 + z^2-0.3), (x, -1.5, 1.5), (y, -1.5, 1.5), (z, -1.5, 1.5))
162
163
Further Chutmov::
164
165
sage: implicit_plot3d(2*(x^2*(3-4*x^2)^2+y^2*(3-4*y^2)^2+z^2*(3-4*z^2)^2) -3, (x, -1.3, 1.3), (y, -1.3, 1.3), (z, -1.3, 1.3))
166
167
Clebsch::
168
169
sage: implicit_plot3d(81*(x^3+y^3+z^3)-189*(x^2*y+x^2*z+y^2*x+y^2*z+z^2*x+z^2*y) +54*x*y*z+126*(x*y+x*z+y*z)-9*(x^2+y^2+z^2)-9*(x+y+z)+1, (x, -1, 1), (y, -1, 1), (z, -1, 1))
170
171
Looks like a water droplet::
172
173
sage: implicit_plot3d(x^2 +y^2 -(1-z)*z^2, (x, -1.5, 1.5), (y, -1.5, 1.5), (z, -1, 1))
174
175
Sphere in a cage::
176
177
sage: implicit_plot3d((x^8 + z^30 + y^8 - (x^4 + z^50 + y^4 -0.3))*(x^2 + y^2 + z^2 -0.5), (x, -1.2, 1.2), (y, -1.3, 1.3), (z, -1.5, 1.5))
178
179
Ortho circle::
180
181
sage: implicit_plot3d(((x^2 + y^2 - 1)^2 + z^2)* ((y^2 + z^2 - 1)^2 + x^2)* ((z^2 + x^2 - 1)^2 + y^2) - 0.075^2 *(1 + 3* (x^2 + y^2 + z^2)), (x, -1.5, 1.5), (y, -1.5, 1.5), (z, -1.5, 1.5))
182
183
Cube sphere::
184
185
sage: implicit_plot3d(12 - ((1/2.3)^2 *(x^2 + y^2 + z^2))^-6 - ( (1/2)^8 * (x^8 + y^8 + z^8) )^6, (x, -2, 2), (y, -2, 2), (z, -2, 2))
186
187
Two cylinders intersect to make a cross::
188
189
sage: implicit_plot3d((x^2 + y^2 - 1) * ( x^2 + z^2 - 1) - 1, (x, -3, 3), (y, -3, 3), (z, -3, 3))
190
191
Three cylinders intersect in a similar fashion::
192
193
sage: implicit_plot3d((x^2 + y^2 - 1) * ( x^2 + z^2 - 1)* ( y^2 + z^2 - 1) - 1, (x, -3, 3), (y, -3, 3), (z, -3, 3))
194
195
A sphere-ish object with twelve holes, four on each XYZ plane::
196
197
sage: implicit_plot3d(3*(cos(x) + cos(y) + cos(z)) + 4* cos(x) * cos(y) * cos(z), (x, -3, 3), (y, -3, 3), (z, -3, 3))
198
199
A gyroid::
200
201
sage: implicit_plot3d(cos(x) * sin(y) + cos(y) * sin(z) + cos(z) * sin(x), (x, -4, 4), (y, -4, 4), (z, -4, 4))
202
203
Tetrahedra::
204
205
sage: implicit_plot3d((x^2 + y^2 + z^2)^2 + 8*x*y*z - 10*(x^2 + y^2 + z^2) + 25, (x, -4, 4), (y, -4, 4), (z, -4, 4))
206
207
TESTS:
208
209
Test a separate resolution in the X direction; this should look like a
210
regular sphere::
211
212
sage: implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=(10, 40, 40), contour=4)
213
214
Test using different plot ranges in the different directions; each
215
of these should generate half of a sphere. Note that we need to use
216
the ``aspect_ratio`` keyword to make it look right with the unequal
217
plot ranges::
218
219
sage: implicit_plot3d(x^2 + y^2 + z^2, (x, 0, 2), (y, -2, 2), (z, -2, 2), contour=4, aspect_ratio=1)
220
221
sage: implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, 0, 2), (z, -2, 2), contour=4, aspect_ratio=1)
222
223
sage: implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, -2, 2), (z, 0, 2), contour=4, aspect_ratio=1)
224
225
Extra keyword arguments will be passed to show()::
226
227
sage: implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), contour=4, viewer='tachyon')
228
229
An implicit plot that doesn't include any surface in the view volume
230
produces an empty plot::
231
232
sage: implicit_plot3d(x^2 + y^2 + z^2 - 5000, (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=6)
233
234
Make sure that implicit_plot3d doesn't error if the function cannot
235
be symbolically differentiated::
236
237
sage: implicit_plot3d(max_symbolic(x, y^2) - z, (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=6)
238
"""
239
240
# These options aren't fully implemented yet:
241
# vertex_color: Either a single callable taking (x,y,z) and returning
242
# (r,g,b), or a triple of three callables. Not used for jmol. Note that
243
# Tachyon only lets you specify a single color for its triangles; this will
244
# be the mean of the three vertex colors of the triangle. If this is None
245
# (the default), we don't provide separate triangle colors to Tachyon.
246
247
# These options, related to rendering with smooth shading, are irrelevant
248
# since IndexFaceSet does not support surface normals:
249
# smooth: (default: False) Whether to use vertex normals to produce a
250
# smooth-looking surface. False is slightly faster.
251
# gradient: (default: None) If smooth is True (the default), then
252
# Tachyon rendering needs vertex normals. In that case, if gradient is None
253
# (the default), then we try to differentiate the function to get the
254
# gradient. If that fails, then we use central differencing on the scalar
255
# field. But it's also possible to specify the gradient; this must be either
256
# a single python callable that takes (x,y,z) and returns a tuple (dx,dy,dz)
257
# or a tuple of three callables that each take (x,y,z) and return dx, dy, dz
258
# respectively.
259
260
261
G = ImplicitSurface(f, xrange, yrange, zrange, **kwds)
262
G._set_extra_kwds(kwds)
263
return G
264
265
266