Chapter 1 Assignment

466 days ago by monetta

Exercise #1


a)

# Function definition for population dynamics # transMat = the transition matrix # initStateVect = the initial state vector # yr = number of years after the census was taken # This function returns the state vector for year 'yr' def population(transMat, initStateVect, yr): if yr == 0: return initStateVect.transpose() elif yr == 1: return transMat * initStateVect.transpose() else: return transMat * population(transMat, initStateVect, yr-1) def float2Int(mat): for i in range(mat.nrows): for j in range(mat.ncols): mat[i][j] = int(mat[i][j]) A = matrix([[.70, .15, .10], [.15, .80, .30], [.15, .05, .60]]) # the transition matrix x0 = vector([300, 350, 200]) # the initial state vector print population(A, x0, 1) 
       
[282.5]
[385.0]
[182.5]
[282.5]
[385.0]
[182.5]

b)

       xn = (An)x0


c)

for i in range(10): print "x\r",i+1, "=" print population(A, x0, i+1) print "\n" 
       
x1 =
[282.5]
[385.0]
[182.5]


x2 =
[ 273.75]
[405.125]
[171.125]


x3 =
[269.50625]
[    416.5]
[163.99375]


x4 =
[  267.52875]
[422.8240625]
[159.6471875]


x5 =
[266.658453125]
[ 426.28271875]
[157.058828125]


x6 =
[266.309207813]
[428.142591406]
[155.548200781]


x7 =
[266.192654258]
[429.124914531]
[154.682431211]


x8 =
[266.171838281]
[429.633559127]
[154.194602592]


x9 =
[266.184780925]
[429.891003821]
[153.924215254]


x10 =
[266.205418746]
[430.017784772]
[153.776796482]
x1 =
[282.5]
[385.0]
[182.5]


x2 =
[ 273.75]
[405.125]
[171.125]


x3 =
[269.50625]
[    416.5]
[163.99375]


x4 =
[  267.52875]
[422.8240625]
[159.6471875]


x5 =
[266.658453125]
[ 426.28271875]
[157.058828125]


x6 =
[266.309207813]
[428.142591406]
[155.548200781]


x7 =
[266.192654258]
[429.124914531]
[154.682431211]


x8 =
[266.171838281]
[429.633559127]
[154.194602592]


x9 =
[266.184780925]
[429.891003821]
[153.924215254]


x10 =
[266.205418746]
[430.017784772]
[153.776796482]

The steady-state populations are 266 for Region A, 430 for Region B, and 153 for Region C.



d)

print "x20 =" print population(A, x0, 20) 
       
x20 =
[ 266.26471079]
[430.120924895]
[153.614364315]
x20 =
[ 266.26471079]
[430.120924895]
[153.614364315]

The result is the same as the result for x10 (removing the decimal) which shows that the steady-state population calculated in part c) is the actual steady-state population.


e)

print "x(-1) =" print population(A.inverse(), x0, 1) 
       
x(-1) =
[334.146341463]
[290.243902439]
[225.609756098]
x(-1) =
[334.146341463]
[290.243902439]
[225.609756098]

f)

for i in range(4): print "x(-\r",i+1, ")=" print population(A.inverse(), x0, i+1) print "\n" 
       
x(-1 )=
[334.146341463]
[290.243902439]
[225.609756098]


x(-2 )=
[399.385286536]
[190.303390839]
[260.311322625]


x(-3 )=
[ 521.71536171]
[27.1213901907]
[301.163248099]


x(-4 )=
[ 747.159711693]
[-231.609306111]
[ 334.449594417]
x(-1 )=
[334.146341463]
[290.243902439]
[225.609756098]


x(-2 )=
[399.385286536]
[190.303390839]
[260.311322625]


x(-3 )=
[ 521.71536171]
[27.1213901907]
[301.163248099]


x(-4 )=
[ 747.159711693]
[-231.609306111]
[ 334.449594417]

In x-4, the population is negative, which is not practically possible; therefore, the current model for the population was not accurate 4 years ago.


g)

x3 = vector([250, 400, 200]) B =(A**(-3))*x3.transpose() C = matrix(ZZ, [[0],[0],[0]]) for i in range(B.nrows()): C[i, 0] = int(B[i][0]) print "x0 =\n" print C 
       
x0 =

[249]
[143]
[456]
x0 =

[249]
[143]
[456]

Exercise #2


a)

# Generate random 6x6 matrices A = random_matrix(ZZ, 6, x=0, y=10) B = random_matrix(ZZ, 6, x=0, y=10) # Display the random matrices print "A=\n", A,"\n" print "B=\n", B, "\n" # Divide matrix A into 4 3x3 matrix blocks using matrix surgery A1 = A.submatrix(0, 0, 3, 3) A2 = A.submatrix(0, 3, 3, 3) A3 = A.submatrix(3, 0, 3, 3) A4 = A.submatrix(3, 3, 3, 3) # Display the blocks of matrix A print "A1=\n", A1, "\n" print "A2=\n", A2, "\n" print "A3=\n", A3, "\n" print "A4=\n", A4, "\n" # Divide matrix B into 4 3x3 matrix blocks using matrix surgery B1 = B.submatrix(0, 0, 3, 3) B2 = B.submatrix(0, 3, 3, 3) B3 = B.submatrix(3, 0, 3, 3) B4 = B.submatrix(3, 3, 3, 3) # Display the blocks of matrix B print "B1=\n", B1, "\n" print "B2=\n", B2, "\n" print "B3=\n", B3, "\n" print "B4=\n", B4, "\n" # Calculate the blocks for the new block matrix AB1 = A1*B1 + A2*B3 AB2 = A1*B2 + A2*B4 AB3 = A3*B1 + A4*B3 AB4 = A3*B2 + A4*B4 # Combine calculated blocks into a 6x6 matrix blockTop = (AB1).augment(AB2) blockBottom = (AB3).augment(AB4) blockMatrix = (blockTop).stack(blockBottom) # Calculate AB using regular matrix multiplication AB = A*B # Display the multiplied matrices print "AB = \n", AB, "\n" print "Block matrix = \n", blockMatrix 
       
A=
[0 2 3 6 9 8]
[3 0 0 4 1 1]
[0 2 9 0 2 0]
[7 8 6 5 8 9]
[3 1 1 9 4 3]
[2 0 8 5 0 8] 

B=
[2 3 8 3 7 0]
[0 2 1 5 3 3]
[1 8 3 8 4 5]
[2 8 7 5 6 9]
[1 1 8 6 0 6]
[7 9 5 8 0 9] 

A1=
[0 2 3]
[3 0 0]
[0 2 9] 

A2=
[6 9 8]
[4 1 1]
[0 2 0] 

A3=
[7 8 6]
[3 1 1]
[2 0 8] 

A4=
[5 8 9]
[9 4 3]
[5 0 8] 

B1=
[2 3 8]
[0 2 1]
[1 8 3] 

B2=
[3 7 0]
[5 3 3]
[8 4 5] 

B3=
[2 8 7]
[1 1 8]
[7 9 5] 

B4=
[5 6 9]
[6 0 6]
[8 0 9] 

AB = 
[ 80 157 165 182  54 201]
[ 22  51  65  43  45  51]
[ 11  78  45  94  42  63]
[101 214 226 254 127 228]
[ 50 122 138 115  82 140]
[ 78 182 115 159  76 157] 

Block matrix = 
[ 80 157 165 182  54 201]
[ 22  51  65  43  45  51]
[ 11  78  45  94  42  63]
[101 214 226 254 127 228]
[ 50 122 138 115  82 140]
[ 78 182 115 159  76 157]
A=
[0 2 3 6 9 8]
[3 0 0 4 1 1]
[0 2 9 0 2 0]
[7 8 6 5 8 9]
[3 1 1 9 4 3]
[2 0 8 5 0 8] 

B=
[2 3 8 3 7 0]
[0 2 1 5 3 3]
[1 8 3 8 4 5]
[2 8 7 5 6 9]
[1 1 8 6 0 6]
[7 9 5 8 0 9] 

A1=
[0 2 3]
[3 0 0]
[0 2 9] 

A2=
[6 9 8]
[4 1 1]
[0 2 0] 

A3=
[7 8 6]
[3 1 1]
[2 0 8] 

A4=
[5 8 9]
[9 4 3]
[5 0 8] 

B1=
[2 3 8]
[0 2 1]
[1 8 3] 

B2=
[3 7 0]
[5 3 3]
[8 4 5] 

