In [1]:
import numpy as np
import numpy.linalg as nla

Let's make a random matrix $A$ and consider a vector of ones $x^{*}={\bf 1}$. Let's let $b=A*x^{*}$. Then we know that $x^{*}$ is the solution to $$ Ax^{*} = b $$

In [2]:
A = np.random.rand(10,10)
xstar = np.ones((10,))
b = A.dot(xstar)

Now, solve the problem with Numpy. We'll first print a measure (that we'll go over later).

In [3]:
x = nla.solve(A,b)
print("                  Sensitivity of A:", nla.cond(A))
print("maximum difference from x to xstar:", np.max(np.abs(x-xstar)))
                  Sensitivity of A: 30.0120972144
maximum difference from x to xstar: 2.22044604925e-15
In [8]:
for xi in x:
    print(repr(xi))
print(A)
1.0000000000000022
0.99999999999999944
0.99999999999999978
1.0000000000000004
0.99999999999999889
0.99999999999999967
0.99999999999999867
0.99999999999999911
1.0000000000000009
1.000000000000002
[[ 0.32011848  0.22375283  0.78332152  0.61314016  0.14198845  0.52635469
   0.00715574  0.56090863  0.20661338  0.47619986]
 [ 0.39026141  0.83387462  0.030901    0.02776573  0.09200949  0.75037409
   0.14052445  0.41269122  0.26856808  0.27825607]
 [ 0.15578142  0.24109948  0.34057596  0.91726667  0.05635939  0.83184004
   0.29916706  0.60920722  0.07064052  0.24144689]
 [ 0.85216247  0.76520405  0.47269014  0.41194506  0.89795682  0.75770944
   0.4499217   0.49570304  0.17522618  0.55382947]
 [ 0.12630016  0.73027911  0.65757261  0.64545701  0.33829199  0.12814605
   0.547304    0.44063139  0.09735939  0.73570469]
 [ 0.39690546  0.15696982  0.04994304  0.86695787  0.19479063  0.28700582
   0.19647977  0.61230859  0.80696025  0.26794089]
 [ 0.02273814  0.53884275  0.58799855  0.18467     0.93091798  0.83832868
   0.29416257  0.07453861  0.67892788  0.81952435]
 [ 0.5031617   0.86830062  0.05512131  0.63413474  0.94710835  0.43494454
   0.23671281  0.20405918  0.64396553  0.16530448]
 [ 0.4317772   0.20813481  0.17837799  0.38255151  0.34840336  0.38509588
   0.16162671  0.90039085  0.54950222  0.43698554]
 [ 0.85805945  0.30528914  0.11335882  0.54435072  0.08306196  0.47864267
   0.59151248  0.84471896  0.93667668  0.12880514]]

Now let's try something. We should get the same thing solving $$ A A x^* = A b $$

In [10]:
Anew = A.dot(A.dot(A.dot(A.dot(A.dot(A.dot(A.dot(A)))))))
bnew = A.dot(A.dot(A.dot(A.dot(A.dot(A.dot(A.dot(b)))))))
x = nla.solve(Anew,bnew)
print("                  Sensitivity of A:", nla.cond(Anew))
print("maximum difference from x to xstar:", np.max(np.abs(x-xstar)))
                  Sensitivity of A: 121777975.6
maximum difference from x to xstar: 1.23165300181e-08
In [11]:
for xi in x:
    print(repr(xi))
1.0000000081868436
0.99999999911988269
1.00000001231653
0.99999999163306419
0.99999999324593591
1.000000000217395
1.0000000016054995
0.9999999961116941
1.0000000056621479
0.99999999792366112
In [12]:
import numpy as np
In [13]:
a = np.array([0.1], dtype=np.float
In [14]:
a.nbytes
Out[14]:
4
In [ ]: