Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: math480-2016
Views: 2158

Math 480: Open Source Mathematical Software

2016-05-04

William Stein

Lectures 17: Linear Algebra (part 2 of 3)

Remarks

  1. I rewrote Sage worksheet rendering during the last three days (e.g., TimeTravel revert, keeps position, shouldn't get corrupted). Please refresh your browser clearing the cache and test. Perhaps it will be better! Also, in response to some problems yesterday, I greatly reworked some core things about the design/efficiency of the web backend this morning, so there's a really huge amount of CPU power for "handling changefeeds", which should make everything a lot more robust. Help me stress test this right now 😉

  2. Reminder: Homework and grading are due Friday at 6pm.

  3. Reminder to me: start screencast.

  4. First: I will systematically show you a bunch of examples from the sage linear algebra quickref guide.

  5. Second: You work on homework and we answer your questions.

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[0] v[1000000]
0 3/4
m = matrix(QQ, 10000, sparse=True)
m
10000 x 10000 sparse matrix over Rational Field
m[0,100] = 4/5
m[0,100]
4/5
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)
identity_matrix(5)
[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]
%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]]) f(x) = x^3 - 2*x + 1 f(A) # FAIL -- this is maybe a bug though (the quickref says it should work) -- I reported this
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/symbolic/expression.pyx", line 4902, in sage.symbolic.expression.Expression.__call__ (/projects/sage/sage-6.10/src/build/cythonized/sage/symbolic/expression.cpp:28201) return self._parent._call_element_(self, *args, **kwds) File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/sage/symbolic/callable.py", line 487, in _call_element_ return SR(_the_element.substitute(**d)) File "sage/symbolic/expression.pyx", line 4808, in sage.symbolic.expression.Expression.substitute (/projects/sage/sage-6.10/src/build/cythonized/sage/symbolic/expression.cpp:27691) (<Expression>self.coerce_in(v))._gobj)) File "sage/symbolic/expression.pyx", line 2803, in sage.symbolic.expression.Expression.coerce_in (/projects/sage/sage-6.10/src/build/cythonized/sage/symbolic/expression.cpp:20012) return self._parent._coerce_(z) File "sage/structure/parent_old.pyx", line 239, in sage.structure.parent_old.Parent._coerce_ (/projects/sage/sage-6.10/src/build/cythonized/sage/structure/parent_old.c:4448) return self.coerce(x) File "sage/structure/parent.pyx", line 1351, in sage.structure.parent.Parent.coerce (/projects/sage/sage-6.10/src/build/cythonized/sage/structure/parent.c:11050) 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
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!
(−122 ((33−11)e33−33−11)e(−12 33+52)233 (33e33−33)e(−12 33+52)111 (33e33−33)e(−12 33+52)122 ((33+11)e33−33+11)e(−12 33+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)
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() [4 2] [3 1] A.adjoint() [ 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⋅(x2−15x−18)\displaystyle x \cdot (x^{2} - 15 x - 18)
show(factor(A.restrict(V).charpoly()))
(x2−15x−18)\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='green', frame_aspect_ratio=[1, 1, 1], 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