5 static void simplify_tensor(void);
7 static int nterms(U
*);
41 if (find(p1
, symbol(FACTORIAL
))) {
49 if (count(p2
) < count(p3
))
69 p2
= alloc_tensor(p1
->u
.tensor
->nelem
);
70 p2
->u
.tensor
->ndim
= p1
->u
.tensor
->ndim
;
71 for (i
= 0; i
< p1
->u
.tensor
->ndim
; i
++)
72 p2
->u
.tensor
->dim
[i
] = p1
->u
.tensor
->dim
[i
];
73 for (i
= 0; i
< p1
->u
.tensor
->nelem
; i
++) {
74 push(p1
->u
.tensor
->elem
[i
]);
76 p2
->u
.tensor
->elem
[i
] = pop();
79 p2
= zero
; // null tensor becomes scalar zero
90 n
+= count(car(p
)) + 1;
103 if (car(p1
) != symbol(ADD
))
108 if (count(p2
) < count(p1
))
117 if (car(p1
) != symbol(ADD
))
122 if (count(p2
) <= count(p1
))
126 // this simplifies forms like (A-B) / (B-A)
138 if (count(p2
) < count(p1
))
142 // try expanding denominators
156 if (count(p2
) < count(p1
))
160 // simplifies trig forms
175 if (find(p1
, symbol(SIN
)) == 0 && find(p1
, symbol(COS
)) == 0)
192 if (count(p4
) < count(p3
) || nterms(p4
) < nterms(p3
))
195 if (count(p3
) < count(p1
) || nterms(p3
) < nterms(p1
))
199 // if it's a sum then try to simplify each term
204 if (car(p1
) != symbol(ADD
))
215 if (count(p2
) < count(p1
))
222 if (car(p
) != symbol(ADD
))
225 return length(p
) - 1;
244 "simplify(A/(A+B)+B/(A+B))",
247 "simplify((A-B)/(B-A))",
250 "A=((A11,A12),(A21,A22))",
253 "simplify(det(A) inv(A) - adj(A))",
259 // this shows need for <= in try_factoring
267 "simplify(-3 exp(-1/3 r + i phi) cos(theta) / sin(theta)"
268 " + 3 exp(-1/3 r + i phi) cos(theta) sin(theta)"
269 " + 3 exp(-1/3 r + i phi) cos(theta)^3 / sin(theta))",
272 "simplify((A^2 C^2 + A^2 D^2 + B^2 C^2 + B^2 D^2)/(A^2+B^2)/(C^2+D^2))",
275 "simplify(d(arctan(y/x),y))",
278 "simplify(d(arctan(y/x),x))",
281 "simplify(1-sin(x)^2)",
284 "simplify(1-cos(x)^2)",
287 "simplify(sin(x)^2-1)",
290 "simplify(cos(x)^2-1)",
293 "simfac(n!/n)-(n-1)!",
296 "simfac(n/n!)-1/(n-1)!",
299 "simfac(rationalize((n+k+1)/(n+k+1)!))-1/(n+k)!",
302 "simfac(condense((n+1)*n!))-(n+1)!",
305 "simfac(1/((n+1)*n!))-1/(n+1)!",
308 "simfac((n+1)!/n!)-n-1",
311 "simfac(n!/(n+1)!)-1/(n+1)",
314 "simfac(binomial(n+1,k)/binomial(n,k))",
317 "simfac(binomial(n,k)/binomial(n+1,k))",
320 "F(nn,kk)=kk*binomial(nn,kk)",
323 "simplify(simfac((F(n,k)+F(n,k-1))/F(n+1,k))-n/(n+1))",
329 "simplify(n!/n)-(n-1)!",
332 "simplify(n/n!)-1/(n-1)!",
335 "simplify(rationalize((n+k+1)/(n+k+1)!))-1/(n+k)!",
338 "simplify(condense((n+1)*n!))-(n+1)!",
341 "simplify(1/((n+1)*n!))-1/(n+1)!",
344 "simplify((n+1)!/n!)-n-1",
347 "simplify(n!/(n+1)!)-1/(n+1)",
350 "simplify(binomial(n+1,k)/binomial(n,k))",
353 "simplify(binomial(n,k)/binomial(n+1,k))",
356 "F(nn,kk)=kk*binomial(nn,kk)",
359 "simplify((F(n,k)+F(n,k-1))/F(n+1,k))-n/(n+1)",
365 "simplify((n+1)/(n+1)!)-1/n!",
371 // Symbol's binding is evaluated, undoing simplify
373 "x=simplify(a*b+a*c)",
383 test(__FILE__
, s
, sizeof s
/ sizeof (char *));