"""
PROGRAM 4: CONVERT FORMS
Rect <-> Polar conversion
TI-84 CE Python v5.8.3
"""
try:
from ti_system import *
except:
pass
import math
while True:
print("\nFORM CONVERTER")
print("="*25)
print("1: Rect -> Polar")
print("2: Polar -> Rect")
print("3: Exit")
choice = input("\nChoice (1/2/3): ")
if choice == "3":
print("\nGoodbye!")
break
if choice == "1":
# Rectangular to Polar
print("\nEnter a + jb:")
a = float(input("Real part a: "))
b = float(input("Imag part b: "))
# Display input
if b >= 0:
print("\nInput: %.3f + j%.3f" % (a, b))
else:
print("\nInput: %.3f - j%.3f" % (a, abs(b)))
# Convert
r = math.sqrt(a**2 + b**2)
theta = math.degrees(math.atan2(b, a))
# Display result
print("\nPolar form:")
print("r = %.4f" % r)
print("theta = %.2f deg" % theta)
print("\n= %.4f at %.2f deg" % (r, theta))
elif choice == "2":
# Polar to Rectangular
print("\nEnter r at theta:")
r = float(input("Magnitude r: "))
theta = float(input("Angle theta (deg): "))
# Display input
print("\nInput: %.3f at %.2f deg" % (r, theta))
# Convert
rad = math.radians(theta)
a = r * math.cos(rad)
b = r * math.sin(rad)
# Display result
print("\nRectangular form:")
print("a = %.4f" % a)
print("b = %.4f" % b)
if b >= 0:
print("\n= %.4f + j%.4f" % (a, b))
else:
print("\n= %.4f - j%.4f" % (a, abs(b)))
else:
print("\nInvalid choice!")
input("\nPress Enter for another...")
"""
FLEXIBLE COMPLEX MATRIX SOLVER
Any size: 2x2, 2x3, 3x4, 4x4, etc.
TI-84 CE Python v5.8.3
"""
try:
from ti_system import *
except:
pass
import math
def c_add(z1, z2):
return [z1[0]+z2[0], z1[1]+z2[1]]
def c_sub(z1, z2):
return [z1[0]-z2[0], z1[1]-z2[1]]
def c_mul(z1, z2):
a, b = z1[0], z1[1]
c, d = z2[0], z2[1]
return [a*c-b*d, a*d+b*c]
def c_div(z1, z2):
a, b = z1[0], z1[1]
c, d = z2[0], z2[1]
denom = c*c + d*d
if denom < 1e-20:
return [0, 0]
return [(a*c+b*d)/denom, (b*c-a*d)/denom]
def c_mag(z):
return math.sqrt(z[0]**2 + z[1]**2)
def c_to_polar(z):
r = math.sqrt(z[0]**2 + z[1]**2)
theta = math.degrees(math.atan2(z[1], z[0]))
return [r, theta]
def c_show_rect(name, z):
if z[1] >= 0:
print("%s= %.3f+j%.3f" % (name, z[0], z[1]))
else:
print("%s= %.3f-j%.3f" % (name, z[0], abs(z[1])))
def c_show_polar(name, z):
p = c_to_polar(z)
print("%s= %.3f at %.2f deg" % (name, p[0], p[1]))
def c_show_both(name, z):
if z[1] >= 0:
print("%s= %.3f+j%.3f" % (name, z[0], z[1]))
else:
print("%s= %.3f-j%.3f" % (name, z[0], abs(z[1])))
p = c_to_polar(z)
print(" = %.3f at %.2f deg" % (p[0], p[1]))
print("FLEXIBLE MATRIX SOLVER")
print("="*25)
print("Solve: Ax = b")
# Get output format preference
print("\nOutput format:")
print("1: Rectangular (a+jb)")
print("2: Polar (r at theta)")
print("3: Both forms")
fmt = input("Choice (1/2/3): ")
# Get matrix dimensions
rows = int(input("\nRows (equations): "))
cols = int(input("Cols (unknowns): "))
if rows < 2 or rows > 5:
print("Rows must be 2-5")
input("\nPress Enter")
exit()
if cols < 2 or cols > 5:
print("Cols must be 2-5")
input("\nPress Enter")
exit()
print("\nSolve %dx%d system" % (rows, cols))
if rows != cols:
print("Note: Over/under-determined")
print("Will find best solution")
# Get matrix A
A = []
for i in range(rows):
row = []
for j in range(cols):
print("\nA[%d][%d]:" % (i+1, j+1))
r = float(input("Real: "))
im = float(input("Imag: "))
row.append([r, im])
row.append([0, 0]) # placeholder for b
A.append(row)
# Get vector b
print("\nVector b:")
for i in range(rows):
print("\nb%d:" % (i+1))
r = float(input("Real: "))
im = float(input("Imag: "))
A[i][cols] = [r, im]
print("\nSolving...")
# Gaussian elimination with RREF
# Handle rectangular matrices
min_dim = min(rows, cols)
for i in range(min_dim):
# Find pivot in column i
max_row = i
max_val = 0
for k in range(i, rows):
val = c_mag(A[k][i])
if val > max_val:
max_val = val
max_row = k
# Check if column is zero
if max_val < 1e-10:
print("\nWarning: Column %d zero" % (i+1))
continue
# Swap rows
A[i], A[max_row] = A[max_row], A[i]
# Scale pivot row
pivot = A[i][i]
for j in range(cols+1):
A[i][j] = c_div(A[i][j], pivot)
# Eliminate column in all other rows
for k in range(rows):
if k != i:
factor = A[k][i]
for j in range(cols+1):
temp = c_mul(factor, A[i][j])
A[k][j] = c_sub(A[k][j], temp)
# Extract solution
print("\nSOLUTION:")
# Choose display function based on user choice
if fmt == "1":
c_show = c_show_rect
elif fmt == "2":
c_show = c_show_polar
else:
c_show = c_show_both
# For square or overdetermined systems
if rows >= cols:
for i in range(cols):
if i < rows:
x = A[i][cols]
c_show("x%d " % (i+1), x)
else:
print("x%d = Free variable" % (i+1))
# Check if overdetermined system is consistent
if rows > cols:
print("\nConsistency check:")
consistent = True
for i in range(cols, rows):
if c_mag(A[i][cols]) > 0.01:
print("Row %d: Inconsistent" % (i+1))
consistent = False
if consistent:
print("✓ System consistent")
# For underdetermined systems
else:
for i in range(rows):
x = A[i][cols]
c_show("x%d " % (i+1), x)
for i in range(rows, cols):
print("x%d = Free (set to 0)" % (i+1))
print("\nNote: Infinite solutions")
print("Free vars can be any value")
print("\n✓ Done!")
input("\nPress Enter to exit")
Simple 2x2 system demo: Solve Ax = b with complex numbers