SharedLecture7LinearAlgebra.ipynbOpen in CoCalc
Author: ortollj
Description: Solve Matrix system N*U=M , U=Unknowns matrix
In [1]:
#import urllib #URL_link="https://ocw.mit.edu/courses/mathematics/18-06-linear-algebra-spring-2010/video-lectures/lecture-7-solving-ax-0-pivot-variables-special-solutions/" #f = urllib.urlopen(URL_link) #myfile = f.read() #print(myfile)
In [6]:
forget() def SolveNUequalsM(N,M) : # solve matrix N*U=M, with U Unknowns matrix nR=N.nrows() nC=N.ncols() mR=M.nrows() mC=M.ncols() # generate unknown matrix variable list ################################# uR=N.ncols() uC=M.ncols() uL=[] for r in [0..uR-1] : for c in [0..uC-1] : uL.append("u_%d%d"%(r,c)) uV=var(uL) show("uV : ",uV) show(" U row : ",uR, " U col : ",uC) Ul=[[0 for c in [0..uC-1]] for r in [0..uR-1]] index=0 for r in [0..uR-1] : for c in [0..uC-1] : Ul[r][c]=uV[index] #print "r : " ,r," c : " ,c ," index : ", index ," uV[index] : ", uV[index] index+=1 U=(matrix(Ul)) show("U : ",U) #show("N : ",N) #show("M : ",M) # NU=N*U eqT=[] for r in [0..mR-1] : for c in [0..mC-1] : eqT.append(NU[r][c]==M[r][c]) # adding suplementary constraints if needed #eqT.append(u_30==0) ########## end adding suplementary constraints for eq in eqT : show(eq) S=solve(eqT,uV) if len(S)<>0 : show("S : ",S) if len(S[0])==U.nrows() * U.ncols() : for s in S[0]: show(s) UnumL=[] # fill the matrix Unum with its numerical values UnumL=[[0 for c in [0..uC-1]] for r in [0..uR-1]] index=0 for r in [0..uR-1] : for c in [0..uC-1] : UnumL[r][c]=S[0][index].rhs() index+=1 Unum=matrix(UnumL) # verify #show("U : ",U," Unum : ",Unum) #show("N : ",N) #show("M : ",M) #show("N*U :",N*U) return U,Unum,S else : print "number of solutions do not match number of unknowns!" return U,U else : print "No solutions !" return U,U # test code now : # here choose size of your unknowns matrix U with these 2 lines nnR=4 # number of rows nnC=3 # number of columns ################################## # solve matrix N*U=M nR=nnR nC=nnR mR=nnR mC=nnC uR=nnR uC=nnC Nl=[[0 for c in [1..nC]] for r in [1..nR]] for r in range(0,nR): for c in range(0,nC): Nl[r][c]=ZZ.random_element(-5,5) N=matrix(QQ,Nl) nR=N.nrows() nC=N.ncols() #show(N) ################################# Ml=[[0 for c in [1..mC]] for r in [1..mR]] for r in range(0,mR): for c in range(0,mC): Ml[r][c]=ZZ.random_element(-5,5) M=matrix(QQ,Ml) # here we specified the two matrices N and M, comment these 2 lines if you want test with random Matrices #N=matrix(QQ,[[ 1 , 2 , 2 , 2 ],[ 2 , 4 , 6 , 8 ],[ 3, 6 , 8 , 10 ],[ 0, 0 , 0 , 0 ]]) #M=matrix(QQ,[0,0,0,0]).transpose() #N=matrix([4,5,6]) #N or A #M=matrix([1,2,3])# M or Y # #N=matrix(QQ,[[ 1 , 2 , 3 ],[ 2 , 7 , 5 ],[ 0, 0 , 0 ]]) #M=matrix(QQ,[0,0,0]).transpose() N=matrix([[1,2,3],[5,7,11]]) #N or A M=matrix([[13,17],[19,23]])# M or Y # solve matrix N*U=M U,Unum,S=SolveNUequalsM(N,M) show( "N : ",N," Unum : ",Unum," N*Unum : ",N*Unum," = M : ",M)
uVx:(u00,u01,u10,u11,u20,u21)\verb|uV|\phantom{\verb!x!}\verb|:| \left(u_{00}, u_{01}, u_{10}, u_{11}, u_{20}, u_{21}\right)
xUxrowx:3xUxcolx:2\phantom{\verb!x!}\verb|U|\phantom{\verb!x!}\verb|row|\phantom{\verb!x!}\verb|:| 3 \phantom{\verb!x!}\verb|U|\phantom{\verb!x!}\verb|col|\phantom{\verb!x!}\verb|:| 2
Ux:(u00u01u10u11u20u21)\verb|U|\phantom{\verb!x!}\verb|:| \left(\begin{array}{rr} u_{00} & u_{01} \\ u_{10} & u_{11} \\ u_{20} & u_{21} \end{array}\right)
u00+2u10+3u20=13u_{00} + 2 \, u_{10} + 3 \, u_{20} = 13
u01+2u11+3u21=17u_{01} + 2 \, u_{11} + 3 \, u_{21} = 17
5u00+7u10+11u20=195 \, u_{00} + 7 \, u_{10} + 11 \, u_{20} = 19
5u01+7u11+11u21=235 \, u_{01} + 7 \, u_{11} + 11 \, u_{21} = 23
Sx:[[u00=13r4533,u01=13r3733,u10=43r4+463,u11=43r3+623,u20=r4,u21=r3]]\verb|S|\phantom{\verb!x!}\verb|:| \left[\left[u_{00} = -\frac{1}{3} \, r_{4} - \frac{53}{3}, u_{01} = -\frac{1}{3} \, r_{3} - \frac{73}{3}, u_{10} = -\frac{4}{3} \, r_{4} + \frac{46}{3}, u_{11} = -\frac{4}{3} \, r_{3} + \frac{62}{3}, u_{20} = r_{4}, u_{21} = r_{3}\right]\right]
u00=13r4533u_{00} = -\frac{1}{3} \, r_{4} - \frac{53}{3}
u01=13r3733u_{01} = -\frac{1}{3} \, r_{3} - \frac{73}{3}
u10=43r4+463u_{10} = -\frac{4}{3} \, r_{4} + \frac{46}{3}
u11=43r3+623u_{11} = -\frac{4}{3} \, r_{3} + \frac{62}{3}
u20=r4u_{20} = r_{4}
u21=r3u_{21} = r_{3}
Nx:(1235711)xxUnumx:(13r453313r373343r4+46343r3+623r4r3)xxN*Unumx:(13171923)xx=xMx:(13171923)\verb|N|\phantom{\verb!x!}\verb|:| \left(\begin{array}{rrr} 1 & 2 & 3 \\ 5 & 7 & 11 \end{array}\right) \phantom{\verb!xx!}\verb|Unum|\phantom{\verb!x!}\verb|:| \left(\begin{array}{rr} -\frac{1}{3} \, r_{4} - \frac{53}{3} & -\frac{1}{3} \, r_{3} - \frac{73}{3} \\ -\frac{4}{3} \, r_{4} + \frac{46}{3} & -\frac{4}{3} \, r_{3} + \frac{62}{3} \\ r_{4} & r_{3} \end{array}\right) \phantom{\verb!xx!}\verb|N*Unum|\phantom{\verb!x!}\verb|:| \left(\begin{array}{rr} 13 & 17 \\ 19 & 23 \end{array}\right) \phantom{\verb!xx!}\verb|=|\phantom{\verb!x!}\verb|M|\phantom{\verb!x!}\verb|:| \left(\begin{array}{rr} 13 & 17 \\ 19 & 23 \end{array}\right)
In [7]:
########################################################################### # solution variables list (solution internal variables ri) def solutionMatricesList(sols,X): XcList=[] varList=[] varStr="" for eqs in sols : for eq in eqs : #show(" eq : ",eq," eq.rhs().variables() : ",eq.rhs().variables()) for v in (eq.rhs().variables()) : if v not in varList : varList.append(v) show(" varList : ",varList," len(varList) : ",len(varList)) if len(varList)==0 : return XcList,varList varListValue=[0] * len(varList) Xc=copy(X) for i in range(0,len(varList)) : Xc=(Xc.subs({varList[i]:0 for i in range(0,len(varList))})) XcList.append(Xc) varListValue[0] =1 for j in range(0,len(varList)) : Xc=copy(X) #show(" varListValue : ",varListValue) for i in range(0,len(varList)) : Xc=(Xc.subs({varList[i]:varListValue[i] for i in range(0,len(varList))})) varListValue.insert(0,varListValue.pop()) XcList.append(Xc) return XcList,varList XcList,varList=solutionMatricesList(S,Unum) show(" XcList : ", XcList) if varList <>0 : for x in XcList : show(N*x)
xvarListx:[r4,r3]xlen(varList)x:2\phantom{\verb!x!}\verb|varList|\phantom{\verb!x!}\verb|:| \left[r_{4}, r_{3}\right] \phantom{\verb!x!}\verb|len(varList)|\phantom{\verb!x!}\verb|:| 2
xXcListxx:[(53373346362300),(187331462310),(53374346358301)]\phantom{\verb!x!}\verb|XcList|\phantom{\verb!xx!}\verb|:| \left[\left(\begin{array}{rr} -\frac{53}{3} & -\frac{73}{3} \\ \frac{46}{3} & \frac{62}{3} \\ 0 & 0 \end{array}\right), \left(\begin{array}{rr} -18 & -\frac{73}{3} \\ 14 & \frac{62}{3} \\ 1 & 0 \end{array}\right), \left(\begin{array}{rr} -\frac{53}{3} & -\frac{74}{3} \\ \frac{46}{3} & \frac{58}{3} \\ 0 & 1 \end{array}\right)\right]
(13171923)\left(\begin{array}{rr} 13 & 17 \\ 19 & 23 \end{array}\right)
(13171923)\left(\begin{array}{rr} 13 & 17 \\ 19 & 23 \end{array}\right)
(13171923)\left(\begin{array}{rr} 13 & 17 \\ 19 & 23 \end{array}\right)
In [ ]: