1 /*************** -*- Mode: MACSYMA; Package: MAXIMA -*- ******************/
2 /***************************************************************************
4 *** Copyright (c) 1984 by William Schelter,University of Texas *****
5 *** All rights reserved *****
6 ***************************************************************************/
9 kill(functions,arrays,values);
11 use_fast_arrays:false;
25 (use_fast_arrays:true,kill(a));
27 lambda([x,y,z],x^2+y^2+z^2);
28 lambda([x,y,z],x^2+y^2+z^2)$
39 m:matrix([a,0],[b,1]);
42 matrix([a^2,0],[b^2,1])$
44 matrix([a^2,0],[a*b+b,1])$
46 matrix([a^2,0],[a*b,a])$
48 matrix([1,1],[1-b,a])$
50 matrix([1/a,0],[-b/a,1])$
53 matrix([a,b,c],[d,e,f],[g,h,i]);
54 matrix([a,b,c],[d,e,f],[g,h,i])$
56 matrix([c*g+b*d+a^2,c*h+b*e+a*b,c*i+b*f+a*c],
57 [f*g+d*e+a*d,f*h+e^2+b*d,f*i+e*f+c*d],
58 [g*i+d*h+a*g,h*i+e*h+b*g,i^2+f*h+c*g])$
69 fib[n]:=if n = 1 or n = 2 then 1 else fib[n-1]+fib[n-2];
70 fib[n]:=if n = 1 or n = 2 then 1 else fib[n-1]+fib[n-2]$
77 eta(mu,nu):=if mu = nu then mu else (if mu > nu then mu-nu else mu+nu);
78 eta(mu,nu):=if mu = nu then mu else (if mu > nu then mu-nu else mu+nu)$
81 eta(eta(7,7),eta(1,2));
83 if not 5 >= 2 and 6 <= 5 or 4+1 > 3 then a else b;
90 determinant(hessian(x^3-3*a*x*y*z+y^3,[x,y,z]));
91 -3*a*y*(9*a^2*x*z+18*a*y^2)-27*a^3*x*y*z-54*a^2*x^3$
93 subst(1,z,quotient(%,-54*a^2));
95 f(x):=block([a,y],local(a),y:4,a[y]:x,display(a[y]));
96 f(x):=block([a,y],local(a),y:4,a[y]:x,display(a[y]))$
106 (use_fast_arrays : false, kill(a), 0);
109 /* ensure that matrix construction works as advertised */
110 (L : makelist ([i], i, 1, 100), apply (matrix, L), [op (%%), args (%%)]);
111 [matrix, ''(makelist ([i], i, 1, 100))];
113 (L : makelist ([i], i, 1, 100), apply (matrix, L), transpose (%%));
114 ''(matrix (tree_reduce (append, L))); /* call tree_reduce instead of append because GCL barfs ... */
116 (matrix (), [op (%%), args (%%)]);
119 /* construct a matrix of modest size */
120 (apply (matrix, makelist ([i], i, 1, 1000)), 0);
123 /* construct a matrix of modest size */
124 (apply (matrix, makelist ([i], i, 1, 10000)), 0);
127 /* verify that arguments are evaluated exactly once */
128 block ([a : b, b : c, c: d, d : 1], matrix ([a, b], [c, d]), [op (%%), args (%%)]);
129 [matrix, '[[b, c], [d, 1]]];
131 /* verify that arguments are evaluated exactly once */
132 block ([a : b, b : c, c: d, d : 1, L1 : '[a, b], L2 : '[c, d]], matrix (L1, L2), [op (%%), args (%%)]);
133 [matrix, '[[a, b], [c, d]]];
135 /* another evaluation puzzle, derived from discussion on mailing list circa 2013-10-28 */
143 /* a more elaborate version of the preceding evaluation puzzle;
144 * result not checked for correctness
149 A : matrix ([a, 1], [1, 0]),
150 integer_pow(x) := block ([k], declare (k, integer), x^k),
151 mat_function (integer_pow, A));
153 matrix([(sqrt(a^2+4)-a)^(k+1)*2^(-k-1)*(-1)^k
154 /((sqrt(a^2+4)+a)/2+(sqrt(a^2+4)-a)/2)
155 +(sqrt(a^2+4)+a)^(k+1)*2^(-k-1)
156 /((sqrt(a^2+4)+a)/2+(sqrt(a^2+4)-a)/2),
157 (sqrt(a^2+4)+a)^k/(((sqrt(a^2+4)+a)/2+(sqrt(a^2+4)-a)/2)*2^k)
158 -(sqrt(a^2+4)-a)^k*(-1)^k/(((sqrt(a^2+4)+a)/2+(sqrt(a^2+4)-a)/2)
160 [(sqrt(a^2+4)-a)*(sqrt(a^2+4)+a)^(k+1)*2^(-k-2)
161 /((sqrt(a^2+4)+a)/2+(sqrt(a^2+4)-a)/2)
162 -(sqrt(a^2+4)-a)^(k+1)*(sqrt(a^2+4)+a)*2^(-k-2)*(-1)^k
163 /((sqrt(a^2+4)+a)/2+(sqrt(a^2+4)-a)/2),
164 (sqrt(a^2+4)-a)^k*(sqrt(a^2+4)+a)*2^(-k-1)*(-1)^k
165 /((sqrt(a^2+4)+a)/2+(sqrt(a^2+4)-a)/2)
166 +(sqrt(a^2+4)-a)*(sqrt(a^2+4)+a)^k*2^(-k-1)
167 /((sqrt(a^2+4)+a)/2+(sqrt(a^2+4)-a)/2)]);
172 /* should trigger an error */
173 errcatch (matrix ([1], [1, 2]));
176 /* should trigger an error */
177 errcatch (matrix ([1], '(a + b)));
180 /* SF bug # 3014545 "submatrix does not work as expected"
181 * works for me, throw in these tests to make sure
184 (submatrix (10, 20, zeromatrix (20, 20)), [length (%%), length (%%[1])]);
187 (kill (F), F : 1 + zeromatrix (5, 5), submatrix (2, 5, F, 2, 5));
188 matrix ([1, 1, 1], [1, 1, 1], [1, 1, 1]);
190 submatrix (3, 5, F, 3, 5);
191 matrix ([1, 1, 1], [1, 1, 1], [1, 1, 1]);
194 matrix ([1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]);
196 (F : matrix ([1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]),
197 submatrix (F, 2, 4));
198 matrix ([1, 3], [5, 7], [9, 11]);
201 matrix ([5, 6, 7, 8]);
203 /* next one is mostly just to ensure it doesn't trigger an error */
204 submatrix (1, 2, 3, F);
207 /* next one is mostly just to ensure it doesn't trigger an error */
208 submatrix (F, 1, 2, 3, 4);
212 matrix ([1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]);
215 matrix ([1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]);
217 /* name collision with special variables in 1-d output
218 * see mailing list circa 2012-01-09, "invert_by_lu does not work as expected"
221 invert_by_lu (matrix ([v [0]]));
222 matrix ([1 / v [0]]);
224 /* additional tests for invert */
226 /* Attempting to verify the effect of the ratmx and detout ev flags
227 * is quite a mess. ratmx produces CRE but the parser produces
228 * expressions which have a different operator (RAT, versus MRAT for CRE).
229 * detout produces an unsimplified "*" expression, which is quite
230 * readily simplified away; I am reminded of 19th century efforts to
231 * isolate halogens and alkali metals. Anyway, we'll do what we can.
234 /* symbolic elements */
236 (kill (M, M1), M : matrix ([a, b], [c, d]), 0);
239 M1 : invert (M), ratsimp;
240 matrix([d/(a*d-b*c),-b/(a*d-b*c)],[-c/(a*d-b*c),a/(a*d-b*c)]);
242 ratsimp ([M1 . M, M . M1]);
243 [matrix ([1, 0], [0, 1]), matrix ([1, 0], [0, 1])];
245 is (invert (M) = M^^-1);
248 (M1 : ev (invert (M), detout=true, doscmxops=false, doallmxops=false),
249 block ([inflag:true], [op (M1), ratsimp (args (M1))]));
250 ["*",[1/(a*d-b*c),matrix([d,-b],[-c,a])]];
252 is (invert (M) = M^^-1), detout=true, doscmxops=false, doallmxops=false;
255 block ([foo : matrix([d/(d*a-c*b),-(b/(d*a-c*b))],[-(c/(d*a-c*b)),a/(d*a-c*b)])],
256 ev (invert (M), ratmx=true), if equal (%%, foo) then true else %%);
259 is (invert (M) = M^^-1), ratmx=true;
262 block ([foo : ev (invert (M), ratmx=true, detout=true, doscmxops=false, doallmxops=false)],
263 [op (foo), first (foo), second (foo)],
264 if equal (%%, ["/", matrix ([d, -b], [-c, a]), a*d - b*c]) then true else %%);
267 is (invert (M) = M^^-1), ratmx=true, detout=true, doscmxops=false, doallmxops=false;
270 /* bigfloat elements */
272 (M : ev (M, a = 1b0, b = 2b0, c = 3b0, d = -4b0), 0);
276 matrix([4.0b-1,2.0b-1],[3.0b-1,-1.0b-1]);
278 is (invert (M) = M^^-1);
281 (M1 : ev (invert (M), detout=true, doscmxops=false, doallmxops=false),
282 ev ([op (M1), args (M1)], simp=false, inflag=true));
283 ["*", [-0.1b0, matrix([-4.0b0, -2.0b0], [-3.0b0, 1.0b0])]];
285 is (invert (M) = M^^-1), detout=true, doscmxops=false, doallmxops=false;
288 (M1 : ev (invert (M), ratmx=true),
289 if every (ratp, M1) and equal (M1, matrix ([2/5, 1/5], [3/10, -(1/10)])) then true else M1);
292 is (invert (M) = M^^-1), ratmx=true;
295 (M1 : ev (invert (M), ratmx=true, detout=true, doscmxops=false, doallmxops=false),
296 [o, a] : ev ([op (M1), args (M1)], simp=false, inflag=true),
297 if ?caar (a [1]) = ?rat and every (ratp (a [2])) and equal (%%, ["*", [-1/10, matrix ([-4, -2], [-3, 1])]]) then true else %%);
300 is (invert (M) = M^^-1), ratmx=true, detout=true, doscmxops=false, doallmxops=false;
309 matrix([4.0e-1,2.0e-1],[3.0e-1,-1.0e-1]);
311 is (invert (M) = M^^-1);
314 (M1 : ev (invert (M), detout=true, doscmxops=false, doallmxops=false),
315 ev ([op (M1), args (M1)], simp=false, inflag=true));
316 ["*", [-0.1e0, matrix([-4.0e0, -2.0e0], [-3.0e0, 1.0e0])]];
318 is (invert (M) = M^^-1), detout=true, doscmxops=false, doallmxops=false;
321 (M1 : ev (invert (M), ratmx=true),
322 if every (ratp, M1) and equal (M1, matrix ([2/5, 1/5], [3/10, -(1/10)])) then true else M1);
325 is (invert (M) = M^^-1), ratmx=true;
328 (M1 : ev (invert (M), ratmx=true, detout=true, doscmxops=false, doallmxops=false),
329 [o, a] : ev ([op (M1), args (M1)], simp=false, inflag=true),
330 if ?caar (a [1]) = ?rat and every (ratp (a [2])) and equal (%%, ["*", [-1/10, matrix ([-4, -2], [-3, 1])]]) then true else %%);
333 is (invert (M) = M^^-1), ratmx=true, detout=true, doscmxops=false, doallmxops=false;
336 /* handle detout=true correctly when determinant=1
337 * reported to mailing list 2015-01-22, "Matrix inversion with detout = true?"
340 matrix ([1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1]) $
342 M^^-1, detout=true, doscmxops=false, doallmxops=false;
343 matrix ([1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1]) $
345 /* test the various matrix inversion functions to make sure they all handle detout correctly */
347 M^^-1, detout=true, doscmxops=false, doallmxops=false, invert_method='adjoint;
348 matrix ([1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1]) $
350 M^^-1, detout=true, doscmxops=false, doallmxops=false, invert_method='lu;
351 matrix ([1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1]) $
353 M^^-1, detout=true, doscmxops=false, doallmxops=false, invert_method='gausselim;
354 matrix ([1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1]) $
356 M : matrix ([17, 29], [1, 42]);
357 matrix ([17, 29], [1, 42]);
359 (M1 : ev (M^^-1, detout=true, doscmxops=false, doallmxops=false, invert_method='adjoint),
360 block ([inflag:true], [op(M1), args(M1)]));
361 ["*",[1/685,matrix([42,-29],[-1,17])]] $
363 (M1 : ev (M^^-1, detout=true, doscmxops=false, doallmxops=false, invert_method='lu),
364 block ([inflag:true], [op(M1), args(M1)]));
365 ["*",[1/685,matrix([42,-29],[-1,17])]] $
367 (M1 : ev (M^^-1, detout=true, doscmxops=false, doallmxops=false, invert_method='gausselim),
368 block ([inflag:true], [op(M1), args(M1)]));
369 ["*",[1/685,matrix([42,-29],[-1,17])]] $
372 /* a matrix of modest size, the subject of bug report #2362 */
374 (M:matrix([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
375 [0,1,0,-1,0,1,-1,-1,1,3,0,-3,0,3,1,-1,-3,-3,-1,1,3,3,-3,-3,3],
376 [0,0,1,0,-1,1,1,-1,-1,0,3,0,-3,1,3,3,1,-1,-3,-3,-1,3,3,-3,-3],
377 [0,1,0,1,0,1,1,1,1,9,0,9,0,9,1,1,9,9,1,1,9,9,9,9,9],
378 [0,0,1,0,1,1,1,1,1,0,9,0,9,1,9,9,1,1,9,9,1,9,9,9,9],
379 [0,0,0,0,0,1,-1,1,-1,0,0,0,0,3,3,-3,-3,3,3,-3,-3,9,-9,9,-9],
380 [0,1,0,-1,0,1,-1,-1,1,27,0,-27,0,27,1,-1,-27,-27,-1,1,27,27,-27,-27,27],
381 [0,0,0,0,0,1,1,-1,-1,0,0,0,0,9,3,3,9,-9,-3,-3,-9,27,27,-27,-27],
382 [0,0,0,0,0,1,-1,-1,1,0,0,0,0,3,9,-9,-3,-3,-9,9,3,27,-27,-27,27],
383 [0,0,1,0,-1,1,1,-1,-1,0,27,0,-27,1,27,27,1,-1,-27,-27,-1,27,27,-27,-27],
384 [0,1,0,1,0,1,1,1,1,81,0,81,0,81,1,1,81,81,1,1,81,81,81,81,81],
385 [0,0,0,0,0,1,-1,1,-1,0,0,0,0,27,3,-3,-27,27,3,-3,-27,81,-81,81,-81],
386 [0,0,0,0,0,1,1,1,1,0,0,0,0,9,9,9,9,9,9,9,9,81,81,81,81],
387 [0,0,0,0,0,1,-1,1,-1,0,0,0,0,3,27,-27,-3,3,27,-27,-3,81,-81,81,-81],
388 [0,0,1,0,1,1,1,1,1,0,81,0,81,1,81,81,1,1,81,81,1,81,81,81,81],
389 [0,0,0,0,0,1,1,-1,-1,0,0,0,0,81,3,3,81,-81,-3,-3,-81,243,243,-243,-243],
390 [0,0,0,0,0,1,-1,-1,1,0,0,0,0,27,9,-9,-27,-27,-9,9,27,243,-243,-243,243],
391 [0,0,0,0,0,1,1,-1,-1,0,0,0,0,9,27,27,9,-9,-27,-27,-9,243,243,-243,-243],
392 [0,0,0,0,0,1,-1,-1,1,0,0,0,0,3,81,-81,-3,-3,-81,81,3,243,-243,-243,243],
393 [0,0,0,0,0,1,1,1,1,0,0,0,0,81,9,9,81,81,9,9,81,729,729,729,729],
394 [0,0,0,0,0,1,-1,1,-1,0,0,0,0,27,27,-27,-27,27,27,-27,-27,729,-729,729,-729],
395 [0,0,0,0,0,1,1,1,1,0,0,0,0,9,81,81,9,9,81,81,9,729,729,729,729],
396 [0,0,0,0,0,1,1,-1,-1,0,0,0,0,81,27,27,81,-81,-27,-27,-81,2187,2187,-2187,-2187],
397 [0,0,0,0,0,1,-1,-1,1,0,0,0,0,27,81,-81,-27,-27,-81,81,27,2187,-2187,-2187,2187],
398 [0,0,0,0,0,1,1,1,1,0,0,0,0,81,81,81,81,81,81,81,81,6561,6561,6561,6561]),
400 matrix([1,0,0,-10/9,-10/9,0,0,0,0,0,1/9,0,100/81,0,1/9,0,0,0,0,-10/81,0,-10/81,0,0,1/81],
401 [0,9/16,0,9/16,0,0,-1/16,0,-5/8,0,-1/16,0,-5/8,0,0,0,5/72,0,1/16,5/72,0,1/16,0,-1/144,-1/144],
402 [0,0,9/16,0,9/16,0,0,-5/8,0,-1/16,0,0,-5/8,0,-1/16,1/16,0,5/72,0,1/16,0,5/72,-1/144,0,-1/144],
403 [0,-9/16,0,9/16,0,0,1/16,0,5/8,0,-1/16,0,-5/8,0,0,0,-5/72,0,-1/16,5/72,0,1/16,0,1/144,-1/144],
404 [0,0,-9/16,0,9/16,0,0,5/8,0,1/16,0,0,-5/8,0,-1/16,-1/16,0,-5/72,0,1/16,0,5/72,1/144,0,-1/144],
405 [0,0,0,0,0,81/256,0,81/256,81/256,0,0,-9/256,81/256,-9/256,0,-9/256,-9/256,-9/256,-9/256,-9/256,1/256,-9/256,1/256,1/256,1/256],
406 [0,0,0,0,0,-81/256,0,81/256,-81/256,0,0,9/256,81/256,9/256,0,-9/256,9/256,-9/256,9/256,-9/256,-1/256,-9/256,1/256,-1/256,1/256],
407 [0,0,0,0,0,81/256,0,-81/256,-81/256,0,0,-9/256,81/256,-9/256,0,9/256,9/256,9/256,9/256,-9/256,1/256,-9/256,-1/256,-1/256,1/256],
408 [0,0,0,0,0,-81/256,0,-81/256,81/256,0,0,9/256,81/256,9/256,0,9/256,-9/256,9/256,-9/256,-9/256,-1/256,-9/256,-1/256,1/256,1/256],
409 [0,-1/48,0,-1/144,0,0,1/48,0,5/216,0,1/144,0,5/648,0,0,0,-5/216,0,-1/432,-5/648,0,-1/1296,0,1/432,1/1296],
410 [0,0,-1/48,0,-1/144,0,0,5/216,0,1/48,0,0,5/648,0,1/144,-1/432,0,-5/216,0,-1/1296,0,-5/648,1/432,0,1/1296],
411 [0,1/48,0,-1/144,0,0,-1/48,0,-5/216,0,1/144,0,5/648,0,0,0,5/216,0,1/432,-5/648,0,-1/1296,0,-1/432,1/1296],
412 [0,0,1/48,0,-1/144,0,0,-5/216,0,-1/48,0,0,5/648,0,1/144,1/432,0,5/216,0,-1/1296,0,-5/648,-1/432,0,1/1296],
413 [0,0,0,0,0,-3/256,0,-1/256,-3/256,0,0,3/256,-1/256,1/768,0,1/256,3/256,1/2304,1/768,1/256,-1/768,1/2304,-1/2304,-1/768,-1/2304],
414 [0,0,0,0,0,-3/256,0,-3/256,-1/256,0,0,1/768,-1/256,3/256,0,1/768,1/2304,3/256,1/256,1/2304,-1/768,1/256,-1/768,-1/2304,-1/2304],
415 [0,0,0,0,0,3/256,0,-3/256,1/256,0,0,-1/768,-1/256,-3/256,0,1/768,-1/2304,3/256,-1/256,1/2304,1/768,1/256,-1/768,1/2304,-1/2304],
416 [0,0,0,0,0,3/256,0,-1/256,3/256,0,0,-3/256,-1/256,-1/768,0,1/256,-3/256,1/2304,-1/768,1/256,1/768,1/2304,-1/2304,1/768,-1/2304],
417 [0,0,0,0,0,-3/256,0,1/256,3/256,0,0,3/256,-1/256,1/768,0,-1/256,-3/256,-1/2304,-1/768,1/256,-1/768,1/2304,1/2304,1/768,-1/2304],
418 [0,0,0,0,0,-3/256,0,3/256,1/256,0,0,1/768,-1/256,3/256,0,-1/768,-1/2304,-3/256,-1/256,1/2304,-1/768,1/256,1/768,1/2304,-1/2304],
419 [0,0,0,0,0,3/256,0,3/256,-1/256,0,0,-1/768,-1/256,-3/256,0,-1/768,1/2304,-3/256,1/256,1/2304,1/768,1/256,1/768,-1/2304,-1/2304],
420 [0,0,0,0,0,3/256,0,1/256,-3/256,0,0,-3/256,-1/256,-1/768,0,-1/256,3/256,-1/2304,1/768,1/256,1/768,1/2304,1/2304,-1/768,-1/2304],
421 [0,0,0,0,0,1/2304,0,1/6912,1/6912,0,0,-1/2304,1/20736,-1/2304,0,-1/6912,-1/6912,-1/6912,-1/6912,-1/20736,1/2304,-1/20736,1/6912,1/6912,1/20736],
422 [0,0,0,0,0,-1/2304,0,1/6912,-1/6912,0,0,1/2304,1/20736,1/2304,0,-1/6912,1/6912,-1/6912,1/6912,-1/20736,-1/2304,-1/20736,1/6912,-1/6912,1/20736],
423 [0,0,0,0,0,1/2304,0,-1/6912,-1/6912,0,0,-1/2304,1/20736,-1/2304,0,1/6912,1/6912,1/6912,1/6912,-1/20736,1/2304,-1/20736,-1/6912,-1/6912,1/20736],
424 [0,0,0,0,0,-1/2304,0,-1/6912,1/6912,0,0,1/2304,1/20736,1/2304,0,1/6912,-1/6912,1/6912,-1/6912,-1/20736,-1/2304,-1/20736,-1/6912,1/6912,1/20736])$
426 /* 16 by 16 example from mailing list 2013-06-27 */
429 K:matrix([1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,494.5054945054945,-178.5714285714286,0,0,54.94505494505494,-13.73626373626375,0,-4.578754578754582,109.8901098901099,4.578754578754582,0,59.52380952380952,-73.26007326007327,-59.52380952380952],[0,0,-178.5714285714286,494.5054945054945,0,0,13.73626373626375,-302.1978021978022,0,-73.26007326007327,4.578754578754582,109.8901098901099,0,-73.26007326007327,-4.578754578754582,109.8901098901099],[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0],[0,0,54.94505494505494,13.73626373626375,0,0,494.5054945054945,178.5714285714286,0,59.52380952380952,73.26007326007327,-59.52380952380952,0,-4.578754578754582,-109.8901098901099,4.578754578754582],[0,0,-13.73626373626375,-302.1978021978022,0,0,178.5714285714286,494.5054945054945,0,73.26007326007327,-4.578754578754582,-109.8901098901099,0,73.26007326007327,4.578754578754582,-109.8901098901099],[0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],[0,0,-4.578754578754582,-73.26007326007327,0,0,59.52380952380952,73.26007326007327,0,110.2389673818245,19.84126984126984,-48.49119134833421,0,7.674864817721962,-19.84126984126984,-22.85016570730857],[0,0,109.8901098901099,4.578754578754582,0,0,73.26007326007327,-4.578754578754582,0,19.84126984126984,110.2389673818245,-19.84126984126984,0,19.84126984126984,-48.49119134833421,-19.84126984126984],[0,0,4.578754578754582,109.8901098901099,0,0,-59.52380952380952,-109.8901098901099,0,-48.49119134833421,-19.84126984126984,110.2389673818245,0,-22.85016570730857,19.84126984126984,7.674864817721962],[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0],[0,0,59.52380952380952,-73.26007326007327,0,0,-4.578754578754582,73.26007326007327,0,7.674864817721962,19.84126984126984,-22.85016570730857,0,110.2389673818245,-19.84126984126984,-48.49119134833421],[0,0,-73.26007326007327,-4.578754578754582,0,0,-109.8901098901099,4.578754578754582,0,-19.84126984126984,-48.49119134833421,19.84126984126984,0,-19.84126984126984,110.2389673818245,19.84126984126984],[0,0,-59.52380952380952,109.8901098901099,0,0,4.578754578754582,-109.8901098901099,0,-22.85016570730857,-19.84126984126984,7.674864817721962,0,-48.49119134833421,19.84126984126984,110.2389673818245]),
431 is (mat_norm (K . invK - ident (16), inf) < 1e-14));
434 /* 4 by 4 example from mailing list 2013-04-16 */
436 (kill (invert_R_from_RealRefraction, R_from_RealRefraction, G, u, a, b, c, d),
437 R_from_RealRefraction: matrix(
438 [ G[a]^2-1, -G[a]*u[a,1], -G[a]*u[a,2] , -G[a]*u[a,3] ],
439 [ -G[b]*u[b,1], 1+u[b,1]^2, u[b,1]*u[b,2], u[b,1]*u[b,3] ],
440 [ -G[c]*u[c,2], u[c,2]*u[c,1], 1+u[c,2]^2, u[c,2]*u[c,3] ],
441 [ -G[d]*u[d,3], u[d,3]*u[d,1], u[d,3]*u[d,2], 1+u[d,3]^2 ]
443 invert_R_from_RealRefraction: invert(R_from_RealRefraction),
444 ratsimp (invert_R_from_RealRefraction . R_from_RealRefraction - ident (4)));
445 matrix ([0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]);
447 /* invert_by_adjoint exists */
449 (kill (foo, foo_inv),
450 foo : matrix ([1, 7, -20], [-1, 4, -2], [3, -2, 7]),
451 foo_inv : invert_by_adjoint (foo),
453 matrix ([1, 0, 0], [0, 1, 0], [0, 0, 1]);
455 /* invert_by_gausselim exists */
457 (kill (foo, foo_inv),
458 foo : matrix ([1, 7, -20], [-1, 4, -2], [3, -2, 7]),
459 foo_inv : invert_by_gausselim (foo),
461 matrix ([1, 0, 0], [0, 1, 0], [0, 0, 1]);
463 /* end additional tests for invert */
465 /* float inf / SIGN1 mischief -- see also rtest_extra */
473 /* verify that verbify is not applied to array name
474 * SF bug #2865: "Use exp as a list"
493 /* verify that $array does not apply $verbify to array name */
496 apply (array, [nounify ('foo), 10]),
497 member (nounify ('foo), arrays));
500 /* verify that arrstore does not apply $verbify to array name */
502 ((nounify ('foo)) [0] :: 123,
503 first (listarray (nounify ('foo))));
512 /* verify that mdefine does not apply $verbify to array function name */
514 (kill (foo, x, a), define (funmake (arraymake (nounify ('foo), [x]), [a]), x^a));
517 member (nounify (foo), arrays);
520 /* verify that mdefine does not apply $verbify to function name */
522 (kill (bar), define (funmake (nounify ('bar), ['x]), 2*'x), 0);
531 /* verify that consfundef does not apply $verbify to function name */
533 apply (fundef, [nounify ('bar)]);
536 /* verify that consfundef does not apply $verbify to array function name */
538 apply (fundef, [nounify ('foo)]);
541 /* function and array names which are strings are verbified, however */
558 member ('(bar(x)), functions);
570 "quux"[u](v) := u - v;
573 member (baz, arrays);
576 member (quux, arrays);
579 /* Bug #481: ('m)[1] (meval) */
586 kill (functions, arrays);
589 /* Verify that we catch malformed lambda expressions when they are simplified.
590 * These used to only be checked for if/when lambda expressions were applied
594 /* no parameter list */
595 errcatch (lambda ())$
599 errcatch (lambda ([x]))$
602 /* non-symbol in parameter list */
603 errcatch (lambda ([42], 'foo))$
606 /* misplaced list parameter (for optional arguments) */
607 errcatch (lambda ([[l], x], 'foo))$
610 /* invalid list parameter (for optional arguments) */
611 errcatch (lambda ([[l1, l2]], 'foo))$
614 /* attempting to bind a constant is allowed */
617 declare (c, constant),
618 errcatch (lambda ([c], c)))$
621 /* Verify that the parameter/variable lists of functions, lambda expressions,
622 * macros and blocks cannot contain duplicate variables. Lots of cases...
625 errcatch (foo (x, x) := x)$
628 errcatch (foo (x, 'x) := x)$
631 errcatch (foo (x, [x]) := x)$
634 errcatch (foo (x, ['x]) := x)$
637 errcatch (foo [x, x] := x)$
640 errcatch (foo [x] (y, y) := y)$
643 errcatch (foo [x] (y, 'y) := y)$
646 errcatch (foo [x] (y, [y]) := y)$
649 errcatch (foo [x] (y, ['y]) := y)$
652 errcatch (foo [x, x] (y) := y)$
655 errcatch (lambda ([x, x], x))$
658 errcatch (lambda ([x, 'x], x))$
661 errcatch (lambda ([x, [x]], x))$
664 errcatch (lambda ([x, ['x]], x))$
667 errcatch (foo (x, x) ::= x)$
670 errcatch (foo (x, [x]) ::= x)$
673 errcatch (block ([x, x], x))$
676 errcatch (block ([x, x:foo], x))$
679 /* try to verify that save(...) handles arrays correctly */
682 myundeclared[1234, foo] : sin(bar),
683 myundeclared[bar, baz + 1] : cos(quux),
684 myundeclared[1729, u^2] : tan(bar),
685 myundeclared[mumble, 7] : 9876,
686 array (mydeclared, 1, 1, 1),
687 mydeclared[0, 0, 0] : bar + 0,
688 mydeclared[0, 0, 1] : bar + 1,
689 mydeclared[0, 1, 0] : bar + 2,
690 mydeclared[0, 1, 1] : bar + 3,
691 mydeclared[1, 0, 0] : bar + 4,
692 mydeclared[1, 0, 1] : bar + 5,
693 mydeclared[1, 1, 0] : bar + 6,
694 mydeclared[1, 1, 1] : bar + 7,
695 myvalue : make_array (fixnum, 2, 2, 2, 2),
696 myvalue[0, 0, 0, 0] : %pi + 1,
697 myvalue[1, 1, 1, 1] : %e - 1,
698 use_fast_arrays : true,
699 myfast[foo, bar, baz] : blurf,
700 myfast["mumble", "abc", "xy", "Z"] : 2*blurf,
701 myfast[sin(foo), 1 - baz] : 3*blurf,
702 reset (use_fast_arrays),
703 save (sconcat (maxima_tempdir, "/tmpsavearrays.lisp"), arrays, values),
708 [[myundeclared, mydeclared], [myvalue, myfast]];
710 arrayinfo (myundeclared);
711 [hashed, 2, [1234,foo],[1729,u^2],[bar,baz+1],[mumble,7]];
713 listarray (myundeclared);
714 [sin(bar), tan(bar), cos(quux), 9876];
716 arrayinfo (mydeclared);
717 [declared, 3, [1, 1, 1]];
719 listarray (mydeclared);
720 [bar, bar + 1, bar + 2, bar + 3, bar + 4, bar + 5, bar + 6, bar + 7];
723 [declared, 4, [1, 1, 1, 1]]; /* "declared" seems wrong here ... oh well */
726 [%pi + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, %e - 1];
728 (arrayinfo (myfast), [%%[1], %%[2], sort (rest (%%, 2))]);
729 [hash_table, true, [["mumble", "abc", "xy", "Z"], [foo, bar, baz], [sin(foo), 1 - baz]]];
731 sort (listarray (myfast));
732 [blurf, 2*blurf, 3*blurf];
734 kill (myundeclared, mydeclared, myvalue, myfast);
740 [errcatch (arrayinfo (myundeclared)),
741 errcatch (arrayinfo (mydeclared)),
742 errcatch (arrayinfo (myvalue)),
743 errcatch (arrayinfo (myfast))];
746 [errcatch (listarray (myundeclared)),
747 errcatch (listarray (mydeclared)),
748 errcatch (listarray (myvalue)),
749 errcatch (listarray (myfast))];
752 (load (sconcat (maxima_tempdir, "/tmpsavearrays.lisp")), 0);
756 [[myundeclared, mydeclared], [myvalue, myfast]];
758 arrayinfo (myundeclared);
759 [hashed, 2, [1234,foo],[1729,u^2],[bar,baz+1],[mumble,7]];
761 listarray (myundeclared);
762 [sin(bar), tan(bar), cos(quux), 9876];
764 arrayinfo (mydeclared);
765 [declared, 3, [1, 1, 1]];
767 listarray (mydeclared);
768 [bar, bar + 1, bar + 2, bar + 3, bar + 4, bar + 5, bar + 6, bar + 7];
771 [declared, 4, [1, 1, 1, 1]]; /* "declared" seems wrong here ... oh well */
774 [%pi + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, %e - 1];
776 (arrayinfo (myfast), [%%[1], %%[2], sort (rest (%%, 2))]);
777 [hash_table, true, [["mumble", "abc", "xy", "Z"], [foo, bar, baz], [sin(foo), 1 - baz]]];
779 sort (listarray (myfast));
780 [blurf, 2*blurf, 3*blurf];
784 (kill (a, b, z), mylist : [a, 3, %pi, b, -2, %e, z, 0, %phi], 0);
797 [a, 3, %pi, b, -2, %e, z, 0];
800 [a, 3, %pi, b, -2, %e, z, 0, %phi];
803 [a, 3, %pi, b, -2, %e, z, 0, %phi];
805 firstn (mylist, 100);
806 [a, 3, %pi, b, -2, %e, z, 0, %phi];
808 errcatch (firstn (mylist, -4));
821 [3, %pi, b, -2, %e, z, 0, %phi];
824 [a, 3, %pi, b, -2, %e, z, 0, %phi];
827 [a, 3, %pi, b, -2, %e, z, 0, %phi];
830 [a, 3, %pi, b, -2, %e, z, 0, %phi];
832 errcatch (lastn (mylist, -4));
835 (kill (foo), fooexpr : funmake (foo, mylist), 0);
848 foo(a, 3, %pi, b, -2, %e, z, 0);
851 foo(a, 3, %pi, b, -2, %e, z, 0, %phi);
853 firstn (fooexpr, 10);
854 foo(a, 3, %pi, b, -2, %e, z, 0, %phi);
856 firstn (fooexpr, 100);
857 foo(a, 3, %pi, b, -2, %e, z, 0, %phi);
859 errcatch (firstn (fooexpr, -4));
872 foo(3, %pi, b, -2, %e, z, 0, %phi);
875 foo(a, 3, %pi, b, -2, %e, z, 0, %phi);
878 foo(a, 3, %pi, b, -2, %e, z, 0, %phi);
880 lastn (fooexpr, 100);
881 foo(a, 3, %pi, b, -2, %e, z, 0, %phi);
883 errcatch (lastn (fooexpr, -4));
886 (kill (bar), nary ("bar"));
889 barexpr : apply ("bar", mylist);
890 a bar 3 bar %pi bar b bar (-2) bar %e bar z bar 0 bar %phi;
899 a bar 3 bar %pi bar b;
902 a bar 3 bar %pi bar b bar (-2) bar %e bar z bar 0;
905 a bar 3 bar %pi bar b bar (-2) bar %e bar z bar 0 bar %phi;
907 firstn (barexpr, 10);
908 a bar 3 bar %pi bar b bar (-2) bar %e bar z bar 0 bar %phi;
910 firstn (barexpr, 100);
911 a bar 3 bar %pi bar b bar (-2) bar %e bar z bar 0 bar %phi;
913 errcatch (firstn (barexpr, -4));
923 %e bar z bar 0 bar %phi;
926 3 bar %pi bar b bar (-2) bar %e bar z bar 0 bar %phi;
929 a bar 3 bar %pi bar b bar (-2) bar %e bar z bar 0 bar %phi;
932 a bar 3 bar %pi bar b bar (-2) bar %e bar z bar 0 bar %phi;
934 lastn (barexpr, 100);
935 a bar 3 bar %pi bar b bar (-2) bar %e bar z bar 0 bar %phi;
937 errcatch (lastn (barexpr, -4));
940 (kill ("bar", baz, k), bazexpr : funmake (baz[k], mylist), 0);
950 baz[k](a, 3, %pi, b);
953 baz[k](a, 3, %pi, b, -2, %e, z, 0);
956 baz[k](a, 3, %pi, b, -2, %e, z, 0, %phi);
958 firstn (bazexpr, 10);
959 baz[k](a, 3, %pi, b, -2, %e, z, 0, %phi);
961 firstn (bazexpr, 100);
962 baz[k](a, 3, %pi, b, -2, %e, z, 0, %phi);
964 errcatch (firstn (bazexpr, -4));
974 baz[k](%e, z, 0, %phi);
977 baz[k](3, %pi, b, -2, %e, z, 0, %phi);
980 baz[k](a, 3, %pi, b, -2, %e, z, 0, %phi);
983 baz[k](a, 3, %pi, b, -2, %e, z, 0, %phi);
985 lastn (bazexpr, 100);
986 baz[k](a, 3, %pi, b, -2, %e, z, 0, %phi);
988 errcatch (lastn (bazexpr, -4));
991 /* bug reported to mailing list 2017-09-05: "compiling a function including sorting" */
993 (kill(test), test(L):=sort(L,lambda([s,t],s[2]>t[2])), 0);
996 (L:[[b,2],[a,1],[d,4],[c,3]], test (L));
997 [[d,4],[c,3],[b,2],[a,1]];
999 (compile (test), test (L));
1000 [[d,4],[c,3],[b,2],[a,1]];
1002 (mycmp (s, t) := s[2]>t[2], 0);
1006 [[d,4],[c,3],[b,2],[a,1]];
1008 (compile (mycmp), is (?fboundp (mycmp) # false));
1012 [[d,4],[c,3],[b,2],[a,1]];
1015 /* bug reported to mailing list 2017-12-21: "array() with use_fast_arrays: true" */
1017 (use_fast_arrays : true,
1018 kill (a, b, a0, a1),
1023 map (?length, [a, b]);
1037 /* mailing list 2017-12-28: "op('(do 1))" */
1039 (e : '(for i:111 thru 114 do push(i, L)),
1041 ["do", [i,111,false,false,114,false,push(i,L)]];
1043 e1 : apply (funmake, ["do", '[i,111,false,false,114,false,push(i,L)]]);
1044 for i:111 thru 114 do push(i, L);
1046 block ([L:[]], ev(e1), L);
1047 [114, 113, 112, 111];
1049 (e : '(for x in [100, 200, 300] do push(x - 1, M)),
1051 ["do_in", [x,[100,200,300],false,false,false,false,push(x-1,M)]];
1053 e1 : apply (funmake, ["do_in", '[x,[100,200,300],false,false,false,false,push(x-1,M)]]);
1054 for x in [100, 200, 300] do push(x - 1, M);
1056 block ([M:[]], ev(e1), M);
1059 (reset(use_fast_arrays),1);
1062 /* ensure arraymake cannot create an invalid expression
1063 * follow-on work to commit 3140a35
1066 (kill (foo, bar, baz, quux),
1067 arraymake (foo, [bar]));
1070 arraymake (foo[bar], [baz]);
1073 arraymake (foo[bar][baz], [quux]);
1074 foo[bar][baz][quux];
1076 arraymake (foo[bar, baz], [111, 222]);
1077 foo[bar, baz][111, 222];
1079 errcatch (arraymake (1, [bar]));
1082 errcatch (arraymake ("foo", [bar]));
1085 errcatch (arraymake (lambda([x], x), [bar]));
1088 /* slightly unusual constructs which do yield valid expressions, and are therefore permissible */
1090 arraymake (foo + bar, [baz]);
1093 arraymake (foo(bar), [baz]);