Post Reply 
CAS.part() behaviour
12-21-2018, 12:20 PM
Post: #1
CAS.part() behaviour
One for M. Parisse I think...

I'm trying to split and process an expression a bit at a time using a NUMERIC mode program.

Mostly CAS.part() will let me do this but there are some inconsistencies that might be the result of me missing the bigger picture. An explanation would be helpful.

Firstly the bit I understand:
Code:
Expression      length(x)    part(x,0)   part(x,1)   part(x,2)
----------      ---------    ---------   ---------   ---------
'SIN(30)'       1            "sin"       30          error
'COMB(10,2)'    2            "COMB"      10          2
'5+6'           2            "+"         5           6
'SIN(5+6)'      1            "sin"       5+6         error
'5+SIN(30)'     2            "+"         5           SIN(30)
The above seems simple enough: a single parameter function has length 1 and its parts are the function name and the parameter. Functions with two or more parameters have a proportionately longer length and proportionately more parts available. Infix operator expressions are treated as two-parameter functions. Lastly, more complicated expressions are nested, meaning that they can be processed recursively. So far so good.


User defined expressions are treated differently.
Code:
Expression      length(x)    part(x,0)   part(x,1)   part(x,2)
----------      ---------    ---------   ---------   ---------
userdef(1,2,3)  2            "of"        "userdef"   [1,2,3]
Getting back the word "of" for the function name is a bit unexpected but at least I can use that to detect a user-defined function.


However...
Code:
Expression      length(x)    part(x,0)   part(x,1)   part(x,2)
----------      ---------    ---------   ---------   ---------
'NOT 5'         2            "of"        NOT         5
Hmm, so maybe "of" can't be used to identify user-defined functions after all.


And lastly...
Code:
Expression      length(x)    part(x,0)   part(x,1)   part(x,2)
----------      ---------    ---------   ---------   ---------
'RANDOM()'      1            "RANDOM"    RANDOM      error
Why doesn't this just return length 0 to signify no parameters?


Possibly (probably?) some of this behaviour is because I'm running in NUMERIC mode, however I have to because, in the CAS environment, I can't find a way to pass expressions as parameters to a CAS mode program without them being simplified before the program gets hold of them, even when quoted.

Version: 2.1.14181 (2018 10 16)
CAS Version: 1.4.9
Find all posts by this user
Quote this message in a reply
12-21-2018, 01:36 PM
Post: #2
RE: CAS.part() behaviour
(12-21-2018 12:20 PM)BruceH Wrote:  However...
Code:
Expression      length(x)    part(x,0)   part(x,1)   part(x,2)
----------      ---------    ---------   ---------   ---------
'NOT 5'         2            "of"        NOT         5
Hmm, so maybe "of" can't be used to identify user-defined functions after all.

And lastly...
Code:
Expression      length(x)    part(x,0)   part(x,1)   part(x,2)
----------      ---------    ---------   ---------   ---------
'RANDOM()'      1            "RANDOM"    RANDOM      error
Why doesn't this just return length 0 to signify no parameters?

FYI, tried that on Xcas 1.4.9-57 (win32):

length('NOT 5') ==> 1
part('NOT 5', 0) ==> not

length('random()') ==> 0
part('random()', 0) ==> random
Find all posts by this user
Quote this message in a reply
12-21-2018, 06:52 PM
Post: #3
RE: CAS.part() behaviour
I don't understand why you can not run a CAS program from the CAS. Nothing is simplified before a CAS program is called, but of course arguments are evaluated unless they are quoted.
RANDOM is not a CAS command, try with rand.
I get "not" for part('NOT 0',0) (and 0 for part('NOT',1))
Find all posts by this user
Quote this message in a reply
12-23-2018, 12:28 AM
Post: #4
RE: CAS.part() behaviour
(12-21-2018 06:52 PM)parisse Wrote:  I don't understand why you can not run a CAS program from the CAS. Nothing is simplified before a CAS program is called, but of course arguments are evaluated unless they are quoted.

You say nothing is simplified - that is not what I am seeing. Try the following:

Starting with the emulator, freshly reset via the menu and running version "Build: 2.1.14181 (2018 10 16)", I created this program:
Code:
#cas
test(a):=
BEGIN
  RETURN a;
END;
#end
Running from CAS, I get test('1+1') --> 2.

If I create this program:
Code:
test(a)
BEGIN
  RETURN a;
END;
Running from HOME I get test('1+1') --> 1+1

If CAS does not simplify, as you say, then how do I get the CAS program to return 1+1 rather than 2?
Find all posts by this user
Quote this message in a reply
12-23-2018, 07:09 AM
Post: #5
RE: CAS.part() behaviour
As I explained, it does not simplify but it evaluates. When you run test('1+1'), quote(1+1) is evaled to 1+1, then test is called with argument a==1+1, and when return(a) is evaled, a is replaced by 1+1 and evaled to 2.
You must run test(quote(quote(1+1))) to get '1+1'. (Assuming postprocessing with regroup or simplify is disabled in your settings).
It's almost impossible to prevent evaluation. Evaluation will only do simple operations, like adding integers or rational fractions. It will not simplify polynomials or do more complex tasks. Evaluation is required to do most CAS tasks, otherwise some strange things could happen (for example sqrt or division will not work correctly).
Find all posts by this user
Quote this message in a reply
Post Reply 




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