Anyone have any pointers to code for simplifying algegraic expressions
expressed as s-exprs? For example, I strongly suspect that:
(/
(- (/ (* 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))
equals 0 for all K > 4. I've already manually simplified 5 cousins to
this, but surely there's a better way. I just don't know where to find
it and I'd rather not have to code it myself. Although I will, if it
comes to that.
I tried Norvig's macsyma and macsymr from PAIP, but it wasn't up to the
task.
Note that the above was already partially simplified (via manually
simplifying the predecessor generating expressions and using those).
FWIW, the "raw" expression is:
(/
(- (/ (* K (- K 1) (- K 2) (- K 3) (- K 4)) 120)
(+
(*
(/
(- (/ (* K (- K 1) (- K 2) (- K 3)) 24)
(+
(*
(/
(- (/ (* K (- K 1) (- K 2)) 6)
(+
(*
(/
(- (/ (* K (- K 1)) 2)
(+
(*
(/ (- K (* (/ (- 1 0) (+ K 1)) (/ (* (+ K 1) K)
2)))
(+ K 0))
(/ (* (+ K -1) (+ K 0)) 2))
(* (/ (- 1 0) (+ K 1))
(/ (* (+ K -1) (+ K 0) (+ K 1)) 6))))
(+ K -1))
(/ (* (+ K -2) (+ K -1)) 2))
(*
(/ (- K (* (/ (- 1 0) (+ K 1)) (/ (* (+ K 1) K) 2)))
(+ K 0))
(/ (* (+ K -2) (+ K -1) (+ K 0)) 6))
(* (/ (- 1 0) (+ K 1))
(/ (* (+ K -2) (+ K -1) (+ K 0) (+ K 1)) 24))))
(+ K -2))
(/ (* (+ K -3) (+ K -2)) 2))
(*
(/
(- (/ (* K (- K 1)) 2)
(+
(*
(/ (- K (* (/ (- 1 0) (+ K 1)) (/ (* (+ K 1) K) 2)))
(+ K 0))
(/ (* (+ K -1) (+ K 0)) 2))
(* (/ (- 1 0) (+ K 1)) (/ (* (+ K -1) (+ K 0) (+ K 1))
6))))
(+ K -1))
(/ (* (+ K -3) (+ K -2) (+ K -1)) 6))
(* (/ (- K (* (/ (- 1 0) (+ K 1)) (/ (* (+ K 1) K) 2))) (+ K
0))
(/ (* (+ K -3) (+ K -2) (+ K -1) (+ K 0)) 24))
(* (/ (- 1 0) (+ K 1))
(/ (* (+ K -3) (+ K -2) (+ K -1) (+ K 0) (+ K 1)) 120))))
(+ K -3))
(/ (* (+ K -4) (+ K -3)) 2))
(*
(/
(- (/ (* K (- K 1) (- K 2)) 6)
(+
(*
(/
(- (/ (* K (- K 1)) 2)
(+
(*
(/ (- K (* (/ (- 1 0) (+ K 1)) (/ (* (+ K 1) K) 2)))
(+ K 0))
(/ (* (+ K -1) (+ K 0)) 2))
(* (/ (- 1 0) (+ K 1)) (/ (* (+ K -1) (+ K 0) (+ K 1))
6))))
(+ K -1))
(/ (* (+ K -2) (+ K -1)) 2))
(* (/ (- K (* (/ (- 1 0) (+ K 1)) (/ (* (+ K 1) K) 2))) (+ K
0))
(/ (* (+ K -2) (+ K -1) (+ K 0)) 6))
(* (/ (- 1 0) (+ K 1))
(/ (* (+ K -2) (+ K -1) (+ K 0) (+ K 1)) 24))))
(+ K -2))
(/ (* (+ K -4) (+ K -3) (+ K -2)) 6))
(*
(/
(- (/ (* K (- K 1)) 2)
(+
(* (/ (- K (* (/ (- 1 0) (+ K 1)) (/ (* (+ K 1) K) 2))) (+ K
0))
(/ (* (+ K -1) (+ K 0)) 2))
(* (/ (- 1 0) (+ K 1)) (/ (* (+ K -1) (+ K 0) (+ K 1)) 6))))
(+ K -1))
(/ (* (+ K -4) (+ K -3) (+ K -2) (+ K -1)) 24))
(* (/ (- K (* (/ (- 1 0) (+ K 1)) (/ (* (+ K 1) K) 2))) (+ K 0))
(/ (* (+ K -4) (+ K -3) (+ K -2) (+ K -1) (+ K 0)) 120))
(* (/ (- 1 0) (+ K 1))
(/ (* (+ K -4) (+ K -3) (+ K -2) (+ K -1) (+ K 0) (+ K 1))
720))))
(+ K -4))
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
--
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Frank Buss <··@frank-buss.de> 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.
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.
Second, there are potentially thousands of these equations.
Although I do have a child who has the student edition of Mathematica.
I wonder if he'd be willing to work off part of his tuition? <evil
grin>. Well, it won't hurt to ask...
Bob Felts <····@stablecross.com> wrote:
> Frank Buss <··@frank-buss.de> 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.
Actually, I copied the right one. However, it has a unary minus
operator that appears to flummox p2i. The original s-expr does evaluate
to zero and when I rearrange the equation to move the unary minus,
maxima says the infix version evaluates to zero.
····@stablecross.com (Bob Felts) writes:
> Frank Buss <··@frank-buss.de> 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
* (Bob Felts) <··························@stablecross.com> :
Wrote on Fri, 12 Sep 2008 22:29:44 -0400:
| Anyone have any pointers to code for simplifying algegraic expressions
| expressed as s-exprs?
For a while I was using WEYL, part of simlab [Richard Zippel, Cornell]
and which lets you do this sort of thing. It does require a bit of work
before running in current lisps.
[I modified it a little fixing some things here and there, but haven't
worked on it for more than two years, I could generate a patch if you
took a look at it and wanted to pursue it ...]
--
Madhu