Post Reply 
[HP PPL] Concatenation inside CAS(..) command does not work
08-04-2024, 03:44 PM
Post: #1
[HP PPL] Concatenation inside CAS(..) command does not work
Hello, new Prime user here. I have the following program:

Code:

  local d, f, expression;
  if input({{f,[2]}}, "Derivate", "f(x)=") 
  then       
    expression:="diff(x^2,x)";
    d:= CAS(expression);
    return d; 
  end;

This program doesn´t work. It returns the string "diff(x^2,x")" instead of the actual differentiation result.

But if replace the above CAS call with:

Code:

    d:= CAS("diff(x^2,x)");

Then I get the proper diff output. Why?
Find all posts by this user
Quote this message in a reply
08-05-2024, 05:55 AM (This post was last modified: 08-05-2024 06:14 AM by komame.)
Post: #2
RE: [HP PPL] Concatenation inside CAS(..) command does not work
When using a variable in a CAS call on the PPL side, you should indicate that it contains an expression rather than a string.

Use expr like this:
Code:
    expression:="2^3";
    d:=CAS(expr(expression));

However, this still won't work in your case because PPL program cannot return symbolic results (Home doesn't work with symbols).

Programs that operate on symbolic expressions should be written in CAS, not PPL.

Piotr Kowalewski
Find all posts by this user
Quote this message in a reply
08-05-2024, 05:52 PM
Post: #3
RE: [HP PPL] Concatenation inside CAS(..) command does not work
(08-05-2024 05:55 AM)komame Wrote:  When using a variable in a CAS call on the PPL side, you should indicate that it contains an expression rather than a string.

Use expr like this:
Code:
    expression:="2^3";
    d:=CAS(expr(expression));

However, this still won't work in your case because PPL program cannot return symbolic results (Home doesn't work with symbols).

Programs that operate on symbolic expressions should be written in CAS, not PPL.

I was actually able to work by receiving the symbolic function as a string argument on input, then solving integrals and derivatives using the CAS. prefix:

Code:

  if input(
  {{f, [2]}, {var, [2]}, {lower,[2]}, {upper,[2]}},
        "Prob Ψ",
        {"Ψ=", "var=", "lower=", "upper="}
      )
  then

    f := "(" + f + ")^2";
    e := "∫(" + f + ", " + var + ", " + lower + ", " + upper + ")";
    result:= CAS.expr(e);
    result:= CAS.simplify(result);
    return result;
  end;

That worked best on real calculator.
Find all posts by this user
Quote this message in a reply
08-05-2024, 06:12 PM
Post: #4
RE: [HP PPL] Concatenation inside CAS(..) command does not work
This:

local d, f, expression;
if input({{f,[2]}}, "Derivate", "f(x)=")
then
expression:="diff(x^2,x)";
d:= CAS(eval(expression));
return d;
end;

should work. At least it does for me.

-road
Find all posts by this user
Quote this message in a reply
08-06-2024, 04:08 AM (This post was last modified: 08-06-2024 04:32 AM by komame.)
Post: #5
RE: [HP PPL] Concatenation inside CAS(..) command does not work
I know that the result can be displayed, but that's just a side effect of the approach you used. The Home mode, even though it displays the result, doesn't actually understand it.

So, the question is, why do it in Home mode when doing it in CAS allows you not only to get the result but also to continue operating on it? For instance, you can substitute the result of one of your functions into another one of your functions (without using strings, just regular symbolic expressions as input).

The HP Prime is designed to do such things on the CAS side (CAS program with #CAS #END region).

Is there a particular reason why you need to do it in PPL (Home)?

Piotr Kowalewski
Find all posts by this user
Quote this message in a reply
08-06-2024, 03:51 PM
Post: #6
RE: [HP PPL] Concatenation inside CAS(..) command does not work
(08-06-2024 04:08 AM)komame Wrote:  Is there a particular reason why you need to do it in PPL (Home)?

No, I rarely do that. I think it is best practice to write cas programs in cas and home programs in ppl. As i recall (and my memory is a little fuzzy so i may not have all the details right) when the prime was in it's infancy it was hard (or impossible) to do what the OP wanted, especially when passing many variables back and forth between cas and home. There were so many complaints that the software was modified to make it easier (or possible).

People still ask that question so i just give an answer.

-road
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)