1 /*******************************************************************************
3 * Examples for the package vect
5 ******************************************************************************/
10 (load("vect.mac"),done);
13 declare([a,b,c,d],nonscalar);
16 /* ---------- Check some rules for vectors ----------------------------------*/
18 /* This does not simplify to zero, because we have removed the property
19 * commutative for the operator "." */
26 vectorsimp(a.(b+c)-(a.b+a.c)),expandall:true;
29 vectorsimp(a~(b+c)-(a~b+a~c)),expandall:true;
32 vectorsimp(a~(b~c)-(b*(a.c)-c*(a.b))),expandall:true;
35 /* Lagrange's identity does not simplify as expected */
36 vectorsimp((a~b).(c~d)-((a.c)*(b.d)-(b.c)*(a.d))),expandall:true;
39 /* Does not simplify to the expected result. */
40 vectorsimp((3*a-2*b)~(2*a+b)),expandall:true;
43 /* ---------- Check some rules for the gradient ------------------------------*/
45 declare([u,v],scalar, c,constant);
47 depends([u,v],[x,y,z]);
56 vectorsimp(grad (u+v)),expandall:true;
59 /* ---------- Check some rules for the divergence ----------------------------*/
67 vectorsimp(div (u+v)),expandall:true;
70 /* The error is a . grad u -> a grad u */
71 vectorsimp(div (u*a)) - (u*div a + (grad u) . a),expandall:true;
74 /* There is no rule do expand the following */
75 vectorsimp(div (a ~ b)),expandall:true;
76 b . curl a - a . curl b;
78 /* ---------- Check some rules for the curl ----------------------------------*/
86 vectorsimp(curl (a+b)),expandall:true;
89 vectorsimp(curl (a*u)),expandall:true;
90 u*curl a + grad u ~ a;
92 vectorsimp(curl (a ~ b)),expandall:true;
93 b . grad a - a . grad b + a * div b - b * div a;
95 /* ---------- Combinations of the operators ----------------------------------*/
101 vectorsimp(curl curl a),expandall:true;
102 grad div a - laplacian a;
104 /* ---------- Examples from bug reports --------------------------------------*/
106 /* Bud ID: 838301 - vect negate cross product simplification
107 * a and b are declare to be nonscalar, c to be constant
115 /* Bug ID: 1212598 - bug in the VECT.MAK - VECTORSIMP cross product
116 * a,b,c,d are declared to be nonscalar
119 vectorsimp( (b+2*a) ~ (c+d) ),expandall:true;
120 b ~ d+b ~ c+2*a ~ d+2*a ~ c;
123 /* Bug ID: 2806446 - ev_diff in vect.mac */
125 potential([y*z,x*z,x*y],[x,y,z]);
128 /* Bug ID: 2011228 -vect redefines "." as commutative, was:Matrix multiplication
129 * Two examples which does not work when "." is declared commutative
132 invert(matrix([-2, -2, 1], [3, 1, 1], [3, 3, 1]));
133 matrix([-1/5,1/2,-3/10],[0,-1/2,1/2],[3/5,0,2/5]);
135 (a : matrix([1,2],[6,7]), b : matrix([1,2],[1,6]), done);
138 matrix([-10,-2],[-24,10]);
142 Some syntax errors can be detected by comparing the declared part of
143 speech to an actual expression. (see documentation for `infix'.). The
144 tests are not automatic because a syntax error is not captured by
147 errcatch(if a~b then 1 else 0);
150 errcatch(if grad a then 1 else 0);
155 /* SF bug #3337: "Wrong scalefactor for cartesian2d" */
157 (scalefactors (cartesian2d), sf);
160 (scalefactors (cartesian3d), sf);
163 /* Additional tests for #3337. These are weak in the sense that
164 * they aren't testing for specific values, but we would have to
165 * work out what is the correct result in each case.
168 (scalefactors (polar), is (length(sf) = length(first(polar))));
171 (scalefactors (polarcylindrical), is (length(sf) = length(first(polarcylindrical))));
174 (scalefactors (spherical), is (length(sf) = length(first(spherical))));
177 (scalefactors (elliptic), is (length(sf) = length(first(elliptic))));
180 (scalefactors (ellipticcylindrical), is (length(sf) = length(first(ellipticcylindrical))));
183 (scalefactors (confocalelliptic), is (length(sf) = length(first(confocalelliptic))));
186 (scalefactors (prolatespheroidalsqrt), is (length(sf) = length(first(prolatespheroidalsqrt))));
189 (scalefactors (oblatespheroidalsqrt), is (length(sf) = length(first(oblatespheroidalsqrt))));
192 (scalefactors (parabolic), is (length(sf) = length(first(parabolic))));
195 (scalefactors (paraboliccylindrical), is (length(sf) = length(first(paraboliccylindrical))));
198 (scalefactors (paraboloidal), is (length(sf) = length(first(paraboloidal))));
201 (scalefactors (prolatespheroidal), is (length(sf) = length(first(prolatespheroidal))));
204 (scalefactors (oblatespheroidal), is (length(sf) = length(first(oblatespheroidal))));
207 (scalefactors (bipolar), is (length(sf) = length(first(bipolar))));
210 (scalefactors (bipolarcylindrical), is (length(sf) = length(first(bipolarcylindrical))));
213 (scalefactors (toroidal), is (length(sf) = length(first(toroidal))));
216 (scalefactors (conical), is (length(sf) = length(first(conical))));
219 (scalefactors (confocalellipsoidal), is (length(sf) = length(first(confocalellipsoidal))));
222 (reset(tr_bound_function_applyp,vect_cross,dotexptsimp,dotassoc,dotscrules),0);
225 /* SF bug #3287: "cross product scalar zero versus vector zero result" */
236 f(a, g(b, c)) ~ f(a, g(b, c));
239 curl (grad (f (a, b, c)));
242 /* examples from the bug report */
244 express([0,1,1]~[0,1,2]);
247 express([0,1,1]~[0,1,1]);
253 /* Levi-Civita Symbol */
254 ε(i,j,k):=e[i].express(e[j]~e[k]),
267 express(ε(1,2,3)*a_[2]*b_[3]);
270 express(ε(1,3,2)*a_[2]*b_[3]);
273 express(ε(1,2,2)*a_[2]*b_[2]);
276 makelist(sum(sum(ε(i,j,k)*a_[j]*b_[k],j,1,3),k,1,3),i,3);
277 [a_2*b_3 - a_3*b_2, a_3*b_1 - a_1*b_3, a_1*b_2 - a_2*b_1];
279 /* bug reported to mailing list 2022-01-04: "Bad vect package?" */
281 /* example from email -- grad */
283 (scalefactors(polarcylindrical),
284 kill (u, E, rho, I, D),
290 foo: D*(grad(u)+E*u);
291 D*([- I*rho*u, 0, 0] + grad(u));
293 ev (express (foo), 'diff);
294 [D*('diff(u, r) - I*rho*u), 0, 0];
296 /* other examples -- cross product, curl */
298 (kill (a, b, x, y, z),
299 (a ~ b) + [x, y, z]);