LINALG.TST 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. if lisp !*rounded then rounded_was_on := t
  2. else rounded_was_on := nil;
  3. mat1 := mat((1,2,3,4,5),(2,3,4,5,6),(3,4,5,6,7),(4,5,6,7,8),(5,6,7,8,9));
  4. mat2 := mat((1,1,1,1),(2,2,2,2),(3,3,3,3),(4,4,4,4));
  5. mat3 := mat((x),(x),(x),(x));
  6. mat4 := mat((3,3),(4,4),(5,5),(6,6));
  7. mat5 := mat((1,2,1,1),(1,2,3,1),(4,5,1,2),(3,4,5,6));
  8. mat6 := mat((i+1,i+2,i+3),(4,5,2),(1,i,0));
  9. mat7 := mat((1,1,0),(1,3,1),(0,1,1));
  10. mat8 := mat((1,3),(-4,3));
  11. mat9 := mat((1,2,3,4),(9,8,7,6));
  12. poly := x^7+x^5+4*x^4+5*x^3+12;
  13. poly1 := x^2+x*y^3+x*y*z^3+y*x+2+y*3;
  14. on errcont;
  15. % Basis matrix manipulations.
  16. add_columns(mat1,1,2,5*y);
  17. add_rows(mat1,1,2,x);
  18. add_to_columns(mat1,3,1000);
  19. add_to_columns(mat1,{1,2,3},y);
  20. add_to_rows(mat1,2,1000);
  21. add_to_rows(mat1,{1,2,3},x);
  22. augment_columns(mat1,2);
  23. augment_columns(mat1,{1,2,5});
  24. stack_rows(mat1,3);
  25. stack_rows(mat1,{1,3,5});
  26. char_poly(mat1,x);
  27. column_dim(mat2);
  28. row_dim(mat1);
  29. copy_into(mat7,mat1,2,3);
  30. copy_into(mat7,mat1,5,5);
  31. diagonal(3);
  32. % diagonal can take both a list of arguments or just the arguments.
  33. diagonal({mat2,mat6});
  34. diagonal(mat1,mat2,mat5);
  35. extend(mat1,3,2,x);
  36. find_companion(mat5,x);
  37. get_columns(mat1,1);
  38. get_columns(mat1,{1,2});
  39. get_rows(mat1,3);
  40. get_rows(mat1,{1,3});
  41. hermitian_tp(mat6);
  42. % matrix_augment and matrix_stack can take both a list of arguments
  43. % or just the arguments.
  44. matrix_augment({mat1,mat2});
  45. matrix_augment(mat4,mat2,mat4);
  46. matrix_stack(mat1,mat2);
  47. matrix_stack({mat6,mat((z,z,z)),mat7});
  48. minor(mat1,2,3);
  49. mult_columns(mat1,3,y);
  50. mult_columns(mat1,{2,3,4},100);
  51. mult_rows(mat1,2,x);
  52. mult_rows(mat1,{1,3,5},10);
  53. pivot(mat1,3,3);
  54. rows_pivot(mat1,3,3,{1,5});
  55. remove_columns(mat1,3);
  56. remove_columns(mat1,{2,3,4});
  57. remove_rows(mat1,2);
  58. remove_rows(mat1,{1,3});
  59. remove_rows(mat1,{1,2,3,4,5});
  60. swap_columns(mat1,2,4);
  61. swap_rows(mat1,1,2);
  62. swap_entries(mat1,{1,1},{5,5});
  63. % Constructors - functions that create matrices.
  64. band_matrix(x,5);
  65. band_matrix({x,y,z},6);
  66. block_matrix(1,2,{mat1,mat2});
  67. block_matrix(2,3,{mat2,mat3,mat2,mat3,mat2,mat2});
  68. char_matrix(mat1,x);
  69. cfmat := coeff_matrix({x+y+4*z=10,y+x-z=20,x+y+4});
  70. first cfmat * second cfmat;
  71. third cfmat;
  72. companion(poly,x);
  73. hessian(poly1,{w,x,y,z});
  74. hilbert(4,1);
  75. hilbert(3,y+x);
  76. jacobian({x^4,x*y^2,x*y*z^3},{w,x,y,z});
  77. jordan_block(x,5);
  78. make_identity(11);
  79. on rounded; % makes things a bit easier to read.
  80. random_matrix(3,3,100);
  81. on not_negative;
  82. random_matrix(3,3,100);
  83. on only_integer;
  84. random_matrix(3,3,100);
  85. on symmetric;
  86. random_matrix(3,3,100);
  87. off symmetric;
  88. on upper_matrix;
  89. random_matrix(3,3,100);
  90. off upper_matrix;
  91. on lower_matrix;
  92. random_matrix(3,3,100);
  93. off lower_matrix;
  94. on imaginary;
  95. off not_negative;
  96. random_matrix(3,3,100);
  97. off rounded;
  98. % toeplitz and vandermonde can take both a list of arguments or just
  99. % the arguments.
  100. toeplitz({1,2,3,4,5});
  101. toeplitz(x,y,z);
  102. vandermonde({1,2,3,4,5});
  103. vandermonde(x,y,z);
  104. % kronecker_product
  105. a1 := mat((1,2),(3,4),(5,6));
  106. a2 := mat((1,x,1),(2,2,2),(3,3,3));
  107. kronecker_product(a1,a2);
  108. clear a1,a2;
  109. % High level algorithms.
  110. on rounded; % makes output easier to read.
  111. ch := cholesky(mat7);
  112. tp first ch - second ch;
  113. tmp := first ch * second ch;
  114. tmp - mat7;
  115. off rounded;
  116. gram_schmidt({1,0,0},{1,1,0},{1,1,1});
  117. gram_schmidt({1,2},{3,4});
  118. on rounded; % again, makes large quotients a bit more readable.
  119. % The algorithm used for lu_decom sometimes swaps the rows of the input
  120. % matrix so that (given matrix A, lu_decom(A) = {L,U,vec}), we find L*U
  121. % does not equal A but a row equivalent of it. The call convert(A,vec)
  122. % will return this row equivalent (ie: L*U = convert(A,vec)).
  123. lu := lu_decom(mat5);
  124. mat5;
  125. tmp := first lu * second lu;
  126. tmp1 := convert(mat5,third lu);
  127. tmp - tmp1;
  128. % and the complex case...
  129. lu1 := lu_decom(mat6);
  130. mat6;
  131. tmp := first lu1 * second lu1;
  132. tmp1 := convert(mat6,third lu1);
  133. tmp - tmp1;
  134. mat9inv := pseudo_inverse(mat9);
  135. mat9 * mat9inv;
  136. simplex(min,2*x1+14*x2+36*x3,{-2*x1+x2+4*x3>=5,-x1-2*x2-3*x3<=2});
  137. simplex(max,10000 x1 + 1000 x2 + 100 x3 + 10 x4 + x5,{ x1 <= 1, 20 x1 +
  138. x2 <= 100, 200 x1 + 20 x2 + x3 <= 10000, 2000 x1 + 200 x2 + 20 x3 + x4
  139. <= 1000000, 20000 x1 + 2000 x2 + 200 x3 + 20 x4 + x5 <= 100000000});
  140. simplex(max, 5 x1 + 4 x2 + 3 x3,
  141. { 2 x1 + 3 x2 + x3 <= 5,
  142. 4 x1 + x2 + 2 x3 <= 11,
  143. 3 x1 + 4 x2 + 2 x3 <= 8 });
  144. simplex(min,3 x1 + 5 x2,{ x1 + 2 x2 >= 2, 22 x1 + x2 >= 3});
  145. simplex(max,10x+5y+5.5z,{5x+3z<=200,0.2x+0.1y+0.5z<=12,0.1x+0.2y+0.3z<=9,
  146. 30x+10y+50z<=1500});
  147. %example of extra variables (>=0) being added.
  148. simplex(min,x-y,{x>=-3});
  149. % unfeasible as simplex algorithm implies all x>=0.
  150. simplex(min,x,{x<=-100});
  151. % three error examples.
  152. simplex(maxx,x,{x>=5});
  153. simplex(max,x,x>=5);
  154. simplex(max,x,{x<=y});
  155. simplex(max, 346 X11 + 346 X12 + 248 X21 + 248 X22 + 399 X31 + 399 X32 +
  156. 200 Y11 + 200 Y12 + 75 Y21 + 75 Y22 + 2.35 Z1 + 3.5 Z2,
  157. {
  158. 4 X11 + 4 X12 + 2 X21 + 2 X22 + X31 + X32 + 250 Y11 + 250 Y12 + 125 Y21 +
  159. 125 Y22 <= 25000,
  160. X11 + X12 + X21 + X22 + X31 + X32 + 2 Y11 + 2 Y12 + Y21 + Y22 <= 300,
  161. 20 X11 + 15 X12 + 30 Y11 + 20 Y21 + Z1 <= 1500,
  162. 40 X12 + 35 X22 + 50 X32 + 15 Y12 + 10 Y22 + Z2 = 5000,
  163. X31 = 0,
  164. Y11 + Y12 <= 50,
  165. Y21 + Y22 <= 100
  166. });
  167. % from Marc van Dongen. Finding the first feasible solution for the
  168. % solution of systems of linear diophantine inequalities.
  169. simplex(max,0,{
  170. 3*X259+4*X261+3*X262+2*X263+X269+2*X270+3*X271+4*X272+5*X273+X229=2,
  171. 7*X259+11*X261+8*X262+5*X263+3*X269+6*X270+9*X271+12*X272+15*X273+X229=4,
  172. 2*X259+5*X261+4*X262+3*X263+3*X268+4*X269+5*X270+6*X271+7*X272+8*X273=1,
  173. X262+2*X263+5*X268+4*X269+3*X270+2*X271+X272+2*X229=1,
  174. X259+X262+2*X263+4*X268+3*X269+2*X270+X271-X273+3*X229=2,
  175. X259+2*X261+2*X262+2*X263+3*X268+3*X269+3*X270+3*X271+3*X272+3*X273+X229=1,
  176. X259+X261+X262+X263+X268+X269+X270+X271+X272+X273+X229=1});
  177. svd_ans := svd(mat8);
  178. tmp := tp first svd_ans * second svd_ans * third svd_ans;
  179. tmp - mat8;
  180. mat9inv := pseudo_inverse(mat9);
  181. mat9 * mat9inv;
  182. % Predicates.
  183. matrixp(mat1);
  184. matrixp(poly);
  185. squarep(mat2);
  186. squarep(mat3);
  187. symmetricp(mat1);
  188. symmetricp(mat3);
  189. if not rounded_was_on then off rounded;
  190. END;