Calling Maxima $diff from java



Rupert Swarbrick <rswarbrick <at> gmail.com> writes:

> 
> Mohamed  Rayes <rayes.o.mohamed <at> gmail.com> writes:
> > I am trying to call maxima functions from a java program. But, I am having
> > trouble passing the correct arguments. For example What is the proper way of
> > calling maxima $DIFF from java with arguments $Diff(sin(x),x). Any help with
> > this will be highly appreciated. 
> 
> For us to help you, you're going to have to tell us slightly more about
> your set-up. Have you got a lisp and Java loaded in the same image? In
> which case, I'd guess you're on ABCL or something like that. I didn't
> know that Maxima worked with ABCL, but I guess that'd be cool.
> 
> Or are you communicating through a pipe, marshalling data through
> Maxima's stdin / stdout?
> 
> Something like "$Diff(sin(x),x)" will never work, since it's a mish-mash
> of Maxima's inbuilt language and a mis-spelt Lisp name (should be
> $DIFF). But in order for us to give you more help, you're going to have
> to tell us what you've done so far.
> 
> Rupert
> 
> 
> _______________________________________________
> Maxima mailing list
> Maxima <at> math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
> 

I have compiled maxima with ABCL as described in
http://comments.gmane.org/gmane.lisp.armedbear.devel/2365. I even successfully
ran a java program that calls maxima $Factors. The program is listed at the end
of this message.

I tried  calling the $DIFF (as in $DIFF(sin(x),x)) function in the following way:

1- manually translated sin(x) to maxima internal rep: sin(x) ==> ((%sin) $x)
2- calling $Diff:

Interpreter I = Interpreter.createInstance ();
I.eval ("(require 'asdf)");
I.eval ("(asdf:operate 'asdf:load-op :maxima)");
Package P = Packages.findPackage ("MAXIMA");
LispObject exp = I.eval("'(%SIN) $x)");
LispObject x = I.eval("'$x");
Symbol S = P.findAccessibleSymbol ("$DIFF");
Function F = (Function) S.getSymbolFunction ();
LispObject result = F.execute(exp,p);

I keep getting the error that $DIFF was not found?

Thanks for all the help.

PS. The following is an example that Robert Dodier kindly posted in
http://permalink.gmane.org/gmane.editors.j.devel/5005

// abcl_maxima.java -- example program for calling Maxima from Java via ABCL
// copyright 2012 by Robert Dodier
// I release this work under terms of the GNU General Public License

import org.armedbear.lisp.Interpreter;
import org.armedbear.lisp.Package;
import org.armedbear.lisp.Packages;
import org.armedbear.lisp.Symbol;
import org.armedbear.lisp.Function;
import org.armedbear.lisp.LispObject;
import org.armedbear.lisp.Fixnum;
import org.armedbear.lisp.Bignum;
import org.armedbear.lisp.Cons;

public class abcl_maxima
{
	public static void main (String [] args) throws Exception
	{
		int a;

		if (args.length > 0)
			a = Integer.parseInt (args [0]);
		else
			a = 12341234;

		Interpreter I = Interpreter.createInstance ();
		System.out.print ("loading Maxima ... ");
		I.eval ("(require 'asdf)");
		I.eval ("(asdf:operate 'asdf:load-op :maxima)");
		System.out.println ("finished.");
		Package P = Packages.findPackage ("MAXIMA");

	//	java.util.List <Symbol> L = P.getAccessibleSymbols ();
	//	for (Symbol S : L)
	//	{
	//		if (P.equals (S.getPackage ()))
	//			System.out.println (S.toString ());
	//	}

		Symbol S = P.findAccessibleSymbol ("$IFACTORS");
		Function F = (Function) S.getSymbolFunction ();
		LispObject x = Bignum.getInstance (a);
		x = F.execute (x);
		System.out.println ("x.getClass => " + x.getClass ());
		System.out.println ("x = " + x);
		System.out.println ("x.javaInstance => " + x.javaInstance ());
		System.out.println ("x.javaInstance.getClass => " + x.javaInstance () .
getClass ());

		int n = ((Cons) x).length ();
		System.out.println ((n - 1) + " factors of " + a);

		for (int i = 1; i < n; i++)
		{
			Cons x1 = (Cons) ((Cons) x).NTH (i);
			Fixnum f = (Fixnum) x1.NTH (1);
			Fixnum m = (Fixnum) x1.NTH (2);
			System.out.println ("factor: " + f.intValue () + ", multiplicity: " +
m.intValue ());
		}
	}
}