Rename *ll* and *ul* to ll and ul in in-interval
[maxima.git] / share / linearalgebra / rtest_linalg.mac
blob2c27f0c9620068c9ba41c68fe5457784e6130390
1 (matrix_element_add : "+", matrix_element_mult : "*",0);
2 0$
4 /*--- ctranspose -----*/
6 (m : matrix([%i,1],[2,%i],[0,2]),0);
7 0$
9 ctranspose(m);
10 matrix([-%i,2,0],[1,-%i,2])$
12 zeromatrixp(m - ctranspose(ctranspose(m)));
13 true$
15 (m : matrix([m,m]),0);
18 (matrix_element_transpose : 'transpose,0);
21 zeromatrixp(ctranspose(mat_unblocker(m)) - mat_unblocker(ctranspose(m)));
22 true$
24 ctranspose(matrix([%i]));
25 matrix([-%i])$
27 (matrix_element_transpose : false,0);
30 /*--- matrix norm ----*/
32 (m : matrix([2,3],[6,7]), 0);
35 mat_norm(m,1);
36 10$
38 mat_norm(m,'inf);
39 13$
41 mat_norm(m,'frobenius);
42 sqrt(2^2 + 3^2 + 6^2 + 7^2)$
44 (m : matrix([2*%i,3],[6,7]), 0);
47 mat_norm(m,1);
48 10$
50 mat_norm(m,'inf);
51 13$
53 mat_norm(m,'frobenius);
54 sqrt(2^2 + 3^2 + 6^2 + 7^2)$
57 /*--- matrix size ----*/
59 matrix_size(matrix([1]));
60 [1,1]$
62 matrix_size(matrix([false],[false]));
63 [2,1]$
65 matrix_size(matrix([false,false]));
66 [1,2]$
68 /*--- matrix linalg_rank and nullity -----*/
70 linalg_rank(matrix([0]));
73 nullity(matrix([0]));
76 linalg_rank(matrix([1]));
79 nullity(matrix([1]));
82 linalg_rank(matrix([0,0]));
85 nullity(matrix([0,0]));
88 linalg_rank(matrix([0,1]));
91 nullity(matrix([0,1]));
94 linalg_rank(matrix([0],[0]));
97 nullity(matrix([0],[0]));
100 linalg_rank(matrix([1],[0]));
103 nullity(matrix([1],[0]));
106 linalg_rank(matrix([0],[1]));
109 nullity(matrix([0],[1]));
112 linalg_rank(matrix([1],[1]));
115 nullity(matrix([1],[1]));
118 linalg_rank(matrix([0],[%i]));
121 nullity(matrix([0],[%i]));
124 linalg_rank(matrix([1],[1]));
127 nullity(matrix([1],[1]));
130 linalg_rank(matrix([1],[%i]));
133 nullity(matrix([1],[%i]));
136 linalg_rank(matrix([1,2],[0,0]));
139 nullity(matrix([1,2],[0,0]));
142 linalg_rank(matrix([1,2],[0,1]));
145 nullity(matrix([1,2],[0,1]));
149 /*--- dotproducts ------*/
151 dotproduct(matrix([1]),matrix([1]));
154 dotproduct(matrix([%i]),matrix([%i]));
157 dotproduct(matrix([1],[2]),matrix([-2],[1]));
160 dotproduct(matrix([1],[%i]),matrix([1],[%i]));
163 /*--- matrix nullity ----*/
165 nullity(matrix([0]));
168 nullity(matrix([1]));
171 nullity(matrix([0,0]));
174 nullity(matrix([0,1]));
177 nullity(matrix([1,0]));
180 nullity(matrix([1,2],[0,0]));
183 nullity(matrix([1,2],[0,1]));
186 /*---orthogonal complements ---*/
188 (m : matrix([1,2,3]),0);
191 (s1 : args(columnspace(m)),0);
194 (s2 : args(nullspace(ctranspose(m))),0);
197 every(lambda([e], e=0), map(lambda([e], apply('dotproduct,e)), 
198      cartesian_product(setify(s1),setify(s2))));
199 true$
201 /*----*/
203 (m : matrix([0,0,3]),0);
206 (s1 : args(columnspace(m)),0);
209 (s2 : args(nullspace(ctranspose(m))),0);
212 every(lambda([e], e=0), map(lambda([e], apply('dotproduct,e)), 
213      cartesian_product(setify(s1),setify(s2))));
215 true$
217 /*----*/
219 (m : matrix([0,0,0]),0);
222 (s1 : args(columnspace(m)),0);
225 (s2 : args(nullspace(ctranspose(m))),0);
228 every(lambda([e], e=0), map(lambda([e], apply('dotproduct,e)), 
229      cartesian_product(setify(s1),setify(s2))));
231 true$
233 /*----*/
235 (m : matrix([0,0,3],[0,0,1]),0);
238 (s1 : args(columnspace(m)),0);
241 (s2 : args(nullspace(ctranspose(m))),0);
244 every(lambda([e], e=0), map(lambda([e], apply('dotproduct,e)), 
245      cartesian_product(setify(s1),setify(s2))));
247 true$
249 /*----*/
251 (m : matrix([1,2,3],[%i,2,1]),0);
254 (s1 : args(columnspace(m)),0);
257 (s2 : args(nullspace(ctranspose(m))),0);
260 every(lambda([e], e=0), map(lambda([e], apply('dotproduct,e)), 
261      cartesian_product(setify(s1),setify(s2))));
263 true$
266 /*----*/
268 (m : matrix([1,2,3],[%i,2,1],[1,2,3],[1,2,3]),0);
271 (s1 : args(columnspace(m)),0);
274 (s2 : args(nullspace(ctranspose(m))),0);
277 every(lambda([e], e=0), map(lambda([e], expand(apply('dotproduct,e))), 
278      cartesian_product(setify(s1),setify(s2))));
280 true$
282 /*----*/
284 (m : matrix([1,2,3],[4,5,6],[7,8,9]),0);
287 (s1 : args(columnspace(m)),0);
290 (s2 : args(nullspace(ctranspose(m))),0);
293 every(lambda([e], e=0), map(lambda([e], apply('dotproduct,e)), 
294      cartesian_product(setify(s1),setify(s2))));
296 true$
299 /*-----*/
301 ptriangularize(matrix([1]),z);
302 matrix([1])$
304 ptriangularize(matrix([0]),z);
305 matrix([0])$
307 ptriangularize(matrix([z]),z);
308 matrix([z])$
310 (m : matrix([1,2],[3,4]) - z*ident(2),0);
313 abs(ratsimp(determinant(m) / determinant(ptriangularize(m,z))));
316 (m : matrix([1,2,3],[4,5,6], [7,8,9]) - z*ident(3),0);
319 abs(ratsimp(determinant(m) / determinant(ptriangularize(m,z))));
322 (m : matrix([1-z,2,3-z],[4,5-z,6], [7-z,8,9+z]),0);
325 abs(ratsimp(determinant(m) / determinant(ptriangularize(m,z))));
328 locate_matrix_entry(matrix([1]),1,1,1,1,lambda([e],e # 0), 'bool);
329 [1,1]$
331 locate_matrix_entry(matrix([0]),1,1,1,1,lambda([e],e # 0), 'bool);
332 false$
334 locate_matrix_entry(matrix([1]),1,1,1,1,identity, 'min);
335 [1,1]$
337 locate_matrix_entry(matrix([1]),1,1,1,1,identity, 'max);
338 [1,1]$
340 locate_matrix_entry(matrix([1,1]),1,1,1,2,identity, 'min);
341 [1,1]$
343 locate_matrix_entry(matrix([1,1]),1,1,1,2,identity, 'max);
344 [1,1]$
346 locate_matrix_entry(matrix([1,2]),1,1,1,2,identity, 'max);
347 [1,2]$
349 locate_matrix_entry(matrix([1,2]),1,1,1,2,identity, 'min);
350 [1,1]$
352 (m : matrix([1,2,3],[4,5,6],[7,8,9]),0);
355 locate_matrix_entry(m,1,1,3,3,lambda([e],e+%pi), min);
356 [1,1]$
358 locate_matrix_entry(m,1,1,3,3,lambda([e],e+%pi), max);
359 [3,3]$
361 locate_matrix_entry(m,1,2,3,3,lambda([e],e+%pi), min);
362 [1,2]$
364 locate_matrix_entry(m,1,2,3,3,lambda([e],e+%pi), max);
365 [3,3]$
367 columnop(matrix([2,1]),1,2,-2);
368 matrix([4,1])$
370 columnop(matrix([2,1]),2,1,x);
371 matrix([2,1-2*x])$
373 columnswap(matrix([a,b]),1,2);
374 matrix([b,a])$
376 columnswap(matrix([a,b,c]),2,3);
377 matrix([a,c,b])$
379 columnswap(columnswap(matrix([a,b],[c,d]),1,2),1,2);
380 matrix([a,b],[c,d])$
382 ctranspose(matrix([5+%i*6]));
383 matrix([5-%i*6])$
385 ctranspose(matrix([1],[%i]));
386 matrix([1,-%i])$
388 (load(unwind_protect), unwind_protect((p[1] : 2014, rowop(matrix([a],[b]),1,2,x)), remarray(p)));
389 matrix([a-b*x],[b])$
391 rowop(matrix([a],[b]),1,2,x);
392 matrix([a-b*x],[b]);
394 rowswap(rowswap(matrix([a,b],[c,d]),1,2),1,2);
395 matrix([a,b],[c,d])$
397 (m : matrix([0,1,1],[0,0,0],[0,0,0]),0);
400 nullspace(m);
401 ''(funmake('span, sort([matrix([1],[0],[0]),matrix([0],[-1],[1])])))$
403 columnspace(m);
404 span(matrix([1],[0],[0]))$
406 zeromatrixp(ctranspose(apply(addcol,args(columnspace(ctranspose(m))))) . apply(addcol,args(nullspace(m))));
407 true$
409 (m : matrix([0,1,2,3],[0,0,1,0],[0,0,0,0]),0);
412 zeromatrixp(ctranspose(apply(addcol,args(columnspace(ctranspose(m))))) . apply(addcol,args(nullspace(m))));
413 true$
415 (m : matrix([0,0,2,3],[0,0,1,0],[0,0,0,0]),0);
418 zeromatrixp(ctranspose(apply(addcol,args(columnspace(ctranspose(m))))) . apply(addcol,args(nullspace(m))));
419 true$
421 (m : matrix([0,0,%i,3],[0,0,%i,0],[0,0,0,0]),0);
424 zeromatrixp(ctranspose(apply(addcol,args(columnspace(ctranspose(m))))) . apply(addcol,args(nullspace(m))));
425 true$
427 (remvalue(s1,s2,m),0);
430 hilbert_matrix(2);
431 matrix([1,1/2],[1/2,1/3])$
433 kronecker_product(matrix([a]),matrix([b]));
434 matrix([a*b]);
436 kronecker_product(matrix([1,2],[3,4]),matrix([a,b],[c,d]));
437 ''(mat_unblocker(matrix([matrix([a,b],[c,d]), 2*matrix([a,b],[c,d])],[3*matrix([a,b],[c,d]),4*matrix([a,b],[c,d])])))$
439 every(lambda([e], is(e < 0)), [-1,-2,3]);
440 false$
442 every(lambda([e], ratnump(e)), [-1,-2,3]);
443 true$
445 every('bfloatp, matrix([1.3b0,7.8b0],[5.7b0, 9.1b0]));
446 true$
448 every('bfloatp, matrix([matrix([1.3b0,7.8b0],[5.7b0, 7/8])]));
449 false$
451 polytocompanion(a*x^3+b*x^2+c*x+d,x);
452 matrix([0,0,-d/a],[1,0,-c/a],[0,1,-b/a])$
454 vandermonde_matrix([a]);
455 matrix([1])$
457 vandermonde_matrix([a,b]);
458 matrix([1,a],[1,b])$
460 vandermonde_matrix([]);
461 matrix()$
463 /* SF bug #1659985 */
465 vandermonde_matrix([0]);
466 matrix([1])$
468 vandermonde_matrix([0,1]);
469 matrix([1,0],[1,1])$
472 /*--- copylist and copymatrix ----*/
474 (m :  [1,[2,3]],0);
477 (mm : copylist(m),0);
480 (mm[2][1] : x,0);
483 is(m[2][1] = 2);
484 true$
486 (m :  matrix([1,2],[3,4]),0);
489 (mm : copymatrix(m),0);
492 (mm[2,2] : x,0);
495 is(m[2,2] = 4);
496 true$
498 /*-- mat_unblocker & friends --*/
500 (m : matrix([0,1],[1,0]),0);
503 (m1 : matrix([m,m],[m,m]),0);
506 mat_unblocker(m1);
507 matrix([0,1,0,1],[1,0,1,0],[0,1,0,1],[1,0,1,0])$
509 mat_fullunblocker(m1);
510 matrix([0,1,0,1],[1,0,1,0],[0,1,0,1],[1,0,1,0])$
512 (m2 : matrix([m1,m1],[m1,m1]),0);
515 mat_unblocker(m2);
516 ''(matrix([m,m,m,m],[m,m,m,m],[m,m,m,m],[m,m,m,m]))$
518 mat_fullunblocker(m2);
519  matrix([0,1,0,1,0,1,0,1],[1,0,1,0,1,0,1,0],[0,1,0,1,0,1,0,1],[1,0,1,0,1,0,1,0],[0,1,0,1,0,1,0,1],[1,0,1,0,1,0,1,0],[0,1,0,1,0,1,0,1],[1,0,1,0,1,0,1,0])$
521 mat_trace(m1);
524 mat_trace(identfor(m1));
527 mat_trace(m1 + identfor(m1));
530 mat_norm(m1,1);
533 mat_norm(m2,inf) - mat_norm(mat_fullunblocker(m2),inf);
536 blockmatrixp(m);
537 false$
539 blockmatrixp(m1);
540 true$
542 blockmatrixp(2/3);
543 false$
545 blockmatrixp(rat(x));
546 false$
548 /*---add matrices---*/
550 (m1 : matrix([8,2],[7,4]),0);
553 (m2 : matrix([1,2],[3,42]),0);
556 (m3 : matrix([96,5],[3,42]),0);
559 addmatrices('max,m1,m2,m3);
560 matrix([96,5],[7,42])$
562 addmatrices("+", m1,m2);
563 matrix([9,4],[10,46])$
565 toeplitz([1,2,3],[x,y,z]);
566 matrix([1,y,z],[2,1,y],[3,2,1])$
568 toeplitz([1,2,3]);
569 matrix([1,2,3],[2,1,2],[3,2,1])$
571 toeplitz([1,2,3],[x]);
572 matrix([1],[2],[3])$
574 hankel([1,2,3]);
575 matrix([1,2,3],[2,3,0],[3,0,0])$
577 hankel([1,2,3],[x]);
578 matrix([1],[2],[3])$
580 diag_matrix(1);
581 matrix([1])$
583 diag_matrix(1,2);
584 matrix([1,0],[0,2])$
586 diag_matrix(diag_matrix(1,2),diag_matrix(3,4));
587 matrix([matrix([1,0],[0,2]),matrix([0,0],[0,0])],[matrix([0,0],[0,0]),matrix([3,0],[0,4])])$
589 identfor(diag_matrix(diag_matrix(1,2),diag_matrix(3,4)));
590 ''(diag_matrix(diag_matrix(1,1),diag_matrix(1,1)))$
592 zerofor(diag_matrix(diag_matrix(1,2),diag_matrix(3,4)));
593 ''(diag_matrix(diag_matrix(0,0),diag_matrix(0,0)))$
595 (m : diag_matrix(diag_matrix(5,6),diag_matrix(8,10)),0);
598 mat_norm(m,inf) - mat_norm(mat_unblocker(m),inf);
601 mat_trace(m) - mat_trace(mat_unblocker(m));
604 hilbert_matrix(3);
605 matrix([1,1/2,1/3],[1/2,1/3,1/4],[1/3,1/4,1/5])$
607 vandermonde_matrix([a,b,c]);
608 matrix([1,a,a^2],[1,b,b^2],[1,c,c^2])$
610 every('integerp, [1,2,3]);
611 true$
613 every('integerp, []);
614 true$
616 every('integerp, [1,2,3,x]);
617 false$
619 every('integerp, [false]);
620 false$
622 every(lambda([x], is (x < 5)), [4,5,6,7]);
623 false$
625 errcatch(every('integerp, false));
628 every(lambda([x], is(x < 1932)), matrix([1,2,3],[8,8,74]));
629 true$
631 (remvalue(mm,m1,m2,m3,m),0);
634 columnspace(matrix([0,1],[0,1]));
635 span(matrix([1],[1]))$
637 linalg_rank(matrix([0,1],[0,1]));
640 sylvester_matrix(0, 0, x);
641 matrix()$
643 sylvester_matrix(1, 0, x);
644 matrix()$
646 sylvester_matrix(0, 1, x);
647 matrix()$
649 sylvester_matrix(1, 1, x);
650 matrix()$
652 sylvester_matrix(x, 1, x);
653 matrix([1])$
655 sylvester_matrix(1, x, x);
656 matrix([1])$
658 sylvester_matrix(x, x, x);
659 matrix([1,0],[1,0])$
661 sylvester_matrix(-x, x, x);
662 matrix([-1,0],[1,0])$
664 sylvester_matrix(a+b*x, x, x);
665 matrix([b,a],[1,0])$
667 sylvester_matrix(a+b*x, x^2, x);
668 matrix([b,a,0],[0,b,a],[1,0,0])$
670 /* blockmatrixp -- SF bug # 1905668*/
672 blockmatrixp(matrix());
673 false$
675 blockmatrixp(matrix([]));
676 false$
678 blockmatrixp(matrix([],[]));
679 false$
681 blockmatrixp(matrix([false]));
682 false$
684 blockmatrixp(matrix([matrix()]));
685 true$
687 blockmatrixp(matrix([matrix([false])]));
688 true$
690 mat_cond(matrix([1,2,3],[4,5,6],[7,8,10]),1);
691 133$
693 mat_cond(matrix([1,2,3],[4,5,6],[7,8,10]),inf);
694 475/3$
696 /* SF bug #3158: "triangularize gives incorrect result on a matrix containing %i" */
698 nullspace (matrix([0, 1, 1, 0], [-1, 0, 0, 1], [0, 0, 0, 1], [0, 0, -1, 0]) + %i * ident(4));
699 span(matrix([2*%i],[2],[0],[0]));