eqsys.py

Created by laigna

Created on March 14, 2026

4.73 KB

Cramer’s rule for 2x2/3x3 systems + determinant calculator, step-by-step results


# Eq System Solver - NumWorks
# Created by Alvar Laigna - https://alvarlaigna.com
# Arrows:select OK:choose Back:back
from kandinsky import fill_rect as F,draw_string as D
from ion import keydown as K
from time import sleep as Z
SW,SH=320,222
BK=(0,)*3;WH=(255,)*3
BL=(50,110,230);TL=(0,190,190)
YL=(220,200,60);GN=(60,190,60)
DG=(120,)*3

okv=1 if K(4)or K(52) else 0

def okd():
 return K(4)or K(52)

def okp():
 global okv
 d=okd()
 if d and not okv:
  okv=1
  return True
 if not d:okv=0
 return False

def wup():
 while okd():Z(0.02)
 Z(0.12)

def menu(title,items,sub=False):
 sel=0;n=len(items)
 while True:
  F(0,0,SW,SH,BK)
  D(title,SW//2-len(title)*5,6,TL,BK)
  F(0,24,SW,2,(40,)*3)
  for i in range(n):
   y=34+i*22
   if i==sel:
    F(8,y,SW-16,20,BL)
    D("> "+items[i],14,y+2,WH,BL)
   else:
    D("  "+items[i],14,y+2,DG,BK)
  if sub:D("Back=return",100,SH-16,(60,)*3,BK)
  else:D("Back=quit",115,SH-16,(60,)*3,BK)
  while True:
   if K(1):sel=(sel-1)%n;Z(0.15);break
   if K(2):sel=(sel+1)%n;Z(0.15);break
   if okp():wup();return sel
   if K(17):return-1
   Z(0.04)

def inp(prompt):
 try:
  v=input(prompt)
  return float(v)
 except:return None

def show(lines):
 F(0,0,SW,SH,BK)
 D("RESULT",130,6,TL,BK)
 F(0,24,SW,2,(40,)*3)
 for i,ln in enumerate(lines):
  c=YL if i==len(lines)-1 else WH
  D(str(ln),14,34+i*20,c,BK)
 D("OK=back",130,SH-16,DG,BK)
 while True:
  if okp()or K(17):wup();return
  Z(0.04)

def r(v):
 return round(v,6)

def det2(a,b,c,d):
 return a*d-b*c

def det3(a):
 return(a[0][0]*(a[1][1]*a[2][2]-a[1][2]*a[2][1])
  -a[0][1]*(a[1][0]*a[2][2]-a[1][2]*a[2][0])
  +a[0][2]*(a[1][0]*a[2][1]-a[1][1]*a[2][0]))

def sys2():
 D("Equation 1: a1*x+b1*y=c1",14,34,WH,BK)
 a1=inp("a1: ")
 if a1 is None:return
 b1=inp("b1: ")
 if b1 is None:return
 c1=inp("c1: ")
 if c1 is None:return
 D("Equation 2: a2*x+b2*y=c2",14,56,WH,BK)
 a2=inp("a2: ")
 if a2 is None:return
 b2=inp("b2: ")
 if b2 is None:return
 c2=inp("c2: ")
 if c2 is None:return
 dt=det2(a1,b1,a2,b2)
 e1=str(r(a1))+"x+"+str(r(b1))+"y="+str(r(c1))
 e2=str(r(a2))+"x+"+str(r(b2))+"y="+str(r(c2))
 if dt==0:
  show([e1,e2,"","Det = 0","No unique solution"])
  return
 x=det2(c1,b1,c2,b2)/dt
 y=det2(a1,c1,a2,c2)/dt
 show([e1,e2,"","Determinant = "+str(r(dt)),
  "x = "+str(r(x)),"y = "+str(r(y))])

def sys3():
 D("Eq 1: a1x+b1y+c1z=d1",14,34,WH,BK)
 a1=inp("a1: ")
 if a1 is None:return
 b1=inp("b1: ")
 if b1 is None:return
 c1=inp("c1: ")
 if c1 is None:return
 d1=inp("d1: ")
 if d1 is None:return
 D("Eq 2: a2x+b2y+c2z=d2",14,56,WH,BK)
 a2=inp("a2: ")
 if a2 is None:return
 b2=inp("b2: ")
 if b2 is None:return
 c2=inp("c2: ")
 if c2 is None:return
 d2=inp("d2: ")
 if d2 is None:return
 D("Eq 3: a3x+b3y+c3z=d3",14,78,WH,BK)
 a3=inp("a3: ")
 if a3 is None:return
 b3=inp("b3: ")
 if b3 is None:return
 c3=inp("c3: ")
 if c3 is None:return
 d3=inp("d3: ")
 if d3 is None:return
 m=[[a1,b1,c1],[a2,b2,c2],[a3,b3,c3]]
 dt=det3(m)
 e1=str(r(a1))+"x+"+str(r(b1))+"y+"+str(r(c1))+"z="+str(r(d1))
 e2=str(r(a2))+"x+"+str(r(b2))+"y+"+str(r(c2))+"z="+str(r(d2))
 e3=str(r(a3))+"x+"+str(r(b3))+"y+"+str(r(c3))+"z="+str(r(d3))
 if dt==0:
  show([e1,e2,e3,"","Det = 0","No unique solution"])
  return
 dx=det3([[d1,b1,c1],[d2,b2,c2],[d3,b3,c3]])
 dy=det3([[a1,d1,c1],[a2,d2,c2],[a3,d3,c3]])
 dz=det3([[a1,b1,d1],[a2,b2,d2],[a3,b3,d3]])
 x=dx/dt;y=dy/dt;z=dz/dt
 show([e1,e2,e3,"","Det = "+str(r(dt)),
  "x = "+str(r(x)),"y = "+str(r(y)),
  "z = "+str(r(z))])

def det2m():
 D("Matrix [[a,b],[c,d]]",14,34,WH,BK)
 a=inp("a: ")
 if a is None:return
 b=inp("b: ")
 if b is None:return
 c=inp("c: ")
 if c is None:return
 d=inp("d: ")
 if d is None:return
 dt=det2(a,b,c,d)
 show(["| "+str(r(a))+"  "+str(r(b))+" |",
  "| "+str(r(c))+"  "+str(r(d))+" |","",
  "det = a*d - b*c",
  "Determinant = "+str(r(dt))])

def det3m():
 D("Matrix 3x3 row by row",14,34,WH,BK)
 D("Row 1:",14,56,WH,BK)
 a1=inp("a1: ")
 if a1 is None:return
 b1=inp("b1: ")
 if b1 is None:return
 c1=inp("c1: ")
 if c1 is None:return
 D("Row 2:",14,78,WH,BK)
 a2=inp("a2: ")
 if a2 is None:return
 b2=inp("b2: ")
 if b2 is None:return
 c2=inp("c2: ")
 if c2 is None:return
 D("Row 3:",14,100,WH,BK)
 a3=inp("a3: ")
 if a3 is None:return
 b3=inp("b3: ")
 if b3 is None:return
 c3=inp("c3: ")
 if c3 is None:return
 m=[[a1,b1,c1],[a2,b2,c2],[a3,b3,c3]]
 dt=det3(m)
 show(["| "+str(r(a1))+" "+str(r(b1))+" "+str(r(c1))+" |",
  "| "+str(r(a2))+" "+str(r(b2))+" "+str(r(c2))+" |",
  "| "+str(r(a3))+" "+str(r(b3))+" "+str(r(c3))+" |","",
  "Determinant = "+str(r(dt))])

def run():
 items=["2x2 System","3x3 System",
  "2x2 Determinant","3x3 Determinant"]
 while True:
  s=menu("Eq System Solver",items)
  if s<0:break
  F(0,0,SW,SH,BK)
  D("Enter coefficients",14,6,TL,BK)
  F(0,24,SW,2,(40,)*3)
  if s==0:sys2()
  elif s==1:sys3()
  elif s==2:det2m()
  elif s==3:det3m()

run()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.