# Re: Algebraic Expression Simplification

wrf3@xxxxxxxxxxxxxxx (Bob Felts) writes:

Frank Buss <fb@xxxxxxxxxxxxx> wrote:

Bob Felts wrote:

Anyone have any pointers to code for simplifying algegraic expressions
expressed as s-exprs?

No source code, but in compiled form. First a quick hack for converting the
prefix form to infix:

(defun p2i (exp)
(let ((op (car exp))
(rest (cdr exp)))
(format t "(")
(loop for term in rest
for first = t then nil do
(unless first
(format t "~a" op))
(if (atom term)
(format t "(~a)" term)
(p2i term)))
(format t ")")))

This gives the more readable term
(((((K)*((K)-(1))*((K)-(2))*((K)-(3))*((K)-(4)))/(120))-((((((K)*((K)
-(1))*((K)-(2)))/(720)))*((((K)+(-4))*((K)+(-3)))/(2)))+((0)*((((K)+
(-4))*((K)+(-3))*((K)+(-2)))/(6)))+(((K)/(12))*((((K)+(-4))*((K)+(-
3))*((K)+(-2))*((K)+(-1)))/(24)))+(((1)/(2))*((((K)+(-4))*((K)+(-3)
)*((K)+(-2))*((K)+(-1))*((K)+(0)))/(120)))+(((1)/((K)+(1)))*((((K)
+(-4))*((K)+(-3))*((K)+(-2))*((K)+(-1))*((K)+(0))*((K)+(1)))/(720)
))))/((K)+(-4)))

Pasting it to Mathematica gives -(k-3)*(k-2)*(k-1)*k/720

Oh, rats. I copied the wrong one. I had already done that one by hand
and came up with the same results as Mathematica.

maxima says the same:

(%i16) (((((K)*((K)-(1))*((K)-(2))*((K)-(3))*((K)-(4)))/(120))-((((((K)*((K)-(1))*((K)-(2)))/(720)))*((((K)+(-4))*((K)+(-3)))/(2)))+((0)*((((K)+(-4))*((K)+(-3))*((K)+(-2)))/(6)))+(((K)/(12))*((((K)+(-4))*((K)+(-3))*((K)+(-2))*((K)+(-1)))/(24)))+(((1)/(2))*((((K)+(-4))*((K)+(-3))*((K)+(-2))*((K)+(-1))*((K)+(0)))/(120)))+(((1)/((K)+(1)))*((((K)+(-4))*((K)+(-3))*((K)+(-2))*((K)+(-1))*((K)+(0))*((K)+(1)))/(720)))))/((K)+(-4)));
(K - 3) (K - 2) (K - 1) K
(%o16) - -------------------------
720

But there are two problems with this. First, I don't have Mathematica
and, if I'm going to spend that kind of money, I'd rather first get
Lispworks.

maxima works on free lisps.

Second, there are potentially thousands of these equations.

you can call lisp from maxima, or maxima from lisp.

(%i25) to_lisp();

Type (to-maxima) to restart, (\$quit) to quit Maxima.

MAXIMA> (dolist (expr (list #\$x^2+4*x+2\$ #\$x^2-2*x+1\$ )) (displa expr) (terpri))
2
x + 4 x + 2

2
x - 2 x + 1

NIL
MAXIMA> (to-maxima)
Returning to Maxima
(%o25) true
(%i26) quit();

Process maxima finished

--
__Pascal Bourguignon__ http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay
.