HP Forums

Full Version: Program help needed
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here's a shortened program construct that involves a CAS command:

Code:
EXPORT aa()
BEGIN
local x,y;
return grad(x^2-3*x*y^3,{x,y});
END;

Naturally, in CAS view grad(x^2-3*x*y^3,{x,y}); returns the desired output. (The program auto-initializes the variables to 0, resulting in non-sense at run time).

Changing to CAS.grad(x^2-3*x*y^3,{x,y}); doesn't work, either. A CAS program works, but this example is a fragment of a larger work, for which I would like to use the conn kit's program editor.

Two questions:

1. Can the above program be constructed to return the {x,y} result? --> [-3*y^3+2*x,-9*x*y^2]

2. I have read several posts describing CAS programming, but context is mostly dated around February 2014. With revisions, changes seem to have made creating a function: f(x):=anything, no longer show up in the [shift] [program] list, so cannot be edited therein. Is there another way to get CAS programs into the program editor?

Help appreciated!

-Dale-
(11-22-2014 01:36 PM)DrD Wrote: [ -> ]1. Can the above program be constructed to return the {x,y} result? --> [-3*y^3+2*x,-9*x*y^2]

Use mat2list(). It will give you a list of components with comma separators (but it won't be a vector in the normal sense).

Quote:2. I have read several posts describing CAS programming, but context is mostly dated around February 2014. With revisions, changes seem to have made creating a function: f(x):=anything, no longer show up in the [shift] [program] list, so cannot be edited therein. Is there another way to get CAS programs into the program editor?

Help appreciated!

-Dale-

If the function was created by typing f(x):=blah, then simply type f on the command line and the function's "source" will be copied onto the history. Copy this output and then in the program editor, create a CAS program and paste the contents into this program. You can now get rid of the one in the CAS environment via purge(f) and just use the one that shows up in the program catalog.
(11-22-2014 01:44 PM)Han Wrote: [ -> ][quote='DrD' pid='21758' dateline='1416663364']
1. Can the above program be constructed to return the {x,y} result? --> [-3*y^3+2*x,-9*x*y^2]

Use mat2list(). It will give you a list of components with comma separators (but it won't be a vector in the normal sense).

Sorry, Han, I was asking: (in terms of the list variables), "how can I get the program (top post) to produce the same result as grad(x^2-3*x*y^3,{x,y}); does when it's manually entered in the CAS view entry line?" grad(x^2-3*x*y^3,{x,y}); --> [-3*y^3+2*x,-9*x*y^2]

Specifically, the program line: return grad(x^2-3*x*y^3,{x,y}); crashes at run-time due to it's variables being auto-initialized to 0. (It's a CAS command vs. non-CAS program thing.)

Thanks for your help!

AND thanks also, to Joe for pointing out that unchecking the "Textbook Display" on pg 2 of Home settings, FIXES the lack of "comma separation in the vector" problem! Otherwise the result of the above example would display like: [-3*y^3+2*x -9*x*y^2] and that's just not good!

-Dale-
(11-22-2014 03:53 PM)DrD Wrote: [ -> ]Sorry, Han, I was asking: (in terms of the list variables), "how can I get the program (top post) to produce the same result as grad(x^2-3*x*y^3,{x,y}); does when it's manually entered in the CAS view entry line?" grad(x^2-3*x*y^3,{x,y}); --> [-3*y^3+2*x,-9*x*y^2]

Specifically, the program line: return grad(x^2-3*x*y^3,{x,y}); crashes at run-time due to it's variables being auto-initialized to 0. (It's a CAS command vs. non-CAS program thing.)

Thanks for your help!

I must have mixed up this post with another prior post of yours about commas in vectors. When you say "crash" are you referring to an actual hardware crash? Or did you simply mean that the program returned an error?

Anyway, the issue here is that regular programs are run in Home view, and not CAS view. If you want to do CAS calculations, you are better off creating a CAS program. That said, you can use CAS.function() or CAS("command string"). The thing to watch out for with CAS.function() is that you don't want the arguments for CAS.function() to be pre-parsed as if run from Home view (which is the case for non-CAS programs). So encapsulate the arguments with double quotes.

