Last active
March 7, 2019 18:07
-
-
Save kaovilai/8c5134a433e5288324a1e2708552144f to your computer and use it in GitHub Desktop.
ECE331 SageMath Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Tiger's ECE331 SageMath Library Version 0.20 | |
#latest at -> https://gist.github.com/kaovilai/8c5134a433e5288324a1e2708552144f/ | |
def getReal(im): | |
if type(im)==complex: | |
return im.real | |
return im.real() | |
def getImag(im): | |
if type(im)==complex: | |
return im.imag | |
return im.imag() | |
def radToDeg(i): | |
return i*180/pi | |
def degToRad(i): | |
return i*pi/180 | |
def polarToCartReal(radius,angleInDegrees): | |
return radius*cos(degToRad(angleInDegrees)) | |
def polarToCartImagn(radius,angleInDegrees): | |
return radius*sin(degToRad(angleInDegrees)) | |
def polarToCart(radius,angleInDegrees): #polar to cartesian complex | |
return complex(polarToCartReal(radius,angleInDegrees),polarToCartImagn(radius,angleInDegrees)) | |
def cartToPolarRadius(i): #cartesian to polar radius | |
return sqrt(getReal(i)^2+getImag(i)^2) | |
def cartToPolarAngleDeg(i): #cartesian to polar amgle in degrees. algorithm source http://www.chemistrylearning.com/geometrical-to-polar-conversion/ | |
#quadrant I top right, (R,Im) = (+,+) | |
if(getReal(i) >=0 and getImag(i) >=0): | |
return radToDeg(atan(getImag(i)/getReal(i))) | |
#quadrant II top left, (R,Im) = (-,+) | |
elif(getReal(i) < 0 and getImag(i) >=0): | |
return radToDeg(atan(getImag(i)/getReal(i)))+180 | |
#quadrant III bottom left, (R,Im) = (-,-) | |
elif(getReal(i) < 0 and getImag(i) <0): | |
return radToDeg(atan(getImag(i)/getReal(i)))-180 | |
#quadrant IV bottom right, (R,Im) = (+,-) | |
elif(getReal(i) >=0 and getImag(i) <0): | |
return -radToDeg(atan(getImag(-i)/getReal(i))) | |
print "cartToPolarAngleDeg Error: case not found" | |
return | |
def cartToPolarAngleRad(i): | |
return degToRad(cartToPolarAngleDeg(i)) | |
def printCartInPolar(complex_cart, rounded=False, units ="units"): | |
if rounded is False: | |
print "radius =[",cartToPolarRadius(complex_cart),"]; angle = [" ,cartToPolarAngleDeg(complex_cart), "] degrees [",units,"]" | |
else: | |
print "radius =[",round(cartToPolarRadius(complex_cart),6),"]; angle = [" ,round(cartToPolarAngleDeg(complex_cart),6), "] degrees [",units,"]" | |
return |
Update to version 0.19
Add ability to handle both simple complex numbers and complex expression numbers
0.20
Fix getImag
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Clarity and feature add