This notebook demonstrates the use of vectors and matrices in IPython. Note that the basis is not explicit in any of these operations. You must keep track of the basis yourself (using variable names, or notes etc).
from numpy import array, dot, outer, sqrt, matrix
from numpy.linalg import eig, eigvals
from matplotlib.pyplot import hist
%matplotlib inline
rv = array([1,2]) # a row vector
rv
array([1, 2])
cv = array([[3],[4]]) # a column vector
cv
array([[3], [4]])
Two kinds of vector products we'll see: inner product (dot product) and outer product
dot(rv,cv)
array([11])
dot(cv,rv)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-6-aa042602721b> in <module>() ----> 1 dot(cv,rv) ValueError: shapes (2,1) and (2,) not aligned: 1 (dim 1) != 2 (dim 0)
outer(rv,cv)
array([[3, 4], [6, 8]])
outer(cv,rv)
array([[3, 6], [4, 8]])
# Complex numbers in python have a j term:
a = 1+2j
v1 = array([1+2j, 3+2j, 5+1j, 4+0j])
The complex conjugate changes the sign of the imaginary part:
v1.conjugate()
array([ 1.-2.j, 3.-2.j, 5.-1.j, 4.-0.j])
dot(v1.conjugate(),v1)
(60+0j)
# a two-dimensional array
m1 = array([[2,1],[2,1]])
m1
array([[2, 1], [2, 1]])
# can find transpose with the T method:
m1.T
array([[2, 2], [1, 1]])
# find the eigenvalues and eigenvectors of a matrix:
eig(m1)
(array([ 3., 0.]), array([[ 0.70710678, -0.4472136 ], [ 0.70710678, 0.89442719]]))
Can also use the matrix
type which is like array but restricts to 2D. Also, matrix
adds .H
and .I
methods for hermitian and inverse, respectively. For more information, see Stack Overflow question #4151128
m2 = matrix( [[2,1],[2,1]])
m2.H
matrix([[2, 2], [1, 1]])
eig(m2)
(array([ 3., 0.]), matrix([[ 0.70710678, -0.4472136 ], [ 0.70710678, 0.89442719]]))
# use a question mark to get help on a command
eig?
M14 = array([[0,1],[-2,3]])
eig(M14)
(array([ 1., 2.]), array([[-0.70710678, -0.4472136 ], [-0.70710678, -0.89442719]]))
Interpret this result: the two eigenvalues are 1 and 2 the eigenvectors are strange decimals, but we can check them against the stated solution:
1/sqrt(2) # this is the value for both entries in the first eigenvector
0.70710678118654746
1/sqrt(5) # this is the first value in the second eigenvector
0.44721359549995793
2/sqrt(5) # this is the second value in the second eigenvector
0.89442719099991586
eigvals(M14)
array([ 1., 2.])
Signs are opposite compared to the book, but it turns out that (-) doesn't matter in the interpretation of eigenvectors: only "direction" matters (the relative size of the entries).
M16 = array([[0,-1j],[1j,0]])
evals, evecs = eig(M16)
evecs
array([[-0.00000000-0.70710678j, 0.70710678+0.j ], [ 0.70710678+0.j , 0.00000000-0.70710678j]])
evecs[:,0]
array([-0.00000000-0.70710678j, 0.70710678+0.j ])
evecs[:,1]
array([ 0.70710678+0.j , 0.00000000-0.70710678j])
dot(evecs[:,0].conjugate(),evecs[:,1])
-1.6653345369377348e-16j
Keeping track of row and column vectors in Ipython is somewhat artificial and tedious. The QuTiP library is designed to take care of many of these headaches
from qutip import *
# Create a row vector:
qv = Qobj([[1,2]])
qv
# Find the corresponding column vector
qv.dag()
qv2 = Qobj([[1+2j,4-1j]])
qv2
qv2.dag()
Only need to know one operator: "*" The product will depend on the order, either inner or outer
qv2*qv2.dag() # inner product (dot product)
qv2.dag()*qv2 # outer product
qm = Qobj([[1,2],[2,1]])
qm
qm.eigenenergies() # in quantum (as we will learn) eigenvalues often correspond to energy levels
array([-1., 3.])
evals, evecs = qm.eigenstates()
evecs
array([ Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket Qobj data = [[-0.70710678] [ 0.70710678]], Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket Qobj data = [[ 0.70710678] [ 0.70710678]]], dtype=object)
evecs[0]
# Solution
n, bins, patches = hist([10,13,14,14,6,8,7,9,12,14,13,11,10,7,7],bins=5,range=(5,14))
# Solution
n
array([ 1., 4., 3., 2., 5.])
# Solution
pvals = n/n.sum()
Hint: using sympy, we can calculate the relevant integral. The conds='none' asks the solver to ignore any strange conditions on the variables in the integral. This is fine for most of our integrals. Usually the variables are real and well-behaved numbers.
# Solution
from sympy import *
c,a,x = symbols("c a x")
Q.positive((c,a))
first = integrate(c*exp(-a*x),(x,0,oo),conds='none')
print("first = ",first)
second = integrate(a*exp(-a*x),(x,0,oo),conds='none')
print("second = ",second)
first = c/a second = 1