Post Reply 
(Feature Request) - Convert to polar (phasor) notation in CAS
11-27-2023, 03:37 PM
Post: #17
RE: (Feature Request) - Convert to polar (phasor) notation in CAS
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.
Find all posts by this user
Quote this message in a reply
Post Reply 


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



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