My 2nd and 3rd Prime Programs



#2

Perhaps pending more discussion in my "My First Prime Program" thread, it appears that for the time being, I will need two programs for Polar conversions:

EXPORT ANG(X,Y)
BEGIN
RETURN ATAN(X/Y);
END;

And:

EXPORT MAG(X,Y)
BEGIN
RETURN SQROOT(X2+Y2);
END;

The angle routine will require some work to get things in the right quadrant for all inputs, of course.

With the above, converting 3 + i4 (represented as 4 in level 2 and 3 in level 1 of the stack) to polar form requires the following:

4
ENTER
3
ENTER
ANG
ENTER
Ans
MAG
ENTER

Is there any way to assign the ANG and MAG programs to two of those tempting-looking blank soft keys at the bottom of the HOME screen?

I promise this won't be series where I publish every trivial thing I figure out how to do.


Edited: 30 Oct 2013, 4:15 p.m.


#3

As Tim W pointed out to me, there's really no need to write a polar-to-rectangular program, since the conversions can be done from the keyboard using complex numbers.

1) In home settings set complex number format to (a,b) and check the box for "Allow complex output from real input".

2) Enter the rectangular coordinates x,y as (x,y) into the stack/history.

3) Key in Shift x (angle symbol) to transform the coordinate to polar form (magnitude,angle).

4) Repeat Shift x to return to rectangular form.

For example (3,4) ---> (5<angle>53.13) assuming angle mode is degrees.


#4

Also, FWIW, I wrote a program to do this before Tim W chimed in as follows:

EXPORT PR_Convert()
BEGIN
LOCAL X1,X2;
CHOOSE(N,"PR_Convert","Polar","Rectangular");
CASE
IF N==1 THEN
INPUT({X1,X2},"To Polar",{"X =","Y ="},{"Enter X","Enter Y"},{0,0});
PRINT("R = " + sqrt(X1²+X2²) );
PRINT("angle = " + ATAN(X2/X1) );
END;
IF N==2 THEN
INPUT({X1,X2},"To Rectangular",{"R =","angle ="},{"Enter R","Enter angle"},{0,0});
PRINT("X = " + X1*COS(X2) );
PRINT("Y = " + X1*SIN(X2) );
END;
DEFAULT
END;
END;
#5

Hi Michael,

I was aware of that functionality. In fact, the Prime's input, display, and general handling of complex numbers is pretty darn close to my ideal. You can enter either form directly from the keyboard, they stay in whatever form you enter them unless you convert as you described, and functions operating on two values in different forms produce consistent results (always in the form of the value in stack-2). Perhaps wanting to treat two reals in stack-1 and stack-2 as a complex number is anachronistic, but so am I. The 35s was raked over the coals for not having polar and rectangular conversion functions, despite having the ability to simply enter and work with complex values in a single register. In the end, I probably don't need the functions, but it would be nice if they could be programmed.

Regarding your PR_convert program, rest assured that I tried it. It works great, but as I indicated in my first post, I'd like to be able to take the values from the stack and return the results to the stack. As far as I can tell, there is no way for me to grab the results from your program for use in a subsequent calculation, is there?

edited for typos


Edited: 31 Oct 2013, 7:40 a.m. after one or more responses were posted


#6

Quote:
As far as I can tell, there is no way for me to grab the results from your program for use in a subsequent calculation, is there?

You can retrieve them by assigning them to the reserved system real variables X,Y,R,<theta>, i.e.

X:=X1*COS(X2)

Y:=X1*SIN(X2)

R:=sqrt(X1^2+X2^2)

<theta>:=ATAN(X2/X1)

Edited: 31 Oct 2013, 3:29 a.m.


#7

Thanks, I'll try that.

#8

hello,

how about

EXPORT ANG(X,Y)
BEGIN
RETURN arg((X,Y));
END;


EXPORT MAG(X,Y)
BEGIN
RETURN abs((X,Y));
END;


#9

Even better! Thanks!


Possibly Related Threads...
Thread Author Replies Views Last Post
  Does the HP Prime really compiles the user programs? CompSystems 3 695 12-13-2013, 01:55 PM
Last Post: Mike Morrow
  HP Prime: Lists in programs Alberto Candel 7 1,016 12-04-2013, 02:16 AM
Last Post: Alberto Candel
  HP Prime: FLOOR, iPart , and their use in programs Alberto Candel 6 811 12-01-2013, 10:17 PM
Last Post: Alberto Candel
  HP Prime: matrices in programs, in need of help Alberto Candel 9 893 11-26-2013, 01:33 AM
Last Post: cyrille de Brébisson
  HP Prime: password protection for programs Davi Ribeiro de Oliveira 2 459 11-22-2013, 12:45 PM
Last Post: Geoff Quickfall
  Prime: Scope of Variable and functions within programs Thomas Chrapkiewicz 9 912 11-18-2013, 03:39 PM
Last Post: Thomas Chrapkiewicz
  Question about transfering programs to the HP Prime Namir 10 1,011 11-17-2013, 04:01 PM
Last Post: Namir
  [Download] PrimeComm: Alternative small utility to upload/download programs to the HP Prime Erwin Ried 6 785 11-17-2013, 10:18 AM
Last Post: Erwin Ried
  HP Prime: do not send programs and shuts down by pressing Apps Davi Ribeiro de Oliveira 1 374 11-12-2013, 11:05 AM
Last Post: Joseph Ec
  Grouping programs on the HP Prime Michael de Estrada 11 1,134 11-04-2013, 01:38 PM
Last Post: Damien

Forum Jump: