2 /* ***** This package may be broken. Please use the VECT
3 package on the SHARE directory. - JPG 6/5/78 ***** */
6 for f in ["grad","div","curl","laplacian","curlgrad","graddiv",
7 "divcurl","curlcurl"] do prefix(f,112,expr,expr)$
8 infix("cross",112,112,expr,expr,expr)$
9 infix("dotdel",108,108,expr,expr,expr)$
10 nofix("christoffel",expr)$
12 dimension():=if dimension=3 then [1,2,3] else [1,2]$
14 type(arg):=if list= /* check required form of answer */
15 (if listp(arg) then /* operation performed on a vector */
16 (for element in arg do /* check each argument */
17 if listp(element) /* an argument is a list */
18 then return(list)) /* return a list */
19 else vector) /* operation performed on a scalar */
20 then result /* return a list */
21 else apply('matrix,[result]) /* return a matrix */$
23 _coordsystem&& coordsystem(sys):=
24 (if sys=rectangular then
27 else if sys=polar then
30 else if sys=cartesian then
33 else if sys=cylindrical then
36 else if sys=spherical then
38 scalefactor:[1,r,r*sin(th)])
39 else (coordvar:read("coordinate variables"),
40 scalefactor:read("scale factors")),
41 dimension:length(coordvar),
44 coordsystem(cartesian)$
46 _cross&& (a cross b) := if dimension=3 then block([result],
47 result:[a[2]*b[3]-a[3]*b[2],
51 else /* 2 dimensional case */
53 (if nonscalarp(b) then /* vector x vector */
55 else block([result], /* vector x scalar */
59 else block([result], /* scalar x vector */
64 _grad&& (grad s) := block([result],
65 result:map(lambda([i],
66 diff(s,coordvar[i])/scalefactor[i]),
70 _div&& (div v) := if dimension=3 then
71 (diff(scalefactor[2]*scalefactor[3]*v[1],coordvar[1])+
72 diff(scalefactor[3]*scalefactor[1]*v[2],coordvar[2])+
73 diff(scalefactor[1]*scalefactor[2]*v[3],coordvar[3]))
74 /scalefactor[1]/scalefactor[2]/scalefactor[3]
75 else /* 2 dimensional case */
76 (diff(scalefactor[2]*v[1],coordvar[1])
77 +diff(scalefactor[1]*v[2],coordvar[2]))
78 /scalefactor[1]/scalefactor[2]$
80 _curl&& (curl a) := if dimension=3 then block([result],
81 result:[(diff(scalefactor[3]*a[3],coordvar[2])
82 -diff(scalefactor[2]*a[2],coordvar[3]))
83 /scalefactor[2]/scalefactor[3],
84 (diff(scalefactor[1]*a[1],coordvar[3])
85 -diff(scalefactor[3]*a[3],coordvar[1]))
86 /scalefactor[3]/scalefactor[1],
87 (diff(scalefactor[2]*a[2],coordvar[1])
88 -diff(scalefactor[1]*a[1],coordvar[2]))
89 /scalefactor[1]/scalefactor[2]],
91 else /* 2 dimensional case */
92 if nonscalarp(a) then block([result],
93 result:(diff(scalefactor[2]*a[2],coordvar[1])
94 -diff(scalefactor[1]*a[1],coordvar[2]))
95 /scalefactor[1]/scalefactor[2],
97 else block([result], /* scalar argument */
98 result:[diff(a,coordvar[2])/scalefactor[2],
99 -diff(a,coordvar[1])/scalefactor[1]],
102 _laplacian&& (laplacian a) := if nonscalarp(a) then grad div a -curl curl a
103 else if dimension=3 then
104 (diff(diff(a,coordvar[1])*scalefactor[2]
105 *scalefactor[3]/scalefactor[1],coordvar[1])
106 +diff(diff(a,coordvar[2])*scalefactor[3]
107 *scalefactor[1]/scalefactor[2],coordvar[2])
108 +diff(diff(a,coordvar[3])*scalefactor[1]
109 *scalefactor[2]/scalefactor[3],coordvar[3]))
110 /scalefactor[1]/scalefactor[2]/scalefactor[3]
111 else /* 2 dimensional case */
112 (diff(diff(a,coordvar[1])*scalefactor[2]
113 /scalefactor[1],coordvar[1])
114 +diff(diff(a,coordvar[2])*scalefactor[1]
115 /scalefactor[2],coordvar[2]))/scalefactor[1]/scalefactor[2]$
117 _dotdel&& (v dotdel b) := if nonscalarp(b) then block([result, b:flatten(args(b)), v:flatten(args(v))],
118 result:if member('christsym, arrays)
119 then /* use christoffel symbols */
121 sum((diff(b[i]*scalefactor[j],
122 coordvar[i])-sum(b[k]*scalefactor[k]
123 *christsym[k,j,i],k,1,dimension))
124 *v[i]/scalefactor[i],i,1,dimension)
125 /scalefactor[j]),dimension())
126 else /* vector b, no christoffel symbols */
128 sum(diff(b[i]*scalefactor[j],coordvar[i])
129 *v[i]/scalefactor[i],i,1,dimension)
130 /scalefactor[j]),dimension()),
132 else block([result], /* scalar b case */
133 result:if member('christsym, arrays)
134 then /* use christoffel symbols */
135 sum((diff(b,coordvar[i])-b*christsym[1,1,i])
136 *v[i]/scalefactor[i],i,1,dimension)
137 else /* no christoffel symbols */
138 sum(diff(b,coordvar[i])*v[i]
139 /scalefactor[i],i,1,dimension),
142 _christoffel&& christoffel := (array(christsym,3,3,3),
145 (christsym[i,i,i]:diff(scalefactor[i],coordvar[i])
147 for j thru 3 do if j#i then
148 (christsym[i,j,i]:christsym[i,i,j]:diff(scalefactor[i],
149 coordvar[j])/scalefactor[i],
150 christsym[j,i,i]:-diff(scalefactor[i],
151 coordvar[j])*scalefactor[i]/scalefactor[j]^2)))$
155 (graddiv v) := block([result],
157 result:map(lambda([i],
158 diff(result,coordvar[i])/scalefactor[i]),