arg() crashes the emulator
02-20-2015, 10:56 AM (This post was last modified: 02-20-2015 11:01 AM by Angus.)
Post: #1
 Angus Member Posts: 212 Joined: Feb 2014
arg() crashes the emulator
fun(a):=a^2
arg(fun)

-> crashes the emulator

fun:=a^2
arg(fun)

-> no crash but trapped in a looping error message. Invalid series expansion.

I would expect an error message like argument is nonsense or something instead of a crash or infinit loop.

To avoid an other thread. Given fun(a):=a^2. I have questions.
1) Is it possibly to get the function's argument as I (accidently) tried above: GetArgs(function)
2) defining fun:=(a)->a^2 using the arrow directly gives an error window. Using minus and greater works, but converts both chars to the built in arrow.... strange
02-20-2015, 11:28 AM
Post: #2
 salvomic Senior Member Posts: 1,366 Joined: Jan 2015
RE: arg() crashes the emulator
(02-20-2015 10:56 AM)Angus Wrote:  1) Is it possibly to get the function's argument as I (accidently) tried above: GetArgs(function)
2) defining fun:=(a)->a^2 using the arrow directly gives an error window. Using minus and greater works, but converts both chars to the built in arrow.... strange

1. try something like (I used this method in some programs:
Code:
 #cas yourfunction(args):= BEGIN local argv, argc, ...; argv:=[args]; argc:= size(argv); first:= args(1); second:= args(2); ... END; #end
It should works for you...
Let me know.

2. yes, me too: I wonder how to get in Windows the builtin arrow (and also other symbols).
With Connectivity Kit there's no luck, we need a real IDE

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
02-20-2015, 11:49 AM (This post was last modified: 02-20-2015 12:16 PM by Angus.)
Post: #3
 Angus Member Posts: 212 Joined: Feb 2014
RE: arg() crashes the emulator
What I tried to do is find out what are the variables of a cas function.
Say fun(a,b):=a^2/b;

Arguments(fun) should give {a,b}. Initially the idea was to be able to subst() a varible to 'X' for compatibility with built in apps. Now I am just wondering if it is possible.
Final goal is to get a plot() function that can be called as my_plot(fun, a) which then plots a fun(X) from the Function app. No special requirements for fun. fun may be a function of some variable or an expression.
As far as I understand I would need the independant var as X because that kind of plotting is not intended. (Maybe the geometry app suits better what made me open the other thread).

Edit: The need to distinguish between calling a function for an expression foo(expr) or a function foo(expr(indep)) should dimish. A program is able to find out if an argument is a function (DOM_FUNC) or an expression (DOM_SYMBOLIC) so it would be nice if it knew what variables are used by the function by itself.

P.S. btw is there a reason for glueing the app (like function app) to the build in fixed variables F1..9 with indepandant X? I'm under the impression that introducing the fixed type-bound variables was a bad idea. Cutting down flexibility and causing many of the known issues and questions that arise when home/cas interact. I see that that approach was economically seductive (code-reuse) and leads to an easy to introduce calculator for the first few hours. But I consider the scenario when you have defined or calculated a function in cas and then want to use exact that thing inside am app like Function the most natural approach. Straightforward and no workarounds needed.
If there are major benefits of the design the prime has I would be happy to learn. Many times I ask myself 'why did they do it this way'? Other than needing a quick solution.
02-20-2015, 12:10 PM
Post: #4
 salvomic Senior Member Posts: 1,366 Joined: Jan 2015
RE: arg() crashes the emulator
(02-20-2015 11:49 AM)Angus Wrote:  A program is able to find out if an argument is a function (DOM_FUNC) or an expression (DOM_SYMBOLIC) so it would be nice if it knew what variables are used by the function by itself.

in this case I try something like
Code:
 ... k:=argv(1); IF (type(k) != DOM_FUNC THEN ... ELSE ...

but surely you would have already tried that...

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
02-20-2015, 12:14 PM (This post was last modified: 02-20-2015 12:15 PM by Angus.)
Post: #5
 Angus Member Posts: 212 Joined: Feb 2014
RE: arg() crashes the emulator
OK, say DOM_FUNC is found. Can I get information on how many arguments the function expects and what names they have? I try to call with fun instead of fun(a,b,c).
02-20-2015, 12:25 PM
Post: #6
 salvomic Senior Member Posts: 1,366 Joined: Jan 2015
RE: arg() crashes the emulator
(02-20-2015 12:14 PM)Angus Wrote:  OK, say DOM_FUNC is found. Can I get information on how many arguments the function expects and what names they have? I try to call with fun instead of fun(a,b,c).

how many he's (see my post above), in particular
argc:= size(argv);

what name is a bit tricky for me...

For example I would like to show the name parameters to the user first of the program begins to remember their use...

Surely Han could help us...

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
02-20-2015, 12:31 PM (This post was last modified: 02-20-2015 12:34 PM by Angus.)
Post: #7
 Angus Member Posts: 212 Joined: Feb 2014
RE: arg() crashes the emulator
Hm I think I don't understand your code, then.

I understand as followed: you define a function foo() which has two arguments. A cas function and a number.
Code:
 #cas foo(args):= BEGIN local argv, argc, ...; argv:=[args]; argc:= size(argv); casfunc:= argv(1); number:= argv(2); ... END; #end

casfunc however could have been defined as casfunc(a):=a^2 or as casfunc(a,b):=a*b.
From inside foo is it possible to get information about casfunc's parameter list.

edit. imagine foo being called as foo(casfunc)

Thank you for the discussion
02-20-2015, 01:43 PM (This post was last modified: 02-20-2015 01:47 PM by salvomic.)
Post: #8
 salvomic Senior Member Posts: 1,366 Joined: Jan 2015
RE: arg() crashes the emulator
I use this code mostly for distinguish between reals, vectors and lists and it works.
It should works also with functions and expressions.
In my program for Fourier Coefficient I treat 2 arguments and a third, optional.
I was advised to use it so:
IF argc=3 THEN f:= zip('unapply', f, argv(2));
program accepts both fourcoeff(fun, k) or fourcoeff(expr, var, k)
So user can input a function or an expression and a parameter for the variable and in both case program get the correct input...

I hope this could be helpful for you.

***
see the thread here in the Forum (by me, Han, Tim, Snorre...)

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
02-23-2015, 07:33 PM (This post was last modified: 02-23-2015 07:55 PM by dg1969.)
Post: #9
 dg1969 Member Posts: 95 Joined: Apr 2014
RE: arg() crashes the emulator
(02-20-2015 11:49 AM)Angus Wrote:  What I tried to do is find out what are the variables of a cas function.
Say fun(a,b):=a^2/b;

Arguments(fun) should give {a,b}. ...

perhaps did I misunderstood your request but I think the " lname()" command included in the hp-prime do exactly that.
02-24-2015, 06:19 AM
Post: #10
 Angus Member Posts: 212 Joined: Feb 2014
RE: arg() crashes the emulator
Yes, lname() sounds very promising. Thank you very much. I will play with that during weekend.
02-24-2015, 12:21 PM
Post: #11
 Han Senior Member Posts: 1,810 Joined: Dec 2013
RE: arg() crashes the emulator
(02-24-2015 06:19 AM)Angus Wrote:  Yes, lname() sounds very promising. Thank you very much. I will play with that during weekend.

Do note that it only returns the names of variables that do not have any value stored in them. So for example, if you had previously created a program that exported the variable 'fx' (just as an example) and then you created an expression such as:

g:=2*fx+4*y-3*x (here, y and x are variables that have nothing stored in them)

then lname(g) would return [ x y ] and not [ fx x y ]

Graph 3D | QPI | SolveSys
02-24-2015, 01:15 PM
Post: #12
 Angus Member Posts: 212 Joined: Feb 2014
RE: arg() crashes the emulator
Is there any way to get information on what I call parameters list? in your example (fx x y)?
02-24-2015, 03:24 PM (This post was last modified: 02-24-2015 03:30 PM by Han.)
Post: #13
 Han Senior Member Posts: 1,810 Joined: Dec 2013
RE: arg() crashes the emulator
(Assuming CAS view)

f(x):=x^2;
f; // returns (x)->(x^2)

lname(f); // returns [ x ]

If you want to make your own program to do what you want to do, you can use the string() function which converts objects into character strings.

fs:=string(f); // convert f into a string
rp:=instring(fs,")"); // find the first ) char
fs:=mid(fs,2,rp-2); // get (rp-2) characters starting at position 2
fpar:=CAS(fs); // convert var list into { var1, var2, ..., varn }

You can use list2mat() if you want fpar to be enclosed by []'s.

This is essentially the same as lname(). (Note lname() is limited to user CAS functions.)

Graph 3D | QPI | SolveSys
02-25-2015, 06:00 AM (This post was last modified: 02-25-2015 06:36 AM by Angus.)
Post: #14
 Angus Member Posts: 212 Joined: Feb 2014
RE: arg() crashes the emulator
Thanks a lot!
Many functions are unknown, hidden or known but just overlooked. All have in common that they come to life by examples. If only I would not suffer from cas<->home paranoia... There is a good chance that I will come up with related problems and questions indicating that I did not fully get all points. :-(
I did not understand why you proposed your code at a first glance. After trying string() in cas mode I noticed that the brackets around (x^2) vanish...so everything makes sense. But not being intuitive I think. That prime is hard to assess....

edit:
...except that string() leads to STRING().
One more question. I've just looked at the help system about mid() command. There it is capitalized.... Is that of importance? You mentioned in an other thread that several commands are essentially the same but behave differently if there is a home<->cas issue with them and if they are written in capital letters or not. I think you mentioned case and return aswell. These are 3 commands with obviously slighty different behaviour. I did not find case and return in the help (case leads to help CASE), so could you point out what differs with them?

case/CASE
return/RETURN
mid/MID
(..)

You said these nuances are important to learn. Thank you

edit2:

In cas the instring() command gives 3. which is a real I think? mid() does not like 3.-2 as parameter. I cannot figure out what causes that. Any
02-25-2015, 08:47 AM
Post: #15
 salvomic Senior Member Posts: 1,366 Joined: Jan 2015
RE: arg() crashes the emulator
(02-25-2015 06:00 AM)Angus Wrote:  ... I did not find case and return in the help (case leads to help CASE), so could you point out what differs with them?

case/CASE
return/RETURN
mid/MID
(..)

You said these nuances are important to learn. Thank you

yes, thank you, it's important also for me, and, please, explain what is the difference between
RETURN f; / return f;
and
RETURN(f);
with ()

Salvo

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
02-25-2015, 02:54 PM
Post: #16
 Han Senior Member Posts: 1,810 Joined: Dec 2013
RE: arg() crashes the emulator
(02-25-2015 08:47 AM)salvomic Wrote:
(02-25-2015 06:00 AM)Angus Wrote:  ... I did not find case and return in the help (case leads to help CASE), so could you point out what differs with them?

case/CASE
return/RETURN
mid/MID
(..)

You said these nuances are important to learn. Thank you

yes, thank you, it's important also for me, and, please, explain what is the difference between
RETURN f; / return f;
and
RETURN(f);
with ()

Salvo

Based on what I can observe, both return and RETURN are used to return a value; however, RETURN will also exit the current function/procedure whereas return does not. As for return 0 vs. return(0), I believe this is the same.

Code:
#cas RETEXAMPLE():= BEGIN   IF 1>0 THEN     return("Yes!");   END;   return("Reached end of program"); END; #end

Compare the results in this program to one in which all the return commands are replaced by RETURN.

As for typing in commands in lower-case and having it appear on the screen in upper-case ... this is just a display effect that probably carried over from Home view. Differences between mid and MID are non-existent if there is no corresponding CAS command named mid. So in the CAS (or Home) view, mid() and MID() are the same commands as they will both be parsed to MID (since there is no CAS mid() command).

Graph 3D | QPI | SolveSys
02-25-2015, 03:02 PM
Post: #17
 Angus Member Posts: 212 Joined: Feb 2014
RE: arg() crashes the emulator
Han, you play a LOT with your prime don't you?
Do you have an idea why instring() returns with 3. and not with 3 in your example?
02-25-2015, 04:02 PM
Post: #18
 Han Senior Member Posts: 1,810 Joined: Dec 2013
RE: arg() crashes the emulator
(02-25-2015 03:02 PM)Angus Wrote:  Han, you play a LOT with your prime don't you?

I did most of my tinkering during my summer/winter breaks. After a while, all these idiosyncrasies are a bit less of a surprise.

Quote:Do you have an idea why instring() returns with 3. and not with 3 in your example?

The issue you are having with instring() is that it's returning a "non-exact" value 3. as opposed to the "exact" value 3 (it's an issue of floating point vs integer). Anyway, mid() requires integer inputs, so you are getting erroneous output. To put it simply, using CAS commands in Home, and non-CAS commands in CAS will always require some care.

As for why you are getting 3. vs 3 it is likely due to your CAS settings (Change apparent integers to exact integers; next to the "Integer" type field)

Graph 3D | QPI | SolveSys
02-25-2015, 04:13 PM
Post: #19
 Angus Member Posts: 212 Joined: Feb 2014
RE: arg() crashes the emulator
Cas settings was what I first checked. I have integers set to exact.
02-25-2015, 04:18 PM (This post was last modified: 02-25-2015 04:20 PM by Han.)
Post: #20
 Han Senior Member Posts: 1,810 Joined: Dec 2013
RE: arg() crashes the emulator
Just to be sure, this is the checkbox next to the "Integer" type field and not the checkbox next to the word "Exact", yes?

I am unable to produce 3. except for when I uncheck that box (in the screenshot).

Graph 3D | QPI | SolveSys
 « Next Oldest | Next Newest »

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