matrix.tst 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. % Miscellaneous matrix tests.
  2. % Tests of eigenfunction/eigenvalue code.
  3. v := mat((1,1,-1,1,0),(1,2,-1,0,1),(-1,2,3,-1,0),
  4. (1,-2,1,2,-1),(2,1,-1,3,0))$
  5. mateigen(v,et);
  6. eigv := third first ws$
  7. % Now check if the equation for the eigenvectors is fulfilled. Note
  8. % that also the last component is zero due to the eigenvalue equation.
  9. v*eigv-et*eigv;
  10. % Example of degenerate eigenvalues.
  11. u := mat((2,-1,1),(0,1,1),(-1,1,1))$
  12. mateigen(u,eta);
  13. % Example of a fourfold degenerate eigenvalue with two corresponding
  14. % eigenvectors.
  15. w := mat((1,-1,1,-1),(-3,3,-5,4),(8,-4,3,-4),
  16. (15,-10,11,-11))$
  17. mateigen(w,al);
  18. eigw := third first ws;
  19. w*eigw - al*eigw;
  20. % Calculate the eigenvectors and eigenvalue equation.
  21. f := mat((0,ex,ey,ez),(-ex,0,bz,-by),(-ey,-bz,0,bx),
  22. (-ez,by,-bx,0))$
  23. factor om;
  24. mateigen(f,om);
  25. % Specialize to perpendicular electric and magnetic field.
  26. let ez=0,ex=0,by=0;
  27. % Note that we find two eigenvectors to the double eigenvalue 0
  28. % (as it must be).
  29. mateigen(f,om);
  30. % The following has 1 as a double eigenvalue. The corresponding
  31. % eigenvector must involve two arbitrary constants.
  32. j := mat((9/8,1/4,-sqrt(3)/8),
  33. (1/4,3/2,-sqrt(3)/4),
  34. (-sqrt(3)/8,-sqrt(3)/4,11/8));
  35. mateigen(j,x);
  36. % The following is a good consistency check.
  37. sym := mat(
  38. (0, 1/2, 1/(2*sqrt(2)), 0, 0),
  39. (1/2, 0, 1/(2*sqrt(2)), 0, 0),
  40. (1/(2*sqrt(2)), 1/(2*sqrt(2)), 0, 1/2, 1/2),
  41. (0, 0, 1/2, 0, 0),
  42. (0, 0, 1/2, 0, 0))$
  43. ans := mateigen(sym,eta);
  44. % Check of correctness for this example.
  45. for each j in ans do
  46. for each k in solve(first j,eta) do
  47. write sub(k,sym*third j - eta*third j);
  48. % Tests of nullspace operator.
  49. a1 := mat((1,2,3,4),(5,6,7,8));
  50. nullspace a1;
  51. b1 := {{1,2,3,4},{5,6,7,8}};
  52. nullspace b1;
  53. % Example taken from a bug report for another CA system.
  54. c1 :=
  55. {{(p1**2*(p1**2 + p2**2 + p3**2 - s*z - z**2))/(p1**2 + p3**2), 0,
  56. (p1*p3*(p1**2 + p2**2 + p3**2 - s*z - z**2))/(p1**2 + p3**2),
  57. -((p1**2*p2*(s + z))/(p1**2 + p3**2)), p1*(s + z),
  58. -((p1*p2*p3*(s + z))/(p1**2 + p3**2)),
  59. -((p1*p3*(p1**2 + p2**2 + p3**2))/(p1**2 + p3**2)), 0,
  60. (p1**2*(p1**2 + p2**2 + p3**2))/(p1**2 + p3**2)},
  61. {0, 0, 0, 0, 0, 0, 0, 0, 0},
  62. {(p1*p3*(p1**2 + p2**2 + p3**2 - s*z - z**2))/(p1**2 + p3**2), 0,
  63. (p3**2*(p1**2 + p2**2 + p3**2 - s*z - z**2))/(p1**2 + p3**2),
  64. -((p1*p2*p3*(s + z))/(p1**2 + p3**2)), p3*(s + z),
  65. -((p2*p3**2*(s + z))/(p1**2 + p3**2)),
  66. -((p3**2*(p1**2 + p2**2 + p3**2))/(p1**2 + p3**2)), 0,
  67. (p1*p3*(p1**2 + p2**2 + p3**2))/(p1**2 + p3**2)},
  68. {-((p1**2*p2*(s + z))/(p1**2 + p3**2)), 0,
  69. -((p1*p2*p3*(s + z))/(p1**2 + p3**2)),
  70. -((p1**2*p2**2*(s + 2*z))/((p1**2 + p3**2)*z)), (p1*p2*(s + 2*z))/z,
  71. -((p1*p2**2*p3*(s + 2*z))/((p1**2 + p3**2)*z)),
  72. -((p1*p2*p3*z)/(p1**2 + p3**2)), 0, (p1**2*p2*z)/(p1**2 + p3**2)},
  73. {p1*(s + z), 0, p3*(s + z), (p1*p2*(s + 2*z))/z,
  74. -(((p1**2+p3**2)*(s+ 2*z))/z), (p2*p3*(s + 2*z))/z, p3*z,0, -(p1*z)},
  75. {-((p1*p2*p3*(s + z))/(p1**2 + p3**2)), 0,
  76. -((p2*p3**2*(s + z))/(p1**2 + p3**2)),
  77. -((p1*p2**2*p3*(s + 2*z))/((p1**2 + p3**2)*z)), (p2*p3*(s + 2*z))/z,
  78. -((p2**2*p3**2*(s + 2*z))/((p1**2 + p3**2)*z)),
  79. -((p2*p3**2*z)/(p1**2 + p3**2)), 0, (p1*p2*p3*z)/(p1**2 + p3**2)},
  80. {-((p1*p3*(p1**2 + p2**2 + p3**2))/(p1**2 + p3**2)), 0,
  81. -((p3**2*(p1**2 + p2**2 + p3**2))/(p1**2 + p3**2)),
  82. -((p1*p2*p3*z)/(p1**2 + p3**2)),p3*z,-((p2*p3**2*z)/(p1**2 + p3**2)),
  83. -((p3**2*(p1**2 + p2**2 + p3**2)*z)/((p1**2 + p3**2)*(s + z))), 0,
  84. (p1*p3*(p1**2 + p2**2 + p3**2)*z)/((p1**2 + p3**2)*(s + z))},
  85. {0, 0, 0, 0, 0, 0, 0, 0, 0},
  86. {(p1**2*(p1**2 + p2**2 + p3**2))/(p1**2 + p3**2), 0,
  87. (p1*p3*(p1**2 + p2**2 + p3**2))/(p1**2 + p3**2),
  88. (p1**2*p2*z)/(p1**2 + p3**2), -(p1*z), (p1*p2*p3*z)/(p1**2 + p3**2),
  89. (p1*p3*(p1**2 + p2**2 + p3**2)*z)/((p1**2 + p3**2)*(s + z)), 0,
  90. -((p1**2*(p1**2 + p2**2 + p3**2)*z)/((p1**2 + p3**2)*(s + z)))}};
  91. nullspace c1;
  92. d1 := mat
  93. (((p1**2*(p1**2 + p2**2 + p3**2 - s*z - z**2))/(p1**2 + p3**2), 0,
  94. (p1*p3*(p1**2 + p2**2 + p3**2 - s*z - z**2))/(p1**2 + p3**2),
  95. -((p1**2*p2*(s + z))/(p1**2 + p3**2)), p1*(s + z),
  96. -((p1*p2*p3*(s + z))/(p1**2 + p3**2)),
  97. -((p1*p3*(p1**2 + p2**2 + p3**2))/(p1**2 + p3**2)), 0,
  98. (p1**2*(p1**2 + p2**2 + p3**2))/(p1**2 + p3**2)),
  99. (0, 0, 0, 0, 0, 0, 0, 0, 0),
  100. ((p1*p3*(p1**2 + p2**2 + p3**2 - s*z - z**2))/(p1**2 + p3**2), 0,
  101. (p3**2*(p1**2 + p2**2 + p3**2 - s*z - z**2))/(p1**2 + p3**2),
  102. -((p1*p2*p3*(s + z))/(p1**2 + p3**2)), p3*(s + z),
  103. -((p2*p3**2*(s + z))/(p1**2 + p3**2)),
  104. -((p3**2*(p1**2 + p2**2 + p3**2))/(p1**2 + p3**2)), 0,
  105. (p1*p3*(p1**2 + p2**2 + p3**2))/(p1**2 + p3**2)),
  106. ( ((p1**2*p2*(s + z))/(p1**2 + p3**2)), 0,
  107. -((p1*p2*p3*(s + z))/(p1**2 + p3**2)),
  108. -((p1**2*p2**2*(s + 2*z))/((p1**2 + p3**2)*z)), (p1*p2*(s + 2*z))/z,
  109. -((p1*p2**2*p3*(s + 2*z))/((p1**2 + p3**2)*z)),
  110. -((p1*p2*p3*z)/(p1**2 + p3**2)), 0, (p1**2*p2*z)/(p1**2 + p3**2)),
  111. (p1*(s + z), 0, p3*(s + z), (p1*p2*(s + 2*z))/z,
  112. -(((p1**2 + p3**2)*(s + 2*z))/z),(p2*p3*(s + 2*z))/z,p3*z,0,-(p1*z)),
  113. (-((p1*p2*p3*(s + z))/(p1**2 + p3**2)), 0,
  114. -((p2*p3**2*(s + z))/(p1**2 + p3**2)),
  115. -((p1*p2**2*p3*(s + 2*z))/((p1**2 + p3**2)*z)), (p2*p3*(s + 2*z))/z,
  116. -((p2**2*p3**2*(s + 2*z))/((p1**2 + p3**2)*z)),
  117. -((p2*p3**2*z)/(p1**2 + p3**2)), 0, (p1*p2*p3*z)/(p1**2 + p3**2)),
  118. (-((p1*p3*(p1**2 + p2**2 + p3**2))/(p1**2 + p3**2)), 0,
  119. -((p3**2*(p1**2 + p2**2 + p3**2))/(p1**2 + p3**2)),
  120. -((p1*p2*p3*z)/(p1**2 + p3**2)),p3*z,-((p2*p3**2*z)/(p1**2 + p3**2)),
  121. -((p3**2*(p1**2 + p2**2 + p3**2)*z)/((p1**2 + p3**2)*(s + z))), 0,
  122. (p1*p3*(p1**2 + p2**2 + p3**2)*z)/((p1**2 + p3**2)*(s + z))),
  123. (0, 0, 0, 0, 0, 0, 0, 0, 0),
  124. ((p1**2*(p1**2 + p2**2 + p3**2))/(p1**2 + p3**2), 0,
  125. (p1*p3*(p1**2 + p2**2 + p3**2))/(p1**2 + p3**2),
  126. (p1**2*p2*z)/(p1**2 + p3**2), -(p1*z), (p1*p2*p3*z)/(p1**2 + p3**2),
  127. (p1*p3*(p1**2 + p2**2 + p3**2)*z)/((p1**2 + p3**2)*(s + z)), 0,
  128. -((p1**2*(p1**2 + p2**2 + p3**2)*z)/((p1**2 + p3**2)*(s + z)))));
  129. nullspace d1;
  130. % The following example, by Kenton Yee, was discussed extensively by
  131. % the sci.math.symbolic newsgroup.
  132. m := mat((e^(-1), e^(-1), e^(-1), e^(-1), e^(-1), e^(-1), e^(-1), 0),
  133. (1, 1, 1, 1, 1, 1, 0, 1),(1, 1, 1, 1, 1, 0, 1, 1),
  134. (1, 1, 1, 1, 0, 1, 1, 1),(1, 1, 1, 0, 1, 1, 1, 1),
  135. (1, 1, 0, 1, 1, 1, 1, 1),(1, 0, 1, 1, 1, 1, 1, 1),
  136. (0, e, e, e, e, e, e, e));
  137. eig := mateigen(m,x);
  138. % Now check the eigenvectors and calculate the eigenvalues in the
  139. % respective eigenspaces:
  140. factor expt;
  141. for each eispace in eig do
  142. begin scalar eivaleq,eival,eivec;
  143. eival := solve(first eispace,x);
  144. for each soln in eival do
  145. <<eival := rhs soln;
  146. eivec := third eispace;
  147. eivec := sub(soln,eivec);
  148. write "eigenvalue = ", eival;
  149. write "check of eigen equation: ",
  150. m*eivec - eival*eivec>>
  151. end;
  152. % For the special choice:
  153. let e = -7 + sqrt 48;
  154. % we get only 7 eigenvectors.
  155. eig := mateigen(m,x);
  156. for each eispace in eig do
  157. begin scalar eivaleq,eival,eivec;
  158. eival := solve(first eispace,x);
  159. for each soln in eival do
  160. <<eival := rhs soln;
  161. eivec := third eispace;
  162. eivec := sub(soln,eivec);
  163. write "eigenvalue = ", eival;
  164. write "check of eigen equation: ",
  165. m*eivec - eival*eivec>>
  166. end;
  167. % The same behaviour for this choice of e.
  168. clear e; let e = -7 - sqrt 48;
  169. % we get only 7 eigenvectors.
  170. eig := mateigen(m,x);
  171. for each eispace in eig do
  172. begin scalar eivaleq,eival,eivec;
  173. eival := solve(first eispace,x);
  174. for each soln in eival do
  175. <<eival := rhs soln;
  176. eivec := third eispace;
  177. eivec := sub(soln,eivec);
  178. write "eigenvalue = ", eival;
  179. write "check of eigen equation: ",
  180. m*eivec - eival*eivec>>
  181. end;
  182. % For this choice of values
  183. clear e; let e = 1;
  184. % the eigenvalue 1 becomes 4-fold degenerate. However, we get a complete
  185. % span of 8 eigenvectors.
  186. eig := mateigen(m,x);
  187. for each eispace in eig do
  188. begin scalar eivaleq,eival,eivec;
  189. eival := solve(first eispace,x);
  190. for each soln in eival do
  191. <<eival := rhs soln;
  192. eivec := third eispace;
  193. eivec := sub(soln,eivec);
  194. write "eigenvalue = ", eival;
  195. write "check of eigen equation: ",
  196. m*eivec - eival*eivec>>
  197. end;
  198. ma := mat((1,a),(0,b));
  199. % case 1:
  200. let a = 0;
  201. mateigen(ma,x);
  202. % case 2:
  203. clear a; let a = 0, b = 1;
  204. mateigen(ma,x);
  205. % case 3:
  206. clear a,b;
  207. mateigen(ma,x);
  208. % case 4:
  209. let b = 1;
  210. mateigen(ma,x);
  211. % Example from H.G. Graebe:
  212. m1:=mat((-sqrt(3)+1,2 ,3 ),
  213. (2 ,-sqrt(3)+3,1 ),
  214. (3 ,1 ,-sqrt(3)+2));
  215. nullspace m1;
  216. for each n in ws collect m1*n;
  217. end;