B3=
[2 8 7]
[1 1 8]
[7 9 5] 

B4=
[5 6 9]
[6 0 6]
[8 0 9] 

AB = 
[ 80 157 165 182  54 201]
[ 22  51  65  43  45  51]
[ 11  78  45  94  42  63]
[101 214 226 254 127 228]
[ 50 122 138 115  82 140]
[ 78 182 115 159  76 157] 

Block matrix = 
[ 80 157 165 182  54 201]
[ 22  51  65  43  45  51]
[ 11  78  45  94  42  63]
[101 214 226 254 127 228]
[ 50 122 138 115  82 140]
[ 78 182 115 159  76 157]

b)

CALCULATION #1

# Generate random matrices A = random_matrix(ZZ, 7, 12, x=0, y=10) # 7x12 matrix B = random_matrix(ZZ, 12, 6, x=0, y=10) # 12x6 matrix # Divide matrix A into 4 matrix blocks using matrix surgery A1 = A.submatrix(0, 0, 3, 5) # 3x5 matrix block A2 = A.submatrix(0, 5, 3, 7) # 3x7 matrix block A3 = A.submatrix(3, 0, 4, 5) # 4x5 matrix block A4 = A.submatrix(3, 5, 4, 7) # 4x7 matrix block # Divide matrix B into 4 matrix blocks using matrix surgery B1 = B.submatrix(0, 0, 5, 2) # 5x2 matrix block B2 = B.submatrix(0, 2, 5, 4) # 5x4 matrix block B3 = B.submatrix(5, 0, 7, 2) # 7x2 matrix block B4 = B.submatrix(5, 2, 7, 4) # 7x4 matrix block # Display the blocks of both matrices print "A=\n", A, "\n" print "B=\n", B, "\n" print "A1=\n", A1, "\n" print "A2=\n", A2, "\n" print "A3=\n", A3, "\n" print "A4=\n", A4, "\n\n" print "B1=\n", B1, "\n" print "B2=\n", B2, "\n" print "B3=\n", B3, "\n" print "B4=\n", B4, "\n" # Calculate the blocks for the new block matrix AB1 = A1*B1 + A2*B3 AB2 = A1*B2 + A2*B4 AB3 = A3*B1 + A4*B3 AB4 = A3*B2 + A4*B4 # Combine calculated blocks into a 7x6 matrix blockTop = (AB1).augment(AB2) blockBottom = (AB3).augment(AB4) blockMatrix = (blockTop).stack(blockBottom) # Calculate AB using regular matrix multiplication AB = A*B # Display matrices print "AB = \n", AB, "\n" print "Block matrix = \n", blockMatrix 
       
A=
[9 7 4 7 7 0 7 4 9 3 6 3]
[5 2 7 3 5 4 3 6 1 3 2 8]
[0 3 2 5 3 1 3 6 2 4 8 2]
[4 3 0 9 4 1 6 2 2 3 6 8]
[8 1 4 0 5 5 4 1 4 8 8 3]
[2 9 8 4 2 8 1 7 6 0 7 5]
[2 4 6 5 0 9 5 4 1 2 7 9] 

B=
[8 3 3 6 2 6]
[7 9 7 5 6 1]
[5 6 6 1 9 2]
[0 0 5 6 3 3]
[6 7 1 8 0 8]
[2 1 5 6 8 7]
[3 8 2 2 1 8]
[3 0 4 9 7 2]
[4 3 9 9 7 1]
[1 5 9 1 5 7]
[9 0 3 5 9 2]
[8 4 8 8 2 4] 

A1=
[9 7 4 7 7]
[5 2 7 3 5]
[0 3 2 5 3] 

A2=
[0 7 4 9 3 6 3]
[4 3 6 1 3 2 8]
[1 3 6 2 4 8 2] 

A3=
[4 3 0 9 4]
[8 1 4 0 5]
[2 9 8 4 2]
[2 4 6 5 0] 

A4=
[1 6 2 2 3 6 8]
[5 4 1 4 8 8 3]
[8 1 7 6 0 7 5]
[9 5 4 1 2 7 9] 


B1=
[8 3]
[7 9]
[5 6]
[0 0]
[6 7] 

B2=
[3 6 2 6]
[7 5 6 1]
[6 1 9 2]
[5 6 3 3]
[1 8 0 8] 

B3=
[2 1]
[3 8]
[3 0]
[4 3]
[1 5]
[9 0]
[8 4] 

B4=
[5 6 8 7]
[2 2 1 8]
[4 9 7 2]
[9 9 7 1]
[9 1 5 7]
[3 5 9 2]
[8 8 2 4] 

AB = 
[333 273 322 379 290 264]
[243 188 247 275 227 217]
[178 119 190 215 214 143]
[232 169 234 276 180 212]
[266 193 253 252 255 254]
[298 203 324 347 371 183]
[260 176 286 286 300 219] 

Block matrix = 
[333 273 322 379 290 264]
[243 188 247 275 227 217]
[178 119 190 215 214 143]
[232 169 234 276 180 212]
[266 193 253 252 255 254]
[298 203 324 347 371 183]
[260 176 286 286 300 219]
A=
[9 7 4 7 7 0 7 4 9 3 6 3]
[5 2 7 3 5 4 3 6 1 3 2 8]
[0 3 2 5 3 1 3 6 2 4 8 2]
[4 3 0 9 4 1 6 2 2 3 6 8]
[8 1 4 0 5 5 4 1 4 8 8 3]
[2 9 8 4 2 8 1 7 6 0 7 5]
[2 4 6 5 0 9 5 4 1 2 7 9] 

B=
[8 3 3 6 2 6]
[7 9 7 5 6 1]
[5 6 6 1 9 2]
[0 0 5 6 3 3]
[6 7 1 8 0 8]
[2 1 5 6 8 7]
[3 8 2 2 1 8]
[3 0 4 9 7 2]
[4 3 9 9 7 1]
[1 5 9 1 5 7]
[9 0 3 5 9 2]
[8 4 8 8 2 4] 

A1=
[9 7 4 7 7]
[5 2 7 3 5]
[0 3 2 5 3] 

A2=
[0 7 4 9 3 6 3]
[4 3 6 1 3 2 8]
[1 3 6 2 4 8 2] 

A3=
[4 3 0 9 4]
[8 1 4 0 5]
[2 9 8 4 2]
[2 4 6 5 0] 

A4=
[1 6 2 2 3 6 8]
[5 4 1 4 8 8 3]
[8 1 7 6 0 7 5]
[9 5 4 1 2 7 9] 


B1=
[8 3]
[7 9]
[5 6]
[0 0]
[6 7] 

B2=
[3 6 2 6]
[7 5 6 1]
[6 1 9 2]
[5 6 3 3]
[1 8 0 8] 

B3=
[2 1]
[3 8]
[3 0]
[4 3]
[1 5]
[9 0]
[8 4] 

B4=
[5 6 8 7]
[2 2 1 8]
[4 9 7 2]
[9 9 7 1]
[9 1 5 7]
[3 5 9 2]
[8 8 2 4] 

AB = 
[333 273 322 379 290 264]
[243 188 247 275 227 217]
[178 119 190 215 214 143]
[232 169 234 276 180 212]
[266 193 253 252 255 254]
[298 203 324 347 371 183]
[260 176 286 286 300 219] 

Block matrix = 
[333 273 322 379 290 264]
[243 188 247 275 227 217]
[178 119 190 215 214 143]
[232 169 234 276 180 212]
[266 193 253 252 255 254]
[298 203 324 347 371 183]
[260 176 286 286 300 219]

CALCULATION #2

# Generate random matrix A = random_matrix(ZZ, 5, x=0, y=10) # 5x5 matrix B = A # matrix B is the same as matrix A # Divide matrix A into 4 matrix blocks using matrix surgery A1 = A.submatrix(0, 0, 2, 3) # 2x3 matrix block A2 = A.submatrix(0, 3, 2, 2) # 2x2 matrix block A3 = A.submatrix(2, 0, 3, 3) # 3x3 matrix block A4 = A.submatrix(2, 3, 3, 2) # 3x2 matrix block # Divide matrix B into 4 matrix blocks using matrix surgery (divide it differently from matrix A) B1 = B.submatrix(0, 0, 3, 2) # 3x2 matrix block B2 = B.submatrix(0, 2, 3, 3) # 3x3 matrix block B3 = B.submatrix(3, 0, 2, 2) # 2x2 matrix block B4 = B.submatrix(3, 2, 2, 3) # 2x3 matrix block # Display the blocks of both matrices print "A=\n", A, "\n" print "B=\n", B, "\n" print "A1=\n", A1, "\n" print "A2=\n", A2, "\n" print "A3=\n", A3, "\n" print "A4=\n", A4, "\n\n" print "B1=\n", B1, "\n" print "B2=\n", B2, "\n" print "B3=\n", B3, "\n" print "B4=\n", B4, "\n" # Calculate the blocks for the new block matrix AB1 = A1*B1 + A2*B3 AB2 = A1*B2 + A2*B4 AB3 = A3*B1 + A4*B3 AB4 = A3*B2 + A4*B4 # Combine calculated blocks into a 5x5 matrix blockTop = (AB1).augment(AB2) blockBottom = (AB3).augment(AB4) blockMatrix = (blockTop).stack(blockBottom) # Calculate AB using regular matrix multiplication AB = A*B # Display matrices print "AB = \n", AB, "\n" print "Block matrix = \n", blockMatrix 
       
A=
[9 4 6 4 4]
[0 4 8 0 8]
[4 4 6 9 6]
[7 9 3 6 2]
[7 5 7 9 1] 

B=
[9 4 6 4 4]
[0 4 8 0 8]
[4 4 6 9 6]
[7 9 3 6 2]
[7 5 7 9 1] 

A1=
[9 4 6]
[0 4 8] 

A2=
[4 4]
[0 8] 

A3=
[4 4 6]
[7 9 3]
[7 5 7] 

A4=
[9 6]
[6 2]
[9 1] 


B1=
[9 4]
[0 4]
[4 4] 

B2=
[6 4 4]
[8 0 8]
[6 9 6] 

B3=
[7 9]
[7 5] 

B4=
[3 6 2]
[7 9 1] 

AB = 
[161 132 162 150 116]
[ 88  88 136 144  88]
[165 167 161 178 108]
[131 140 164 109 132]
[161 162 158 154 129] 

Block matrix = 
[161 132 162 150 116]
[ 88  88 136 144  88]
[165 167 161 178 108]
[131 140 164 109 132]
[161 162 158 154 129]
A=
[9 4 6 4 4]
[0 4 8 0 8]
[4 4 6 9 6]
[7 9 3 6 2]
[7 5 7 9 1] 

B=
[9 4 6 4 4]
[0 4 8 0 8]
[4 4 6 9 6]
[7 9 3 6 2]
[7 5 7 9 1] 

A1=
[9 4 6]
[0 4 8] 

A2=
[4 4]
[0 8] 

A3=
[4 4 6]
[7 9 3]
[7 5 7] 

A4=
[9 6]
[6 2]
[9 1] 


B1=
[9 4]
[0 4]
[4 4] 

B2=
[6 4 4]
[8 0 8]
[6 9 6] 

B3=
[7 9]
[7 5] 

B4=
[3 6 2]
[7 9 1] 

AB = 
[161 132 162 150 116]
[ 88  88 136 144  88]
[165 167 161 178 108]
[131 140 164 109 132]
[161 162 158 154 129] 

Block matrix = 
[161 132 162 150 116]
[ 88  88 136 144  88]
[165 167 161 178 108]
[131 140 164 109 132]
[161 162 158 154 129]

CALCULATION #3

# Generate random matrices A = random_matrix(ZZ, 8, x=0, y=10) # 8x8 matrix B = random_matrix(ZZ, 8, x=0, y=10) # 8x8 matrix # Divide matrix A into 4 4x4 matrix blocks using matrix surgery A1 = A.submatrix(0, 0, 4, 4) A2 = A.submatrix(0, 4, 4, 4) A3 = A.submatrix(4, 0, 4, 4) A4 = A.submatrix(4, 4, 4, 4) # Divide matrix B into 4 4x4 matrix blocks using matrix surgery B1 = B.submatrix(0, 0, 4, 4) B2 = B.submatrix(0, 4, 4, 4) B3 = B.submatrix(4, 0, 4, 4) B4 = B.submatrix(4, 4, 4, 4) # Display the blocks of the both matrices print "A=\n", A, "\n" print "B=\n", B, "\n" print "A1=\n", A1, "\n" print "A2=\n", A2, "\n" print "A3=\n", A3, "\n" print "A4=\n", A4, "\n\n" print "B1=\n", B1, "\n" print "B2=\n", B2, "\n" print "B3=\n", B3, "\n" print "B4=\n", B4, "\n" # Calculate the blocks for the new block matrix AB1 = A1*B1 + A2*B3 AB2 = A1*B2 + A2*B4 AB3 = A3*B1 + A4*B3 AB4 = A3*B2 + A4*B4 # Combine calculated blocks into an 8x8 matrix blockTop = (AB1).augment(AB2) blockBottom = (AB3).augment(AB4) blockMatrix = (blockTop).stack(blockBottom) # Calculate AB using regular matrix multiplication AB = A*B # Display matrices print "AB = \n", AB, "\n" print "Block matrix = \n", blockMatrix 
       
A=
[1 3 5 0 9 5 2 0]
[1 0 2 4 6 0 8 5]
[0 2 0 3 7 8 4 4]
[9 6 6 1 5 9 5 6]
[8 5 9 7 0 6 1 1]
[2 4 7 7 3 7 6 7]
[6 7 3 2 1 7 6 9]
[5 2 1 5 3 9 0 0] 

B=
[6 6 2 0 4 4 6 9]
[2 9 9 2 1 2 8 7]
[2 9 2 1 5 6 6 8]
[7 7 5 5 2 3 0 0]
[4 1 3 4 2 4 6 4]
[1 9 2 5 4 1 6 2]
[2 3 1 5 0 0 3 0]
[2 5 4 7 8 5 9 9] 

A1=
[1 3 5 0]
[1 0 2 4]
[0 2 0 3]
[9 6 6 1] 

A2=
[9 5 2 0]
[6 0 8 5]
[7 8 4 4]
[5 9 5 6] 

A3=
[8 5 9 7]
[2 4 7 7]
[6 7 3 2]
[5 2 1 5] 

A4=
[0 6 1 1]
[3 7 6 7]
[1 7 6 9]
[3 9 0 0] 


B1=
[6 6 2 0]
[2 9 9 2]
[2 9 2 1]
[7 7 5 5] 

B2=
[4 4 6 9]
[1 2 8 7]
[5 6 6 8]
[2 3 0 0] 

B3=
[4 1 3 4]
[1 9 2 5]
[2 3 1 5]
[2 5 4 7] 

B4=
[2 4 6 4]
[4 1 6 2]
[0 0 3 0]
[8 5 9 9] 

AB = 
[ 67 138  78  82  70  81 150 116]
[ 88 107  72 121  74  77 123  94]
[ 77 150  90 135  86  69 154  94]
[136 300 151 155 168 146 291 263]
[135 285 131  96 128 128 190 200]
[128 279 146 176 151 133 227 191]
[111 267 150 159 152 118 257 226]
[ 92 176  82  87  79  66 124  97] 

Block matrix = 
[ 67 138  78  82  70  81 150 116]
[ 88 107  72 121  74  77 123  94]
[ 77 150  90 135  86  69 154  94]
[136 300 151 155 168 146 291 263]
[135 285 131  96 128 128 190 200]
[128 279 146 176 151 133 227 191]
[111 267 150 159 152 118 257 226]
[ 92 176  82  87  79  66 124  97]
A=
[1 3 5 0 9 5 2 0]
[1 0 2 4 6 0 8 5]
[0 2 0 3 7 8 4 4]
[9 6 6 1 5 9 5 6]
[8 5 9 7 0 6 1 1]
[2 4 7 7 3 7 6 7]
[6 7 3 2 1 7 6 9]
[5 2 1 5 3 9 0 0] 

B=
[6 6 2 0 4 4 6 9]
[2 9 9 2 1 2 8 7]
[2 9 2 1 5 6 6 8]
[7 7 5 5 2 3 0 0]
[4 1 3 4 2 4 6 4]
[1 9 2 5 4 1 6 2]
[2 3 1 5 0 0 3 0]
[2 5 4 7 8 5 9 9] 

A1=
[1 3 5 0]
[1 0 2 4]
[0 2 0 3]
[9 6 6 1] 

A2=
[9 5 2 0]
[6 0 8 5]
[7 8 4 4]
[5 9 5 6] 

A3=
[8 5 9 7]
[2 4 7 7]
[6 7 3 2]
[5 2 1 5] 

A4=
[0 6 1 1]
[3 7 6 7]
[1 7 6 9]
[3 9 0 0] 


B1=
[6 6 2 0]
[2 9 9 2]
[2 9 2 1]
[7 7 5 5] 

B2=
[4 4 6 9]
[1 2 8 7]
[5 6 6 8]
[2 3 0 0] 

B3=
[4 1 3 4]
[1 9 2 5]
[2 3 1 5]
[2 5 4 7] 

B4=
[2 4 6 4]
[4 1 6 2]
[0 0 3 0]
[8 5 9 9] 

AB = 
[ 67 138  78  82  70  81 150 116]
[ 88 107  72 121  74  77 123  94]
[ 77 150  90 135  86  69 154  94]
[136 300 151 155 168 146 291 263]
[135 285 131  96 128 128 190 200]
[128 279 146 176 151 133 227 191]
[111 267 150 159 152 118 257 226]
[ 92 176  82  87  79  66 124  97] 

Block matrix = 
[ 67 138  78  82  70  81 150 116]
[ 88 107  72 121  74  77 123  94]
[ 77 150  90 135  86  69 154  94]
[136 300 151 155 168 146 291 263]
[135 285 131  96 128 128 190 200]
[128 279 146 176 151 133 227 191]
[111 267 150 159 152 118 257 226]
[ 92 176  82  87  79  66 124  97]

c)

# Generate random matrices A = random_matrix(ZZ, 2, 3, x=0, y=10) # 2x3 matrix B = random_matrix(ZZ, 3, x=0, y=10) # 3x3 matrix # Divide matrix A into 4 matrix blocks using matrix surgery A1 = A.submatrix(0, 0, 1, 2) # 1x2 matrix block A2 = A.submatrix(0, 2, 1, 1) # 1x1 matrix block A3 = A.submatrix(1, 0, 1, 2) # 1x2 matrix block A4 = A.submatrix(1, 2, 1, 1) # 1x1 matrix block # Divide matrix B into 4 matrix blocks using matrix surgery B1 = B.submatrix(0, 0, 2, 1) # 2x1 matrix block B2 = B.submatrix(0, 1, 2, 2) # 2x2 matrix block B3 = B.submatrix(2, 0, 1, 1) # 1x1 matrix block B4 = B.submatrix(2, 1, 1, 2) # 1x2 matrix block # Display the blocks of both matrices print "A=\n", A, "\n" print "B=\n", B, "\n" print "A1=\n", A1, "\n" print "A2=\n", A2, "\n" print "A3=\n", A3, "\n" print "A4=\n", A4, "\n\n" print "B1=\n", B1, "\n" print "B2=\n", B2, "\n" print "B3=\n", B3, "\n" print "B4=\n", B4, "\n" # Calculate the blocks for the new block matrix AB1 = A1*B1 + A2*B3 AB2 = A1*B2 + A2*B4 AB3 = A3*B1 + A4*B3 AB4 = A3*B2 + A4*B4 # Combine calculated blocks into a 6x6 matrix blockTop = (AB1).augment(AB2) blockBottom = (AB3).augment(AB4) blockMatrix = (blockTop).stack(blockBottom) # Calculate AB using regular matrix multiplication AB = A*B # Display matrices print "AB = \n", AB, "\n" print "Block matrix = \n", blockMatrix 
       
A=
[1 2 2]
[5 1 3] 

B=
[3 4 4]
[5 7 6]
[2 0 2] 

A1=
[1 2] 

A2=
[2] 

A3=
[5 1] 

A4=
[3] 


B1=
[3]
[5] 

B2=
[4 4]
[7 6] 

B3=
[2] 

B4=
[0 2] 

AB = 
[17 18 20]
[26 27 32] 

Block matrix = 
[17 18 20]
[26 27 32]
A=
[1 2 2]
[5 1 3] 

B=
[3 4 4]
[5 7 6]
[2 0 2] 

A1=
[1 2] 

A2=
[2] 

A3=
[5 1] 

A4=
[3] 


B1=
[3]
[5] 

B2=
[4 4]
[7 6] 

B3=
[2] 

B4=
[0 2] 

AB = 
[17 18 20]
[26 27 32] 

Block matrix = 
[17 18 20]
[26 27 32]

Exercise #3


a)

# Function definition for making a n x n Hilbert matrix def hilbert_matrix(n): return matrix([[1/i for i in [j..j+(n-1)]] for j in [1..n]]) A = hilbert_matrix(8).change_ring(RR) B = A.inverse() for i in range(8, 13): A = hilbert_matrix(i).change_ring(RR) B = A.inverse() print "***For a", i,"x",i, "hilbert matrix***\n" print "AB =" print (A*B),"\n" print "(AB)11 - (I)11 = ", print AB[0][0] - identity_matrix(i)[0][0], "\n\n" 
       
***For a 8 x 8 hilbert matrix***

AB =
[    0.999999999949068   2.03726813197136e-9  -3.81842255592346e-8  
1.45286321640015e-7  -4.02331352233887e-7   6.25848770141602e-7 
-4.76837158203125e-7   1.37835741043091e-7]
[-3.36513039655983e-11      1.00000000133878  -2.04890966415405e-8  
1.22934579849243e-7  -3.27825546264648e-7   3.57627868652344e-7 
-2.98023223876953e-7   8.56816768646240e-8]
[-2.18278728425503e-11  7.56699591875076e-10     0.999999983236194  
6.33299350738525e-8  -1.86264514923096e-7   2.08616256713867e-7 
-1.93715095520020e-7   5.96046447753906e-8]
[-1.72803993336856e-11  5.52972778677940e-10  -1.07102096080780e-8     
1.00000004097819  -5.21540641784668e-8   1.63912773132324e-7 
-8.19563865661621e-8   4.09781932830811e-8]
[-1.09139364212751e-11  2.91038304567337e-10  -9.31322574615479e-9  
3.72529029846191e-8     0.999999940395355   1.34110450744629e-7 
-7.45058059692383e-8   3.72529029846191e-8]
[-1.04591890703887e-11  5.52972778677940e-10  -1.21071934700012e-8  
3.72529029846191e-8  -5.96046447753906e-8      1.00000011920929 
-8.19563865661621e-8   4.09781932830811e-8]
[-1.36424205265939e-11  4.07453626394272e-10  -8.38190317153931e-9  
2.60770320892334e-8  -7.45058059692383e-8   1.86264514923096e-7    
0.999999947845936   2.23517417907715e-8]
[-5.45696821063757e-12  1.16415321826935e-10  -4.65661287307739e-9  
9.31322574615479e-9  -3.72529029846191e-8   7.45058059692383e-8 
-4.47034835815430e-8      1.00000001117587] 

(AB)11 - (I)11 =  -4.52622771263123e-7 


***For a 9 x 9 hilbert matrix***

AB =
[     0.999999999476131    3.65544110536575e-8   -6.51925802230835e-7   
3.84449958801270e-6 -0.0000170469284057617  0.0000391006469726562
-0.0000398159027099609  0.0000252723693847656   -6.02006912231445e-6]
[ -4.22005541622639e-10       1.00000002630986   -5.02914190292358e-7   
2.80141830444336e-6 -0.0000116825103759766  0.0000278949737548828
-0.0000274181365966797  0.0000169277191162109   -4.35113906860352e-6]
[ -3.09228198602796e-10    1.95577740669250e-8      0.999999668449163   
2.05636024475098e-6   -8.70227813720703e-6  0.0000212192535400391
-0.0000195503234863281  0.0000126361846923828   -3.39746475219727e-6]
[ -2.25554686039686e-10    1.69966369867325e-8   -3.09199094772339e-7   
1.00000140070915   -7.15255737304688e-6  0.0000185966491699219
-0.0000178813934326172  0.0000109672546386719   -2.83122062683105e-6]
[ -2.07364792004228e-10    1.25728547573090e-8   -2.53319740295410e-7   
1.01327896118164e-6      0.999994397163391  0.0000135898590087891
-0.0000154972076416016    9.29832458496094e-6   -2.23517417907715e-6]
[ -1.94631866179407e-10    1.21071934700012e-8   -2.08616256713867e-7   
1.25169754028320e-6   -5.96046447753906e-6       1.00001263618469
-0.0000121593475341797    8.10623168945312e-6   -2.11596488952637e-6]
[ -1.58252078108490e-10    1.02445483207703e-8   -2.01165676116943e-7   
9.23871994018555e-7   -4.47034835815430e-6  0.0000129938125610352     
0.999988079071045    7.39097595214844e-6   -1.66893005371094e-6]
[ -1.50976120494306e-10    8.49831849336624e-9   -1.63912773132324e-7   
7.74860382080078e-7   -4.17232513427734e-6    9.65595245361328e-6
-0.0000100135803222656       1.00000607967377   -1.57952308654785e-6]
[ -1.52795109897852e-10    8.14907252788544e-9   -1.60187482833862e-7   
8.64267349243164e-7   -3.69548797607422e-6  0.0000101327896118164  
-8.58306884765625e-6    5.00679016113281e-6      0.999998450279236] 

(AB)11 - (I)11 =  -4.52622771263123e-7 


***For a 10 x 10 hilbert matrix***

AB =
[     0.999999994382961    4.88944351673126e-7 -0.0000110566616058350 
0.0000932216644287109  -0.000480651855468750    0.00126647949218750  
-0.00211334228515625    0.00212860107421875   -0.00112152099609375  
0.000254631042480469]
[  -4.35102265328169e-9       1.00000037997961   -8.43405723571777e-6 
0.0000715255737304688  -0.000368118286132812   0.000957489013671875  
-0.00163269042968750    0.00161743164062500  -0.000846862792968750  
0.000194549560546875]
[  -3.49245965480804e-9    2.90572643280029e-7      0.999993264675140 
0.0000576972961425781  -0.000279426574707031   0.000782012939453125  
-0.00128173828125000    0.00129699707031250  -0.000671386718750000  
0.000161170959472656]
[  -2.93948687613010e-9    2.50525772571564e-7   -5.72204589843750e-6   
1.00004911422729  -0.000253677368164062   0.000644683837890625  
-0.00107574462890625    0.00114440917968750  -0.000583648681640625  
0.000142097473144531]
[  -2.59024091064930e-9    2.26311385631561e-7   -4.93228435516357e-6 
0.0000422000885009766      0.999775886535645   0.000597000122070312 
-0.000991821289062500   0.000980377197265625  -0.000514984130859375  
0.000116348266601562]
[  -2.36468622460961e-9    1.96509063243866e-7   -4.33623790740967e-6 
0.0000379085540771484  -0.000193595886230469       1.00051689147949 
-0.000877380371093750   0.000862121582031250  -0.000452041625976562  
0.000104904174804688]
[  -2.06637196242809e-9    1.75088644027710e-7   -3.91900539398193e-6 
0.0000355243682861328  -0.000173568725585938   0.000461578369140625     
0.999244689941406   0.000774383544921875  -0.000411987304687500 
0.0000953674316406250]
[  -1.84081727638841e-9    1.62981450557709e-7   -3.54647636413574e-6 
0.0000299215316772461  -0.000161170959472656   0.000413894653320312 
-0.000694274902343750       1.00075912475586  -0.000368118286132812 
0.0000886917114257812]
[  -1.65891833603382e-9    1.46217644214630e-7   -3.51667404174805e-6 
0.0000286102294921875  -0.000146865844726562   0.000375747680664062 
-0.000648498535156250   0.000671386718750000      0.999656677246094 
0.0000767707824707031]
[  -1.49884726852179e-9    1.24797224998474e-7   -2.90572643280029e-6 
0.0000237226486206055  -0.000122070312500000   0.000316619873046875 
-0.000541687011718750   0.000572204589843750  -0.000295639038085938     
1.00006961822510] 

(AB)11 - (I)11 =  -4.52622771263123e-7 


***For a 11 x 11 hilbert matrix***

AB =
[     0.999999948835466    5.55813312530518e-6  -0.000151157379150391   
0.00175666809082031    -0.0100860595703125     0.0374450683593750   
-0.0812988281250000      0.109130859375000    -0.0935058593750000    
0.0443725585937500   -0.00860595703125000]
[  -4.03379090130329e-8       1.00000444054604  -0.000122785568237305   
0.00138473510742188   -0.00817871093750000     0.0292968750000000   
-0.0629882812500000     0.0874023437500000    -0.0745849609375000    
0.0355224609375000   -0.00682830810546875]
[  -3.42261046171188e-8    3.67686152458191e-6      0.999900102615356   
0.00115394592285156   -0.00648498535156250     0.0242919921875000   
-0.0527343750000000     0.0717773437500000    -0.0628662109375000    
0.0291137695312500   -0.00565338134765625]
[  -2.86381691694260e-8    3.14787030220032e-6 -0.0000857114791870117   
1.00096511840820   -0.00578308105468750     0.0215454101562500   
-0.0466308593750000     0.0616455078125000    -0.0517578125000000    
0.0255126953125000   -0.00487518310546875]
[  -2.52330210059881e-8    2.81631946563721e-6 -0.0000758171081542969  
0.000877380371093750      0.994888305664062     0.0185241699218750   
-0.0413208007812500     0.0546875000000000    -0.0476074218750000    
0.0225524902343750   -0.00438690185546875]
[  -2.24972609430552e-8    2.49221920967102e-6 -0.0000698566436767578  
0.000775337219238281   -0.00448608398437500       1.01708984375000   
-0.0362548828125000     0.0491943359375000    -0.0422363281250000    
0.0202941894531250   -0.00385284423828125]
[  -2.05764081329107e-8    2.21282243728638e-6 -0.0000618696212768555  
0.000711441040039062   -0.00408172607421875     0.0150146484375000     
0.966735839843750     0.0440673828125000    -0.0374145507812500    
0.0180053710937500   -0.00351715087890625]
[  -1.86555553227663e-8    2.01538205146790e-6 -0.0000551939010620117  
0.000661849975585938   -0.00367736816406250     0.0137939453125000   
-0.0306396484375000       1.03991699218750    -0.0350952148437500    
0.0166931152343750   -0.00316619873046875]
[  -1.71421561390162e-8    1.85146927833557e-6 -0.0000511407852172852  
0.000595092773437500   -0.00344085693359375     0.0129089355468750   
-0.0272827148437500     0.0374145507812500      0.967773437500000    
0.0153503417968750   -0.00293731689453125]
[  -1.52504071593285e-8    1.62422657012939e-6 -0.0000468492507934570  
0.000545501708984375   -0.00308990478515625     0.0112915039062500   
-0.0244750976562500     0.0332031250000000    -0.0287475585937500      
1.01385498046875   -0.00255584716796875]
[  -1.51921994984150e-8    1.67265534400940e-6 -0.0000469684600830078  
0.000529289245605469   -0.00308990478515625     0.0115966796875000   
-0.0243530273437500     0.0329589843750000    -0.0286865234375000    
0.0137634277343750      0.997409820556641] 

(AB)11 - (I)11 =  -4.52622771263123e-7 


***For a 12 x 12 hilbert matrix***

AB =
[    0.999999547377229 0.0000594854354858398  -0.00183391571044922   
0.0258941650390625    -0.193847656250000     0.862304687500000    
-2.37207031250000      4.33984375000000     -5.11132812500000     
3.79492187500000     -1.54980468750000     0.283203125000000]
[ -3.67872416973114e-7      1.00004822015762  -0.00149536132812500   
0.0208282470703125    -0.157775878906250     0.703125000000000    
-1.91894531250000      3.57812500000000     -4.11914062500000     
3.08789062500000     -1.27441406250000     0.232788085937500]
[ -3.09664756059647e-7 0.0000409781932830811     0.998748779296875   
0.0177688598632812    -0.132507324218750     0.596435546875000    
-1.62500000000000      3.01757812500000     -3.53515625000000     
2.59179687500000     -1.07421875000000     0.195434570312500]
[ -2.74158082902431e-7 0.0000355243682861328  -0.00110483169555664     
1.01567840576172    -0.116882324218750     0.518554687500000    
-1.42480468750000      2.61132812500000     -3.09179687500000     
2.26953125000000    -0.923828125000000     0.171630859375000]
[ -2.42376700043678e-7 0.0000318288803100586 -0.000974178314208984   
0.0140380859375000     0.896667480468750     0.462890625000000    
-1.27929687500000      2.33789062500000     -2.76367187500000     
2.02832031250000    -0.835449218750000     0.153808593750000]
[ -2.18977220356464e-7 0.0000284612178802490 -0.000881671905517578   
0.0122528076171875   -0.0919189453125000      1.41992187500000    
-1.16406250000000      2.11718750000000     -2.44140625000000     
1.83007812500000    -0.751953125000000     0.137817382812500]
[ -2.00816430151463e-7 0.0000259429216384888 -0.000795364379882812   
0.0114059448242188   -0.0855102539062500     0.381591796875000  
-0.0556640625000000      1.91406250000000     -2.22265625000000     
1.65234375000000    -0.673828125000000     0.124145507812500]
[ -1.83936208486557e-7 0.0000238567590713501 -0.000733852386474609   
0.0102844238281250   -0.0788574218750000     0.348632812500000   
-0.964843750000000      2.75878906250000     -2.05664062500000     
1.53320312500000    -0.625488281250000     0.113891601562500]
[ -1.70315615832806e-7 0.0000225305557250977 -0.000677108764648438  
0.00964355468750000   -0.0725097656250000     0.319335937500000   
-0.900390625000000      1.66406250000000    -0.925781250000000     
1.43066406250000    -0.590332031250000     0.107055664062500]
[ -1.52387656271458e-7 0.0000205636024475098 -0.000630378723144531  
0.00892639160156250   -0.0670776367187500     0.294677734375000   
-0.817382812500000      1.48046875000000     -1.74218750000000     
2.30371093750000    -0.532226562500000    0.0985107421875000]
[ -1.52620486915112e-7 0.0000196695327758789 -0.000599384307861328  
0.00843048095703125   -0.0640869140625000     0.286376953125000   
-0.789550781250000      1.45117187500000     -1.66113281250000     
1.25488281250000     0.490722656250000    0.0959472656250000]
[ -1.34808942675591e-7 0.0000179857015609741 -0.000552654266357422  
0.00791931152343750   -0.0593261718750000     0.263183593750000   
-0.714355468750000      1.32128906250000     -1.52636718750000     
1.15136718750000    -0.467285156250000      1.08709716796875] 

(AB)11 - (I)11 =  -4.52622771263123e-7 
***For a 8 x 8 hilbert matrix***

AB =
[    0.999999999949068   2.03726813197136e-9  -3.81842255592346e-8   1.45286321640015e-7  -4.02331352233887e-7   6.25848770141602e-7  -4.76837158203125e-7   1.37835741043091e-7]
[-3.36513039655983e-11      1.00000000133878  -2.04890966415405e-8   1.22934579849243e-7  -3.27825546264648e-7   3.57627868652344e-7  -2.98023223876953e-7   8.56816768646240e-8]
[-2.18278728425503e-11  7.56699591875076e-10     0.999999983236194   6.33299350738525e-8  -1.86264514923096e-7   2.08616256713867e-7  -1.93715095520020e-7   5.96046447753906e-8]
[-1.72803993336856e-11  5.52972778677940e-10  -1.07102096080780e-8      1.00000004097819  -5.21540641784668e-8   1.63912773132324e-7  -8.19563865661621e-8   4.09781932830811e-8]
[-1.09139364212751e-11  2.91038304567337e-10  -9.31322574615479e-9   3.72529029846191e-8     0.999999940395355   1.34110450744629e-7  -7.45058059692383e-8   3.72529029846191e-8]
[-1.04591890703887e-11  5.52972778677940e-10  -1.21071934700012e-8   3.72529029846191e-8  -5.96046447753906e-8      1.00000011920929  -8.19563865661621e-8   4.09781932830811e-8]
[-1.36424205265939e-11  4.07453626394272e-10  -8.38190317153931e-9   2.60770320892334e-8  -7.45058059692383e-8   1.86264514923096e-7     0.999999947845936   2.23517417907715e-8]
[-5.45696821063757e-12  1.16415321826935e-10  -4.65661287307739e-9   9.31322574615479e-9  -3.72529029846191e-8   7.45058059692383e-8  -4.47034835815430e-8      1.00000001117587] 

(AB)11 - (I)11 =  -4.52622771263123e-7 


***For a 9 x 9 hilbert matrix***

AB =
[     0.999999999476131    3.65544110536575e-8   -6.51925802230835e-7    3.84449958801270e-6 -0.0000170469284057617  0.0000391006469726562 -0.0000398159027099609  0.0000252723693847656   -6.02006912231445e-6]
[ -4.22005541622639e-10       1.00000002630986   -5.02914190292358e-7    2.80141830444336e-6 -0.0000116825103759766  0.0000278949737548828 -0.0000274181365966797  0.0000169277191162109   -4.35113906860352e-6]
[ -3.09228198602796e-10    1.95577740669250e-8      0.999999668449163    2.05636024475098e-6   -8.70227813720703e-6  0.0000212192535400391 -0.0000195503234863281  0.0000126361846923828   -3.39746475219727e-6]
[ -2.25554686039686e-10    1.69966369867325e-8   -3.09199094772339e-7       1.00000140070915   -7.15255737304688e-6  0.0000185966491699219 -0.0000178813934326172  0.0000109672546386719   -2.83122062683105e-6]
[ -2.07364792004228e-10    1.25728547573090e-8   -2.53319740295410e-7    1.01327896118164e-6      0.999994397163391  0.0000135898590087891 -0.0000154972076416016    9.29832458496094e-6   -2.23517417907715e-6]
[ -1.94631866179407e-10    1.21071934700012e-8   -2.08616256713867e-7    1.25169754028320e-6   -5.96046447753906e-6       1.00001263618469 -0.0000121593475341797    8.10623168945312e-6   -2.11596488952637e-6]
[ -1.58252078108490e-10    1.02445483207703e-8   -2.01165676116943e-7    9.23871994018555e-7   -4.47034835815430e-6  0.0000129938125610352      0.999988079071045    7.39097595214844e-6   -1.66893005371094e-6]
[ -1.50976120494306e-10    8.49831849336624e-9   -1.63912773132324e-7    7.74860382080078e-7   -4.17232513427734e-6    9.65595245361328e-6 -0.0000100135803222656       1.00000607967377   -1.57952308654785e-6]
[ -1.52795109897852e-10    8.14907252788544e-9   -1.60187482833862e-7    8.64267349243164e-7   -3.69548797607422e-6  0.0000101327896118164   -8.58306884765625e-6    5.00679016113281e-6      0.999998450279236] 

(AB)11 - (I)11 =  -4.52622771263123e-7 


***For a 10 x 10 hilbert matrix***

AB =
[     0.999999994382961    4.88944351673126e-7 -0.0000110566616058350  0.0000932216644287109  -0.000480651855468750    0.00126647949218750   -0.00211334228515625    0.00212860107421875   -0.00112152099609375   0.000254631042480469]
[  -4.35102265328169e-9       1.00000037997961   -8.43405723571777e-6  0.0000715255737304688  -0.000368118286132812   0.000957489013671875   -0.00163269042968750    0.00161743164062500  -0.000846862792968750   0.000194549560546875]
[  -3.49245965480804e-9    2.90572643280029e-7      0.999993264675140  0.0000576972961425781  -0.000279426574707031   0.000782012939453125   -0.00128173828125000    0.00129699707031250  -0.000671386718750000   0.000161170959472656]
[  -2.93948687613010e-9    2.50525772571564e-7   -5.72204589843750e-6       1.00004911422729  -0.000253677368164062   0.000644683837890625   -0.00107574462890625    0.00114440917968750  -0.000583648681640625   0.000142097473144531]
[  -2.59024091064930e-9    2.26311385631561e-7   -4.93228435516357e-6  0.0000422000885009766      0.999775886535645   0.000597000122070312  -0.000991821289062500   0.000980377197265625  -0.000514984130859375   0.000116348266601562]
[  -2.36468622460961e-9    1.96509063243866e-7   -4.33623790740967e-6  0.0000379085540771484  -0.000193595886230469       1.00051689147949  -0.000877380371093750   0.000862121582031250  -0.000452041625976562   0.000104904174804688]
[  -2.06637196242809e-9    1.75088644027710e-7   -3.91900539398193e-6  0.0000355243682861328  -0.000173568725585938   0.000461578369140625      0.999244689941406   0.000774383544921875  -0.000411987304687500  0.0000953674316406250]
[  -1.84081727638841e-9    1.62981450557709e-7   -3.54647636413574e-6  0.0000299215316772461  -0.000161170959472656   0.000413894653320312  -0.000694274902343750       1.00075912475586  -0.000368118286132812  0.0000886917114257812]
[  -1.65891833603382e-9    1.46217644214630e-7   -3.51667404174805e-6  0.0000286102294921875  -0.000146865844726562   0.000375747680664062  -0.000648498535156250   0.000671386718750000      0.999656677246094  0.0000767707824707031]
[  -1.49884726852179e-9    1.24797224998474e-7   -2.90572643280029e-6  0.0000237226486206055  -0.000122070312500000   0.000316619873046875  -0.000541687011718750   0.000572204589843750  -0.000295639038085938       1.00006961822510] 

(AB)11 - (I)11 =  -4.52622771263123e-7 


***For a 11 x 11 hilbert matrix***

AB =
[     0.999999948835466    5.55813312530518e-6  -0.000151157379150391    0.00175666809082031    -0.0100860595703125     0.0374450683593750    -0.0812988281250000      0.109130859375000    -0.0935058593750000     0.0443725585937500   -0.00860595703125000]
[  -4.03379090130329e-8       1.00000444054604  -0.000122785568237305    0.00138473510742188   -0.00817871093750000     0.0292968750000000    -0.0629882812500000     0.0874023437500000    -0.0745849609375000     0.0355224609375000   -0.00682830810546875]
[  -3.42261046171188e-8    3.67686152458191e-6      0.999900102615356    0.00115394592285156   -0.00648498535156250     0.0242919921875000    -0.0527343750000000     0.0717773437500000    -0.0628662109375000     0.0291137695312500   -0.00565338134765625]
[  -2.86381691694260e-8    3.14787030220032e-6 -0.0000857114791870117       1.00096511840820   -0.00578308105468750     0.0215454101562500    -0.0466308593750000     0.0616455078125000    -0.0517578125000000     0.0255126953125000   -0.00487518310546875]
[  -2.52330210059881e-8    2.81631946563721e-6 -0.0000758171081542969   0.000877380371093750      0.994888305664062     0.0185241699218750    -0.0413208007812500     0.0546875000000000    -0.0476074218750000     0.0225524902343750   -0.00438690185546875]
[  -2.24972609430552e-8    2.49221920967102e-6 -0.0000698566436767578   0.000775337219238281   -0.00448608398437500       1.01708984375000    -0.0362548828125000     0.0491943359375000    -0.0422363281250000     0.0202941894531250   -0.00385284423828125]
[  -2.05764081329107e-8    2.21282243728638e-6 -0.0000618696212768555   0.000711441040039062   -0.00408172607421875     0.0150146484375000      0.966735839843750     0.0440673828125000    -0.0374145507812500     0.0180053710937500   -0.00351715087890625]
[  -1.86555553227663e-8    2.01538205146790e-6 -0.0000551939010620117   0.000661849975585938   -0.00367736816406250     0.0137939453125000    -0.0306396484375000       1.03991699218750    -0.0350952148437500     0.0166931152343750   -0.00316619873046875]
[  -1.71421561390162e-8    1.85146927833557e-6 -0.0000511407852172852   0.000595092773437500   -0.00344085693359375     0.0129089355468750    -0.0272827148437500     0.0374145507812500      0.967773437500000     0.0153503417968750   -0.00293731689453125]
[  -1.52504071593285e-8    1.62422657012939e-6 -0.0000468492507934570   0.000545501708984375   -0.00308990478515625     0.0112915039062500    -0.0244750976562500     0.0332031250000000    -0.0287475585937500       1.01385498046875   -0.00255584716796875]
[  -1.51921994984150e-8    1.67265534400940e-6 -0.0000469684600830078   0.000529289245605469   -0.00308990478515625     0.0115966796875000    -0.0243530273437500     0.0329589843750000    -0.0286865234375000     0.0137634277343750      0.997409820556641] 

(AB)11 - (I)11 =  -4.52622771263123e-7 


***For a 12 x 12 hilbert matrix***

AB =
[    0.999999547377229 0.0000594854354858398  -0.00183391571044922    0.0258941650390625    -0.193847656250000     0.862304687500000     -2.37207031250000      4.33984375000000     -5.11132812500000      3.79492187500000     -1.54980468750000     0.283203125000000]
[ -3.67872416973114e-7      1.00004822015762  -0.00149536132812500    0.0208282470703125    -0.157775878906250     0.703125000000000     -1.91894531250000      3.57812500000000     -4.11914062500000      3.08789062500000     -1.27441406250000     0.232788085937500]
[ -3.09664756059647e-7 0.0000409781932830811     0.998748779296875    0.0177688598632812    -0.132507324218750     0.596435546875000     -1.62500000000000      3.01757812500000     -3.53515625000000      2.59179687500000     -1.07421875000000     0.195434570312500]
[ -2.74158082902431e-7 0.0000355243682861328  -0.00110483169555664      1.01567840576172    -0.116882324218750     0.518554687500000     -1.42480468750000      2.61132812500000     -3.09179687500000      2.26953125000000    -0.923828125000000     0.171630859375000]
[ -2.42376700043678e-7 0.0000318288803100586 -0.000974178314208984    0.0140380859375000     0.896667480468750     0.462890625000000     -1.27929687500000      2.33789062500000     -2.76367187500000      2.02832031250000    -0.835449218750000     0.153808593750000]
[ -2.18977220356464e-7 0.0000284612178802490 -0.000881671905517578    0.0122528076171875   -0.0919189453125000      1.41992187500000     -1.16406250000000      2.11718750000000     -2.44140625000000      1.83007812500000    -0.751953125000000     0.137817382812500]
[ -2.00816430151463e-7 0.0000259429216384888 -0.000795364379882812    0.0114059448242188   -0.0855102539062500     0.381591796875000   -0.0556640625000000      1.91406250000000     -2.22265625000000      1.65234375000000    -0.673828125000000     0.124145507812500]
[ -1.83936208486557e-7 0.0000238567590713501 -0.000733852386474609    0.0102844238281250   -0.0788574218750000     0.348632812500000    -0.964843750000000      2.75878906250000     -2.05664062500000      1.53320312500000    -0.625488281250000     0.113891601562500]
[ -1.70315615832806e-7 0.0000225305557250977 -0.000677108764648438   0.00964355468750000   -0.0725097656250000     0.319335937500000    -0.900390625000000      1.66406250000000    -0.925781250000000      1.43066406250000    -0.590332031250000     0.107055664062500]
[ -1.52387656271458e-7 0.0000205636024475098 -0.000630378723144531   0.00892639160156250   -0.0670776367187500     0.294677734375000    -0.817382812500000      1.48046875000000     -1.74218750000000      2.30371093750000    -0.532226562500000    0.0985107421875000]
[ -1.52620486915112e-7 0.0000196695327758789 -0.000599384307861328   0.00843048095703125   -0.0640869140625000     0.286376953125000    -0.789550781250000      1.45117187500000     -1.66113281250000      1.25488281250000     0.490722656250000    0.0959472656250000]
[ -1.34808942675591e-7 0.0000179857015609741 -0.000552654266357422   0.00791931152343750   -0.0593261718750000     0.263183593750000    -0.714355468750000      1.32128906250000     -1.52636718750000      1.15136718750000    -0.467285156250000      1.08709716796875] 

(AB)11 - (I)11 =  -4.52622771263123e-7 

b)

A = hilbert_matrix(9) b = ones_matrix(QQ, 9, 1) u = A.inverse()*b print "u =\n", u, "\n" b[3,0] = 1.001 # change the fourth component to 1.001 v = A.inverse()*b print "v =\n", v.change_ring(RR),"\n" print "u-v =\n", (u-v).change_ring(RR) 
       
u =
[       9]
[    -720]
[   13860]
[ -110880]
[  450450]
[-1009008]
[ 1261260]
[ -823680]
[  218790] 

v =
[  -240.480000000000]
[   15246.7200000000]
[  -242272.800000000]
[ 1.64545920000000e6]
[-5.79278700000000e6]
[ 1.14219705600000e7]
[-1.27235908800000e7]
[ 7.47901440000000e6]
[-1.80282960000000e6] 

u-v =
[   249.480000000000]
[  -15966.7200000000]
[   256132.800000000]
[-1.75633920000000e6]
[ 6.24323700000000e6]
[-1.24309785600000e7]
[ 1.39848508800000e7]
[-8.30269440000000e6]
[ 2.02161960000000e6]
u =
[       9]
[    -720]
[   13860]
[ -110880]
[  450450]
[-1009008]
[ 1261260]
[ -823680]
[  218790] 

v =
[  -240.480000000000]
[   15246.7200000000]
[  -242272.800000000]
[ 1.64545920000000e6]
[-5.79278700000000e6]
[ 1.14219705600000e7]
[-1.27235908800000e7]
[ 7.47901440000000e6]
[-1.80282960000000e6] 

