# coding: utf-8
# 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)))
# In[8]:
for xi in x:
print(repr(xi))
print(A)
# 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)))
# In[11]:
for xi in x:
print(repr(xi))
# In[12]:
import numpy as np
# In[13]:
a = np.array([0.1], dtype=np.float
# In[14]:
a.nbytes
# In[ ]: