How to provide inputs, e.g. to the calculate command in library mode

Asked by vanfrunikenfv130tn

Hi,
I am trying to use the matholibrary, which I installed successfully, and am calling it from Objective-C (from C, really), because my app needs symbolic manipulation. Many commands work. I send a sequence of commands, then obtain the result (see code below).

My question:
Is there a way to send the command calculate its argument values in line, as follows? (maybe there is an option that I don't know of, or syntax, e.g. ":1" and ":2" for the parameters
Command sequence: "x=3*a+4*b","calculate","1","2"

The code: ...
  NSArray* mathoCommands=[NSArray arrayWithObjects:@"y^2=9",@"solve y",nil];
  NSString* mathoAnswer=[ImplementingClass askMatho:mathoCommands];
  NSLog(@"Result from %@ is %@",mathoCommands,mathoAnswer);...
gives me the following console log:
  Result from ( "y^2=9", "solve y" ) is y = 3*sign
...
@implementation ImplementingClass
+(NSString*)askMatho:(NSArray*)cmds { //sequence of commands produces 1 result string
  NSString *rslt;
  char *output; matho_init();
  for(NSString* cmd in cmds) {
    int cStrLen=[cmd length]+1; char *cmdCStr=malloc(cStrLen);
    BOOL cStrOK=CFStringGetCString(cmd,cmdCStr,cStrLen,kCFStringEncodingASCII);
    if (cStrOK &&matho_process(cmdCStr, &output) && output) {
      rslt=[NSString stringWithCString:output encoding:NSASCIIStringEncoding];
      free(output);
    }
  }
  return rslt;
}

Question information

Language:
English Edit question
Status:
Solved
For:
Mathomatic Edit question
Assignee:
No assignee Edit question
Solved by:
vanfrunikenfv130tn
Solved:
Last query:
Last reply:
Revision history for this message
vanfrunikenfv130tn (fv130) said :
#1

Hi,
I am trying to use the matholibrary, which I installed successfully, and am calling it from Objective-C (from C, really), because my app needs symbolic manipulation. Many commands work. I send a sequence of commands, then obtain the result (see code below).

My question:
Is there a way to send the command calculate its argument values in line, as follows?
Command sequence: 'x=3*a+4*b','calculate -args "1,2" '
OR
Command sequence: "x=3*a+4*b","calculate","1","2"
 (maybe there is an option that I don't know of, or syntax, e.g. ":1" and ":2" for the parameters)

The code: ...
  NSArray* mathoCommands=[NSArray arrayWithObjects:@"y^2=9",@"solve y",nil];
  NSString* mathoAnswer=[ImplementingClass askMatho:mathoCommands];
  NSLog(@"Result from %@ is %@",mathoCommands,mathoAnswer);...
gives me the following console log:
  Result from ( "y^2=9", "solve y" ) is y = 3*sign
...
@implementation ImplementingClass
+(NSString*)askMatho:(NSArray*)cmds { //sequence of commands produces 1 result string
  NSString *rslt;
  char *output; matho_init();
  for(NSString* cmd in cmds) {
    int cStrLen=[cmd length]+1; char *cmdCStr=malloc(cStrLen);
    BOOL cStrOK=CFStringGetCString(cmd,cmdCStr,cStrLen,kCFStringEncodingASCII);
    if (cStrOK &&matho_process(cmdCStr, &output) && output) {
      rslt=[NSString stringWithCString:output encoding:NSASCIIStringEncoding];
      free(output);
    }
  }
  return rslt;
}

Revision history for this message
George Gesslein II (georgegesslein) said :
#2

Excellent question, thank you!

The calculate command is disabled in the library, because there is currently no way to send or receive the multiple inputs or outputs it sometimes requires and generates.

To have similar functionality to the calculate command, use the approximate command and the "simplify sign" command.

To plug in values like in "x=3*a+4*b", use the eliminate command:

Mathomatic library test/example program.
1-> clear all
1-> x=3*a+4*b
1: x = (3*a) + (4*b)
1-> a=1
2: a = 1
2-> b=2
3: b = 2
3-> #1
1: x = (3*a) + (4*b)
1-> eliminate all
1: x = 11
1->

Almost everything that is done by the calculate command can be done selectively with the above commands.

Good luck and I am happy to answer any further questions you have while using the Mathomatic symbolic math library via email, because there will probably be a lot of them :-)
Someday I will make the appropriate FAQs or documentation you need, I am currently learning what people want to know.
Thank you for your time and interest!

Regards,
George Gesslein II
<email address hidden>

Revision history for this message
George Gesslein II (georgegesslein) said :
#3

Perhaps an easier to manage method of variable substitution is the following:

Mathomatic library test/example program.
1-> x=3*a+4*b
1: x = (3*a) + (4*b)
1-> replace a with 1
1: x = 3 + (4*b)
1-> replace b with 2
1: x = 11
1->

Just use the replace command and follow "with" with any expression, it is that simple! I hope that answers your question; if not, please write again.

Regards,
George Gesslein II

Revision history for this message
vanfrunikenfv130tn (fv130) said :
#4

Thank you so much for your speedy answer.
In fact, in the mean time I independently discovered that the eliminate command could serve my purposes.
I also found that with some ordering of the equations, adding an "y=x" (rather than "#1", which is less readable), then "repeat eliminate all" seems to be robust against equation order.

But your last suggestion, to use replace is even better, because that is actually all I need after having obtained and simplified the (in real life much more complicated:) equation for x.

Franklin

Revision history for this message
George Gesslein II (georgegesslein) said :
#5

Dear Franklin,

That is 100% correct.
You are welcome.
Thanks for the good question.
Write me any time.

Regards,
George Gesslein II
<email address hidden>