Struggling with CAS programming
09-17-2018, 12:54 AM (This post was last modified: 09-17-2018 12:57 AM by rushfan.)
Post: #1
 rushfan Junior Member Posts: 22 Joined: May 2017
Struggling with CAS programming
I wrote a program to calculate the state space form a transfer function. It works a bit like the Matlab tf2ss function, but return state space in canonical phase form. It receives a vector num and a vector den, and returns 4 matrices A B C D. I managed to make it work line by line in the CAS mode, but when I run it as a CAS function, it does not work.
Code:
 #cas tf2ss(num,den):= BEGIN // declare vars and alocate matrices local ordem := size(den)-1; local a:= identity(ordem)*0; local b:= identity(ordem)*0; local c:= revlist(num); local d:=0; // fill A is in the form [0 1 0; 0 0 1; -p/m -o/m -n/m], where n m o p are from den a:= replace(a,{ordem,1},-revlist(den[2..(ordem+1)])/den(1)); FOR lpv FROM 1 TO ordem-1 DO a(lpv,lpv+1) = 1; end; // fill b  b:= delrows(b,2..ordem); b:= replace(b,{1,ordem},[1]); b:= tranpose(b); return a; //return b; //return c; //return d; END; #end

When I run the function with tf2ss([1 2 3 4], [1 2 3 4 5]) I receive "Error: Invalid Input". I haven't been able to figure out what is wrong with the code. Documentation is sparse and hard to find.
09-17-2018, 03:27 AM
Post: #2
 Didier Lachieze Senior Member Posts: 1,508 Joined: Dec 2013
RE: Struggling with CAS programming
Two things :
1. I've found you need to declare your local variables before assigning them
2. the line a(lpv,lpv+1) = 1; should be replaced by a[lpv,lpv+1]:=1;

Code:
#cas tf2ss(num,den):= BEGIN // declare vars and allocate matrices local ordem,a,b,c,d; ordem := size(den)-1; a:= identity(ordem)*0; b:= identity(ordem)*0; c:= revlist(num); d:=0; // fill A is in the form [0 1 0; 0 0 1; -p/m -o/m -n/m], where n m o p are from den a:= replace(a,{ordem,1},-revlist(den[2..(ordem+1)])/den(1)); FOR lpv FROM 1 TO ordem-1 DO a[lpv,lpv+1]:=1; end; // fill b  b:= delrows(b,2..ordem); b:= replace(b,{1,ordem},[1]); b:= tranpose(b); return a; //return b; //return c; //return d; END; #end
09-17-2018, 03:45 AM (This post was last modified: 09-17-2018 03:50 AM by rushfan.)
Post: #3
 rushfan Junior Member Posts: 22 Joined: May 2017
RE: Struggling with CAS programming
(09-17-2018 03:27 AM)Didier Lachieze Wrote:  Two things :
1. I've found you need to declare your local variables before assigning them
2. the line a(lpv,lpv+1) = 1; should be replaced by a[lpv,lpv+1]:=1;

Code:
#cas tf2ss(num,den):= BEGIN // declare vars and allocate matrices local ordem,a,b,c,d; ordem := size(den)-1; a:= identity(ordem)*0; b:= identity(ordem)*0; c:= revlist(num); d:=0; // fill A is in the form [0 1 0; 0 0 1; -p/m -o/m -n/m], where n m o p are from den a:= replace(a,{ordem,1},-revlist(den[2..(ordem+1)])/den(1)); FOR lpv FROM 1 TO ordem-1 DO a[lpv,lpv+1]:=1; end; // fill b  b:= delrows(b,2..ordem); b:= replace(b,{1,ordem},[1]); b:= tranpose(b); return a; return b; return c; return d; END; #end

The code still does not work, even with the changes proposed. It gives "Error: Unmatch control word"
09-17-2018, 04:22 AM
Post: #4
 Didier Lachieze Senior Member Posts: 1,508 Joined: Dec 2013
RE: Struggling with CAS programming
For me it works well on my Prime with the latest firmware (2.0.0.13865) :

You may try to restart the CAS by typing 'restart'.
09-17-2018, 01:44 PM
Post: #5
 Nigel (UK) Senior Member Posts: 448 Joined: Dec 2013
RE: Struggling with CAS programming
I notice that the code in the post above says tranpose instead of transpose. If it has been copied and pasted, could that be the source of the error?

Nigel (UK)
09-17-2018, 01:49 PM (This post was last modified: 09-17-2018 01:58 PM by rushfan.)
Post: #6
 rushfan Junior Member Posts: 22 Joined: May 2017
RE: Struggling with CAS programming
I managed to get it working, though there are still some things I have to polish.
If I type "restart" to clear variable definitions, the program disappears from the cas and I have to load it again.
The Prime seems to be a very powerful tool, but there is still a lot very frustrating and unnecessary friction working between the CAS and Home, and writing programs. I still don't see why the Home exists, when the CAS is perfectly capable of making numeric calculations, and the existence of the Home places a lot of friction and restrictions.

