User Tools

Site Tools


prime:cashome

This is an old revision of the document!


CAS vs Home

CAS is designed to calculate things symbolically, so sometimes we can be surprised with results for numerical calculations. e.g. SUM(1/x^2,x,1,100) tries to sum the expression (1/x^2) for x = 1 to 100. However, under CAS, it will result in a ratio of 2 very large integers. You'll have to run approx() to get the decimal result. If you try it with 5000 instead of 100, it will take quite a while and when complete will result in undef as it ran out of decimal digits for the numerator and denominator of the result. It's not trying to evaluate 1/x^2 in a loop and adding the result to a sum. It's trying to evaluate it in a way that it could do it with SUM(1/y^2,y,x,1000x) resulting in Psi(x,1)-Psi(1000*x+1,1).

To use a conventional numerical loop, don't use CAS. In this case, go to home mode and use the Σ symbol from the math template key….as in Σ(1/x^2,x,1,5000). This runs much faster than SUM() as it's not trying to solve symbolically, and will result in a numerical solution (1.6447… in this case).

Just remember the rule that CAS is trying to solve symbolically, Home solves numerically, so use the appropriate CAS or Home function depending on which you are trying to do.

Generally speaking, if an algorithm is put together using CAS commands, and a Home program is put together using Home commands to run the same algorithm, then the Home program will run faster. There are some CAS commands whereby a single command executes a full loop. In those cases, the CAS command will run faster than the loop in the Home Program.

In CAS, it is best to always use * when you mean it. e.g. 2*x, a*x, etc.. You can get away with using 2x (a number multiplied by a variable), but never with 2 variables, as it will see ax as a single variable called 'ax', so you'd always have to use a*x.

Also, understand that when using variables in an expression, if that variable has a value assigned to it, CAS may be using that value, instead of solving it symbolically. You can remove the variable from being defined using the purge() function. The restart command purges all the CAS variables.

Some CAS functionality can be assisted by telling the system some characteristics about the variables involved, using the assume() and additionally() functions. The function about(var) returns any assumptions on the variable. Purging the variable removes any assumptions. e.g. To get the integral of e^(-a*x) from 0 to infinity will return undef unless you say assume(RE(a)>0) beforehand. Then it will return 1/a as the answer correctly.

Sometimes, CAS will state something cryptic about how it's answer may not be perfect and when you hit enter again, it then gives you it's answer. This is because some math can be complex and it's trying to tell you what it knows. Don't let these statements worry you. It's just the CAS system being complete.

Make sure you read the help docs for the about, purge, assume and additionally commands.

There are unusual ops that will get translated into functions when you manually enter them, but it's good to learn them as they can allow you faster entry. e.g. a+=3 gets translated to increment(a,3) etc.. These include +=, *=, /=, etc. as listed here. However, they are documented in the XCas documentation. =< is a reference store and gets translated to array_sto(). It's a good idea to become familiar with these operators. Remember that the HP Prime CAS is converted from XCas, documented here. Good HP Prime CAS manual (but in French) here.

Indexing in HP Basic is done using () braces, as in L1(5). In CAS, () can be used with a constant index in read only mode, but when a variable index is used, due to parsing issues (it thinks L1(x) is a function call), you must use [] braces instead, as in L1[i]. So, it is recommended if in CAS mode or in a CAS program, it is best to use [] to index always. This applies to Lists, Matrices and Vectors (1D Matrix). In XCas, lists, matrices and vectors are all lists. Note that XCas uses [] always anyway, so this keeps the Prime's CAS compatible with XCas.

In Home mode, to return multiple values, return it as a list. e.g. f1(x) returns a list containing 3 integers. The values returned can be assigned to variables like this: L:=f1(x);A:=L(1);B:=L(2);C:=L(3); In CAS mode, you can do {A,B,C}:=f1(x); Note that from Home mode the CAS function sto(f1(x),{'A','B','C'}); can be called but it's slower than the other way.

If you define a CAS program using the #CAS…#END directives in a mixed mode program (mixture of Home and CAS functions) and compile it by closing the program editor, then if you delete the function later in the program editor, it does not delete the program from the CAS point of view. You still have to use Shift-Toolbox/Mem and select CAS Variables and select the program there and hit the Delete menu button.

To remove characters from a string, you can use S:=suppress(S,3); to remove the 3rd character from the string S. However, the optimal way to drop characters from a string is to use the following function, STRDEL(). This function is orders of magnitude faster than suppress as suppress() is a CAS function. Note that this version uses substrings, but you could use RIGHT/LEFT function calls to implement it. Note that when using RIGHT/LEFT that there also exist other CAS functions right/left (lower case) related to expressions. Note that to insert string, can use format S(1):=“substring”; to insert “substring” into the string S. If “substring” runs past end of S, it'll get truncated.

// Drop chars from string.
// S   String.
// L   Pos of leftmost char before deletion.
// R   Pos of first char on right after deletion.
EXPORT STRDEL(S,L,R)
BEGIN
LOCAL N:=DIM(S);
RETURN IFTE(L<1,"",S(1,L))+IFTE(R>N,"",S(R,N)); // N just has to go past end of string
END; // BEGIN...
prime/cashome.1509509335.txt.gz · Last modified: 2017/10/31 21:08 by webmasterpdx