cancel
Showing results for 
Search instead for 
Did you mean: 
Layzer
Level 2
14 9 1 0
Message 1 of 10
1,373
Flag Post

Solved!

Help in using the CAS.solve(STRING1,STRING2) command inside a program

HP Recommended
Hp prime

Hello and hope you can help me with my problem!

 

I'm writing a somewhat complicated program (non cas function mode) which has a subroutine called STSOLVE where its input is always a string coming from the main program which contains a formula like "«I»=«V»/«R»,«R»" and its purpose is to replace all variables contained within «» to standard CAS-compatible automatically generated variables (teiXX where XX is a unique increasing number) and then use the solve command to evaluate that formula (say final, temporary formula "tei1=tei2/tei3,tei3"). Before returning, it substitutes the temporary, automatically generated variables with the original ones on the result so the original formula will be sent back solved in the main program.

 

My problem:

 

When the CAS.solve command is used it either returns the result {undef} or simply {} even in expressions that absolutely work if used with the solve command manually, in the calculators main CAS screen! Although I know that the input for this command is correct according to the tutorials I've read in the forums, I just can't get it to work correctly.

 

I've tried all the methods of writing the command and I only managed to get some result when I wrote it like:

 

 

RPART1:=CAS.expr(RPART1);              

RESLT:=CAS.solve(RPART1,"tei2");

 

 

...where RPART1 contains, say, the part "tei1=tei2/tei3" (NOT AS A STRING!) and "tei2" MUST be written as stated here, a string, manually entered just for testing purposes (can be "tei1" or "tei3" too for this particular case). However, if I put "tei2" into the subroutine's local variable RPART2 as a string and then put that variable into the solve command, say:

 

RPART1:=CAS.expr(RPART1);

RESLT:=CAS.solve(RPART1,RPART2);  //RPART2 contains string "tei2", "tei1" or "tei3"

 

then again I get a {} as a result!

 

I've tried so many things already that I don't even know what exactly to mention here, but I hope I described the main situation detailed enough for a start and of course I will give you any other info you may require to give me further help. I wanted to paste the whole subroutine or even the whole program but I have no windows pc to connect the HP Prime and copy the program. I'm writing from an Android tablet and I should possibly type the whole thing here so it's not very easy, I'll do it of course if you request it.

 

I maybe should also note that the generated vars tei1, tei2 etc do not exist as variables in the calculator, however if I copy the very same strings that the program tries to execute in the CAS.solve command and write them manually in the cas screen (not a strings of course) then they get solved without problems.

 

Hope to get help and learn from you and I wish I could had found more help about this online, but it seems very difficult and also all the tutorials I found about the cas.solve command within programs didn't work in my case!

 

 

Andreas

0 Kudos
Tags (1)
9 REPLIES 9
Jan_D
Level 6
305 297 32 76
Message 2 of 10
Flag Post
HP Recommended

This works, or is not this the problem?

 

CAS.solve("tei1=tei2/tei3","tei2");

 

I get as output: {tei1*tei3} which is correct.

0 Kudos
Tags (1)
Layzer
Author
Level 2
14 9 1 0
Message 3 of 10
Flag Post
HP Recommended

Hello Jan_D and thanx for your interest and your response!

 

The exact formulation you used in your answer:

 

CAS.solve("tei1=tei2/tei3","tei2");

 

works for me too.

 

However, if I write it like this:

 

RPART1:="tei1=tei2/tei3";

RPART2:="tei2";

RSLT:=CAS.solve(RPART1,RPART2);

 

where RPART1 and RPART2 = local program variables, it returns an {undef} result!

 

If I un-string RPART1, however, and also write "tei2" directly as a string in the command, and not put it stored in a variable instead, then I get correct results, but of course I need to be able to dynamically change for what variable it must be solved for during program execution and can't work with a fixed string in the command.

 

UPDATE: Yesterday I found out that I was not using the latest firmware of the calculator but the previous one. After I upgraded, the command seems to work finally, even tho again both parts (RPART1 and RPART2) have to be un-string by using the command CAS.expr() on them before putting them in the CAS.solve() command. That's not exactly the way of writing them according to the tutorials I read (where they are stating that the CAS.solve command requires strings as arguments) but still it's fine by me since my program can finally run and give me results. 

 

 

Again, thank you for your contribution and help!

 

Andreas

0 Kudos
Jan_D
Level 6
305 297 32 76
Message 4 of 10
Flag Post
HP Recommended

Hello Andreas,

 

Could you tell me which version of the firmware you have?

My Help information says that I have Software Version 2015 7 28 (8151)

And CAS Version 1.1.2-11

 

But I have not the physical device but the Android app on my tablet, which is in many respects identical.

 

Let's come back to the problem.

You said you first unstringed RPART1 and then used the CAS.solve command, entering "tei2" for the second argument of CAS.solve, and that this works.

 

Could you give the exact code you used?

When I write the following program it does not work:

 

EXPORT MYPROGRAM()

BEGIN
  LOCAL RPART1,RPART2;

  RPART1:= ”tei1=tei2/tei3”;
  RPART1:=CAS.expr(“RPART1”);

  RSLT:=CAS.solve(RPART1,”tei2”);   //RSLT is a global variable
END;

 

The next program works however, is straightforward and I do not need to unstring.

Essential is however that RPART1 and RPART2 are local variables, when they are global it does not work.

This is the program:

 

EXPORT MYPROGRAM()