CAS.grad("x^2-3*x*y^3","{x,y}");

or (if using the other syntax):

CAS("grad(x^2-3*x*y^3, {x,y}");

The first method is good for a single CAS calculation (usually if it's the result you want to return). The second method is better for multiple CAS calculations. For example, if you wanted to use the result and compute the norm of the gradient:

CAS("gr:=grad(x^2-3*x*y^3, {x,y}");
CAS("(gr(1)^2+gr(2)^2)^(1/2)");

This is also how one can do CAS calculations while in Home view. Specifically for your example, you can type (in Home view):

grad("x^2-3*x*y^3", "{x,y}");

and it will work just fine.

Quote:AND thanks also, to Joe for pointing out that unchecking the "Textbook Display" on pg 2 of Home settings, FIXES the lack of "comma separation in the vector" problem! Otherwise the result of the above example would display like: [-3*y^3+2*x -9*x*y^2] and that's just not good!

-Dale-

Have you tried mat2list() as suggested? If you use mat2list(), you won't have to disable the textbook display.
Before asking for help, I tried encapsulating the variables in 'single' quotes, without success.

Using "double" quotes works, though, and I'll make note of that. The notion of single quotes came from previous posts, where things like the quote() function seemed to solve similar problems. Help says the quote() function, used with Sto, to save a function in a variable; but it is clearly not the same as double quotes!

I did try mat2list, but it doesn't work in certain cases with or without Textbook Display checked:

comma separated:
mat2list(grad(x^2-3*x*y^3,{x,y})) ---> {-3.*y^3+2.*x,-9.*x*y^2}

no comma's:
mat2list( [-3*y^3+2*x -9*x*y^2] ) ---> {-9*x*y^2-3*y^3+2*x}

I was looking for a setting to provide comma separation, and the checkbox was just the ticket for that.

It's not the smoothest interface, but if it works, that's a good thing!

Thank you!

-Dale-
(11-22-2014 08:34 PM)DrD Wrote: [ -> ]Before asking for help, I tried encapsulating the variables in 'single' quotes, without success.

Using "double" quotes works, though, and I'll make note of that. The notion of single quotes came from previous posts, where things like the quote() function seemed to solve similar problems. Help says the quote() function, used with Sto, to save a function in a variable; but it is clearly not the same as double quotes!

I did try mat2list, but it doesn't work in certain cases with or without Textbook Display checked:

comma separated:
mat2list(grad(x^2-3*x*y^3,{x,y})) ---> {-3.*y^3+2.*x,-9.*x*y^2}

no comma's:
mat2list( [-3*y^3+2*x -9*x*y^2] ) ---> {-9*x*y^2-3*y^3+2*x}

I was looking for a setting to provide comma separation, and the checkbox was just the ticket for that.

It's not the smoothest interface, but if it works, that's a good thing!

Thank you!

-Dale-

It looks like in your second example, you fed mat2list() a vector consisting of only one element (so the result is also a 1-element list). In algebraic entry, you are require to use a comma to separate entries in a vector [a,b,c]. So [a b c] would cause the calculator to complain. Likewise for textbook mode, except pressing comma places a space between entries. If, on the other hand, you manually type in a space, you will generally receive a syntax error. However, if you type something like:

[-3*y^3+2*x (space) -9*x*y^2 ]

and the minus sign in front of the 9 is the subtraction symbol, then you haven't created 2 entries. What looks like a 2-entry vector is actually a single entry -- the CAS gracefully handles spaces inside an algebraic expression as if they aren't there. You can test this by using the size command.
Well, that's part of the problem ... There is a space where a comma would (more clearly) separate the TWO parts of the vector. However, 'Textbook Display' checked, resulted in the gradient function producing a vector with a space. Not only can I not clearly see the components in this example, mat2list() may not be able to discern it either.

Is this a bug, perhaps? I've been working with planes and lines tangent to level surfaces, lately. Hence the example. It is important to my old eyes to be able see the vector components clearly. All I needed was to find a simple, dependable way to make that happen. Joe's suggestion, (as always), solved my dilemma.

For expressions with positive terms, it's somewhat less of a visual problem, but with a negative term in subsequent positions it looks like it all rolled into just ONE expression.

You've helped me quite a bit, and I am most thankful. The double quote trick moves me past an impasse that stalled my ppl program progress. I was avoiding a cas program because of conn kit editor limitations, and my lack of handy documentation for cas programming.
(11-22-2014 11:29 PM)DrD Wrote: [ -> ]Well, that's part of the problem ... There is a space where a comma would (more clearly) separate the TWO parts of the vector. However, 'Textbook Display' checked, resulted in the gradient function producing a vector with a space. Not only can I not clearly see the components in this example, mat2list() may not be able to discern it either.

Is this a bug, perhaps?

I'm not disagreeing with you about the usefulness of having an actual comma be visible in all modes of entry and display. However, I personally don't think there is any bug. On the other hand, I do suspect that there is still a lack of clarity in my explanation as to why what you're experiencing is not a bug.

1. Any algebraic expression may have as much white space as we would like to insert, and it is treated as a single expression--unless the expression is invalid. For example:

2*x -9

is valid despite the spaces (the symbol in front of the 9 is the symbol from the minus key, above the addition key). On the other hand,

2*x -9

is invalid if the symbol in front of the 9 is the negative sign (from the +/- key). This has to do with the fact that the negative sign is a unary operator whereas the subtraction sign is binary (hence one errors whereas the other does not). Regardless of the input/display mode, the calculator generally does not recognize using spaces to separate entries in a vector (or matrix... or for most things). You must always use commas, regardless of whether they actually get displayed (both in the entry and in the output). Textbook entry happens to not display the comma but instead places a space in between entries, and inserts a tiny "input" box that must be filled in.

Note also that in the programming environment, there is no distinction between the negation symbol and the subtraction symbol. However, your post suggests you did this in the CAS view, so I'll just leave it at that.

2. I suspect that when you typed: mat2list( [-3*y^3+2*x -9*x*y^2] ) you used the subtraction key and not the negation key. Otherwise, the calculator would have prevented you from executing this command due to a syntax error. That it did not strongly suggests you used the subtraction key after the space, and preceding the 9. Therefore, the object you created was a valid, single-element vector (regardless of whether you were using algebraic or textbook entry). So mat2list() was not misbehaving at all. No commas were inserted because there is only a single algebraic entry in the vector (now a list). If you had followed up with size() and copied the resulting list into the size() command, I would be surprised if the result was anything different from 1.

So that leads me to a question, the answer to which I am most curious:

Did you create the vector [-3*y^3+2*x -9*x*y^2] in Textbook Entry mode? And if so, did you use the comma button to create the space or did you use the space key?

If you can answer yes to the first question using the comma button, then you indeed have found a bug (though one which I am unable to duplicate). Otherwise, points #1 and #2 should sufficiently explain (I hope) why you are seeing the behavior that you are reporting. I honestly don't think mat2list() has any trouble deciphering vectors (though I've been wrong plenty of times before).

As I wrote earlier, if you want to see Textbook Display, and have commas in your vectors, then mat2list() will do what you want. If you are having trouble discerning whether your input is correct (see point #1 above), then I would recommend to use the Algebraic entry mode instead of Textbook entry mode (different from the Textbook Display) and leave Textbook Display on (unless you find that not so useful).

Whatever you choose, a comma will always be required during input (even if said comma does not show up as a result of input-mode settings).

Edit: What I wrote above applies to the CAS view only.
1. ... "However, your post suggests you did this in the CAS view" ....

I used the same CAS view example: grad(x^2-3*x*y^3,{x,y})); throughout this thread. It is from that, the [vector] [-3*y^3+2*x -9*x*y^2] appears. The y-component, -9xy^2, is from the virtual calc, (not me), and looks like a (smaller) negation symbol, to me. All of the expressions used in this thread were from cut/paste activities. Without comma separation, the visual appearance looks like a single expression! (It acts normally though).

[Textbook Display checked]
Enter this: subst([-3*y^3+2*x, -9*x*y^2],{x = 1,y = 2});
The vector's comma disappears, replaced by a space, the vector suddenly looks like a single expression.

Displays this: subst([-3*y^3+2*x -9*x*y^2],{x = 1,y = 2}) ===> [-22 -36]


2. ... "I suspect that when you typed: mat2list( [-3*y^3+2*x -9*x*y^2] ) you used the subtraction key and not the negation key. So that leads me to a question, the answer to which I am most curious:
Did you create the vector [-3*y^3+2*x -9*x*y^2] in Textbook Entry mode? And if so, did you use the comma button to create the space or did you use the space key?
" ...

Maybe the sign was changed along the way {unintentionally}, not by my hand directly. IF you see comma separations then Textbook Display was unchecked. IF you see space separations, Textbook Display was checked. Cut/pasting is how the vector was entered in CAS view. With positive vector components only, the display still behaves similarly, of course, but the space separator becomes more obvious.

To conclude this thread:
Unchecking "Textbook Display" thus using comma separators, makes visually displayed results more legible. Using comma separated vectors is likely to be less error prone for students (especially in my case).
Double quoting CAS command expressions and variables makes them functional in HOME view, or ppl programs.

Thanks, Han. I do appreciate, very much, all the informative help you've provided. It sure would be helpful if HP would spend a dime on improving documentation, and let the hardware and software rest awhile.

-Dale-
(11-23-2014 03:25 PM)DrD Wrote: [ -> ]is from the virtual calc, (not me), and looks like a (smaller) negation symbol, to me.

Oh, I assumed you were using the calculator. One final question. Since you're using the emulator, may I ask if you used the copy/paste from the emulator's menu? or were you using the copy/paste from within the virtual calculator (via [Shift][View] or tapping on the history, etc.)?
When I prepare for posting in forums, etc., I try to remember to also test my posted info on the hardware calc. This way, other forum readers should be able to just copy and paste the info, if desired. Sometimes I use Notes to pass complicated expressions from the CK to the hdwe. (Usually, I just manually enter them). Most of the time, the virtual calc is the better tool for forum interaction, since cut/paste is readily available. I haven't found a way to use CTRL-C / CTRL-V between the CK and the Prime hdwe other than via notes, but it would sure be very handy to do so!

Do you know of another way to shuttle off calc information to the hardware device?

[Virtual Calc]
When copying from the entry line for use off calc, I frequently use [shift][copy] | All, (because CTRL-A doesn't work), along with CTRL-C, to quick copy, then CTRL-V to quick paste into other apps. Wouldn't it be nice if other shortcuts were supported, like CTRL-Z, CTRL-Y (undo / redo), but no...

I use (mouse) touch-select a lot for grabbing historical items from the display, and [Enter] to place them on the entry line. Often I also use the [()] key, first. This has the nice feature of placing grabbed-history items INSIDE those parens, thus properly enclosing expressions / functions / commands!

I also use [shift][copy] along with [shift][paste] to re-use entry line items (or parts thereof).

The efficiency of copy and paste really pays off for me, in both speed and accuracy. I use the different methods quite a lot for different purposes at different times.

There is one other bug-a-boo (not a bugzilla thing) that bugs me: the distinction between subst() and the 'where' | operator. I understand the before / after evaluation detail, as described by Parisse, but this is nearly as useful as copy/paste. I much prefer the shorthand "|" operator.

Efficiency tips and tricks for the Prime would make a nice stand alone "Sticky" for this part of the forum!

-Dale-
Thank you for sharing. I think in light of your most recent post, there is a good chance that you may have found a bug (at least with copying/pasting). I had convinced myself that mat2list() was properly behaving because I myself was unable to duplicate an instance of a 2-entry vector that, through copy/paste using [Shift][Copy] and [Shift][Paste], was mishandled by mat2list(). Of course, that doesn't mean that the issue cannot happen.

If you can reproduce it again, I'd be very interested because a use mat2list() often.

As for working between hardware and emulator/conn. kit, I've been doing pretty much the same as you and am equally interested in learning about better ways.

As for sending stuff to the hardware, I often bypass the conn. kit and just send from emulator to the calculator. This doesn't always work (e.g. you can't do this with stuff from the history).
Reference URL's