{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "#import urllib\n", "#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/\"\n", "#f = urllib.urlopen(URL_link)\n", "#myfile = f.read()\n", "#print(myfile)\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "'uV : ' (u_00, u_01, u_10, u_11, u_20, u_21)" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "' U row : ' 3 ' U col : ' 2" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "'U : ' [u_00 u_01]\n", "[u_10 u_11]\n", "[u_20 u_21]" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "u_00 + 2*u_10 + 3*u_20 == 13" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "u_01 + 2*u_11 + 3*u_21 == 17" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "5*u_00 + 7*u_10 + 11*u_20 == 19" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "5*u_01 + 7*u_11 + 11*u_21 == 23" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "'S : ' [[u_00 == -1/3*r4 - 53/3, u_01 == -1/3*r3 - 73/3, u_10 == -4/3*r4 + 46/3, u_11 == -4/3*r3 + 62/3, u_20 == r4, u_21 == r3]]" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "u_00 == -1/3*r4 - 53/3" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "u_01 == -1/3*r3 - 73/3" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "u_10 == -4/3*r4 + 46/3" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "u_11 == -4/3*r3 + 62/3" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "u_20 == r4" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "u_21 == r3" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "'N : ' [ 1 2 3]\n", "[ 5 7 11] ' Unum : ' [-1/3*r4 - 53/3 -1/3*r3 - 73/3]\n", "[-4/3*r4 + 46/3 -4/3*r3 + 62/3]\n", "[ r4 r3] ' N*Unum : ' [13 17]\n", "[19 23] ' = M : ' [13 17]\n", "[19 23]" ] }, "execution_count": 6, "metadata": { }, "output_type": "execute_result" } ], "source": [ "forget()\n", "\n", "def SolveNUequalsM(N,M) :\n", " # solve matrix N*U=M, with U Unknowns matrix\n", " nR=N.nrows()\n", " nC=N.ncols()\n", " mR=M.nrows()\n", " mC=M.ncols()\n", "\n", " # generate unknown matrix variable list\n", " #################################\n", " uR=N.ncols()\n", " uC=M.ncols()\n", " uL=[]\n", " for r in [0..uR-1] :\n", " for c in [0..uC-1] :\n", " uL.append(\"u_%d%d\"%(r,c))\n", " uV=var(uL)\n", " show(\"uV : \",uV)\n", " show(\" U row : \",uR, \" U col : \",uC)\n", " \n", " \n", " Ul=[[0 for c in [0..uC-1]] for r in [0..uR-1]]\n", " \n", " index=0\n", " for r in [0..uR-1] :\n", " for c in [0..uC-1] :\n", " Ul[r][c]=uV[index]\n", " \n", " #print \"r : \" ,r,\" c : \" ,c ,\" index : \", index ,\" uV[index] : \", uV[index]\n", " index+=1\n", " U=(matrix(Ul))\n", " show(\"U : \",U)\n", " #show(\"N : \",N)\n", " #show(\"M : \",M) \n", " #\n", "\n", " NU=N*U\n", " eqT=[]\n", " for r in [0..mR-1] :\n", " for c in [0..mC-1] :\n", " eqT.append(NU[r][c]==M[r][c])\n", " # adding suplementary constraints if needed\n", " #eqT.append(u_30==0)\n", " \n", " ########## end adding suplementary constraints\n", " for eq in eqT :\n", " show(eq)\n", " S=solve(eqT,uV)\n", " if len(S)<>0 :\n", " \n", " show(\"S : \",S)\n", " if len(S[0])==U.nrows() * U.ncols() :\n", " for s in S[0]:\n", " show(s)\n", " UnumL=[]\n", " \n", " # fill the matrix Unum with its numerical values\n", " UnumL=[[0 for c in [0..uC-1]] for r in [0..uR-1]]\n", " index=0\n", " for r in [0..uR-1] :\n", " for c in [0..uC-1] :\n", " UnumL[r][c]=S[0][index].rhs()\n", " index+=1\n", " Unum=matrix(UnumL)\n", " \n", " # verify\n", " #show(\"U : \",U,\" Unum : \",Unum)\n", " #show(\"N : \",N)\n", " #show(\"M : \",M)\n", " #show(\"N*U :\",N*U)\n", " return U,Unum,S\n", " else :\n", " print \"number of solutions do not match number of unknowns!\"\n", " return U,U\n", " else :\n", " print \"No solutions !\"\n", " return U,U\n", " \n", " \n", "\n", "# test code now :\n", "\n", "# here choose size of your unknowns matrix U with these 2 lines\n", "nnR=4 # number of rows\n", "nnC=3 # number of columns\n", "##################################\n", "# solve matrix N*U=M\n", "nR=nnR\n", "nC=nnR\n", "mR=nnR\n", "mC=nnC\n", "uR=nnR\n", "uC=nnC\n", "\n", "Nl=[[0 for c in [1..nC]] for r in [1..nR]]\n", "for r in range(0,nR):\n", " for c in range(0,nC):\n", " Nl[r][c]=ZZ.random_element(-5,5)\n", "N=matrix(QQ,Nl)\n", "nR=N.nrows()\n", "nC=N.ncols()\n", "#show(N)\n", "#################################\n", "Ml=[[0 for c in [1..mC]] for r in [1..mR]]\n", "for r in range(0,mR):\n", " for c in range(0,mC):\n", " Ml[r][c]=ZZ.random_element(-5,5)\n", "M=matrix(QQ,Ml)\n", "\n", "# here we specified the two matrices N and M, comment these 2 lines if you want test with random Matrices\n", "#N=matrix(QQ,[[ 1 , 2 , 2 , 2 ],[ 2 , 4 , 6 , 8 ],[ 3, 6 , 8 , 10 ],[ 0, 0 , 0 , 0 ]])\n", "#M=matrix(QQ,[0,0,0,0]).transpose()\n", "\n", "#N=matrix([4,5,6]) #N or A\n", "#M=matrix([1,2,3])# M or Y\n", "\n", "# \n", "#N=matrix(QQ,[[ 1 , 2 , 3 ],[ 2 , 7 , 5 ],[ 0, 0 , 0 ]])\n", "#M=matrix(QQ,[0,0,0]).transpose()\n", "\n", "N=matrix([[1,2,3],[5,7,11]]) #N or A\n", "M=matrix([[13,17],[19,23]])# M or Y\n", "\n", "# solve matrix N*U=M\n", "U,Unum,S=SolveNUequalsM(N,M)\n", "show( \"N : \",N,\" Unum : \",Unum,\" N*Unum : \",N*Unum,\" = M : \",M)\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "' varList : ' [r4, r3] ' len(varList) : ' 2" ] }, "execution_count": 7, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "' XcList : ' [\n", "[-53/3 -73/3] [ -18 -73/3] [-53/3 -74/3]\n", "[ 46/3 62/3] [ 14 62/3] [ 46/3 58/3]\n", "[ 0 0], [ 1 0], [ 0 1]\n", "]" ] }, "execution_count": 7, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "[13 17]\n", "[19 23]" ] }, "execution_count": 7, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "[13 17]\n", "[19 23]" ] }, "execution_count": 7, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "[13 17]\n", "[19 23]" ] }, "execution_count": 7, "metadata": { }, "output_type": "execute_result" } ], "source": [ "###########################################################################\n", "# solution variables list (solution internal variables ri)\n", "def solutionMatricesList(sols,X):\n", " XcList=[]\n", " varList=[]\n", " varStr=\"\"\n", " for eqs in sols :\n", " for eq in eqs :\n", " #show(\" eq : \",eq,\" eq.rhs().variables() : \",eq.rhs().variables())\n", " for v in (eq.rhs().variables()) :\n", " if v not in varList :\n", " varList.append(v)\n", " show(\" varList : \",varList,\" len(varList) : \",len(varList))\n", " if len(varList)==0 :\n", " return XcList,varList\n", " varListValue=[0] * len(varList)\n", " Xc=copy(X)\n", " for i in range(0,len(varList)) :\n", " Xc=(Xc.subs({varList[i]:0 for i in range(0,len(varList))}))\n", " XcList.append(Xc)\n", " varListValue[0] =1\n", " for j in range(0,len(varList)) :\n", " Xc=copy(X)\n", " #show(\" varListValue : \",varListValue)\n", " for i in range(0,len(varList)) :\n", " Xc=(Xc.subs({varList[i]:varListValue[i] for i in range(0,len(varList))}))\n", " varListValue.insert(0,varListValue.pop())\n", " XcList.append(Xc)\n", " return XcList,varList\n", "XcList,varList=solutionMatricesList(S,Unum) \n", "show(\" XcList : \", XcList)\n", "if varList <>0 :\n", " for x in XcList :\n", " show(N*x)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ ] } ], "metadata": { "kernelspec": { "display_name": "SageMath (stable)", "language": "sagemath", "metadata": { "cocalc": { "description": "Open-source mathematical software system", "priority": 10, "url": "https://www.sagemath.org/" } }, "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.15" } }, "nbformat": 4, "nbformat_minor": 0 }