Proper way(s) to substitute X for x in a CAS expression
12-17-2013, 12:32 AM
Post: #1
 CR Haeger Member Posts: 275 Joined: Dec 2013
Proper way(s) to substitute X for x in a CAS expression
Sometimes, its useful to take an expression (say x^2-5) from the CAS and plot it in Function app.

I know that its possible to substitute X for x in a CAS expression using the following

f(x):= x^2-5 //which creates a CAS program f
Function.F1:=f //which saves X^2-5 to F1

Are there any other straightforward ways of doing this? The subst() command does not seem to do it.

Thanks
12-17-2013, 03:23 AM
Post: #2
 Helge Gabert Senior Member Posts: 467 Joined: Dec 2013
RE: Proper way(s) to substitute X for x in a CAS expression
subst() does work for me.

e.g., subst(x^2-5,x='X') in CAS view returns the desired X^2-5.

Then, store it in one of the predefined function names F1, etc.
12-17-2013, 05:15 PM
Post: #3
 CR Haeger Member Posts: 275 Joined: Dec 2013
RE: Proper way(s) to substitute X for x in a CAS expression
(12-17-2013 03:23 AM)Helge Gabert Wrote:  subst() does work for me.

e.g., subst(x^2-5,x='X') in CAS view returns the desired X^2-5.

Then, store it in one of the predefined function names F1, etc.

Thank you - yes I see where this substitutes X for x properly (I forgot the ' ' marks).

Unfortunately, when storing this in Function.F1 using

X^2-5 Sto> F1 or F1:=X^2-5

The expression is evaluated (using real X value) which results in a real value in F1, not the expression.

The f(x):=expression then F1:f steps are not too onerous for now.
12-20-2013, 09:07 AM
Post: #4
 cyrille de brébisson Senior Member Posts: 1,047 Joined: Dec 2013
RE: Proper way(s) to substitute X for x in a CAS expression
Hello,

Quote:Unfortunately, when storing this in Function.F1 using
X^2-5 Sto> F1 or F1:=X^2-5
The expression is evaluated (using real X value) which results in a real value in F1, not the expression.

Yes, in the same way that you would expect A:=1+2 to result in A=3 and not 1+2...

try using QUOTE (or '') as in:
F1:='X²-5', it will work...
QUOTE(input) (or 'input') stands for "do not evaluate what is inside" and is used just for that type of cases.

Merry xmass!
Cyrille
12-20-2013, 02:32 PM
Post: #5
 CR Haeger Member Posts: 275 Joined: Dec 2013
RE: Proper way(s) to substitute X for x in a CAS expression
(12-20-2013 09:07 AM)cyrille de brébisson Wrote:  try using QUOTE (or '') as in:
F1:='X²-5', it will work...
QUOTE(input) (or 'input') stands for "do not evaluate what is inside" and is used just for that type of cases.

Merry xmass!
Cyrille

Thank you. The above works as you show as long as CAS is in EXACT mode correct? It seems if it is in Approx mode then it still seems to evaluate 'X^2-5'.

Happy holidays.
12-20-2013, 05:53 PM (This post was last modified: 12-20-2013 05:55 PM by Han.)
Post: #6
 Han Senior Member Posts: 1,883 Joined: Dec 2013
RE: Proper way(s) to substitute X for x in a CAS expression
As much as I like the HP Prime, I must say that the way programs interact with the CAS at the moment feels a lot like "hacking" -- as in, there really isn't a straightforward way to do it and it looks like a bunch of tricks to get things to play nicely.

That said, a symbolic object on the HP Prime is surrounded by quotes. However, the Home view and CAS view treat symbolic objects differently as they have different parsing methods. For the time being, you must always assume that you are working in the Home view when you create any program. So one of the issues you will run into is the inability to use undefined variables in the Home view. This is why 'x^2-1' will produce an error (despite being an algebraic object) whereas as soon as you define lower case x, then the errors go away. The reason is because in the Home view, all objects can can be evaluated must return a real value when evaluated. So 'X^2-1' always works since capital X is always defined.

So just how does one pass an expression so that it can be handled in CAS properly? For now, I would suggest strings. Use a combination of STRING(), EXPR(), and CAS() in addition to CAS.function() where possible. Below are some examples:

Code:
 CAS("f(x):=x^2-1"); // creates a CAS function named f -- noticed the ; may be left off LOCAL g="f(x):=x^2-1"; CAS(g+3); // creates a CAS function named f; f(x)=x^2-13  CAS("f(x):=x^2-1"+3); // does string addition according to CAS rules and returns "f(x):=x^2-13" as a string

The three examples above illustrate the following:
1. If the argument of CAS() is a single string, then the string is passed to the CAS parser and evaluated as if typed from the CAS command line
2. If the arguments of CAS() include explicit strings (as opposed to named strings) then you are essentially doing string manipulation with CAS rules (which, to my knowledge, has no special rules for strings). If in the second example, CAS(g+3) had been changed to CAS(g+"3") then the result would have been the same as in the last example!

So, do what you need to do with strings, then pass the final result to the CAS() command. Here's an example of how to use symbolics inside a matrix:

Code:
 // usage: // CASEX("x^3*y^2", {"x", "y"}) EXPORT CASEX(f,v) BEGIN LOCAL g="[[ ", h="diff(", t;   // differentiate f with respect to the first variable   t:=h+f+","+v(1)+")"; // basically, t:="diff(f, var1)"   t:=STRING(CAS(t));   g:=g+t+",";   // differentiate f with respect to second variable   t:=h+f+","+v(2)+")"; // t:="diff(f, var2)"   t:=STRING(CAS(t));    g:=g+t+"]]";   // return the gradient of f   CAS(g);

Convoluted, but if you study it carefully, it follows the scheme I outlined earlier. Now, to really muddy up the waters, certain CAS commands actually behave normally in the Home view (likely because their Home execution falls back on the CAS rules -.- *sigh*). In particular, the diff() command could be used with CAS.diff() or even just as diff() provided you use single quotes! But I'll save that for another day to discuss.

(12-20-2013 02:32 PM)CR Haeger Wrote:
(12-20-2013 09:07 AM)cyrille de brébisson Wrote:  try using QUOTE (or '') as in:
F1:='X²-5', it will work...
QUOTE(input) (or 'input') stands for "do not evaluate what is inside" and is used just for that type of cases.

Merry xmass!
Cyrille

Thank you. The above works as you show as long as CAS is in EXACT mode correct? It seems if it is in Approx mode then it still seems to evaluate 'X^2-5'.

Happy holidays.

Specifically for built-in variables which take "formulas" for input, you can even get away with a string. For example:

Code:
 F1:="X^2-3*X+4";

works just fine provided the string properly parses (i.e. if you were to type the formula in with double quotes, it must be a proper expression).

Graph 3D | QPI | SolveSys
12-20-2013, 08:13 PM
Post: #7
 John P Member Posts: 202 Joined: Dec 2013
RE: Proper way(s) to substitute X for x in a CAS expression
Thanks Han. Your post helped me to understand why I could not write prgm to do implicit differentiation on HP Prime. I didn't know about CAS and strings.
 « Next Oldest | Next Newest »

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