generating C or Java, was: Why is *print-circle* set to T by default? WAS: grinding, etc.
Subject: generating C or Java, was: Why is *print-circle* set to T by default? WAS: grinding, etc.
From: Stavros Macrakis
Date: Thu, 10 Feb 2011 09:57:07 -0500
If you don't care about formatting (the main function of grind), wouldn't it
be simpler to just do it all in Maxima, something like this:
/* kill c_op to remove any memoized values */
kill(c_op)$
c_op["+"]: myplus$
c_op["/"]: mydivide$
c_op["^"]: myexp$
c_op["["]: mylist$
c_op[x] := x$
m_to_c1(ex) :=
if atom(ex) then ex
elseif subvarp(ex) then
m_to_c1(apply('mysubscript,cons(part(ex,0),args(ex))))
/* Cases that one might want to add:
* subscripted functions like f[i](x)
* special case for rationals like 2/3
* special case for bigfloats like 2.3b3
*/
else block([op,ar],
ex: de_nary(ex),
concat(c_op[op(ex)],"(",m_to_cn(args(ex)),")"))$
m_to_cn(l) :=
if l=[] then ""
elseif length(l)=1 then m_to_c1(l[1])
else concat(m_to_c1(l[1]),",",m_to_cn(rest(l)))$
de_nary(ex) := if member(op(ex),["+","*"]) then lreduce(op(ex),args(ex))
else ex$
m_to_c(ex):=
block([simp:false,
inflag:false], /* use inflag: true here if a/b should be treated
as a*b^-1 */
m_to_c1(totaldisrep(ex)))$
/* Example
m_to_c([23,a[i]/(sin(x)+g(23)+1/2)])
=>
mylist(23,mydivide(mysubscript(a,i),myplus(myplus(sin(x),g(23)),mydivide(1,2))))
*/
On Thu, Feb 10, 2011 at 08:02, Leo T Butler <l.butler at ed.ac.uk> wrote:
> I also need to write for Gnu GMP under C++, which has
>>> some nasty notations.
>>>
>>
>> Yeah, it's quite a disaster, isn't it ... but tell us what you want
>> and I'll give it a try.
>>
>
> Jon Wilkening at Berkeley has written a nice C++ wrapper around GMP,
> so that the code can look quite natural unlike C code using GMP.
>
> http://math.berkeley.edu/~wilken/code/gmpfrxx/
>
> I seem to recall I persuaded him to release the code under GPL,
> as I used it in a project a few years ago and we corresponded then.
>
>
>
>> Moreover i want to do these computations for complex numbers,
>>> so i have to promote reals to
>>> complex numbers when they appear in the expressions, etc.
>>>
>>
> I am not sure if the wrapper handles complex numbers at this point,
> though.
>
> Leo
>
> --
> The University of Edinburgh is a charitable body, registered in
> Scotland, with registration number SC005336.
>
>
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>