|Re: Programming examples|
Message #6 Posted by Han on 22 Oct 2013, 4:47 p.m.,
in response to message #5 by dg1969
Sometime you don't use CAS() cmd :
F0:=expr(" 'X-F1(X)/(" + diff(F1(X),X) + ")' ");
Sometime you must use CAS()...
I really need to clarify things. In a program what is the best way to capture a symbolic expression from the INPUT command ?
How to store results of symbolics manipulations in a locals variables ?
Firstly, all symbolic objects must start and end with: '
EXPR() simply takes a string and tries to parse into something the calculator recognizes. It does all calculations in Home view. Fortunately, symbolic objects are recognized by the single quotes.
So EXPR("'X-2*X'") would just be the symbolic X-2*X whereas EXPR("X-2*X") would actually compute the value of X-2*X using the current value of X. -- Note the ' in the first case and the lack of ' in the latter case.
If you want to do symbolic manipulation in the sense of integration, laplace transforms, derivatives, etc. then it must be done in CAS.
That said, some CAS commands _do_ work in Home View (such as subst() and diff().
If the only goal is to construct a symbolic object, and it can be done with strings, then use string objects whose first and last characters are ' and then convert from string into expressions with EXPR().
fsquare:=expr("'(" + string(f) + ")^2'");
In the example above, fsquare is a symbolic object. It is NOT a function.
EXPR() takes a string and tries to parse the string. CAS() is almost the same, but it parses it according to the CAS view rules.
CAS() basically does all the "stuff" inside the () as if you are in CAS view. However, there is a slight difference how CAS() works. If the argument of CAS() is a single string, then CAS() will actually execute the string as if it were typed in the command line.
CAS("x:=5+2/3") will cause the CAS to store 17/3 into the variable 'x'
CAS("f:=2*X-5") will cause the CAS to stored the formula 2*X-5 into f -- however because X is a global variable, it actually stores 2*0-5 or just -5 into f. So if you want to use global variables, you must enclose the "formula" in single quotes. That is, CAS("f:='2*X-5'") would be required. The same is true if you use CAS("f:=2*x-5") and x is defined as a number. Unlike X, x can be purged. So we can be lazy and type CAS("f:=2*x-5") and still get 2*x-5 (and not an evaluation of 2*x-5) as long as x is purged.
CAS("subst(2x-5,x,'i*X')") will return 2(i*X)-5
CAS("x:=5+" + "2/3") will simply do string addition in the CAS environment.
So if X:=0 and f:='X^2-5' then something like F1:=CAS("subst(" + f + ",X,i*X)") would actually return just the string "subst(-5,X,i*X)" because f is evaluated to -5, which is then converted to a string, and then the strings "subst(", "-5", and ",X,i*X)" are added together, and finally (because we are storing into F1), this string is converted into a symbolic.
The issue here is that we have local variables which we want to use in the CAS environment. Unfortunately, CAS view DOES NOT recongize local variables! We cannot do anything like:
CAS("subst(" + f + ",X,i*X)")
because this just does string addition inside the CAS view.
In the home view, you can type:
CAS(subst('X^2-5','X','i*X')) and it would give the same result as typing in CAS view without using CAS(). Unfortunately, something like CAS(subst(f,'X','i*X')) would not work because f is a local variable inside a non-CAS program. So f would be evaluated according to non-CAS rules before being passed to subst().
This is where we can use CAS.function() and expr() together.
myexpression:=CAS.subst( expr("'" + string(f) + "'"), 'X', 'i*X'))
Note the addition of the single quotes (because the string command does not add them automatically for us).
I was able to get away with EXPR("'X-F(X)/(" + diff(F1(X),X) + ")'"); because diff() happens to behave nicely in home view when used with F1. F1(X) is treated as a symbolic object so I do not need to do any crazy type conversions.
Edited: 22 Oct 2013, 4:57 p.m.