u-v =
[   249.480000000000]
[  -15966.7200000000]
[   256132.800000000]
[-1.75633920000000e6]
[ 6.24323700000000e6]
[-1.24309785600000e7]
[ 1.39848508800000e7]
[-8.30269440000000e6]
[ 2.02161960000000e6]

c)

b = ones_matrix(QQ, 9, 1) A = hilbert_matrix(9).change_ring(RR) w = A.solve_right(b) # rref(A, b) print "w =\n", w, "\n" print "u =\n", u, "\n" print "u-w =\n", (u-w).change_ring(RR) 
       
w =
[   8.99996023855113]
[  -719.997192559782]
[   13859.9516760646]
[  -110879.650532027]
[   450448.704905007]
[-1.00900533334011e6]
[ 1.26125691599743e6]
[  -823678.126182198]
[   218789.534675520] 

u =
[       9]
[    -720]
[   13860]
[ -110880]
[  450450]
[-1009008]
[ 1261260]
[ -823680]
[  218790] 

u-w =
[0.0000397614488729658]
[ -0.00280744021824830]
[   0.0483239354089164]
[   -0.349467973486753]
[     1.29509499319829]
[    -2.66665989405010]
[     3.08400257374160]
[    -1.87381780170836]
[    0.465324480232084]
w =
[   8.99996023855113]
[  -719.997192559782]
[   13859.9516760646]
[  -110879.650532027]
[   450448.704905007]
[-1.00900533334011e6]
[ 1.26125691599743e6]
[  -823678.126182198]
[   218789.534675520] 

u =
[       9]
[    -720]
[   13860]
[ -110880]
[  450450]
[-1009008]
[ 1261260]
[ -823680]
[  218790] 

u-w =
[0.0000397614488729658]
[ -0.00280744021824830]
[   0.0483239354089164]
[   -0.349467973486753]
[     1.29509499319829]
[    -2.66665989405010]
[     3.08400257374160]
[    -1.87381780170836]
[    0.465324480232084]

There is a great difference between vector u and vector v. They are completely two different vectors. The largest component (in absolute value) of the difference vector is the seventh component.


d)

# Function definition for finding the condition number of a matrix def cond(m,p=2): return m.inverse().norm(p) * m.norm(p) # Display table print "n\t||xc - xt|| / ||xt||\t(10^-17) * Cond(A)" for i in range(3, 10): A = hilbert_matrix(i) b = ones_matrix(QQ, i, 1) xt = A.inverse()*b xc = A.change_ring(RealField(53)).inverse() * b.change_ring(RealField(53)) A_cond = cond(A) print i, "\t", print (xc-xt).norm() / xt.norm(), "\t", print (10**-17 * A_cond) 
       
n	||xc - xt|| / ||xt||	(10^-17) * Cond(A)
3 	5.26710597287e-15 	5.24056777586e-15
4 	5.13676310306e-14 	1.55137387389e-13
5 	6.21261748026e-13 	4.76607250243e-12
6 	8.92938376224e-11 	1.49510586401e-10
7 	3.72077200048e-09 	4.75367354988e-09
8 	8.9210088462e-09 	1.52575757416e-07
9 	2.49788716617e-06 	4.93154926972e-06
n	||xc - xt|| / ||xt||	(10^-17) * Cond(A)
3 	5.26710597287e-15 	5.24056777586e-15
4 	5.13676310306e-14 	1.55137387389e-13
5 	6.21261748026e-13 	4.76607250243e-12
6 	8.92938376224e-11 	1.49510586401e-10
7 	3.72077200048e-09 	4.75367354988e-09
8 	8.9210088462e-09 	1.52575757416e-07
9 	2.49788716617e-06 	4.93154926972e-06
# Display xc and xt pairs for i in range(3, 10): A = hilbert_matrix(i) b = ones_matrix(QQ, i, 1) xt = A.inverse()*b xc = A.change_ring(RR).inverse() * b.change_ring(RR) print "***", i, "***\n", "xc =\n", xc, "\nxt =\n", xt, "\n" 
       
*** 3 ***
xc =
[ 3.00000000000003]
[-24.0000000000001]
[ 30.0000000000001] 
xt =
[  3]
[-24]
[ 30] 

*** 4 ***
xc =
[-3.99999999999972]
[ 59.9999999999961]
[-179.999999999990]
[ 139.999999999994] 
xt =
[  -4]
[  60]
[-180]
[ 140] 

*** 5 ***
xc =
[ 4.99999999999284]
[-119.999999999884]
[ 629.999999999534]
[-1119.99999999932]
[ 629.999999999687] 
xt =
[    5]
[ -120]
[  630]
[-1120]
[  630] 

*** 6 ***
xc =
[-6.00000000088448]
[ 210.000000026324]
[-1680.00000018231]
[ 5040.00000048196]
[-6300.00000053947]
[ 2772.00000021420] 
xt =
[   -6]
[  210]
[-1680]
[ 5040]
[-6300]
[ 2772] 

*** 7 ***
xc =
[ 7.00000004812682]
[-336.000001928245]
[ 3780.00001863018]
[-16800.0000725538]
[ 34650.0001331642]
[-33264.0001151711]
[ 12012.0000378471] 
xt =
[     7]
[  -336]
[  3780]
[-16800]
[ 34650]
[-33264]
[ 12012] 

*** 8 ***
xc =
[-8.00000025866757]
[ 504.000011667609]
[-7560.00013140589]
[ 46200.0006247163]
[-138600.001496553]
[ 216216.001904607]
[-168168.001228929]
[ 51480.0003165901] 
xt =
[     -8]
[    504]
[  -7560]
[  46200]
[-138600]
[ 216216]
[-168168]
[  51480] 

*** 9 ***
xc =
[   8.99996023674612]
[  -719.997192425653]
[   13859.9516739249]
[  -110879.650518417]
[   450448.704844475]
[-1.00900533320618e6]
[ 1.26125691587067e6]
[  -823678.126085281]
[   218789.534652233] 
xt =
[       9]
[    -720]
[   13860]
[ -110880]
[  450450]
[-1009008]
[ 1261260]
[ -823680]
[  218790] 
*** 3 ***
xc =
[ 3.00000000000003]
[-24.0000000000001]
[ 30.0000000000001] 
xt =
[  3]
[-24]
[ 30] 

*** 4 ***
xc =
[-3.99999999999972]
[ 59.9999999999961]
[-179.999999999990]
[ 139.999999999994] 
xt =
[  -4]
[  60]
[-180]
[ 140] 

*** 5 ***
xc =
[ 4.99999999999284]
[-119.999999999884]
[ 629.999999999534]
[-1119.99999999932]
[ 629.999999999687] 
xt =
[    5]
[ -120]
[  630]
[-1120]
[  630] 

*** 6 ***
xc =
[-6.00000000088448]
[ 210.000000026324]
[-1680.00000018231]
[ 5040.00000048196]
[-6300.00000053947]
[ 2772.00000021420] 
xt =
[   -6]
[  210]
[-1680]
[ 5040]
[-6300]
[ 2772] 

*** 7 ***
xc =
[ 7.00000004812682]
[-336.000001928245]
[ 3780.00001863018]
[-16800.0000725538]
[ 34650.0001331642]
[-33264.0001151711]
[ 12012.0000378471] 
xt =
[     7]
[  -336]
[  3780]
[-16800]
[ 34650]
[-33264]
[ 12012] 

*** 8 ***
xc =
[-8.00000025866757]
[ 504.000011667609]
[-7560.00013140589]
[ 46200.0006247163]
[-138600.001496553]
[ 216216.001904607]
[-168168.001228929]
[ 51480.0003165901] 
xt =
[     -8]
[    504]
[  -7560]
[  46200]
[-138600]
[ 216216]
[-168168]
[  51480] 

*** 9 ***
xc =
[   8.99996023674612]
[  -719.997192425653]
[   13859.9516739249]
[  -110879.650518417]
[   450448.704844475]
[-1.00900533320618e6]
[ 1.26125691587067e6]
[  -823678.126085281]
[   218789.534652233] 
xt =
[       9]
[    -720]
[   13860]
[ -110880]
[  450450]
[-1009008]
[ 1261260]
[ -823680]
[  218790] 

With many of the pairs, the magnitude of the relative error shows almost the exact number of correct places in xc. In other pairs, the number of correct places is almost 0 and the relative error does not seem to reflect the number of correct places although these pairs are very close in value.