HP Forums
(Feature Request) - Convert to polar (phasor) notation in CAS - 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: (Feature Request) - Convert to polar (phasor) notation in CAS (/thread-14737.html)



(Feature Request) - Convert to polar (phasor) notation in CAS - Dands - 03-29-2020 07:48 AM

Hi,

If you have a complex number in rectangular form in the home screen and want to convert it to phasor notation (r∠θ), you can simply press shift + "∠".

Why this doesn't work in CAS? Is there any way I can request this to HP?

-Edit-
See below an example of a matrix containing complex numbers in rectangular form and the result in phasor notation. This is easy to do in Home, but won't work in CAS.

[Image: P5-62-3.png]

Thanks


RE: Convert to polar (phasor) representation in CAS - Eddie W. Shore - 03-29-2020 03:12 PM

You can file a bug report, see the thread about the Beta thread.

In the mean time, there are two conversion CAS commands that work both in Home and CAS modes:

polar_coordinates( real + imag*i ) -> [ approximate radius, approximate angle ]

polar_coordinates( real, imag ) -> [ radius, angle ]
Approximate at Home, Exact at CAS

rectangular_coordinates( radius, angle ) -> [ real, imag ]
Approximate at Home, Exact at CAS

Both polar_coordinates and rectangular_coordiantes are found in the Catalog.

polar_coordinates is also found in [Toolbox], (App), Geometry, Cartesian, Polar Coordinates


RE: Convert to polar (phasor) representation in CAS - Dands - 03-30-2020 09:06 PM

(03-29-2020 03:12 PM)Eddie W. Shore Wrote:  You can file a bug report, see the thread about the Beta thread.

In the mean time, there are two conversion CAS commands that work both in Home and CAS modes:

polar_coordinates( real + imag*i ) -> [ approximate radius, approximate angle ]

polar_coordinates( real, imag ) -> [ radius, angle ]
Approximate at Home, Exact at CAS

rectangular_coordinates( radius, angle ) -> [ real, imag ]
Approximate at Home, Exact at CAS

Both polar_coordinates and rectangular_coordiantes are found in the Catalog.

polar_coordinates is also found in [Toolbox], (App), Geometry, Cartesian, Polar Coordinates

Thanks for your reply. Unfortunately those don't work well for me because I often pass matrices with multiple entries to be converted. I really wished the "home" functionality also worked in CAS because it's so easy to use. See below.

Also, where exactly should I request new features? How does posting them here gets sent to HP?

[Image: P5-62-3.png]

Thanks


RE: Convert to polar (phasor) notation in CAS - rprosperi - 03-30-2020 09:56 PM

(03-30-2020 09:06 PM)Dands Wrote:  Also, where exactly should I request new features? How does posting them here gets sent to HP?

Members of the Prime team are quite active MoHPC Forum members and they will see your posts here. That said, if you want to suggest new or modified features, it's best to make a new post with something like "Feature Request" in the title, rather than burying it on some other long or only somewhat related thread.

Also, history has shown that general enhancements that affect lots of users, with detailed explanations and examples of how the new (or modified) feature should (could) work receive the best response.

But also understand that deep inside the Prime, HOME and CAS modes are not tightly integrated, so quite often requests along the lines of "Please make CAS treatment of XYZ act like HOME mode" or vice-versa, can't practically be implemented. There are some areas where they obviously can behave similarly, but others where it either makes no sense, or isn't technically feasible.


RE: (Feature Request) - Convert to polar (phasor) notation in CAS - Dands - 03-30-2020 10:49 PM

(03-30-2020 09:56 PM)rprosperi Wrote:  
(03-30-2020 09:06 PM)Dands Wrote:  Also, where exactly should I request new features? How does posting them here gets sent to HP?

Members of the Prime team are quite active MoHPC Forum members and they will see your posts here. That said, if you want to suggest new or modified features, it's best to make a new post with something like "Feature Request" in the title, rather than burying it on some other long or only somewhat related thread.

Also, history has shown that general enhancements that affect lots of users, with detailed explanations and examples of how the new (or modified) feature should (could) work receive the best response.

