4
$\begingroup$

I have both Maple and Mathematica installed on same PC (Linux).

I would like to call Mathematica command from inside my Maple worksheet (say Integrate, but it could be any other command) and get back the result, and convert it to Maple syntax and use it.

How to do that? Do I need to use wolframscript ?

$\endgroup$

1 Answer 1

5
$\begingroup$

May be this could be of use for some.

Create a Maple worksheet and generate the Mathematica commands to call. Save these in script.m file. Then invoke Maple's ssystem call. This will call Wolframscript and wait for the command to complete. The result is next converted back to Maple using Maple's MmaTranslator.

Here is the code. Open a Maple worksheet and type

Version 1. Uses Mathematica syntax for input

restart;
table_of_integrals:=["1/(a^2-b^2*x^2)",
                     "1+Sin[x]^2+x^3",
                     "Sin[x]*Exp[2*x]-3",
                     "1/Sin[x]",
                     "Exp[x/2]/Sqrt[Exp[x]-1]"];

currentdir(interface(worksheetdir)); #set default to where this worksheet is located
    
the_output :=Array(1..0):
the_output ,= ["integrand","Maple result","Mathematica result"]:

for item in table_of_integrals do
   try
      file_id := fopen("my_script.m",WRITE);
   catch:
       error StringTools:-FormatMessage(lastexception[2..-1]);
   end try;
   fprintf(file_id,"%s",cat("Print[Integrate[",item,",x]];\nExit[];")):
   fclose(file_id):
   the_result:=ssystem("/usr/local/Wolfram/Wolfram/14.3/Executables/wolframscript -script my_script.m"):
   integrand:=MmaTranslator:-FromMma(item):
   the_output,=[integrand,int(integrand,x),MmaTranslator:-FromMma(the_result[2])];
od:

the_output:=convert(the_output,listlist):
DocumentTools:-Tabulate(the_output,width=60):

The result of the above is

enter image description here

You can change the Integrate command to anything else. Say DSolve

This ofcourse assumes you have both Maple and Mathematica installed on same PC and have WolframScript also.

Version 2. Uses Maple syntax for input

This version is much more useful for users of Maple, since the expression to use in Mathematica is Maple's expression as is. So no need to manually write it using Mathematica syntax first as the above version did.

This allows one to call Mathematica directly using maple expressions. The trick is to convert Maple expression (integrand in this example, but can be anything) to MathML. (do not use latex for this, as it is not robust and can lead to problems), MathML works much better.

This below is the same as above, but uses Maple expression as is.

restart;
currentdir(interface(worksheetdir)); #set default to where this worksheet is located
table_of_integrals:=[1/(a^2-b^2*x^2),
                     1+sin(x)^2+x^3,
                     sin(x)*exp(2*x)-3,
                     1/sin(x),
                     exp(x/2)/sqrt(exp(x)-1) ];
    
the_output :=Array(1..0):
the_output ,= ["integrand","Maple result","Mathematica result"]:
for item in table_of_integrals do
   try
      file_id := fopen("my_script.m",WRITE);
   catch:
       error StringTools:-FormatMessage(lastexception[2..-1]);
   end try;
   the_string := MathML:-Export(item);

   fprintf(file_id,"%s",cat("theString=\"",
        the_string,
        "\";\nintergand=ToExpression[theString,MathMLForm];",
        "\nPrint[Integrate[intergand,x]];\nExit[];")):

   fclose(file_id):
   the_result:=ssystem("/usr/local/Wolfram/Wolfram/14.3/Executables/wolframscript -script my_script.m");
   the_output,=[item,int(item,x),MmaTranslator:-FromMma(the_result[2])];
od:
the_output:=convert(the_output,listlist):
DocumentTools:-Tabulate(the_output,width=60):

Which gives the same exact result as before

enter image description here

Here is a standalone Maple function to call, to do integration in Mathematica in Maple !

restart;

integrate_using_mathematica:=proc(integrand,x::symbol)
   local file_id,the_string::string,the_result;
   try
      file_id := fopen("my_script.m",WRITE);
   catch:
       error StringTools:-FormatMessage(lastexception[2..-1]);
   end try;
   the_string := MathML:-Export(integrand);

   fprintf(file_id,"%s",
      cat("theString=\"",the_string,  
         "\";\nintergand=ToExpression[theString,MathMLForm];",
         "\nPrint[Integrate[intergand,",
          String(x),"]];\nExit[];")):

   fclose(file_id):
   the_result:=ssystem("/usr/local/Wolfram/Wolfram/14.3/Executables/wolframscript -script my_script.m");
   MmaTranslator:-FromMma(the_result[2]);
end proc:

Example usage from inside Maple worksheet:

enter image description here

Bug reports are welcome.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.