import sglib
reload(sglib)
from sglib import *
# The following allows us to use 'k' as a "variable"
k=sy.Symbol('k')
a=col(1,2); b=col(3,4); c=col(-2,5)
#(1)
3*a
#(2)
6*b
#(3)
k*c
#(4)
a+b
#(5)
b.transpose()+c.transpose()
#(6)
a-c
#(7)
2*a+3*b
#(8)
3*b-2*c+a
#(9)
-2*b.transpose()+2*c.transpose()
#(10)
inner_product(a,b)
#(11)
inner_product(b,c)
#(12)
inner_product(c,a)
#(13)
Matrix([(1,2), (3,4)]) * col(1,-1)
#(14)
Matrix([(0,1), (1,0)]) * col(2,3)
#(15)
# NOTE: Matrix is Identity Matrix, so we don't really need to work this!
a=sy.Symbol('a'); b=sy.Symbol('b') # Need a,b as variables
Matrix([(1,0), (0,1)]) * col(a,b)
z1=2+3*i; z2=1-i; z3=5*i
#(16)
z1+z2
#(17)
z1-z3
#(18)
z1+z2-z3
#(19)
(z1*z2).simplify()
#(20)
(z2*z3).simplify()
#(21)
z1.conjugate()
#(22)
z2.conjugate()
#(23)
abs(z1)
#(24)
abs(z2)
#(25)
abs(z3)
#(26)
Matrix([(1,2), (3,4)]).transpose()
#(27)
Matrix([(1,0,1), (0,2,3), (3,4,5)]).transpose()
#(28)
a=sy.Symbol('a'); b=sy.Symbol('b'); c=sy.Symbol('c'); d=sy.Symbol('d')
w=sy.Symbol('w'); x=sy.Symbol('x'); y=sy.Symbol('y'); z=sy.Symbol('z')
Matrix([(w,x),(y,z)]).transpose()
#(29)
Matrix([(1,2),(3,4)])*Matrix([(4,3),(2,1)])
#(30)
# 2nd one is Identity Matrix!
Matrix([(1,2),(3,4)])*Matrix([(1,0),(0,1)])
#(31)
Matrix([(a,b),(c,d)])*Matrix([(w,x),(y,z)])
#(32)
row(1,2)*col(4,3)
#(33)
col(1,2)*row(4,3)
#(34)
# Not the inverse
Matrix([(0,1),(1,0)])*Matrix([(1,0),(0,1)])
# Their product IS the Identity, so YES
Matrix([(0,1),(1,0)])*Matrix([(0,1),(1,0)])
# NO
Matrix([(0,1),(1,0)])*Matrix([(0,0),(0,0)])
#(35)
# We'll just ask for the inverse directly ...
# It turns out to be (c)
M = Matrix([(1,2),(3,4)])
Mi = M.inverse_ADJ()
Mi
# But, still we should CHECK it ...
M*Mi
#(36)
# (a) - NO
Matrix([(1,0),(0,0)]) * Matrix([(1,0),(0,0)])
# (b) - NO
Matrix([(1,0),(0,0)]) * Matrix([(0,0),(0,1)])
# (c) - NO
Matrix([(1,0),(0,0)]) * Matrix([(-1,0),(0,0)])
# So, its (d) - none of the above.
# Actually, this matrix has no inverse ...
Matrix([(1,0),(0,0)]).inverse_ADJ()
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-46-1190c5d443b8> in <module>() 1 # So, its (d) - none of the above. 2 # Actually, this matrix has no inverse ... ----> 3 Matrix([(1,0),(0,0)]).inverse_ADJ() /usr/local/lib/python2.7/dist-packages/sympy/matrices/matrices.pyc in inverse_ADJ(self, iszerofunc) 2627 zero = any(iszerofunc(ok[j, j]) for j in range(ok.rows)) 2628 if zero: -> 2629 raise ValueError("Matrix det == 0; not invertible.") 2630 2631 return self.adjugate() / d ValueError: Matrix det == 0; not invertible.
#
# Change of Basis Problems
#
b11 = 1/sqrt(2)*col(1,1); b12 = 1/sqrt(2)*col(1,-1)
b21 = 1/sqrt(5)*col(2,1); b22 = 1/sqrt(5)*col(-1,2)
#(37)
v = col(2,3)
v_b1 = col(inner_product(b11,v), inner_product(b12,v))
Print('$%s$' %myltx(v_b1))
#(38)
v = col(3,2)
v_b2 = col(inner_product(b21,v), inner_product(b22,v))
Print('$%s$' %myltx(v_b2))
#(39)
v = col(1,0)
v_b2 = col(inner_product(b21,v), inner_product(b22,v))
Print('$%s$' %myltx(v_b2))
#(40)
v = col(5,5)
v_b1 = col(inner_product(b11,v), inner_product(b12,v))
Print('$%s$' %myltx(v_b1))
3.1 Dimension
In the case of a vector, the dimension is the number of elements. In the case of a (square) matrix, the dimension is the number of elements in a single row or column. So, the answers are:
(41) Dim=3, (42) Dim=2, (43) Dim=3, (44) Dim=2
3.2 Commutativity
(45) Is addition of vectors commutative? -- Yes
(46) Is addition of matrices commutative? -- Yes
(47) Is multiplication of matrices commutative? -- No
(48) Is addition of complex numbers commutative? -- Yes
(49) Is multiplication of complex numbers commutative? -- Yes
(50) If two vectors both contain real numbers, then is their inner product commutative? -- Yes
(51) Proving a couple of these ...
M1 = Matrix([(1,2),(3,4)])
M2 = Matrix([(1,0),(0,0)])
M1*M2
M2*M1
Now let's take the last one (50). If two vectors contain only real numbers (no imaginary parts) then the "general case" (for Dim=2) is:
v1 = col(a,b); v2=col(c,d)
v1,v2
The inner product $\langle v_1 | v_2 \rangle$ is: $ac+bd$.
The inner product $\langle v_2 | v_1 \rangle$ is: $ca + db$.
Since scalar multiplication is commutative, $ac=ca$ and $bd=db$, hence the two products are the same. This procedure will work just the same way with vectors of any dimension.
#(52)
ip = inner_product
# Yes
v1 = col(1,0); v2 = col(0,1)
v1.norm(), v2.norm(), ip(v1,v2)
#(53) No - Orthogonal but not normalized
v1 = col(2,0); v2 = col(0,2)
v1.norm(), v2.norm(), ip(v1,v2)
#(54) Yes (the "blue" basis)
v1 = col(1,1)/sqrt(2); v2 = col(1,-1)/sqrt(2)
v1.norm(), v2.norm(), ip(v1,v2)
#(55) Yes
v1 = col(1,0); v2 = col(0,-1)
v1.norm(), v2.norm(), ip(v1,v2)
#(56) Yes
v1 = col(2,1)/sqrt(5); v2 = col(-1,2)/sqrt(5)
v1.norm(), v2.norm(), ip(v1,v2)
#(57) No - Normalized but not orthogonal
v1 = col(1,1)/sqrt(2); v2 = col(-1,2)/sqrt(5)
v1.norm(), v2.norm(), ip(v1,v2)