Post Reply 
(Feature Request) - Convert to polar (phasor) notation in CAS
03-31-2020, 12:31 PM
Post: #9
RE: (Feature Request) - Convert to polar (phasor) notation in CAS
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.
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 - victorvbc - 03-31-2020 12:31 PM



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