Post Reply 
Proper way(s) to substitute X for x in a CAS expression
12-20-2013, 05:53 PM (This post was last modified: 12-20-2013 05:55 PM by Han.)
Post: #6
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
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Proper way(s) to substitute X for x in a CAS expression - Han - 12-20-2013 05:53 PM



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