HP Forums
atan2() Storing symbolic expression into numeric only vector object *Error* - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: atan2() Storing symbolic expression into numeric only vector object *Error* (/thread-11332.html)



atan2() Storing symbolic expression into numeric only vector object *Error* - Magnus512 - 09-03-2018 10:06 PM

Hi there, I'm making my own program to change between coordinate systems:
-------------
EXPORT SPHERE;
//Rectangular to Spherical
//(x,y,z)-->(ρ,θ,φ)
EXPORT RECT2SPHE(CORD)
BEGIN
//Separacion de coordenadas en x,y y z.
LOCAL x:=CORD(1),y:=CORD(2),z:=CORD(3);
//Variables para guardar resultado temporal
LOCAL ρ:=0,θ:=0,φ:=0;
//Operaciones para convertir los valores
ρ:=SQRT((x)^2+(y)^2+(z)^2);
θ:=atan(y/x);
φ:=ACOS(z/ρ);
//Guardar el resultado en la variable global SPHERE
SPHERE:=[ρ,θ,φ];
RETURN("ρ="+ρ+" θ="+θ+" φ="+φ);
END;
---------------------------
This is a part of the code, but I have some torubles here, when I store the results into SPHERE variable using atan to get the angle (θ:=atan(y/x)) it works just perfect, but if I try to use atan2(y,x) I get this message
[attachment=6291]
The error comes out when I use radian mode, but it works in degrees.


RE: atan2() Storing value into a vector *Error* - ijabbott - 09-03-2018 11:46 PM

Is atan2(x,y) implemented by HP Prime? I can't see it in the catalog.


RE: atan2() Storing value into a vector *Error* - Magnus512 - 09-04-2018 12:10 AM

(09-03-2018 11:46 PM)ijabbott Wrote:  Is atan2(x,y) implemented by HP Prime? I can't see it in the catalog.

Is not there, but it is implemented, try it


RE: atan2() Storing value into a vector *Error* - Tim Wessman - 09-04-2018 12:28 AM

Apparently it is.... looks like the CAS author added it in his parser at rev 13787.

Wouldn't you need to separate your argument into 2 inputs?

When I do that and run the debugger on your progam, I discover that you get back a symbolic object of 'ATAN(...)' which isn't a valid input into a vector. You need to evaluate that to a number.

Put EVAL( ) around your atan2 call.


RE: atan2() Storing value into a vector *Error* - Albert Chan - 09-04-2018 12:30 AM

(09-04-2018 12:10 AM)Magnus512 Wrote:  
(09-03-2018 11:46 PM)ijabbott Wrote:  Is atan2(x,y) implemented by HP Prime? I can't see it in the catalog.

Is not there, but it is implemented, try it

Should it be atan2(y, x) ?

Quote:The error comes out when I use radian mode, but it works in degrees.

Does the undocumented atan2() work in radian mode ?


RE: atan2() Storing value into a vector *Error* - Magnus512 - 09-04-2018 12:33 AM

(09-04-2018 12:28 AM)Tim Wessman Wrote:  Apparently it is.... looks like the CAS author added it in his parser at rev 13787.

Wouldn't you need to separate your argument into 2 inputs?

I don't get it, you mean RECT2SPHE(A,B,C) by example instead of use a vector to entry the data ?


RE: atan2() Storing value into a vector *Error* - Magnus512 - 09-04-2018 12:35 AM

(09-04-2018 12:30 AM)Albert Chan Wrote:  
(09-04-2018 12:10 AM)Magnus512 Wrote:  Is not there, but it is implemented, try it

Should it be atan2(y, x) ?

Does the undocumented atan2() work in radian mode ?

When I use atan2(y,x)... in radian it works, but when I implement it in a function and try to "concatenate" in to a vector with the other two answers it doesn't work, again this is just in radian mode, no degree mode problem


RE: atan2() Storing value into a vector *Error* - Tim Wessman - 09-04-2018 12:39 AM

Like I mentioned, run the debugger and you'll see the problem on the first pass... Smile

A better title for this thread should actually be "atan2() Storing symbolic expression into numeric only vector object *Error*"


RE: atan2() Storing value into a vector *Error* - Magnus512 - 09-04-2018 12:48 AM

(09-04-2018 12:39 AM)Tim Wessman Wrote:  Like I mentioned, run the debugger and you'll see the problem on the first pass... Smile

A better title for this thread should actually be "atan2() Storing symbolic expression into numeric only vector object *Error*"

I ran the debugger but everything looks just fine until the storing point Tongue, and yes the first result of atan2() is amm "exact" I don't think symbolic... so maybe if a pass it to eval comand ... and I found this and seems to work http://www.hpmuseum.org/forum/archive/index.php?thread-3604.html


