Mathematica to maxima
- Subject: Mathematica to maxima
- From: Richard Fateman
- Date: Wed, 30 Jan 2002 13:29:27 -0800
after some conversion, and
printing in macsyma, I got the
following result for the body of the function
below. Translation of for, while, if, patterns
have to be dealt with specially.
BLOCK([NODES : V, FLAGS : F, NUMBERS : NR, EDGES : E, STARTNODES : STNODES,
TAILNODES : ENDNODES, I, TMPEDGE, TMPG], (EDGE : PART(NODES, NODENR),
(WHILE, (UNEQUAL, EDGE, 0), (TMPEDGE : EDGE, EDGE : PART(EDGES, EDGE),
PART(EDGES, TMPEDGE) : - 1,
[PART(STARTNODES, TMPEDGE), PART(TAILNODES, TMPEDGE)] : [- 1, - 1])),
[PART(NODES, NODENR), PART(FLAGS, NODENR), PART(NUMBERS, NODENR)] : [-
1, - 1, - 1],
TMPG : (GRAPH, NODES, FLAGS, NUMBERS, EDGES, STARTNODES, TAILNODES),
(FOR, I : 1, I <= (LENGTH, EDGES), INCREMENT(I), (IF, PART(TAILNODES, I)
= NODENR,
TMPG : (DELETEEDGE, TMPG, I))), NULL))
.......the lisp version
> (Module
> (List (Set nodes v) (Set flags f) (Set numbers nr) (Set edges e) (Set startnodes stnodes) (Set
> tailnodes endnodes)
> i tmpedge tmpg)
> (CompoundExpression (Set edge (Part nodes nodenr))
> (While (Unequal edge 0)
> (CompoundExpression (Set tmpedge edge) (Set edge (Part edges edge))
> (Set (Part edges tmpedge) -1)
> (Set (List (Part startnodes tmpedge) (Part tailnodes
> tmpedge))
> (List -1 -1))))
> (Set (List (Part nodes nodenr) (Part flags nodenr) (Part numbers nodenr)) (List
> -1 -1 -1))
> (Set tmpg (Graph nodes flags numbers edges startnodes tailnodes))
> (For (Set i 1) (LessEqual i (Length edges)) (Increment i)
> (If (Equal (Part tailnodes i) nodenr) (Set tmpg (DeleteEdge tmpg i))))
> Null))
.........
the original
>>
>> DeleteVertex[Graph[v_,f_,nr_,e_,stnodes_,endnodes_ ],nodenr_Integer]:=
>> Module[{nodes = v,flags=f,numbers=nr,edges=e,
>> startnodes=stnodes,tailnodes=endnodes,i,tmpedge,tmpg},
>> edge = nodes[[nodenr]];
>> While[edge!=0,tmpedge= edge;
>> edge=edges[[edge]];
>> edges[[tmpedge]]=-1;
>> {startnodes[[tmpedge]],tailnodes[[tmpedge]]}={-1,-1}
>> ]; (* end while *)
>> {nodes[[nodenr]],flags[[nodenr]],numbers[[nodenr]]} = {-1,-1,-1};
>> tmpg = Graph[nodes,flags,numbers,edges,startnodes,tailnodes];
>> For[i=1,i<=Length[edges],i++,
>> If[tailnodes[[i]]==nodenr,
>>
>> tmpg = DeleteEdge[tmpg,i]
>>
>> ] (* end if *)
>> ]; (* end for *)
>> tmpg
>> ] (* end module *)
>>
>>End[ ]
>>EndPackage[ ]
>>
Here's the lisp translation...
(defparameter macsubs ;; data for translation from mathematica to macsyma
'((Set . (msetq))
(SetDelayed . (mdefine))
(Equal . (mequal))
(if . (mif))
(Pattern . (Pattern)) ; hardly enough
(Blank . (Blank))
(Increment . ($Increment))
(Part . ($Part))
(Greater . (mgreaterp))
(GreaterEqual . (mgeqp))
(LessEqual . (mleqp))
(Plus . (mplus))
(Times . (mtimes))
(Module . (mprog))
(List . (mlist))
(CompoundExpression . (mprogn))))
(defun tomacsyma(r)
(cond ((numberp r) r)
((symbolp r)
(let ((l (assoc r macsubs)))
(cond (l (cdr l)) ;found a translation
(t (intern (format nil"$~a"r)))))) ;; a symbol foo -> $foo or |$foo| perhaps
;; here we should check for while, for, other complex stuff
(t (mapcar #'tomacsyma r))))
;; . while, for, ordinary function calls.
;; while n>0 do (print(n),n:n-1) looks like this...
; ((MDO) NIL NIL NIL NIL NIL ((MNOT) ((MGREATERP) |$n| 0))
; ((MPROGN) (($PRINT) |$n|) ((MSETQ) |$n| ((MPLUS) |$n| ((MMINUS) 1)))))