HP Forums

Full Version: arg() crashes the emulator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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 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 Smile
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 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...
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: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... Smile

Surely Han could help us...
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
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...)
(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.
Yes, lname() sounds very promising. Thank you very much. I will play with that during weekend.
(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 ]
Is there any way to get information on what I call parameters list? in your example (fx x y)?
(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.)
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 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
(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).
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 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)
Cas settings was what I first checked. I have integers set to exact.
Just to be sure, this is the checkbox next to the "Integer" type field and not the checkbox next to the word "Exact", yes?

[attachment=1663]

I am unable to produce 3. except for when I uncheck that box (in the screenshot).
Pages: 1 2
Reference URL's