This code is working:

Code:
 #cas tf2ss(num,den):= BEGIN // declare vars and allocate matrices local ordem,a,b,c,d; ordem := size(den)-1; a:= identity(ordem)*0; // fill A is in the form [0 1 0; 0 0 1; -p/m -o/m -n/m], where n m o p are from den a:= replace(a,{ordem,1},-revlist(den[2..(ordem+1)])/den(1)); FOR lpv FROM 1 TO ordem-1 DO a[lpv,lpv+1]:=1; end; // fill b  b:= identity(ordem)*0; b:= delrows(b,2..ordem); b:= replace(b,{1,ordem},[1]); b:= transpose(b); c := revlist(num); d:=0; return {a,b,c,d}; END; #end
09-17-2018, 09:57 PM
Post: #7
 CyberAngel Member Posts: 296 Joined: Jul 2018
RE: Struggling with CAS programming
(09-17-2018 03:45 AM)rushfan Wrote:
(09-17-2018 03:27 AM)Didier Lachieze Wrote:  Two things :
1. I've found you need to declare your local variables before assigning them
2. the line a(lpv,lpv+1) = 1; should be replaced by a[lpv,lpv+1]:=1;

Code:
#cas tf2ss(num,den):= BEGIN // declare vars and allocate matrices local ordem,a,b,c,d; ordem := size(den)-1; a:= identity(ordem)*0; b:= identity(ordem)*0; c:= revlist(num); d:=0; // fill A is in the form [0 1 0; 0 0 1; -p/m -o/m -n/m], where n m o p are from den a:= replace(a,{ordem,1},-revlist(den[2..(ordem+1)])/den(1)); FOR lpv FROM 1 TO ordem-1 DO a[lpv,lpv+1]:=1; end; // fill b  b:= delrows(b,2..ordem); b:= replace(b,{1,ordem},[1]); b:= tranpose(b); return a; return b; return c; return d; END; #end

The code still does not work, even with the changes proposed. It gives "Error: Unmatch control word"

You seem to have noticed that a function returns only one value
because your last version combined the RETURNs into a list.

LOCAL d:=0; // is allowed since a simple value can be assigned in a variable declaration
but the code is more clear the way you do it.

Note that you didn't declare the FOR lpv
I remember once having code where I needed even FOR variable declaration.
VPN
09-18-2018, 12:05 AM
Post: #8
 toml_12953 Senior Member Posts: 1,917 Joined: Dec 2013
RE: Struggling with CAS programming
(09-17-2018 01:49 PM)rushfan Wrote:  I still don't see why the Home exists, when the CAS is perfectly capable of making numeric calculations, and the existence of the Home places a lot of friction and restrictions.

I'm the exact opposite. I never use CAS and use Home exclusively. CAS is good for students and people who study theoretical math but I need numeric answers and CAS gets in my way for that.
My main use of the Prime is HPPL programming.

Tom L
Cui bono?
09-18-2018, 01:12 AM
Post: #9
 rushfan Junior Member Posts: 22 Joined: May 2017
RE: Struggling with CAS programming
(09-18-2018 12:05 AM)toml_12953 Wrote:
(09-17-2018 01:49 PM)rushfan Wrote:  I still don't see why the Home exists, when the CAS is perfectly capable of making numeric calculations, and the existence of the Home places a lot of friction and restrictions.

I'm the exact opposite. I never use CAS and use Home exclusively. CAS is good for students and people who study theoretical math but I need numeric answers and CAS gets in my way for that.
My main use of the Prime is HPPL programming.

Wtf theoretical math??

If you need numeric answers, then use numbers instead of symbols. In the CAS.

The only thing I can see the Home doing better than the CAS is grouping digits. CAS results are often unreadable because there are no thousands separator.
09-18-2018, 01:14 AM
Post: #10
 rushfan Junior Member Posts: 22 Joined: May 2017
RE: Struggling with CAS programming
(09-17-2018 09:57 PM)CyberAngel Wrote:
(09-17-2018 03:45 AM)rushfan Wrote:  The code still does not work, even with the changes proposed. It gives "Error: Unmatch control word"

You seem to have noticed that a function returns only one value
because your last version combined the RETURNs into a list.

LOCAL d:=0; // is allowed since a simple value can be assigned in a variable declaration
but the code is more clear the way you do it.

Note that you didn't declare the FOR lpv
I remember once having code where I needed even FOR variable declaration.
VPN

Yep, I noticed that. Since lpv is not declared, it ends being used as global and so returns value when function returns.
09-18-2018, 01:48 AM
Post: #11
 Steve Simpkin Senior Member Posts: 836 Joined: Dec 2013
