Search code examples
tclmaxima

Tcl calling maxima


How to send and receive data from external program in tcl? I'm try this:

set d exec maxima --batch-string='5+10;'
puts d;

But its didn't work. I need to call Maxima(http://maxima.sourceforge.net/index.html) from tcl and return in tcl maxima results. Official documentation is empty or very old and examples not working.


Solution

  • I've wrote the function maxima_call to easily call Maxima. You need to set your own path to Maxima program in maxima_path variable. On Linux it could be set maxima_path maxima

    proc maxima_call {expression} {
      set maxima_path "M:\\Programs\\maxima-5.40.0\\bin\\maxima.bat"
      set keys ""
    #  set keys "display2d:false\$"
    
      set result [split [exec ${maxima_path}  --batch-string=${keys}${expression}\;] \n]
      set result [lreplace $result 0 4]
      return [join $result \n]
    }
    
    puts [maxima_call "5+10"]
    

    But I'm not clearly understand how are you going to parse results of calculation. For example, i've got this output from Maxima.

    set expression "\[aa : 1, bb : 2, cc : 3\]; (aa + bb + cc)/(dd + ee)"
    puts [maxima_call $expression]
    

    >

    (%i1) [aa:1,bb:2,cc:3]
    (%o1)                              [1, 2, 3]
    (%i2) (aa+bb+cc)/(dd+ee)
                                           6
    (%o2)                               -------
                                        ee + dd