Some Basic Mathematica
The goal is to express many of the fundamentals of elementary
differential geometry in terms of Mathematica. These notes
are intended to supply a basis for other files in this website, so there is
some bias in favor of surfaces over curves. In particular, we have
included the tensor formula for Gaussian curvature that applies to
surfaces in Euclidean space of any dimension. Differential equations are
absent since they will appear (numerically, at least) in "Plotting Geodesics."
A subplot of these notes is to show how a reader with regular access
to Mathematica can store, in just three select files, the most
useful Mathematica commands for surface theory. These are
commands too lengthy for for constant retyping, but typed or
copied--just once--into these Save files, they can be used immediately
in all later work.
Errors are annoying; please report any you notice to
bon@math.ucla.edu.
Contents
1. Fundamentals
2. Surfaces
3. Gaussian
Curvature
4. Isometries
5. Plots
1. Fundamentals
References
[G] Gray, Alfred, "Modern Differential Geometry of Curves and Surfaces"
(first or second edition), CRC Press, ~1000 pages. Organized as a
geometry course, it contains an immense number of Mathematica
commands.
[D] Don, Eugene, "Schaum's Outline of Mathematica",
Prentice-Hall, ~350 pages. Elementary and explicit.
[W] Wolfram, Stephen, "The Mathematica Book" (versions 3,4,5),
Wolfram Media, ~1400+ pages. A detailed account of how
Mathematica works and what it can do.
Starting Up
A few details, notably how to start using
Mathematica, depend on your particular computer system and
are best learned locally--from documentation or other users.
Help
Our use of Mathematica code is limited and should not involve
many problems. Mathematica's online help is quite good: For
common terms, ?term will produce a descript-xion. Searching
the Help menu will usually produce examples as well as more details.
In addition, Mathematica has excellent error notification,
often useful in correcting bad typing.
==Syntax
- There are no prompts or termination symbols--except that a final
semicolon suppresses display of the output. Input (new or old) is
activated by the command Shift-RETURN
or Shift- ENTER, and the input and resulting
output are numbered.
- Parentheses (...) are used for algebraic grouping, brackets [...]
for arguments of functions, and braces {...} for lists.
- Multiplication is given by an asterisk, e.g., x*y, but for an integer n
only, nX=n*X, where X is not an integer. Exponents are given by a caret,
e.g., x^2. (Multiplication can also be indicated by a blank space,
though this risks error.)
- Colon-equal (:=) for definitions, e.g., z:=x+3; double equal
signs for mathematical equations, e.g., x+y==1. Rarer: Single equal for
assigning value to a variable, e.g., x=2;
- Previous outputs are called up by either names assigned by the
user or %n for the nth output.
- Substitution by slash-dot. For example, if expr is
an expression involving x and y, then expr /. {x -> 2u+1,
y -> v^2} replaces every x in the expression by 2u+1 and every y
by v2.
- Extra spaces between characters are ignored, that is, two or
more consecutive spaces are treated as a single space.
==Calculus notation
Functions are given, for example, by
f[x_] := x^3-2x+1
or
g[u_,v_] := u*Cos[v]-u^2*Sin[v]
Here, as always, an underscore ( _ ) following a letter (or string)
makes it a variable. Thus the function f defined above can be
evaluated on u or 3.14 or a2+b2, etc.
Derivatives (including partial derivatives) by
D[f[x],x] or
D[g[u,v],v].
Definite Integrals by Int[f[x], {x,a,b}]. For numerical
integration, replace Int by NInt.
==Vectors and Matrices
A vector is just a list v={v1,...,vn} whose components
can be numbers or any expressions. The usual vector addition is by "+"
and scalar multiplication is done by asterisk, with input
s*{v1,...,vn} yielding {s*v1,...,s*vn}. Dot products
are given by a period: v.w
Mathematica describes a matrix as a list of lists, the latter
being its rows. For example, {{a,b},{c,d}} is a matrix and is
treated as such in all contexts. To show it in the usual 2 × 2 form,
apply the command MatrixForm.
The determinant of a square matrix m is given by
Det[m].
The full power of the dot operator (.) appears only when
matrices are involved. First, if p and q are properly
sized matrices, then p.q is their product. Next, if m
is an m × n matrix and v is an n-vector, then
m .v gives the usual operation of m on v.
Taking m=n=3 for example, if m1, m2, m3 are the rows of
m and v={v1,v2,v3}, then Mathematica
defines
m .v to
be {m1.v, m2.v, m3.v}
This can be seen to be the result of m (in 3 × 3
form) matrix-multiplying the column-vector corresponding to v, with
the resulting column-vector restated as a n-tuple. In this sense,
Mathematica obeys the "column-vector convention."
==Curves
A curve in Rn is a list whose n components
are expressions in a single variable. For example,
c[t_] := {2*Cos[t], 2*Sin[t], 3t}
gives a helix in R3. Then the vector derivative
(i.e., velocity) is returned by D[c[t],t] and the second derivative by
D[c[t],t,t], and so on.
2. Surfaces
A parametrization x of a surface M in Rn is
given as a list whose n components are expressions in two variables.
For example,
x[u_,v_] := {2u*Cos[v],2u*Sin[v],3v}
gives a helicoid in R3.
Surfaces with parameters. A more general helicoid is
defined by
helicoid[a_,b_][u_,v_] :=
[a*u*Cos[v],a*u*Sin[v],b*v].
Then helicoid[2,3] is the special case above.
For any parametrization x : D
M, the metric components are defined to be
E = xu
xu,
F = xu
xv,
G = xv
xv
These functions describe the way x distorts the Euclidean
geometry of the plane region D in applying it to M. Specifically,
E measures stretching (or
compression) in the u-direction,
F measures the change in
angle between the u- and v-directions,
G measures stretching (or
compression) in the v-direction.
The metric components are expressed as follows by
Mathematica:
ee[x_][u_,v_] :=
Simplify[D[x[uu,vv],uu].D[x[uu,vv],uu]] /.{uu->,vv->v}
ff[x_][u_,v_] :=
Simplify[D[x[uu,vv],uu].D[x[uu,vv],vv]] /.{uu->,vv->v}
gg[x_][u_,v_] :=
Simplify[D[x[uu,vv],vv].D[x[uu,vv],vv]] /.{uu->,vv->v}
The dummy variables uu and vv are needed here to make
ee[x], for instance, a real-valued function on D (note the
dash-dot "/," substitution). Otherwise it would only be an expression
in the specific variables u and v.
We usually represent capital letters (E) by double lower case
letters (ee) since many capitals have special meaning for Mathematica.
==Comments
- "Simplify" is the principal Mathematica simplification weapon;
however, it cannot be expected to give ideal results in every case.
"FullSimplify" is stronger but slower. Thus human intervention is
often required, either to do hands-on simplification or to use
further computer commands such as "Factor" and "Expand."
Of course, many results are inherently complicated, but
this is usually not an obstacle to their further use by the computer
- The distinction between functions and expressions is always
crucial. Applying ee (a function that
feeds on functions) to any parametrization x (a function)
gives a real-valued function ee[x] which can be applied to
any x1, x2, not just u,v. But ee[x[u,v]], for example, will
fail, since x[u,v] isn't a function.
- These commands work for a mapping x into a Euclidean
space Rn of any dimension.
==Saving the commands ee, ff, gg in your
Mathematica
These commands are probably the most frequently used throughout
the geometry of surfaces. So instead of retyping them whenever
they're needed, it's a good idea to save them in a dot-m file
efg.m. Then in any later Mathematica session, input
this file by: << efg.m and all three commands
(though not displayed) will be installed, ready for use.
Here's how to build the dot-m file:
- Open a new Mathematica page.
- Type the commands into it. Then from the Cell menu, make each of
the three cells an Initialization Cell.
- Close this page and name it efg.nb. You will be given
two choices: pick "Create Auto Save Package." This produces
the required file, efg.m.
A frequently occuring combination of the metric components is
W = (EG-F2)1/2, which
Mathematica renders as
ww[x_][u_,v_] :=
Sqrt[Simplify[ee[x][u,v]*gg[x][u,v]-ff[x][u,v]^2]]
In the special case of a parametrized surface in
R3, there is second set of functions, analogous to
the metric components, but involving the second derivatives
of x. In fact, L, M, and
N are the normal components of
xuu,
xuv, and xvv,
respectively, that is,
L =
xuu
U,
M =
xuv
U,
N =
xvv
U,
where U is the (unique up to sign) unit normal vector field of the
surface. These functions describe, near each point, the shape of the
surface in R3.
To express L, M, and N in Mathematica, note first that U is the
unit vector in the direction of the cross product
xu× xv.
By a well known vector identity, the length of this cross product
is just W=(EG-F2)1/2,
as above
Another vector identity asserts that the triple scalar product
U.(V x W) equals the determinant of the matrix whose rows are
U,V,W. Thus, writing ll (double el) for L,
ll[x_][u_,v_] := Simplify[
Det[{D[x[uu,vv],uu,uu],D[x[uu,vv],uu],D[x[uu,vv],vv]}] /
ww[x][uu,vv]] /.
{uu->u,vv->v}
The formulas for mm=M and
nn=N are the same except that the double
derivative uu,uu is replaced by uu,vv and vv,vv respectively.
==Saving the commands ee, ff, gg, ww, ll, mm, nn in your
Mathematica
The procedure for building a dot-m file efgwlmn.m follows
same pattern as that given above for efg.m, but with seven
commands instead of three. It's economical to include ee, ff, gg
since they're likely to be used whenever ll, mm, nn are.
3. Gaussian Curvature K
We give three commands for Gaussian curvature. The first,
gaussK, is the fastest, but applies only to surfaces in
R3. The second, tensorK, works for surfaces
in any Rn. The third, superK, is a simple
extension of tensorK.
==gaussK
For a parametrized surface in R3, Gaussian
curvature K is given by a standard formula:
K=(LN-
M2)/(EG-F2),
The command gaussK results when Mathematica's
expressions for E,F,G,L,M,N are
substituted.
To describe the command gaussK we use a Module.
This creates an enclave where local definitions can be made that cannot
escape to the outside, so the desired formula can be expressed more
simply.
In the module below, the names of the local variables are listed
within braces {...}, next these variables are defined (each definition
followed by a semicolon), then the formula for the command is given.
gaussK[x_][u_,v_] :=
Module[{xu,xv,xuu,xuv,xvv},
xu=D[x[uu,vv],uu]; xv=D[x[uu,vv],vv];
xuu=D[x[uu,vv],uu,uu];
xuv=D[x[uu,vv],uu,vv];
xvv=D[x[uu,vv],vv,vv];
Simplify[(Det[{xuu,xu,xv}]*Det[{xvv,xu,xv}]-Det[{xuv,xu,xv}]^2)/
(xu.xu*xv.xv-(xu.xv)^2)^2]]/.{uu->u,vv->v}
==Saving gaussK from the computer screen to your
Mathematica
This a variant of the construction of efg.m described in
Section 2.
- Open a new Mathematica page.
- From your computer screen, select the definition above, all in one
batch.
- Copy it, and paste it into the Mathematica page as a single
cell. (We gave the formula in Plain Text rather than Bold to ease this
operation.)
- Using the Cell menu, designate this cell an Initialization Cell.
- Close this page, and save it as gaussK.nb. As before, you
will then be given two choices; pick "Create Auto Save Package." and
this produces the dot-m file gaussK.m.
Again, in any future Mathematica session, input the dot-m file
by: << gaussK.m and the command (though not
displayed) will be installed, ready to use.
Note. Of course, instead of copy-and-paste as above, one
can always just type in the command.
==tensorK
This command computes Gaussian curvature solely in terms of
the metric components E, F, G. Thus it is the computational
expression of Gauss's theorema egregium, which initiated
modern differential geometry.
Again we use a module:
tensorK[ee_,ff_,gg_][u_,v_] := Module[{w,e,f,g},
e=ee[uu,vv]; f=ff[uu,vv]; g=gg[uu,vv];
w=Simplify[Sqrt[e*g-f^2]];
Simplify[(1/(2w))*
(D[(f*D[e,vv] - e*D[g,uu])/(e*w),uu] +
D[(2e*D[f,uu] - f*D[e,uu] - e*D[e,vv])/
(e*w),vv])]] /.{uu->u,vv->v}
Wait to save tensorK till we reach an associated command.
==superK
Combining the four previous commands gives a command that
finds the Gaussian curvature of any parametrization
x: D
Rn.
superK[x_][u_,v_] := tensorK[ee[x],ff[x],gg[x]][u,v]
==Saving superK in a dot-m file
The command superK is meaningful in a Mathematica session only when tensorK is already installed--as well as the
commands for ee, ff, gg given in Section 2.
Thus the simplest plan is to build a dot-m file containing all five:
ee, ff, gg, tensorK, superK. This can be done as for the
previous dot-m files, with tensorK and superK copied,
but ee, ff, gg perhaps just typed in. (They are included to avoid
having to remember to install them separately.)
We call the resulting file tensorsuperK.m, following
Mathematica's style of long but descript-xive names.
==Testing superK
One of the examples in "Surfaces in R4+" involves the
following mapping of R3 into R6:
H(x,y,z) := {x^2/Sqrt[2], y^2/Sqrt[2],
z^2/Sqrt[2], x*y, y*z, z*x}
Restricting this map to the unit sphere in R3 leads
to this parametrization of a surface in R6:
x[u_,v_] := H[x,y,z]/. {x->Cos[v]*Cos[u],
y->Cos[v]*Sin[u], z->Sin[v]}
In context, we expect this surface to have Gaussian curvature +1,
hence here we expect superK[x][u,v] to give +1.
A test of tensorK requires a detour.
==Abstract Surfaces
When functions E,F,G such that E>0, G>0,
and EG-F2 > 0, are defined on a region D in
the plane, they make D an abstract geometric surface whose
scalar product <...> is not the usual dot product, but is
characterized by:
<U,U> = E,
<U,V> = F, <V,V> = G,
where U and V are the unit vector fields in the u- and v-directions,
respectively.
For example, on the disk D: r< 2 in R2, define:
eeh[u_,v_] :=
1/(1-(u^2+v^2)/4)^2
ffh[u_,v_] := 0
ggh[u_,v_] :=
1/(1-(u^2+v^2)/4)^2
These functions turn D into the hyperbolic plane (Poincaré disk model). To find its Gaussian curvature,
neither gaussK or superK will work, since D
does not derive its metric from a containing Euclidean space.
But if all is well, tensorK[eeh,ffh,ggh][u,v]
will give -1.
Historical Note: This model of the hyperbolic plane was
found by Poincaré in the late 1800s. It may seem to be just
a clever little artifact, but it served two important purposes:
First, it pointed the way to
Riemannian manifolds, the central objects of study in
differential geometry. In such a manifold, the geometric structure is
axiomatized without reference to any containing Euclidean space.
Second, it presented a simple
case of a difficult question: can the hyperbolic plane (or an isometric
copy of it) be found in some Euclidean space? This question remained
unanswered until 1956, when John Nash (of "A Beautiful Mind") proved
that every Riemannian manifold can be found in a Euclidean
spaceof sufficiently high dimension.
Then an imbedding of the hyperbolic plane in R6 was
soon found by Blanusa.
4. Isometries
A one-to-one differentiable mapping F: M
M' is an isometry provided (*) each pair
of tangent vectors v,w at a point of M is carried to tangent vectors
v',w' on M' with the same inner product.
It follows that if c is a curve in M, then F(c) has the same length in
M', and if G is a region in M then F(G) has the same area as G, and so on.
When such an isometry exists, the surface M and M' are said to be
isometric and to have the same intrinsic geometry.
Gauss's theorema egregium asserts that Gaussian curvature is
preserved by isometries and thus belongs to the intrinsic geometry of
surfaces.
Intuitively, this geometry of M is what is observed by its inhabitants,
who have no conception of the shape of M in any surrounding Euclidean
space.
To find a computational expression of the (*) condition above, let
x: D
M be a coordinate patch. Then the
coordinate vector fields xu, xv at any point
of x(D) form a basis for all the tangent vectors at that point.
Now let y=F(x). Then xu and xv are
carried to yu and yv. So
the (*) criterion holds for all tangent vectors <=> the (*) criterion
holds for xu and xv <=> E=E', F=F',
G=G'.
5. Plots
There are four basic types:
- Plot and Plot3D plot the graphs of real-valued
functions of one and two variables, respectively. Examples:
Plot[f[x]//Evaluate, {x,a,b}]
Plot3D[g[x,y]//Evaluate, {x,a,b}, {y,c,d}]
Here //Evaluate improves the speed of the plotting.
- ParametricPlot plots the image of a parametrized
curve in the plane R2, and
- ParametricPlot3D plots the image of a parametrized
curve or of a parametrization of a surface in R3.
For example, if x: D
R3 is defined on the rectangle D:
0
u
1,
0
v
, then its image is plotted by
ParametricPlot3D[x[u,v]//Evaluate, {u,0,1},
{v,0,
}]
Plots can be refined in many, many ways by specifying values for various options. The form is option->value.
For example, in
Plot[f[x]//Evaluate, {x,a,b},
AspectRatio->Automatic]
Mathematica is ordered to use the same scale on both
axes, and in
ParametricPlot3D[x[u,v]//Evaluate, {u,0,1},
{v,0,
}, Boxed->False]
the box ordinarily containing the figure does not appear.
The options available for a given command cmd, along
with their default values, are listed by Options[cmd]. Then
?opt will give a descript-xion of the option.
Previously drawn (and named) plots can be shown on the same
page by
Show[plot1, plot2,plot3]