RE: Struggling with CAS programming
(09-17-2018 01:49 PM)rushfan Wrote:  The Prime seems to be a very powerful tool, but there is still a lot very frustrating and unnecessary friction working between the CAS and Home, and writing programs. I still don't see why the Home exists, when the CAS is perfectly capable of making numeric calculations, and the existence of the Home places a lot of friction and restrictions.

A little background on why there is a Home and CAS environment (mode) and when to use one or the other:

-----------------------------------------------------------------------------
Re: HP Prime: Proper Use of Home View and CAS View
Message #4 Posted by Joe Horn on 4 Dec 2013, 8:52 p.m.,
I'm not a design team member, or even an HP employee, but I think I understand the concept behind Home versus CAS.
HP Prime Home is like Approx mode on the HP 50g.
HP Prime CAS is like Exact mode on the HP 50g.

For those without 50g experience:
Home is primarily for approximate numeric calculations.
CAS is primarily for exact symbolic operations.

Simple example: A student is asked, "What is the square root of 12?" Should the student answer "Approximately 3.46410161514", or "Exactly 2 times the square root of 3"? Both answers are correct, and both are needed in different circumstances. That's why Prime offers both environments.

Another simple example: What is 2^1234? Home says it's approximately 2.958 times 10^371. That's plenty good enough for most real-world purposes. But if you ever need every digit of the exact answer (e.g. for Number Theory work), CAS returns all 372 digits of 2^1234. Whichever you need, Prime offers both.

I hope that this distinction clarifies why both modes exist, and why SIN(pi) (in radian mode) gets different results in Home and CAS.

Even 0.5-0.4-0.1 gets different results in Home and CAS. And it's not a bug. Nor is it a surprise to anyone who understands the difference in purpose and implementation of Home and CAS. It's only a surprise (and an annoyance) to those who expect Home and CAS to work the same. (If they worked the same, there would be no need for both to exist).
-Joe-

-----------------------------------------------------------------------------
Re: HP Prime: Proper Use of Home View and CAS View
Message #9 Posted by cyrille de Brébisson on 5 Dec 2013, 7:45 a.m.,

Hello,
Joe's answer is great for when to use CAS and when to use home. A short summary would be: - If you are expecting a NUMERICAL answer, go to home (1/3 -> 0.3333...) - If you are Using the VALUES of variables, go to home - If you are expecting a symbolic answer, go to CAS (sqrt(12) -> 2*sqrt(3) - If you are using variables as variables, go to CAS

as for RANDOM, as it turns out, the CAS syntax for RANDOM is weired, it's RANDOM or RANDOM value; (with no ()!) so, RANDOM (1,5); is RANDOM of a value. This value match the short syntax for complex numbers, hence the strange output!
Cyrille
09-18-2018, 02:14 AM
Post: #12
 Joe Horn Senior Member Posts: 1,873 Joined: Dec 2013
RE: Struggling with CAS programming
(09-17-2018 09:57 PM)CyberAngel Wrote:  LOCAL d:=0; // is allowed since a simple value can be assigned in a variable declaration...

IIRC, normal (Home) programs allow that, but CAS programs disallow it. Has that changed in a recent firmware update?

<0|ɸ|0>
-Joe-
09-18-2018, 02:40 AM
Post: #13
 rushfan Junior Member Posts: 22 Joined: May 2017
RE: Struggling with CAS programming
(09-18-2018 01:48 AM)Steve Simpkin Wrote:
(09-17-2018 01:49 PM)rushfan Wrote:  The Prime seems to be a very powerful tool, but there is still a lot very frustrating and unnecessary friction working between the CAS and Home, and writing programs. I still don't see why the Home exists, when the CAS is perfectly capable of making numeric calculations, and the existence of the Home places a lot of friction and restrictions.

A little background on why there is a Home and CAS environment (mode) and when to use one or the other:

-----------------------------------------------------------------------------
Re: HP Prime: Proper Use of Home View and CAS View
Message #4 Posted by Joe Horn on 4 Dec 2013, 8:52 p.m.,
I'm not a design team member, or even an HP employee, but I think I understand the concept behind Home versus CAS.
HP Prime Home is like Approx mode on the HP 50g.
HP Prime CAS is like Exact mode on the HP 50g.

For those without 50g experience:
Home is primarily for approximate numeric calculations.
CAS is primarily for exact symbolic operations.

Simple example: A student is asked, "What is the square root of 12?" Should the student answer "Approximately 3.46410161514", or "Exactly 2 times the square root of 3"? Both answers are correct, and both are needed in different circumstances. That's why Prime offers both environments.

Another simple example: What is 2^1234? Home says it's approximately 2.958 times 10^371. That's plenty good enough for most real-world purposes. But if you ever need every digit of the exact answer (e.g. for Number Theory work), CAS returns all 372 digits of 2^1234. Whichever you need, Prime offers both.

