In [1]:
from sympy import *
init_printing()
In [ ]:
In [2]:
A = Matrix([
[3, 3],
[2, S(3)/2]])
A
Out[2]:
In [3]:
b = Matrix([6,5])
In [4]:
AUG = A.row_join(b)
AUG # the augmented matrix
Out[4]:
In [5]:
AUGA = AUG.copy()
AUGA[0,:] = AUGA[0,:]/3
AUGA
Out[5]:
In [6]:
AUGA[1,:] = AUGA[1,:] - 2*AUGA[0,:]
AUGA
Out[6]:
In [7]:
AUGA[1,:] = -2*AUGA[1,:]
AUGA
Out[7]:
In [8]:
AUGA[0,:] = AUGA[0,:] - AUGA[1,:]
AUGA
Out[8]:
In [9]:
AUGB = AUG.copy()
AUGB[0,:] = AUGB[0,:] - AUGB[1,:]
AUGB
Out[9]:
In [10]:
AUGB[1,:] = AUGB[1,:] - 2*AUGB[0,:]
AUGB
Out[10]:
In [11]:
AUGB[1,:] = -1*S(2)/3*AUGB[1,:]
AUGB
Out[11]:
In [12]:
AUGB[0,:] = AUGB[0,:] - S(3)/2*AUGB[1,:]
AUGB
Out[12]:
In [ ]:
In [13]:
AUGC = AUG.copy()
AUGC[0,:], AUGC[1,:] = AUGC[1,:], AUGC[0,:]
AUGC
Out[13]:
In [14]:
AUGC[0,:] = AUGC[0,:]/2
AUGC
Out[14]:
In [15]:
AUGC[1,:] = AUGC[1,:] - 3*AUGC[0,:]
AUGC
Out[15]:
In [16]:
AUGC[1,:] = S(4)/3*AUGC[1,:]
AUGC
Out[16]:
In [17]:
AUGC[0,:] = AUGC[0,:] - S(3)/4*AUGC[1,:]
AUGC
Out[17]:
In [18]:
# define agmented matrices for three systems of eqns. with unique sol'ns
A = Matrix([
[ -1, -2, -2],
[ 3, 3, 0]])
B = Matrix([
[ 1, -1, -2, 1],
[-2, 3, 3, -1],
[-1, 0, 1, 2]])
C = Matrix([
[ 2, -2, 3, 2],
[ 1, -2, -1, 0],
[-2, 2, 2, 1]])
In [19]:
A
Out[19]:
In [20]:
A.rref()
Out[20]:
In [21]:
B
Out[21]:
In [22]:
B.rref()
Out[22]:
In [23]:
C
Out[23]:
In [24]:
C.rref()
Out[24]:
In [25]:
# now for three systems of eqns. with infinitely many sol'ns
D = Matrix([
[ -1, -2, -2],
[ 3, 6, 6]])
E = Matrix([
[ 1, -1, -2, 1],
[-2, 3, 3, -1],
[-1, 2, 1, 0]])
F = Matrix([
[ 2, -2, 3, 2],
[ 0, 0, 5, 3],
[-2, 2, 2, 1]])
In [26]:
D
Out[26]:
In [27]:
D.rref()
Out[27]:
In [28]:
D[0:2,0:2].nullspace()
Out[28]:
In [29]:
# the solutions to the sytem of equations represented by D
# is of the form point + nullspace
point = D.rref()[0][:,2]
nullspace = D[0:2,0:2].nullspace()
In [30]:
# the point is also called he particular solution
point
Out[30]:
In [31]:
# if A aug matrix is [A|b], then the point satisfies A*point = b.
print( D[0:2,0:2]*point == D[:,2] )
D[0:2,0:2]*point
Out[31]:
In [32]:
# the nullspace of A in aug. matrix [A|b] is one dimensional and spanned by
n = nullspace[0]
n
# every vector n in the nullspace of A satisfies A*n=0
Out[32]:
In [33]:
# so solution to A*x=b is any (point+s*n) where s is any real number
# since A*(point +s*n) = A*point + sA*n = A*point + 0 = b.
# verify claim for 20 values of s in range -5,-4,-3,-2,-1,0,1,2,3,4,5
for s in range(-5,6):
print( D[0:2,0:2]*(point + s*n),
D[0:2,0:2]*(point + s*n) == D[:,2] )
In [34]:
E
Out[34]:
In [35]:
E.rref()
Out[35]:
In [36]:
point_E = E.rref()[0][:,3]
nullspace_E = E[0:3,0:3].nullspace()[0]
s = symbols('s')
point_E + s*nullspace_E
Out[36]:
In [37]:
F
Out[37]:
In [38]:
F.rref()
Out[38]:
In [39]:
point_F = F.rref()[0][:,3]
nullspace_F = F[0:3,0:3].nullspace()[0]
s = symbols('s')
point_F + s*nullspace_F
Out[39]:
In [ ]: