compact.rlg 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. Tue Apr 15 00:32:57 2008 run on win32
  2. % Tests of the COMPACT package.
  3. % Author: Anthony C. Hearn.
  4. % First some simple examples.
  5. aa := {cos(x)^2+sin(x)^2-1};
  6. 2 2
  7. aa := {cos(x) + sin(x) - 1}
  8. xx := 2*cos(x)^2+2*sin(x)^2-2;
  9. 2 2
  10. xx := 2*(cos(x) + sin(x) - 1)
  11. compact(xx,aa);
  12. 0
  13. xx := (1-cos(x)^2)^4;
  14. 8 6 4 2
  15. xx := cos(x) - 4*cos(x) + 6*cos(x) - 4*cos(x) + 1
  16. compact(xx,aa);
  17. 8
  18. sin(x)
  19. % These examples are from Lars Hornfeldt.
  20. % This should be cos x^10*sin x^10.
  21. compact(((1-(sin x)**2)**5)*((1-(cos x)**2)**5)
  22. *(((sin x)**2+(cos x)**2)**5),
  23. {cos x^2+sin x^2=1});
  24. 10 2 4 10 8 2
  25. sin(x) *(10*cos(x) *sin(x) - sin(x) + 5*sin(x) - 5*sin(x) + 1)
  26. % This example illustrates the problem in the above. It is cos(x)^6.
  27. compact(-3cos(x)^2*sin(x)^2-sin(x)^6+1,{cos x^2+sin x^2-1});
  28. 2 2 6
  29. - 3*cos(x) *sin(x) - sin(x) + 1
  30. compact(s*(1-(sin x**2))+c*(1-(cos x)**2)+(sin x)**2+(cos x)**2,
  31. {cos x^2+sin x^2=1});
  32. 2 2
  33. cos(x) *s + sin(x) *c + 1
  34. xx := s*(1-(sin x**2))+c*(1-(cos x)**2)+(sin x)**2+(cos x)**2
  35. *((sin x)**2+(cos x)**2)*(sin x)**499*(cos x)**499;
  36. 503 499 501 501 2 2
  37. xx := cos(x) *sin(x) + cos(x) *sin(x) - cos(x) *c - sin(x) *s
  38. 2
  39. + sin(x) + c + s
  40. compact(xx,{cos(x)^2+sin(x)^2=1});
  41. 501 499 2 2 2
  42. cos(x) *sin(x) + cos(x) *s + sin(x) *c + sin(x)
  43. compact((s*(1-(sin x**2))+c*(1-(cos x)**2)+(sin x)**2+(cos x)**2)
  44. *((sin x)**2+(cos x)**2)*(sin x)**499*(cos x)**499,
  45. {cos x^2+sin x^2=1});
  46. 499 499 2 2
  47. cos(x) *sin(x) *(cos(x) *s + sin(x) *c + 1)
  48. compact(df((1-(sin x)**2)**4,x),{cos x^2+sin x^2=1});
  49. 2 2 6
  50. 8*cos(x)*sin(x)*(3*cos(x) *sin(x) + sin(x) - 1)
  51. % End of Lars Hornfeld examples.
  52. xx := a*(cos(x)+2*sin(x))^3-w*(cos(x)-sin(x))^2;
  53. 3 2 2 2
  54. xx := cos(x) *a + 6*cos(x) *sin(x)*a - cos(x) *w + 12*cos(x)*sin(x) *a
  55. 3 2
  56. + 2*cos(x)*sin(x)*w + 8*sin(x) *a - sin(x) *w
  57. compact(xx,aa);
  58. 2 3
  59. 11*cos(x)*sin(x) *a + 2*cos(x)*sin(x)*w + cos(x)*a + 2*sin(x) *a + 6*sin(x)*a
  60. - w
  61. xx := (1-cos(x)^2)^2+(1-sin(x)^2)^2;
  62. 4 2 4 2
  63. xx := cos(x) - 2*cos(x) + sin(x) - 2*sin(x) + 2
  64. compact(xx,aa);
  65. 2 2
  66. - 2*cos(x) *sin(x) + 1
  67. xx := (c^2-1)^6+7(s-1)^4+23(c+s)^5;
  68. 12 10 8 6 5 4 4 3 2
  69. xx := c - 6*c + 15*c - 20*c + 23*c + 115*c *s + 15*c + 230*c *s
  70. 2 3 2 4 5 4 3 2
  71. + 230*c *s - 6*c + 115*c*s + 23*s + 7*s - 28*s + 42*s - 28*s + 8
  72. compact(xx,{c+s=1});
  73. 12 10 8 6 4 2
  74. c - 6*c + 15*c - 20*c + 22*c - 6*c + 24
  75. yy := (c+1)^6*s^6+7c^4+23;
  76. 6 6 5 6 4 6 4 3 6 2 6 6 6
  77. yy := c *s + 6*c *s + 15*c *s + 7*c + 20*c *s + 15*c *s + 6*c*s + s + 23
  78. compact(yy,{c+s=1});
  79. 6 6 5 6 4 6 4 3 6 2 6 6 6
  80. c *s + 6*c *s + 15*c *s + 7*c + 20*c *s + 15*c *s + 6*c*s + s + 23
  81. zz := xx^3+c^6*s^6$
  82. compact(zz,{c+s=1});
  83. 36 34 32 30 28 26 24 22
  84. c - 18*c + 153*c - 816*c + 3081*c - 8820*c + 20019*c - 37272*c
  85. 20 18 16 14 12 11
  86. + 58854*c - 81314*c + 100488*c - 111840*c + 111341*c - 6*c
  87. 10 9 8 7 6 4 2
  88. - 97545*c - 20*c + 80439*c - 6*c - 53783*c + 40608*c - 10368*c + 13824
  89. xx := (c+s)^5 - 55(1-s)^2 + 77(1-c)^3 + (c+2s)^8;
  90. 8 7 6 2 5 3 5 4 4 4
  91. xx := c + 16*c *s + 112*c *s + 448*c *s + c + 1120*c *s + 5*c *s
  92. 3 5 3 2 3 2 6 2 3 2
  93. + 1792*c *s + 10*c *s - 77*c + 1792*c *s + 10*c *s + 231*c
  94. 7 4 8 5 2
  95. + 1024*c*s + 5*c*s - 231*c + 256*s + s - 55*s + 110*s + 22
  96. % This should reduce to something like:
  97. yy := 1 - 55c^2 + 77s^3 + (1+s)^8;
  98. 2 8 7 6 5 4 3 2
  99. yy := - 55*c + s + 8*s + 28*s + 56*s + 70*s + 133*s + 28*s + 8*s + 2
  100. % The result contains the same number but different terms.
  101. compact(xx,{c+s=1});
  102. 8 7 6 5 4 3 2
  103. s + 8*s + 28*s + 56*s + 70*s + 133*s - 27*s + 118*s - 53
  104. compact(yy,{c+s=1});
  105. 8 7 6 5 4 3 2
  106. s + 8*s + 28*s + 56*s + 70*s + 133*s - 27*s + 118*s - 53
  107. % Test showing order of expressions is important.
  108. d2:= - 4*r3a**2 - 4*r3b**2 - 4*r3c**2 + 3*r3**2$
  109. d1:= 4 * r3a**2 * r3
  110. + 4 * r3b**2 * r3
  111. + 4 * r3c**2 * r3
  112. + 16 * r3a * r3b * r3c
  113. - r3**3$
  114. d0:= 16 * r3a**4
  115. + 16 * r3b**4
  116. + 16 * r3c**4
  117. + r3**4
  118. - 32 * r3a**2 * r3b**2
  119. - 32 * r3a**2 * r3c**2
  120. - 32 * r3b**2 * r3c**2
  121. - 8 * r3a**2 * r3**2
  122. - 8 * r3b**2 * r3**2
  123. - 8 * r3c**2 * r3**2
  124. - 64 * r3a * r3b * r3c * r3$
  125. alist := { c0 = d0, c1 = d1, c2 = d2}$
  126. blist := { c2 = d2, c1 = d1, c0 = d0}$
  127. d:= d2 * l*l + d1 * l + d0;
  128. 2 2 2 2 2 2 2 2 3 2
  129. d := 3*l *r3 - 4*l *r3a - 4*l *r3b - 4*l *r3c - l*r3 + 4*l*r3*r3a
  130. 2 2 4 2 2
  131. + 4*l*r3*r3b + 4*l*r3*r3c + 16*l*r3a*r3b*r3c + r3 - 8*r3 *r3a
  132. 2 2 2 2 4 2 2
  133. - 8*r3 *r3b - 8*r3 *r3c - 64*r3*r3a*r3b*r3c + 16*r3a - 32*r3a *r3b
  134. 2 2 4 2 2 4
  135. - 32*r3a *r3c + 16*r3b - 32*r3b *r3c + 16*r3c
  136. compact(d,alist);
  137. 2
  138. c0 + c1*l + c2*l
  139. % Works fine.
  140. compact(d,blist);
  141. 2 2 2 3 4
  142. c2*l - c2*l*r3 + 2*c2*r3 + 8*c2*r3a + 2*l*r3 + 16*l*r3a*r3b*r3c - 5*r3
  143. 2 2 4 4 2 2 4
  144. - 24*r3 *r3a - 64*r3*r3a*r3b*r3c + 48*r3a + 16*r3b - 32*r3b *r3c + 16*r3c
  145. % Only c2=d2 is applied.
  146. % This example illustrates why parallel application of the individual
  147. % side relations is necessary.
  148. lst:={x1=a+b+c, x2=a-b-c, x3=-a+b-c, x4=-a-b+c};
  149. lst := {x1=a + b + c,
  150. x2=a - b - c,
  151. x3= - a + b - c,
  152. x4= - a - b + c}
  153. z1:=(a+b+c)*(a-b-c)*(-a+b-c);
  154. 3 2 2 2 2 3 2 2 3
  155. z1 := - a + a *b - a *c + a*b + 2*a*b*c + a*c - b - b *c + b*c + c
  156. % This is x1*x2*x3.
  157. z2:=(a+b+c)*(a-b-c)*(-a+b-c)*(-a-b+c);
  158. 4 2 2 2 2 4 2 2 4
  159. z2 := a - 2*a *b - 2*a *c + b - 2*b *c + c
  160. % This is x1*x2*x3*x4.
  161. compact(z1,lst);
  162. 2
  163. x1*(4*a*b + 2*c*x1 - x1 )
  164. % Not the best solution but better than nothing.
  165. compact(z2,lst);
  166. 4 2 2 2 2 4 2 2 4
  167. a - 2*a *b - 2*a *c + b - 2*b *c + c
  168. % Does nothing.
  169. end;
  170. Time for test: 33 ms, plus GC time: 1 ms