Welcome, guest | Sign In | My Account | Store | Cart

Solves simultaneous linear equations of any order using Crammer's rule. Required input is two lists..one for coefficients, and other for constants eg. 2x+3y=8 x+4y=6 will be written as simul([[2,3],[1,4]],[8,6])

Python, 48 lines
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48``` ```def det(l): n=len(l) if (n>2): i=1 t=0 sum=0 while t<=n-1: d={} t1=1 while t1<=n-1: m=0 d[t1]=[] while m<=n-1: if (m==t): u=0 else: d[t1].append(l[t1][m]) m+=1 t1+=1 l1=[d[x] for x in d] sum=sum+i*(l[0][t])*(det(l1)) i=i*(-1) t+=1 return sum else: return (l[0][0]*l[1][1]-l[0][1]*l[1][0]) def simul(leq,lcoeff): D=float(det(leq)) if D==0: return -1 else: t=0 n=len(leq) l=[] while t<=(n-1): lt=[] for x in leq: ltemp=[] for y in x: ltemp.append(y) lt.append(ltemp) m=0 while m<=(n-1): lt[m][t]=lcoeff[m] m+=1 l.append(det(lt)/D) t+=1 return l ```
 Created by Sachin Joglekar on Tue, 24 Apr 2012 (MIT)