Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 16658
%md # Math 152: Intro to Mathematical Software ### 2017-02-03 ### Kiran Kedlaya; University of California, San Diego ### adapted from lectures by William Stein, University of Washington ## **Lecture 11: Linear Algebra (part 2)**

Math 152: Intro to Mathematical Software

2017-02-03

Kiran Kedlaya; University of California, San Diego

adapted from lectures by William Stein, University of Washington

Lecture 11: Linear Algebra (part 2)

Announcements:

  • Last call for the week 4 feedback survey! (See Wednesday's handout for the link.)

  • If you are still on the waitlist and still want to enroll, see me ASAP.

Today: more examples from the sage linear algebra quickref guide.

u = vector(QQ, [5, 3/2, -1]) # dense vector show(u)
(5,32,1)\displaystyle \left(5,\,\frac{3}{2},\,-1\right)
u[0] # 0 based
5
parent(u)
Vector space of dimension 3 over Rational Field
# a *sparse* vector -- same functions, but underlying algorithms for sparse vectors (and matrices) use sparse data structures/algorithms v = vector({2:4, 95:4, 1000000:3/4}) parent(v)
Sparse vector space of dimension 1000001 over Rational Field
v[15] v[1000000]
0 3/4
w = vector(QQ, {3:5, 4:6, 1000000:0}) parent(w)
Sparse vector space of dimension 1000001 over Rational Field
w1 = v+w parent(w1)
Sparse vector space of dimension 1000001 over Rational Field
w2 = vector({2:4, 95:4, 1000000:3/4, 3:5, 4:6}) print(w1 == w2)
True
af5ae59-3cb3-49fe-bc7e-cb4e72cf6ec6︠
m = matrix(QQ, 10000, sparse=True)
m== 0
True
m[0,100] = 4/5
m[0,99]
0
m = matrix(QQ, 2, sparse=True) m[0,1] = 3 print(m)
[0 3] [0 0]
u = vector([1, 3/2, -1]); v = vector([1/3, 8, -2]) u.dot_product(v) u.cross_product(v) u.inner_product(v) u.pairwise_product(v)
43/3 (5, 5/3, 15/2) 43/3 (1/3, 12, 2)
u = identity_matrix(CC, 5); print(u)
[ 1.00000000000000 0.000000000000000 0.000000000000000 0.000000000000000 0.000000000000000] [0.000000000000000 1.00000000000000 0.000000000000000 0.000000000000000 0.000000000000000] [0.000000000000000 0.000000000000000 1.00000000000000 0.000000000000000 0.000000000000000] [0.000000000000000 0.000000000000000 0.000000000000000 1.00000000000000 0.000000000000000] [0.000000000000000 0.000000000000000 0.000000000000000 0.000000000000000 1.00000000000000]
identity_matrix?
File: /projects/sage/sage-7.5/local/lib/python2.7/site-packages/sage/matrix/special.py Signature : identity_matrix(ring, n=0, sparse=False) Docstring : This function is available as identity_matrix(...) and matrix.identity(...). Return the n x n identity matrix over the given ring. The default ring is the integers. EXAMPLES: sage: M = identity_matrix(QQ, 2); M [1 0] [0 1] sage: M.parent() Full MatrixSpace of 2 by 2 dense matrices over Rational Field sage: M = identity_matrix(2); M [1 0] [0 1] sage: M.parent() Full MatrixSpace of 2 by 2 dense matrices over Integer Ring sage: M.is_mutable() True sage: M = identity_matrix(3, sparse=True); M [1 0 0] [0 1 0] [0 0 1] sage: M.parent() Full MatrixSpace of 3 by 3 sparse matrices over Integer Ring sage: M.is_mutable() True
%octave eye(5)
ans = 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
A = matrix(QQ, [[1,2],[3,4]]) A^3 - 2*A + 1
[ 36 50] [ 75 111]
A = matrix(QQ, [[1,2],[3,4]]) f(x) = x^3 - 2*x + 1 f(A) # FAIL -- this should work, but doesn't
Error in lines 3-3 Traceback (most recent call last): File "/projects/sage/sage-7.5/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 982, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "sage/symbolic/expression.pyx", line 5066, in sage.symbolic.expression.Expression.__call__ (/projects/sage/sage-7.5/src/build/cythonized/sage/symbolic/expression.cpp:30530) return self._parent._call_element_(self, *args, **kwds) File "/projects/sage/sage-7.5/local/lib/python2.7/site-packages/sage/symbolic/callable.py", line 464, in _call_element_ return SR(_the_element.substitute(**d)) File "sage/symbolic/expression.pyx", line 4972, in sage.symbolic.expression.Expression.substitute (/projects/sage/sage-7.5/src/build/cythonized/sage/symbolic/expression.cpp:29999) (<Expression>self.coerce_in(v))._gobj)) File "sage/symbolic/expression.pyx", line 2885, in sage.symbolic.expression.Expression.coerce_in (/projects/sage/sage-7.5/src/build/cythonized/sage/symbolic/expression.cpp:21670) return self._parent._coerce_(z) File "sage/structure/parent_old.pyx", line 240, in sage.structure.parent_old.Parent._coerce_ (/projects/sage/sage-7.5/src/build/cythonized/sage/structure/parent_old.c:4949) return self.coerce(x) File "sage/structure/parent.pyx", line 1195, in sage.structure.parent.Parent.coerce (/projects/sage/sage-7.5/src/build/cythonized/sage/structure/parent.c:11166) raise TypeError("no canonical coercion from %s to %s" % (parent_c(x), self)) TypeError: no canonical coercion from Full MatrixSpace of 2 by 2 dense matrices over Rational Field to Callable function ring with argument x
R.<y> = PolynomialRing(QQ) parent(y)
Univariate Polynomial Ring in y over Rational Field
A = matrix(QQ, [[1,2],[3,4]]) R.<y> = PolynomialRing(QQ) f = y^3 - 2*y + 1 f(A) # works
[ 36 50] [ 75 111]
show(A.exp()) # compute sum A^k/k! type(A.exp())
(122((3311)e333311)e(1233+52)233(33e3333)e(1233+52)111(33e3333)e(1233+52)122((33+11)e3333+11)e(1233+52))\displaystyle \left(\begin{array}{rr} -\frac{1}{22} \, {\left({\left(\sqrt{33} - 11\right)} e^{\sqrt{33}} - \sqrt{33} - 11\right)} e^{\left(-\frac{1}{2} \, \sqrt{33} + \frac{5}{2}\right)} & \frac{2}{33} \, {\left(\sqrt{33} e^{\sqrt{33}} - \sqrt{33}\right)} e^{\left(-\frac{1}{2} \, \sqrt{33} + \frac{5}{2}\right)} \\ \frac{1}{11} \, {\left(\sqrt{33} e^{\sqrt{33}} - \sqrt{33}\right)} e^{\left(-\frac{1}{2} \, \sqrt{33} + \frac{5}{2}\right)} & \frac{1}{22} \, {\left({\left(\sqrt{33} + 11\right)} e^{\sqrt{33}} - \sqrt{33} + 11\right)} e^{\left(-\frac{1}{2} \, \sqrt{33} + \frac{5}{2}\right)} \end{array}\right)
<type 'sage.matrix.matrix_symbolic_dense.Matrix_symbolic_dense'>
print "A.inverse()" A.inverse() print "A^(-1)" A^(-1) print "~A" ~A print "A.transpose()" A.transpose() print "A.conjugate() -- boring since real" A.conjugate() print "A.antitranspose()" A.antitranspose? print "A.adjoint() -- matrix ov adjoint matrices" A.adjoint()
A.inverse() [ -2 1] [ 3/2 -1/2] A^(-1) [ -2 1] [ 3/2 -1/2] ~A [ -2 1] [ 3/2 -1/2] A.transpose() [1 3] [2 4] A.conjugate() -- boring since real [1 2] [3 4] A.antitranspose()
File: /projects/sage/sage-7.5/src/sage/matrix/matrix_rational_dense.pyx Signature : A.antitranspose() Docstring : Returns the antitranspose of self, without changing self. EXAMPLES: sage: A = matrix(QQ,2,3,range(6)) sage: type(A) <type 'sage.matrix.matrix_rational_dense.Matrix_rational_dense'> sage: A.antitranspose() [5 2] [4 1] [3 0] sage: A [0 1 2] [3 4 5] sage: A.subdivide(1,2); A [0 1|2] [---+-] [3 4|5] sage: A.antitranspose() [5|2] [-+-] [4|1] [3|0]
A.adjoint() -- matrix ov adjoint matrices [ 4 -2] [-3 1]
A = matrix(QQ, 3, [1..9]) show(A) V = A.image() W = A.kernel() print "image: " print V print "kernel: " print W
(123456789)\displaystyle \left(\begin{array}{rrr} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}\right)
image: Vector space of degree 3 and dimension 2 over Rational Field Basis matrix: [ 1 0 -1] [ 0 1 2] kernel: Vector space of degree 3 and dimension 1 over Rational Field Basis matrix: [ 1 -2 1]
A.restrict(V) # V = image
[-6 -6] [18 21]
A.restrict(W)
[0]
show(factor(A.charpoly()))
x(x215x18)\displaystyle x \cdot (x^{2} - 15 x - 18)
show(factor(A.restrict(V).charpoly()))
(x215x18)\displaystyle (x^{2} - 15 x - 18)
M = span(QQ, [vector([1,0,0])]) # A doesn't leave the subspace M invariant... so get an error :-) M A.restrict(M)
Vector space of degree 3 and dimension 1 over Rational Field Basis matrix: [1 0 0]
Error in lines 3-3 Traceback (most recent call last): File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "sage/matrix/matrix2.pyx", line 4894, in sage.matrix.matrix2.Matrix.restrict (/projects/sage/sage-6.10/src/build/cythonized/sage/matrix/matrix2.c:36811) raise ArithmeticError, "subspace is not invariant under matrix" ArithmeticError: subspace is not invariant under matrix

And some fun...

matrix_plot(random_matrix(RDF,50)^3, cmap='summer', colorbar=True)
show(DiGraph(A))
d3-based renderer not yet implemented
n = 10; list_plot3d(random_matrix(RDF, n), texture='red', frame_aspect_ratio=[4, 4, 10], mesh=1)
3D rendering not yet implemented
show(DiGraph(matrix(ZZ,2,2,[2,1, 0,1])))
d3-based renderer not yet implemented
v = vector([1,2,3]) w = vector([0,2,5]) %var s, t g = parametric_plot3d(s*v + t*w, (s, -1,1), (t,-1,1), opacity=.5) v = vector([1,-2,3]) w = vector([0,3,-5]) h = parametric_plot3d(s*v + t*w, (s, -1,1), (t,-1,1), color='red') #i = parametric_plot3d(...., (s, -2, 2), ...) #g+h+i show(g+h)
3D rendering not yet implemented