Post Reply 
[SOLVED] LNAME - exctracting variables from stringed expression?
01-16-2017, 09:34 AM (This post was last modified: 01-16-2017 12:38 PM by chromos.)
Post: #1
[SOLVED] LNAME - exctracting variables from stringed expression?
Hi,
I have stringed valid expression in list, for example L1(1):="(C-A)". How can I extract variables C and A from it with LNAME?

Thank you in advance.

Prime G2, 15C CE
Find all posts by this user
Quote this message in a reply
01-16-2017, 11:41 AM (This post was last modified: 01-16-2017 11:42 AM by DrD.)
Post: #2
RE: LNAME - exctracting variables from stringed expression?
I can share this, and hopefully, it's helpful for you and others ...
(You can copy each command line up to the semi, and paste into the command entry for testing):

1. Example: b:="X+Y<4"; // We know X,Y are reserved vars, so:
LNAME(CAS("CAS(b)")); // ==> {};

2. Convert to lower case variables, one way to avoid reserved vars:
b:=LOWER(b); // X+Y<4 ==> x+y<4
LNAME(CAS("CAS(b)")); // ==> {x,y}
LNAME(CAS("right(CAS(b)")); // ==> {x,y} I won't mention the CAS left/right thing.

3. Extending the example to your case:
b:="(X-Y)"; ==> similar to your expression, using X and Y
b:=LOWER(b); ==> non reserved variables
LNAME(CAS("CAS(b)")); // ==> {x,y}
LNAME(CAS("left(CAS(b)")); // ==> {x} I won't mention the CAS left/right thing.

b:= "(C-A)"; ==> Precisely your provided expression
b:=LOWER(b); ==> To get non-reserved variables
LNAME(CAS("CAS(b)")); // ==> {}

// Check to see if there was any pre-existing content in variables c or a:
// I previously used a and c, so let's change to different variables: "(G-F)"

b:= "(G-F)"; ==> Modeling your provided expression
b:=LOWER(b); ==> To get non-reserved variables
LNAME(CAS("CAS(b)")); // ==> {g,f}

Summary:

1. LNAME is shown in the [Toolbox] and command entry, as a Home command, and shown in the [Help] as a CAS command.

2. LNAME requires non-reserved variables, (symbolics only).

3. Possibly one way to get non-reserved variables is to shift them to lower case equivalents. They must not pre-exist, only symbolic.

4. An LNAME acceptable string argument can get "evaluated" in the CAS system using the CAS command.

5. Be careful when using the CAS to obtain left or right expressions from a statement. The opinions expressed, may not be your own.

-Dale-
Find all posts by this user
Quote this message in a reply
01-16-2017, 12:23 PM
Post: #3
RE: LNAME - exctracting variables from stringed expression?
You can also do:
Code:
LNAME(EXPR("'"+L1(1)+"'"))

In your case, with L1(1):="(C-A)", it will return {C,A}

If you have to use it several times it may be interesting to create a small function such as:
Code:
EXPORT STR2EXPR(str)
BEGIN
RETURN EXPR("'"+str+"'");
END;

and just do: LNAME(STR2EXPR(L1(1)))
Find all posts by this user
Quote this message in a reply
01-16-2017, 12:38 PM
Post: #4
RE: LNAME - exctracting variables from stringed expression?
Thank you Dale for your in-depth explanation and examples, but I think if I must convert variables to lower letters then I can simply use CAS.lname("x+y").

Didier, thank you very much, your code is exactly what I was trying to find!

Prime G2, 15C CE
Find all posts by this user
Quote this message in a reply
01-16-2017, 02:46 PM
Post: #5
RE: [SOLVED] LNAME - exctracting variables from stringed expression?
(01-16-2017 11:41 AM)DrD Wrote:  Summary:

1. LNAME is shown in the [Toolbox] and command entry, as a Home command, and shown in the [Help] as a CAS command.

2. LNAME requires non-reserved variables, (symbolics only).

3. Possibly one way to get non-reserved variables is to shift them to lower case equivalents. They must not pre-exist, only symbolic.

4. An LNAME acceptable string argument can get "evaluated" in the CAS system using the CAS command.


-Dale-

#1: I wonder if this is incomplete documentation. There are two commands: lname and LNAME that do similar things. In Home view, you need to provide an unevaluated expression to LNAME.

#2: LNAME can return any type of variable: non-reserved, reserved, even app names and local names from within programs.

#3: Pre-existing variables are fine. Use a quoted expression to force non-evaluation. LNAME('X+Y') will return { X, Y } in the Home screen. Typing in LNAME('X+Y') vs lname('X+Y') in the CAS view will net different results (probably due to differences in how each view passes parameters to the other view; or it could simply be a bug). Generally speaking, use uppercase in Home view and lowercase in CAS view for command names.

#4: Not "can" but "will" -- this is a guaranteed behavior. I am unaware of any instance in which this is not the case, but I have been wrong on many occasions (and quite possibly more often than not when it comes to the CAS). The CAS() command takes as its argument the content of the string and always evaluates the content. Evaluating CAS("x+2*y-3") is the same as literally typing x+2*y-3 in the command line while in CAS view (and the command line necessarily evaluates what was entered). To get an unevaluated expression, quote the content: CAS(" 'x+2*y-3' "). Quoted expressions are still evaluated -- effectively, their quotes are stripped.

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
01-16-2017, 03:05 PM (This post was last modified: 01-16-2017 03:35 PM by DrD.)
Post: #6
RE: [SOLVED] LNAME - exctracting variables from stringed expression?
I mentioned the LOWER("Expression") idea, hopefully, to be useful to all who might benefit from such things as cut and paste, from on line activities.

For example, an objective function, with several related constraint inequalities, were part of some problems I have been working with recently. I was able to quickly paste them, as found, into a prime program using that technique. In my program it was necessary to use the LNAME command, (and several other CAS commands), to eventually accomplish the desired result.

It often happens that when information is requested in this forum, it might already exist from previous posts. The 'trick' is how to quickly search for it. Assuming my contributions are at all useful, there may be some future value in sharing details. I have found that sometimes a specific answer doesn't quite reveal enough information to resolve the real underlying problem, and often the help resources don't provide quite enough.

I have found this forum to be quite a valuable resource, thanks to the very knowledgeable people here!
Find all posts by this user
Quote this message in a reply
01-16-2017, 03:33 PM
Post: #7
RE: [SOLVED] LNAME - exctracting variables from stringed expression?
Prime can be confusing. If Han writes a book on the subject, I'm going to try to buy it!

Here's a few variations on the theme:

[CAS]
LNAME('X+y'); // ==> [X y] (vector)
lname('X+y'); // ==> Shows up as LNAME and returns {y} (list)

[Home]
LNAME('X+y'); // ==> Syntax error (we know why).
lname('X+y'); // ==> same deal.
LNAME("'X+y'"); // ==> {} blank as my mind.
lname("'X+y'"); // ==> same deal
CAS("lname('X+y');"); ==> [139,y] (previously X=139 here)
CAS("LNAME('X+y');"); ==>{y}

b:="X+y";
LNAME(CAS("CAS(b)")); // ==> {y}, the reserved variable X is not present.
b:=LOWER(b);
LNAME(CAS("CAS(b)")); // ==> {x,y}, life is good.

What I learn from this is that there is much to be learned.
Find all posts by this user
Quote this message in a reply
Post Reply 




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