parsing expressions




On Wed, 4 Aug 2010, Sheldon Newhouse wrote:

< Hello,
<  I work in a unix environment (Fedora Core 12) and I want to make use of an
< external program in conjunction with maxima.
< 
< The external program does not accept the character '^' for powers.
< 
<  It has a function "pwr(x,n)" which outputs the same as maxima's  "x^n"    It
< also uses " := " as the assignment operator
<  instead of maxima's " : ".
< 
<  So, basically, I want to take some maxima input saved into a file (e.g. with
< the stringout command) and import it to this external program.
< 
< Thus, suppose that I have the following maxima commands
< 
< (%i2) p: x^3*sin(x^4-1)^2 - x^2 + 1.2;
< 
< (%o2) x^3*sin(x^4-1)^2-x^2+1.2
< (%i3) stringout("data_3", p);
< 
< (%o3) "/home/sen/Current_Work/VI_prac/data_3"
< (%i4) system("cat data_3");
< 
< x^3*sin(x^4-1)^2-x^2+1.2;
< 
< (%o4) ?\#\<process\ 22543\ \:EXITED\>
< 
< Thus, we have the string
<  x^3*sin(x^4-1)^2-x^2+1.2;
< 
< in the file "data_3".
< 
< I want to change the output string to
< 
<  pwr(x,3)*pwr(sin(pwr(x,4)-1),2 - pwr(x,2) + 1.2;
< 
< for input into the other program.

You can use Maxima's simplifier:

(%i475) matchdeclare(alpha,true,beta,true);
(%o475) done
(%i476) tellsimpafter(alpha^beta,pwr(alpha,beta));
(%o476) [?\^rule1,?simpexpt]
(%i477) p:x^3*sin(x^4-1)^2-x^2+1.2;
(%o477) pwr(x,3)*pwr(sin(pwr(x,4)-1),2)-pwr(x,2)+1.2

If you use stringout(file,p), you don't need to worry about
':', but if you use stringout(file,values), then the assignment
operator will appear. 

perl -wpl -e 's/:([^:=])/:=$1/g'

will translate : to := in the output file.

Leo 

-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.