RE: atan2() Storing symbolic expression into numeric only vector object *Error* - Magnus512 - 09-04-2018 12:54 AM

Parsing atan2(y,x) into EVAL() did the trick. Big Grin. If I remeber well, this error is just with vectors a list works well


RE: atan2() Storing symbolic expression into numeric only vector object *Error* - Albert Chan - 09-04-2018 01:48 AM

(09-04-2018 12:54 AM)Magnus512 Wrote:  Parsing atan2(y,x) into EVAL() did the trick. Big Grin. If I remeber well, this error is just with vectors a list works well

I am not so sure. I think atan2(y,x) has an object reference count bug.
This is my guess ...

It fail inside RECT2SPHE (radian mode) because local variable is gone after leaving function.
(Object reference count drop to 0, object thus becomes "garbage")

It work with degrees because atan2 value multiply by factor, creating a new object.
EVAL work for the same reason (new object).

If true, it will fail with list too. Try it without EVAL, and RECT2SPHE return list instead.


RE: atan2() Storing symbolic expression into numeric only vector object *Error* - Magnus512 - 09-04-2018 02:43 AM

(09-04-2018 01:48 AM)Albert Chan Wrote:  
(09-04-2018 12:54 AM)Magnus512 Wrote:  Parsing atan2(y,x) into EVAL() did the trick. Big Grin. If I remeber well, this error is just with vectors a list works well

I am not so sure. I think atan2(y,x) has an object reference count bug.
This is my guess ...

It fail inside RECT2SPHE (radian mode) because local variable is gone after leaving function.
(Object reference count drop to 0, object thus becomes "garbage")

It work with degrees because atan2 value multiply by factor, creating a new object.
EVAL work for the same reason (new object).

If true, it will fail with list too. Try it without EVAL, and RECT2SPHE return list instead.
Here it is, without EVAL(), usign atan(y,x), and returning a list
[attachment=6294]


RE: atan2() Storing symbolic expression into numeric only vector object *Error* - parisse - 09-04-2018 05:56 AM

atan2 was added for Python compatibility, it uses the same convention as Python or the standard libc for the arguments.


RE: atan2() Storing symbolic expression into numeric only vector object *Error* - Albert Chan - 09-04-2018 10:54 AM

(09-04-2018 02:43 AM)Magnus512 Wrote:  Here it is, without EVAL(), usign atan(y,x), and returning a list

My guess was wrong, but the test did reveal the bug.

For first quadrant, atan2(y,x) and atan(y/x) should return the same result.
You should not need an EVAL to do it.


RE: atan2() Storing symbolic expression into numeric only vector object *Error* - Tim Wessman - 09-04-2018 11:40 AM

Yes, it does. The reason why is that exact values, or fractions will result in exact symbolic results being returned from the CAS command atan2 Then when that local varaible is used in the vector, it errors.

Approxate decimals will work for non integer, but the only way to be sure is to eval or approximate that value.


RE: atan2() Storing symbolic expression into numeric only vector object *Error* - Magnus512 - 09-04-2018 12:32 PM

(09-04-2018 11:40 AM)Tim Wessman Wrote:  Yes, it does. The reason why is that exact values, or fractions will result in exact symbolic results being returned from the CAS command atan2 Then when that local varaible is used in the vector, it errors.

Approxate decimals will work for non integer, but the only way to be sure is to eval or approximate that value.

So any chance to get this working, without the EVAL() command?... Is this a bug, or it's the normal behavior of system working with vectors?


RE: atan2() Storing symbolic expression into numeric only vector object *Error* - Albert Chan - 09-04-2018 12:52 PM

Hi, Magnus512

I still think atan2(3, 2) not honors approx. mode is a bug.
It should be able to substitute directly for atan(3/2), but better (correct angle for others quadrants)

Can you try remove EVAL, but set θ:=atan2(y, x) + 0 ?
Unless Prime can optimize away + 0, it should force atan2 to honor exact/approx mode.


RE: atan2() Storing symbolic expression into numeric only vector object *Error* - JMB - 09-04-2018 03:17 PM

The documented function ARG(x+y*i), returns the angle in the correct quadrant.

ARG((-2,-1)) -> -2.67... (radiant mode)

ARG((-2,-1)) -> -153.43... (degree mode)


RE: atan2() Storing symbolic expression into numeric only vector object *Error* - Magnus512 - 09-04-2018 04:20 PM

(09-04-2018 03:17 PM)JMB Wrote:  The documented function ARG(x+y*i), returns the angle in the correct quadrant.

ARG((-2,-1)) -> -2.67... (radiant mode)

ARG((-2,-1)) -> -153.43... (degree mode)

This is good, and I have notice this yet, but the focus point is the behavior of the vector, why we can't store and aprox. result there.