BEGIN
  LOCAL RPART1,RPART2;

  RPART1:= ”tei1=tei2/tei3”;
  RPART2:= “tei2”;

  CAS.solve(RPART1,RPART2);
END;

 

This simple program also works:

 

 

EXPORT MYPROGRAM(RPART1,RPART2)

BEGIN
   CAS.solve(RPART1,RPART2);

END;

It is also possible to use a CAS program as helpprogram.

A CAS program has the structure: #cas ... #end

 

At the beginning I declare both procedures: the main program and the help program.

The main program calls the help program, and the only thing which the help CAS program needs to do is unstring both variables and use the solve command.

 

Strange thing is however that in this case RPART1 and RPART2 have to be global variables, where in the previous examples they had to be local.

 

This is the program:

 

MYmainPROGRAM() ;                 //declarations
MYcashelpPROGRAM();               //declarations

EXPORT MYmainPROGRAM()            //implementation main program

BEGIN 
  RPART1:= ”tei1=tei2/tei3”;      //in this case RPART1 and RPART2 have to 
  RPART2:= “tei2”;                //be global vaiables.
  MYcashelpPROGRAM(RPART1,RPART2);
END;



#cas                              //implementation helpprogram
MYcashelpPROGRAM(A,B)
  BEGIN
       LOCAL a1,a2;
       a1:=expr(A);
       a2:=expr(B);
      solve (a1,a2);
  END;
#end

 

0 Kudos
Tim_Wessman
Level 8
Level 8
747 744 121 187
Message 5 of 10
Flag Post
HP Recommended

Well, the real change here was specifically to address the problems of variables not working as expected when passing to CAS functions. You can now use the variables (global, local, etc) and they work like variables should have worked.

 

There was an important limitation in the old method in that ALL strings were (silently) automatically evaluated. This meant you couldn't ever pass a string to a CAS function.

 

Now you can pass variables directly, but if a CAS command doesn't know what do with the string it will error. So placing EVAL() around the string being passed to the CAS command will cause it to be evaluated as it was in 8151 and similar.

TW

Although I work for the HP calculator group as a head developer of the HP Prime, the views and opinions I post here are my own.
0 Kudos
Jan_D
Level 6
305 297 32 76
Message 6 of 10
Flag Post
HP Recommended

@Tim_Wessman wrote:

....... You can now use the variables (global, local, etc) and they work like variables should have worked.....

 



Hello Tim,

I have a problem with the locality of variables though.

 

The following 2 problems are only slightly different, and the one works, while the other does not.

 

This works:

 

EXPORT MYPROGRAM()

BEGIN
  LOCAL RPART1,RPART2;            //variables strictly local

  RPART1:= ”tei1=tei2/tei3”;
  RPART2:= “tei2”;

  CAS.solve(RPART1,RPART2);
END;

And this does not work:

 

LOCAL RPART1,RPART2;              //locality source file wide

EXPORT MYPROGRAM()

BEGIN
  
  RPART1:= ”tei1=tei2/tei3”;
  RPART2:= “tei2”;

  CAS.solve(RPART1,RPART2);
END;

The difference is the extent of locality of the variables RPART1 and RPART2.

So these variables have to be strictly local in order for the command CAS.solve(RPART1,RPART2) to work.

 

Are you saying  that this is due to the fact that I have firmware version 2015 7 28 (8151) and that this problem has been solved in the newist firmware version?

 

I can not check it because I have only the Android app and depend on updates from Google Play.

 

0 Kudos
Layzer
Author
Level 2
14 9 1 0
Message 7 of 10
Flag Post
HP Recommended
Hi again, sorry for my delayed answer and thank you both for your interest and your responses.

Jan, I don't have my calc available right now but I can tell you that I updated its firmware in 22/9, one day after publishing my first post here. The previous firmware was from April 2016 if I remember correctly. So it seems that placing the LOCAL command above the begin command was the only thing on earth I didn't try, and possibly the reason I almost threw the calculator out of the window after endless hours of frustration and efforts to understand what was wrong and the solve command gave me errors.

Seems that Tim gave the explanation of the real problem and I'm glad everything is OK now, after the last firmware update.


Thank you both again and wish I knew before that it was a calculator software problem so I would not struggle all that time 😄
0 Kudos
Tim_Wessman
Level 8
Level 8
747 744 121 187
Message 8 of 10
Flag Post
HP Recommended

Yes, both would work with 1 edit -

 

CAS.solve(EVAL(RPART1),EVAL(RPART2));

 

You could even directly modify the variables in a further CAS subroutine, another subroutine, etc.

 

The reason for the EVAL is because since you are passing a string argument, and a string isn't a valid input for the integral command you would want to indicate that it should be evaluated first.

TW

Although I work for the HP calculator group as a head developer of the HP Prime, the views and opinions I post here are my own.
0 Kudos
Jan_D
Level 6
305 297 32 76
Message 9 of 10
Flag Post
HP Recommended

Strange, because EVAL("x^2+3*x")="x^2+3*x"

 

and even EVAL("X^2+3*X")="X^2+3*X"

 

So EVAL does nothing with strings and leaves them strings.

0 Kudos
Tim_Wessman
Level 8
Level 8
747 744 121 187
Message 10 of 10
Flag Post
HP Recommended

It doesn't actually call the command. Rather, the parser/evaluator sees the EVAL function call in the CAS command arguements, and performs the old "evaluation" of the string.

 

TW

Although I work for the HP calculator group as a head developer of the HP Prime, the views and opinions I post here are my own.
0 Kudos
† The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation