HP Prime CAS programming

02192015, 08:21 PM
(This post was last modified: 03022015 04:27 PM by Han.)
Post: #1




HP Prime CAS programming
There are several methods to make use of CAS commands in a program on the HP Prime.
1. CAS("command(arg1,arg2,...,argn)") 2. CAS.command("arg1","arg2",...,"argn") or CAS.command("arg1,arg2,...,argn") 3. Create an actual CAS function via #cas and #end Recall that an HP Prime Programming Language (HPPPL) program is essentially a collection of commands that are executed in the Home view. And in the Home view, most CAS commands must be treated with care. Techniques 1 and 2 behave mostly the same, and are generally used within HPPPL programs to gain access to CAS commands. Technique 3 is equivalent to creating a function in the CAS view, but the function remains resident in storage even after the use of the 'restart' command. This needs further clarification: the program's source remains in storage, however the command must be "recompiled" in order to reuse it. Thus, technique 3 is more akin to creating a macro file that needs to be run (i.e. "compiled") after each instance of the 'restart' command. This is the advantage of creating an HPPPL program that uses techniques 1 and 2 since HPPPL programs are always resident in memory after compilation (there is no 'restart' equivalent for Home view). Example of Technique 1 Suppose we wish to create a program that takes a formula for the function \( f(x) \) and determine the values of \( x \) such that \( f'(x) = 0 \). For our example, let \( f(x) = x^33x \). If we do this by hand in the CAS view, we would likely type in: Code: f:=x^33*x; and obtain the list: { 1, 1 }. We could simplify these steps into: solve(diff(f,x),x); This simplification allows us to create the following program: Code: EXPORT CASCMD1(f) To actually use the program, we must type: CASCMD1("x^33*x"). Notice that the argument f is actually a string representing the expression \( x^33x \). Some important points:
Example of Technique 2 Again, under the same premise as in the previous example, we can create the following code: Code: EXPORT CASCMD2(f) We can use 'result' directly with the solve() command. If, however, we had wanted to solve the equation \( f'(x) = 3 \), or equivalently \( f'(x) + 3 = 0 \) then it is NOT simply a matter of changing the last line of code to: result:=CAS.solve(result+3,"x"); because the expression result+3 is computed as if we were in the Home view. And in the Home view, the content of 'result'  which is the expression '3*x^23'  must be resolvable. If the variable 'x' does not exist, then an error occurs. So the following modification is required: Code: // modification so that we solve f'(x)=3 Example of Technique 3 The simplest (in terms of coding and legibility of code) is by creating a CAS function. The easiest way to create a CAS program is to simply "solve" our problem in the CAS view. Then, open the program editor and create a new CAS program. Finally, simply copy and paste (from the CAS view) the relevant commands used in the previous session. Code: #cas Some important notes:
The Hybrid Approach It is also possible to create a hybrid project. That is, a single source file could potentially have both nonCAS programs _and_ CAS programs. For example: Code: EXPORT NONCAS(f) CASPROG("x^33*x") from the Home view will return { 1, 1 }. Pros and Cons There are benefits as well as drawbacks to using each technique. As of firmware 6975:
Graph 3D  QPI  SolveSys 

02192015, 08:41 PM
(This post was last modified: 02202015 10:40 AM by salvomic.)
Post: #2




RE: HP Prime CAS programming
Thank you, Han,
I'm bookmarking this guide, very interesting for me. Please, explain also something about how pass arguments, i.e how pass an optional argument... Have a nice day! *** EDIT: please, Han, advise, if possible, how to make a Help for a customized program. I would like to help about the list of parameters when the program start (or pressing Help key, but I don't know if it's possible)... ∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C  DM42, DM41X  WP34s Prime Soft. Lib 

02192015, 10:35 PM
(This post was last modified: 02192015 10:44 PM by dg1969.)
Post: #3




RE: HP Prime CAS programming
Thank you very much Han, for theses explanations. I am very interested in this Chapter. I will follow assiduously this post.
In my mind local variables were prohibited in the CAS side... And for me instructions inside #CAS #end (in a non cas program) were carried out only once during compilation (a kind of script)... Can we place several blocks #cas #end in a source file ? 

02202015, 01:22 PM
Post: #4




RE: HP Prime CAS programming
(02192015 08:21 PM)Han Wrote: There are several methods to make use of CAS commands in a program on the HP Prime... This is excellent Han, even after all this time, your article has cleared up many details and annoyances with CAS, at least for me. Thanks very much! Suggestion: Move this post to the "Articles" Forum, intended for reference works like this. Moderator suggestion: Possible addition of a new "Prime Articles" forum. The Prime is so vast and new, and complex to learn, that a dedicated place for articles such as this would be tremendously helpful to folks new to the Prime and/or the forum, where they can pick from a small, focused set of instructional articles. While there is a wealth of info stuffed into the current Prime forum now, a reader must wade through hundreds of threads with thousands of messages to find the dozen or so seminal articles that explain and illustrate with examples many of the most confusing and/or complex issues learning Prime. Bob Prosperi 

02212015, 03:21 AM
(This post was last modified: 02212015 03:21 AM by Han.)
Post: #5




RE: HP Prime CAS programming
(02202015 01:22 PM)rprosperi Wrote: Suggestion: Move this post to the "Articles" Forum, intended for reference works like this. Eventually, it will be moved. I find it more helpful to write about topics that are more important to users and prefer to this forum for the simple fact that it allows replies and keeps the discussion to a single thread. So for now (since it's nowhere finished), I thought I'd post here so that people can post replies/suggestions/criticisms/etc since that sort of discussion is not allowed in the articles forum. Graph 3D  QPI  SolveSys 

02212015, 06:21 AM
Post: #6




RE: HP Prime CAS programming
(02212015 03:21 AM)Han Wrote: So for now (since it's nowhere finished), I thought I'd post here so that people can post replies/suggestions/criticisms/etc since that sort of discussion is not allowed in the articles forum. I agree! TU ∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C  DM42, DM41X  WP34s Prime Soft. Lib 

02212015, 01:54 PM
Post: #7




RE: HP Prime CAS programming
(02212015 03:21 AM)Han Wrote:(02202015 01:22 PM)rprosperi Wrote: Suggestion: Move this post to the "Articles" Forum, intended for reference works like this. I agree completely, sorry if not clear; it's much more useful for future readers once the "discussion" has been captured here. My suggesition is just that; compose and discuss here, intereacting with folks to capture the most common questions and answers on this topic, and once "complete", move it to a (general or Primededicated) Articles forum so new users seeking instruction can find it easily. And thanks for conducting class here, it's very helpful. Bob Prosperi 

02232015, 05:21 PM
Post: #8




RE: HP Prime CAS programming
(02192015 08:41 PM)salvomic Wrote: Thank you, Han, One way is to mimic commands from *nix environments. Create a CAS program that acts as a wrapper for the program. The CAS program should handle dynamic input so that if no input is given, it spits out the help. Otherwise, the wrapper sends it to the actual program. Code:
Graph 3D  QPI  SolveSys 

03302015, 04:29 PM
Post: #9




RE: HP Prime CAS programming
Thanks Han!
This is one of the reasons the Prime is unusable as a reliable and time saving tool, extremely inconsistent and unintuitive handling and results. Have been trying to make something work that I have spent hours trying to solve before.. Would you please show me how to use "quorem" using arguments in a nonCAS program? Would be REALLY helpful! Like (this obviously don't work..): EXPORT TEST(x,y) BEGIN CAS.quorem(x,y); END; 

03302015, 05:46 PM
Post: #10




RE: HP Prime CAS programming
(03302015 04:29 PM)pr0 Wrote: Thanks Han! This works fine when I try TEST([1,2,3,4],[1,2]). However, it seems that quorem() is broken when using doublequoted arguments such as: quorem("x^3+2*x^2+3*x+4","x+2") My suspicion is that both vectors and strings are pointerbased, and the parser is probably misinterpreting the string as a vector (I get [undef] as the result). I think you may have found a bug either in the CAS parser or the quorem command itself. Graph 3D  QPI  SolveSys 

04162017, 08:46 AM
Post: #11




RE: HP Prime CAS programming
Una publicación en Español fuera del foro: Programación CAS
Viga C  TD  FB 

01222020, 12:21 PM
Post: #12




RE: HP Prime CAS programming
I am not sure if this is an obvious or rediculous question:
a CAS function is listed under "Mem">"CAS Vars". The system seems to be aware of the CAS function being a (Function). Is it possible to have these 'Cas Vars' listed like in the Toolbox? Is that even a problem for others? I feel like I forget what functions i have implemented AND it would be way faster to call a function by choosing it under 'Toolbox'/'CAS functions'/name than being forced to remember and type its name. 

01222020, 01:02 PM
Post: #13




RE: HP Prime CAS programming
(01222020 12:21 PM)leprechaun Wrote: I am not sure if this is an obvious or rediculous question: You don't need to type their names. They are available in the [Vars] (CAS) menus. <0ɸ0> Joe 

01222020, 01:15 PM
(This post was last modified: 01222020 01:26 PM by Tim Wessman.)
Post: #14




RE: HP Prime CAS programming
The problem is we cannot tell if it is variable or a function as the CAS doesn't really make that distinction in a way we have access to.
TW Although I work for the HP calculator group, the views and opinions I post here are my own. 

01222020, 03:09 PM
Post: #15




RE: HP Prime CAS programming
Thank you! That is what I was asking for. I did a good amount of trying out menus and keys and I could could swear there was nothing shown, but obviously it is there. No idea why I have not found it prior to posting....
thanks again 

01252020, 02:58 AM
(This post was last modified: 01252020 02:20 PM by compsystems.)
Post: #16




RE: HP Prime CAS programming
An alternative way to define functions is to use the following template
PHP Code: #cas and you can even delete the directive. #end PHP Code: #cas Example with the new template PHP Code: #cas standard notation for CAS programs PHP Code: #cas PHP Code: #cas PHP Code: #cas SBASIC > https://smallbasicpublicwebsite.azurewebsites.net/ 

01252020, 10:31 AM
Post: #17




RE: HP Prime CAS programming  
01252020, 02:40 PM
(This post was last modified: 01252020 02:46 PM by compsystems.)
Post: #18




RE: HP Prime CAS programming
(01222020 01:15 PM)Tim Wessman Wrote: The problem is we cannot tell if it is variable or a function as the CAS doesn't really make that distinction in a way we have access to. a function in CAS, internally contains the > symbol or the block > BEGIN END which will identify the type of data function of a symbolic variable 1: PHP Code: #cas is recoded as PHP Code: casprg := ( seq[ ] ) > RETURN( ABS( x + y*i ) ) while a variable does not contain the > symbol casvar1 := x+y*i 2: PHP Code: #cas is recoded as PHP Code: casprg1 := ( seq[ ] ) SBASIC > https://smallbasicpublicwebsite.azurewebsites.net/ 

01262020, 07:42 AM
Post: #19




RE: HP Prime CAS programming
CAS programs are expressions of root node 'program' (with a child of type DOM_LIST, a list of 3 arguments). If f is a variable containing a program (for example defined like this f(x):=sin(x)), type(f) will return DOM_FUNC and f[1] or f[0] (depends whether Python compatibility is enabled) is 'program'.
In contrast, if g:=sin(x), g is an expression of rootnode 'sin' (with x as child) and g[1] or g[0] is 'sin'. 

08282020, 06:21 PM
(This post was last modified: 08282020 10:39 PM by ramon_ea1gth.)
Post: #20




RE: HP Prime CAS programming
I have been very interested in understanding how to call CAS functions from HOME (thinking about including these calls in a HOME program or a custom app). So I found this post extremely interesting, although the original hints have some years. Thus, I have done some tests with firmware version 2.1.14433 (2020 01 21), using variables of string type and in the end the options that work for me are summarised in this small program:
Code: EXPORT TEST() Ramón Valladolid, Spain TI50, Casio fx180P, HP48GX, HP50g, HP Prime G2 

« Next Oldest  Next Newest »

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