But also understand that deep inside the Prime, HOME and CAS modes are not tightly integrated, so quite often requests along the lines of "Please make CAS treatment of XYZ act like HOME mode" or vice-versa, can't practically be implemented. There are some areas where they obviously can behave similarly, but others where it either makes no sense, or isn't technically feasible.

That's good to know. I will leave this post here as a request. Hopefully this functionality won't be too hard to implement.

Thanks


RE: (Feature Request) - Convert to polar (phasor) notation in CAS - Tim Wessman - 03-31-2020 05:54 AM

It actually is very hard to implement because there is no such thing as a "complex" object in the CAS. You have expressions that contain complex parts, but those can be in very varying forms and varieties. Whereas in home and numeric parts, the two values are intrinsically linked as a single object. That object has a flag that means "display in rectangular" or "display in polar". Hence nothing changes and it just toggles the bit on that individual object.

Bernard the CAS author could possibly extend the polar_coordinates/rectangular_coordinates to also work with vectors of vectors (there is no MATRIX object in CAS actually either, which is another major difference), but that will depend on him desiring to do so. Smile

It will not be able to use the same display routines and keep the pairs together because that type of object just does not exist in CAS land...


RE: (Feature Request) - Convert to polar (phasor) notation in CAS - Dands - 03-31-2020 06:24 AM

(03-31-2020 05:54 AM)Tim Wessman Wrote:  It actually is very hard to implement because there is no such thing as a "complex" object in the CAS. You have expressions that contain complex parts, but those can be in very varying forms and varieties. Whereas in home and numeric parts, the two values are intrinsically linked as a single object. That object has a flag that means "display in rectangular" or "display in polar". Hence nothing changes and it just toggles the bit on that individual object.

Bernard the CAS author could possibly extend the polar_coordinates/rectangular_coordinates to also work with vectors of vectors (there is no MATRIX object in CAS actually either, which is another major difference), but that will depend on him desiring to do so. Smile

It will not be able to use the same display routines and keep the pairs together because that type of object just does not exist in CAS land...

That is pretty interesting, kind of bad news...

I really wished CAS handled phasor notation better since it is so relevant in many electrical engineering applications.

Meanwhile, I just found a possible bug. In CAS, I can write 1∠45 and execute. The calculator will change the notation to ∠(1,45). But if you try yourself to use this notation, it gives you an error. See below. Might worth taking a look?

[Image: 1.png]
[Image: 2.png]
[Image: 3.png]


RE: (Feature Request) - Convert to polar (phasor) notation in CAS - parisse - 03-31-2020 11:25 AM

I have added some list processing to polar/rectangular_coordinates.
For the copy/paste problem with the angle symbol, it seems to happen only in textbook display mode, therefore it's probably not a CAS bug.


RE: (Feature Request) - Convert to polar (phasor) notation in CAS - victorvbc - 03-31-2020 12:31 PM

This is the code I made for displaying polar coordinates {magnitude,angle}, it works with matrices and lists.

I'm also quite new to the Prime, so I don't know if there's any better way to do it...

Code:

//This program takes an expression or matrix and turns it into polar coordinates
#cas
toPolar(z):= 
BEGIN
LOCAL sizes,j,k;

IF type(z)=DOM_LIST THEN //check if is list/matrix
  sizes:=SIZE(z); //get dimension of rows and columns
  IF size(sizes)=1 THEN //row vector
    FOR j FROM 1 to sizes DO
      z[j]:=convert_polar(z[j]); //convert each element to polar
    END;
  ELSE //column vector or matrix
    FOR j FROM 1 to sizes(1) DO
      FOR k FROM 1 to sizes(2) DO
        z[j,k]:=convert_polar(z[j,k]); //convert each element to polar
      END; 
    END;
  END;
ELSE
  z:=convert_polar(z); //single element
END;

RETURN z;
END;

convert_polar(x):=
BEGIN
  RETURN simplify({ABS(x),ARG(x)}); //conversion formula
END;

#end

Keep in mind that it's purely for display purposes, you can't make any operations with this format.

Also, if you want to convert any expressions that have real-valued variables, you should do something like assume(x,float) before doing the conversion. Otherwise you can have very complicated expressions.