I hope that this distinction clarifies why both modes exist, and why SIN(pi) (in radian mode) gets different results in Home and CAS.

Even 0.5-0.4-0.1 gets different results in Home and CAS. And it's not a bug. Nor is it a surprise to anyone who understands the difference in purpose and implementation of Home and CAS. It's only a surprise (and an annoyance) to those who expect Home and CAS to work the same. (If they worked the same, there would be no need for both to exist).
-Joe-

-----------------------------------------------------------------------------
Re: HP Prime: Proper Use of Home View and CAS View
Message #9 Posted by cyrille de Brébisson on 5 Dec 2013, 7:45 a.m.,

Hello,
Joe's answer is great for when to use CAS and when to use home. A short summary would be: - If you are expecting a NUMERICAL answer, go to home (1/3 -> 0.3333...) - If you are Using the VALUES of variables, go to home - If you are expecting a symbolic answer, go to CAS (sqrt(12) -> 2*sqrt(3) - If you are using variables as variables, go to CAS

as for RANDOM, as it turns out, the CAS syntax for RANDOM is weired, it's RANDOM or RANDOM value; (with no ()!) so, RANDOM (1,5); is RANDOM of a value. This value match the short syntax for complex numbers, hence the strange output!
Cyrille

Most of this I think misses the point. It seems that what most people want is a CAS with pretty typing. That is what I want too and why frequently the HP Prime feels annoying to use. I don't want to see all the 372 digits of 2^1234. Is anyone doing number theory on a pocket calculator?

Is there any way to change the precision and the formatting of results in the CAS?
09-18-2018, 03:06 AM
Post: #14
 CyberAngel Member Posts: 296 Joined: Jul 2018
RE: Struggling with CAS programming
(09-18-2018 02:14 AM)Joe Horn Wrote:
(09-17-2018 09:57 PM)CyberAngel Wrote:  LOCAL d:=0; // is allowed since a simple value can be assigned in a variable declaration...

IIRC, normal (Home) programs allow that, but CAS programs disallow it. Has that changed in a recent firmware update?

Works fine in the latest Beta (today: see this post's date).
I actually used in a #cas function a
LOCAL f:=1;
I dare you to check it with all of your HP Primes
which hopefully have different firmwares.
I only have the original HP Prime Model A
I wish I were an official Private Beta Tester like you.
I use to beta test a part of the vger project & do other work for HP
Those were the days
VPN
PS: I just bought another newer laptop for testing the software.
Went from 2- to 4-core, RAM: 8GB->32GB, Intel graphics to NVidia Mobile 4GB.
Tomorrow I start unpacking and installing stuff.
I wish I had HP Prime G2 for testing - I don't actually need a CAS or graphing calculator.
09-18-2018, 03:24 AM
Post: #15
 Joe Horn Senior Member Posts: 1,873 Joined: Dec 2013
RE: Struggling with CAS programming
(09-18-2018 02:40 AM)rushfan Wrote:  Is anyone doing number theory on a pocket calculator?

Yes.

<0|ɸ|0>
-Joe-
09-18-2018, 03:28 AM
Post: #16
 Joe Horn Senior Member Posts: 1,873 Joined: Dec 2013
RE: Struggling with CAS programming
(09-18-2018 03:06 AM)CyberAngel Wrote:
(09-18-2018 02:14 AM)Joe Horn Wrote:  IIRC, normal (Home) programs allow that, but CAS programs disallow it. Has that changed in a recent firmware update?

Works fine in the latest Beta (today: see this post's date).

Cool!

<0|ɸ|0>
-Joe-
09-18-2018, 03:39 AM
Post: #17
 CyberAngel Member Posts: 296 Joined: Jul 2018
RE: Struggling with CAS programming
(09-18-2018 03:24 AM)Joe Horn Wrote:
(09-18-2018 02:40 AM)rushfan Wrote:  Is anyone doing number theory on a pocket calculator?

Yes.

A few years bragged about being the President of the Finnish PCC equivalent and offered to help an engineering student.
It turned out that I had to do all the CAS stuff on pencil and paper and It took the summer to teach just obe university course. I wish I could have used my HP 50g.
If I remember correctly
the course was mainly handling parametric functions:
f=f(x,y), where x=x(t) and y=y(t)
09-18-2018, 05:37 AM
Post: #18
 parisse Senior Member Posts: 1,236 Joined: Dec 2013
RE: Struggling with CAS programming
You can use local declaration with assign, but I don't recommend for 2 reasons:
1/ if you do that for several variables, parenthesis are required because of operator precedence: local (a:=1),(b:=2);
2/ local assign is eval-ed before function arguments are assigned
 « Next Oldest | Next Newest »

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