Content under Creative Commons Attribution license CC-BY 4.0, code under MIT license (c)2014 Sergio Rojas (srojas@usb.ve) and Erik A Christensen (erikcny@aol.com).
NOTE: This IPython notebook should be read alonside the corresponding chapter in the book, where each piece of code is fully explained.
This chapter explores the treatment of matrices (whether normal or sparse) with the modules on linear algebra – linalg and sparse.linalg, which expand and improve the NumPy module with the same name.
In [1]:
import numpy
vectorA = numpy.array([1,2,3,4,5,6,7])
vectorA
Out[1]:
In [2]:
vectorB = vectorA[::-1].copy()
vectorB
Out[2]:
In [3]:
vectorB[0]=123
vectorB
Out[3]:
In [4]:
vectorA
Out[4]:
In [5]:
vectorB = vectorA[::-1].copy()
vectorB
Out[5]:
In [6]:
vectorC = vectorA + vectorB
vectorC
Out[6]:
In [7]:
vectorD = vectorB - vectorA
vectorD
Out[7]:
In [8]:
dotProduct1 = numpy.dot(vectorA,vectorB)
dotProduct1
Out[8]:
In [9]:
dotProduct2 = (vectorA*vectorB).sum()
dotProduct2
Out[9]:
In [10]:
vectorA = numpy.array([5, 6, 7])
vectorA
Out[10]:
In [11]:
vectorB = numpy.array([7, 6, 5])
vectorB
Out[11]:
In [12]:
crossProduct = numpy.cross(vectorA,vectorB)
crossProduct
Out[12]:
In [13]:
crossProduct = numpy.cross(vectorB,vectorA)
crossProduct
Out[13]:
In [14]:
import numpy
In [15]:
A=numpy.matrix("1,2,3;4,5,6")
print(A)
In [16]:
A=numpy.matrix([[1,2,3],[4,5,6]])
print(A)
Please, refers to the corresponding section on the book to read about the meaning of the following matrix
In [17]:
A=numpy.matrix([ [0,10,0,0,0], [0,0,20,0,0], [0,0,0,30,0],
[0,0,0,0,40], [0,0,0,0,0] ])
A
Out[17]:
In [18]:
A[0,1],A[1,2],A[2,3],A[3,4]
Out[18]:
In [19]:
rows=numpy.array([0,1,2,3])
cols=numpy.array([1,2,3,4])
vals=numpy.array([10,20,30,40])
In [20]:
import scipy.sparse
In [21]:
A=scipy.sparse.coo_matrix( (vals,(rows,cols)) )
print(A)
In [22]:
print(A.todense())
In [23]:
scipy.sparse.isspmatrix_coo(A)
Out[23]:
In [24]:
B=numpy.mat(numpy.ones((3,3)))
W=numpy.mat(numpy.zeros((3,3)))
print(numpy.bmat('B,W;W,B'))
In [25]:
a=numpy.array([[1,2],[3,4]])
a
Out[25]:
In [26]:
a*a
Out[26]:
Please, refers to the corresponding section on the book to read about the meaning of the following matrix product
In [27]:
A=numpy.mat(a)
A
Out[27]:
In [28]:
A*A
Out[28]:
In [29]:
numpy.dot(A,A)
Out[29]:
In [30]:
b=numpy.array([[1,2,3],[3,4,5]])
numpy.dot(a,b)
Out[30]:
In [31]:
numpy.multiply(A,A)
Out[31]:
In [32]:
a=numpy.arange(5); A=numpy.mat(a)
a.shape, A.shape, a.transpose().shape, A.transpose().shape
Out[32]:
In [33]:
import scipy.linalg
In [34]:
A=scipy.linalg.hadamard(8)
zero_sum_rows = (numpy.sum(A,0)==0)
B=A[zero_sum_rows,:]
print(B[0:3,:])
In [35]:
import numpy
A = numpy.matrix("1+1j, 2-1j; 3-1j, 4+1j")
print (A)
In [36]:
print (A.T)
In [37]:
print (A.H)
Please, refers to the corresponding section on the book to read about the meaning of the following basis vectors
In [38]:
mu=1/numpy.sqrt(2)
A=numpy.matrix([[mu,0,mu],[0,1,0],[mu,0,-mu]])
B=scipy.linalg.kron(A,A)
In [39]:
print (B[:,0:-1:2])
In [40]:
A=numpy.matrix("1,1j;21,3")
print (A**2);
In [41]:
print (numpy.asarray(A)**2)
In [42]:
a=numpy.arange(0,2*numpy.pi,1.6)
A = scipy.linalg.toeplitz(a)
print (A)
In [43]:
print (numpy.exp(A))
In [44]:
print (scipy.linalg.expm(A))
In [45]:
x=10**100; y=9; v=numpy.matrix([x,y])
scipy.linalg.norm(v,2) # the right method
Out[45]:
As mentioned in the book, the following command will generate an error from the python computational engine
In [46]:
numpy.sqrt(x*x+y*y) # the wrong method
This section refers the reader to the SciPy documentation related to eigenvalues problems and matrix decomposition
http://docs.scipy.org/doc/scipy/reference/linalg.html
http://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.eigvals.html
Please, refers to the corresponding section on the book to read about the meaning of the following two eaquations
In [47]:
%matplotlib inline
In [48]:
import numpy
import scipy.misc
from scipy.linalg import svd
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (12.0, 8.0)
img=scipy.misc.lena()
U,s,Vh=svd(img) # Singular Value Decomposition
A = numpy.dot( U[:,0:32], # use only 32 singular values
numpy.dot( numpy.diag(s[0:32]),
Vh[0:32,:]))
plt.subplot(121,aspect='equal');
plt.gray()
plt.imshow(img)
plt.subplot(122,aspect='equal');
plt.imshow(A)
plt.show()
Please, refers to the corresponding section on the book to read about the meaning of the following eaquation
In [49]:
A=numpy.mat(numpy.eye(3,k=1))
print(A)
In [50]:
b=numpy.mat(numpy.arange(3) + 1).T
print(b)
In [51]:
xinfo=scipy.linalg.lstsq(A,b)
print (xinfo[0].T) # output the solution
Content under Creative Commons Attribution license CC-BY 4.0, code under MIT license (c)2014 Sergio Rojas (srojas@usb.ve) and Erik A Christensen (erikcny@aol.com).