RE: (Feature Request) - Convert to polar (phasor) notation in CAS - Dands - 03-31-2020 04:22 PM

(03-31-2020 12:31 PM)victorvbc Wrote:  This is the code I made for displaying polar coordinates {magnitude,angle}, it works with matrices and lists.

I'm also quite new to the Prime, so I don't know if there's any better way to do it...

Code:

//This program takes an expression or matrix and turns it into polar coordinates
#cas
toPolar(z):= 
BEGIN
LOCAL sizes,j,k;

IF type(z)=DOM_LIST THEN //check if is list/matrix
  sizes:=SIZE(z); //get dimension of rows and columns
  IF size(sizes)=1 THEN //row vector
    FOR j FROM 1 to sizes DO
      z[j]:=convert_polar(z[j]); //convert each element to polar
    END;
  ELSE //column vector or matrix
    FOR j FROM 1 to sizes(1) DO
      FOR k FROM 1 to sizes(2) DO
        z[j,k]:=convert_polar(z[j,k]); //convert each element to polar
      END; 
    END;
  END;
ELSE
  z:=convert_polar(z); //single element
END;

RETURN z;
END;

convert_polar(x):=
BEGIN
  RETURN simplify({ABS(x),ARG(x)}); //conversion formula
END;

#end

Keep in mind that it's purely for display purposes, you can't make any operations with this format.

Also, if you want to convert any expressions that have real-valued variables, you should do something like assume(x,float) before doing the conversion. Otherwise you can have very complicated expressions.

This program works great, however, it's showing angles in radians. It might be because you're using the convert_polar function which is based in rad. Is there a way to modify it so that it uses the system's preferred angle output? I mostly need my angles in degrees.

Thank you!


RE: (Feature Request) - Convert to polar (phasor) notation in CAS - victorvbc - 03-31-2020 04:38 PM

(03-31-2020 04:22 PM)Dands Wrote:  This program works great, however, it's showing angles in radians. It might be because you're using the convert_polar function which is based in rad. Is there a way to modify it so that it uses the system's preferred angle output? I mostly need my angles in degrees.

Thank you!

The function is defined right under the main program. It uses the modulus and argument commands, which should function according to the system angle setting.

Are you sure you switched it right? The calc. should have a <° symbol on the top right corner. On mine it works as expected, outputs a float in degrees.

If you check the variable 'HAngle', it should return 1.


RE: (Feature Request) - Convert to polar (phasor) notation in CAS - Dands - 03-31-2020 08:14 PM

(03-31-2020 04:38 PM)victorvbc Wrote:  
(03-31-2020 04:22 PM)Dands Wrote:  This program works great, however, it's showing angles in radians. It might be because you're using the convert_polar function which is based in rad. Is there a way to modify it so that it uses the system's preferred angle output? I mostly need my angles in degrees.

Thank you!

The function is defined right under the main program. It uses the modulus and argument commands, which should function according to the system angle setting.

Are you sure you switched it right? The calc. should have a <° symbol on the top right corner. On mine it works as expected, outputs a float in degrees.

If you check the variable 'HAngle', it should return 1.

I think something weird is happening, probably because you used the simplify command in the output. I removed it and it persisted, so I'm clueless. Both of these should provide an angle of 45deg. I think it is not handling it properly when coefficients a and b are not integers.

Also, is it normal to see the terminal showing so many alerts?

[Image: 5.png]
[Image: 6.png]


RE: (Feature Request) - Convert to polar (phasor) notation in CAS - victorvbc - 03-31-2020 09:16 PM

(03-31-2020 08:14 PM)Dands Wrote:  I think something weird is happening, probably because you used the simplify command in the output. I removed it and it persisted, so I'm clueless. Both of these should provide an angle of 45deg. I think it is not handling it properly when coefficients a and b are not integers.

Also, is it normal to see the terminal showing so many alerts?


That is quite weird... I guess the CAS doesn't like converting angles from floating point complex numbers to degrees. Anyway, here's a quick & dirty fix:

Code:

//This program takes an expression or matrix and turns it into polar coordinates
#cas
toPolar(z):= 
BEGIN
LOCAL sizes,j,k;

IF type(z)=DOM_LIST THEN //check if is list/matrix
  sizes:=SIZE(z); //get dimension of rows and columns
  IF size(sizes)=1 THEN //row vector
    FOR j FROM 1 to sizes DO
      z[j]:=convert_polar(z[j]); //convert each element to polar
    END;
  ELSE //column vector or matrix
    FOR j FROM 1 to sizes(1) DO
      FOR k FROM 1 to sizes(2) DO
        z[j,k]:=convert_polar(z[j,k]); //convert each element to polar
      END; 
    END;
  END;
ELSE
  z:=convert_polar(z); //single element
END;

RETURN z;
END;

convert_polar(x):=
BEGIN
  IF type(IM(x))=DOM_FLOAT OR type(RE(x))=DOM_FLOAT THEN
    x:=exact(x);
      RETURN approx({ABS(x),ARG(x)});
  ELSE
    RETURN {ABS(x),ARG(x)};
  END;
END;
#end

It works for that case now. I don't know about the errors, I didn't get them.

[attachment=8274]
[attachment=8275]


RE: (Feature Request) - Convert to polar (phasor) notation in CAS - Dands - 04-01-2020 05:52 AM

(03-31-2020 09:16 PM)victorvbc Wrote:  
(03-31-2020 08:14 PM)Dands Wrote:  I think something weird is happening, probably because you used the simplify command in the output. I removed it and it persisted, so I'm clueless. Both of these should provide an angle of 45deg. I think it is not handling it properly when coefficients a and b are not integers.

Also, is it normal to see the terminal showing so many alerts?


That is quite weird... I guess the CAS doesn't like converting angles from floating point complex numbers to degrees. Anyway, here's a quick & dirty fix:

Code:

//This program takes an expression or matrix and turns it into polar coordinates
#cas
toPolar(z):= 
BEGIN
LOCAL sizes,j,k;

IF type(z)=DOM_LIST THEN //check if is list/matrix
  sizes:=SIZE(z); //get dimension of rows and columns
  IF size(sizes)=1 THEN //row vector
    FOR j FROM 1 to sizes DO
      z[j]:=convert_polar(z[j]); //convert each element to polar
    END;
  ELSE //column vector or matrix
    FOR j FROM 1 to sizes(1) DO
      FOR k FROM 1 to sizes(2) DO
        z[j,k]:=convert_polar(z[j,k]); //convert each element to polar
      END; 
    END;
  END;
ELSE
  z:=convert_polar(z); //single element
END;

RETURN z;
END;

convert_polar(x):=
BEGIN
  IF type(IM(x))=DOM_FLOAT OR type(RE(x))=DOM_FLOAT THEN
    x:=exact(x);
      RETURN approx({ABS(x),ARG(x)});
  ELSE
    RETURN {ABS(x),ARG(x)};
  END;
END;
#end

It works for that case now. I don't know about the errors, I didn't get them.


Works great now. Thanks a lot. Still hoping it gets officially implemented in the future though.


RE: (Feature Request) - Convert to polar (phasor) notation in CAS - compsystems - 04-05-2020 01:35 PM

Another request

The imaginary unit i is used like z = a+b*i in general mathematics, however, sometimes j is used as an imaginary unit in the electricity or magnetism classroom because the symbol i has the meaning of current. The imaginary unit may be changed to j on the Imaginary Unit setting screen in System application on casio classPad


[Image: mainexample_06.GIF]
[Image: mainexample_07.GIF]
[Image: mainexample_08.GIF]

Even the character i must be free to be used as a counter variable, therefore another unicode character is required to show (i, e), this will help us to use the i, j counters of the for loops


RE: (Feature Request) - Convert to polar (phasor) notation in CAS - CyberAngel - 04-13-2020 12:23 AM

(04-05-2020 01:35 PM)compsystems Wrote:  Another request

The imaginary unit i is used like z = a+b*i in general mathematics, however, sometimes j is used as an imaginary unit in the electricity or magnetism classroom because the symbol i has the meaning of current. The imaginary unit may be changed to j on the Imaginary Unit setting screen in System application on casio classPad


[Image: mainexample_06.GIF]
[Image: mainexample_07.GIF]
[Image: mainexample_08.GIF]

Even the character i must be free to be used as a counter variable, therefore another unicode character is required to show (i, e), this will help us to use the i, j counters of the for loops

There is a special character for the imaginary 'i' with a dedicated key [ Shift] [ 2 ].
Another character is needed for the new imaginary 'j'.
Perhaps HOLDING DOWN the [ Shift ] key would do it?
Reintroducing an old HP 50g concept: shift levels via hold-the-shift-down
and an extra parameter for the
- -
VPN


RE: (Feature Request) - Convert to polar (phasor) notation in CAS - NullGeodesic - 11-27-2023 03:37 PM

I found this thread because like Dands, I also needed a way to convert matrices and vectors to polar form within the CAS view.

Based on victorvbc's helpful code I have written a new program that outputs the polar result directly into the CAS entry line after pressing a user mode hotkey. Please note that the program only outputs the results in numeric form because I could not get exact form outputs to work reliably.

The program works by assembling the polar matrix/vector using strings and then passing the string as a return value for a hotkey.

Code:

//  Hotkey to convert complex numbers from rectangular to polar form in CAS mode
// (gives approximate numerical result)
KEY K_Eex() // "P key for "Polar form"
BEGIN
RETURN toPolarCAS(Ans);
END;
 
// This program takes a CAS expression or matrix and turns it into polar coordinates
// (gives approximate numerical result)
#cas
toPolarCAS(z):= 
BEGIN
LOCAL sizes,rows,cols,j,k,output_string;

IF type(z)=DOM_LIST THEN //check if is list/matrix
  sizes:=SIZE(z); //get list of row and column counts
  rows:=sizes(1); //get number of rows
  cols:=sizes(2); //get number of columns
  
  // Iterate over all matrix elements and build output string in polar form

  output_string:="["; // Start matrix
  FOR j FROM 1 to rows DO // Iterate over rows
      output_string:=output_string + "["; // Start row
      FOR k FROM 1 to cols DO  // Iterate over columns in row
        output_string:=output_string + convert_polar(z[j,k]); // Convert matrix element to polar form
        IF (k < cols) THEN
          output_string:=output_string + ","; // Separate elements in row
        END;
      END;
       output_string:=output_string + "]"; // End row
      IF (j < rows) THEN
        output_string:=output_string + ","; // Separate rows
      END;
  END;
     output_string:=output_string + "]"; // End matrix
  ELSE
     output_string:=convert_polar(z); //single element is converted directly
  END;

RETURN output_string; // Return matrix in the form of a string
END;
#end

// Helper function to convert numbers to string
// For some reason the string command gives errors inside a #cas environment
EXPORT STRING_NONCAS(x)
BEGIN
   RETURN STRING(x,HFormat+1,12); // Force current display mode with maximum precision
END;

// Helper function to convert matrix elements from rectangular to polar form
// (gives approximate numerical result)
#cas
convert_polar(x):=
BEGIN
   LOCAL x_polar;
   IF ARG(x)==0 THEN // Suppress argument zero for real numbers because it confuses the CAS
      x_polar:=REPLACE(STRING_NONCAS(x),"-","−"); //Minus character is replaced by negative sign
   ELSE
      x_polar:=STRING_NONCAS(ABS(x)) + "∡" + REPLACE(STRING_NONCAS(ARG(x)),"-","−"); //Conversion to polar using strings
   END;
   RETURN x_polar;
END;
#end

Note: The approx() command can be used to convert matrices back to rectangular form in CAS mode.


RE: (Feature Request) - Convert to polar (phasor) notation in CAS - Dands - 09-16-2024 09:49 PM

Just wanted to bring this thread back to life because a few years passed, I went back to school and I still would love to see a solution for this. I'm now working on my Master's degree in power engineering and the HP Prime remains as the best calculator in my area. The CAS app is absolutely amazing handling matrices with complex numbers, but won't convert results to phasor notation. I have to either use the programs posted here (thank you very much once again) or bring my results to Home and then convert.

I know it's not easy to implement in CAS as explained above, but please try. Power engineers will be forever grateful Smile