Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download
Views: 18
Image: ubuntu2204
v = vector(QQ, [1,-2,3]) u = vector(QQ, [3,2,4]) print(u+v) print(2*u+3*v)
(4, 0, 7) (9, -2, 17)
O = vector(QQ,[0,0,0]) vec_u = arrow3d(O, u, color='green') vec_v = arrow3d(O, v, color='blue') vec_uv = arrow3d(O, u+v, color='blue') l1 = line3d((u,u+v),color='black',linestyle="--") l2 = line3d((v,u+v),color='black',linestyle="--") show(vec_u+vec_v+vec_uv+l1+l2,frame=True) w1 = vector([2,3]) w2 = vector([4,2]) plot(w1)+plot(w2, color='green')
3D rendering not yet implemented
## Norms of Vectors v.norm(2),v.norm(1),v.norm(oo)
(sqrt(14), 6, 3)
u,v
((3, 2, 4), (1, -2, 3))
## Dot Product u*v
11
## Cross Product w = u.cross_product(v) w
(14, -5, -8)
print(u.dot_product(w)) print(v.dot_product(w))
0 0
cross_uv = arrow3d(O, w, color='red') show(vec_u+vec_v+vec_uv+cross_uv,frame=True)
3D rendering not yet implemented
w.dot_product(u), w.dot_product(v)
(0, 0)
## Orthogonal Projection v.dot_product(u)/norm(u)^2*u
(33/29, 22/29, 44/29)
u.dot_product(v-w)
11
norm(u+v).n()<=(norm(u)+norm(v)).n()
True
## Defining Mtarices M = matrix(QQ, [[1,3,7,-2],[2,-7,1,4],[9,10,16,2]]) M
[ 1 3 7 -2] [ 2 -7 1 4] [ 9 10 16 2]
show(M) latex(M)
(13722714910162)\displaystyle \left(\begin{array}{rrrr} 1 & 3 & 7 & -2 \\ 2 & -7 & 1 & 4 \\ 9 & 10 & 16 & 2 \end{array}\right)
\left(\begin{array}{rrrr} 1 & 3 & 7 & -2 \\ 2 & -7 & 1 & 4 \\ 9 & 10 & 16 & 2 \end{array}\right)
M1 = M*M.T M1
[ 63 -20 147] [-20 70 -28] [147 -28 441]
M1.det()
371028
M1.inverse()
[ 307/3786 8/631 -695/26502] [ 8/631 21/1262 -2/631] [ -695/26502 -2/631 2005/185514]
M.pseudoinverse()
[ -1717/13251 11/631 6002/92757] [ -2855/26502 -139/1262 9571/185514] [ 4259/26502 69/1262 -2563/185514] [ -724/4417 22/631 1898/30919]
## Matrix from vectors u,v,w
((3, 2, 4), (1, -2, 3), (14, -5, -8))
column_matrix([u,v,w])
[ 3 1 14] [ 2 -2 -5] [ 4 3 -8]
A = matrix([randint(-10,10) for i in range(16)],nrows=4)
A
[ 0 0 7 1] [ 8 0 -5 1] [-3 -3 -2 -8] [ 5 -6 -9 -9]
2*A+4*A
[ 0 0 42 6] [ 48 0 -30 6] [-18 -18 -12 -48] [ 30 -36 -54 -54]
## Elementary operation M = matrix([randint(-10,10) for i in range(16)],nrows=4) M
[ 10 7 8 -2] [ -4 -8 3 -3] [ 6 -4 -10 1] [ -7 1 1 -5]
M[1,2]
3
M[0]
(10, 7, 8, -2)
M[:,1]
[ 7] [-8] [-4] [ 1]
M
[ 10 7 8 -2] [ -4 -8 3 -3] [ 6 -4 -10 1] [ -7 1 1 -5]
S = copy(M)
S.swap_rows(1,3)
S
[ 10 7 8 -2] [ -7 1 1 -5] [ 6 -4 -10 1] [ -4 -8 3 -3]
S.rescale_row(2,10)
S
[ 10 7 8 -2] [ -7 1 1 -5] [ 60 -40 -100 10] [ -4 -8 3 -3]
S.add_multiple_of_row(1,2,-5)
S
[ 10 7 8 -2] [-307 201 501 -55] [ 60 -40 -100 10] [ -4 -8 3 -3]
M.rref()
[1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]
## Step by step RREF A=matrix(QQ, [[3,-2,1],[6, -1, 3],[4, 1, 8]]) m=A.nrows() print ('The original matrix is') show(A) for k in range(0,m-1): if A[k,k] == 0: listA = [(A[j,k],j) for j in range(k,m)] maxv, pivot = max(listA) A[pivot,:],A[k,:]=A[k,:],A[pivot,:] print ('We permute rows %d and %d'%(k+1,pivot+1)) show(A) for n in range(k+1,m): a=A[k,k] if A[n,k] != 0: print( "We add %s times row %d to row %d"%(-A[n,k]/a, k+1, n+1)) A=A.with_added_multiple_of_row(n,k,-A[n,k]/a) show(A) for k in range(m-1,-1,-1): for n in range(k-1,-1,-1): a=A[k,k] if A[n,k]!=0: print( "We add %s times row %d to the row %d"%(-A[n,k]/a, k+1, n+1)) A=A.with_added_multiple_of_row(n,k,-A[n,k]/a) show(A) for k in range(0,m): if A[k,k]!=1: print ('We divide row %d by %s'%(k+1,A[k,k])) A = A.with_rescaled_row(k,1/A[k,k]) show(A)
The original matrix is
(321613418)\displaystyle \left(\begin{array}{rrr} 3 & -2 & 1 \\ 6 & -1 & 3 \\ 4 & 1 & 8 \end{array}\right)
We add -2 times row 1 to row 2
(321031418)\displaystyle \left(\begin{array}{rrr} 3 & -2 & 1 \\ 0 & 3 & 1 \\ 4 & 1 & 8 \end{array}\right)
We add -4/3 times row 1 to row 3
(3210310113203)\displaystyle \left(\begin{array}{rrr} 3 & -2 & 1 \\ 0 & 3 & 1 \\ 0 & \frac{11}{3} & \frac{20}{3} \end{array}\right)
We add -11/9 times row 2 to row 3
(32103100499)\displaystyle \left(\begin{array}{rrr} 3 & -2 & 1 \\ 0 & 3 & 1 \\ 0 & 0 & \frac{49}{9} \end{array}\right)
We add -9/49 times row 3 to the row 2
(32103000499)\displaystyle \left(\begin{array}{rrr} 3 & -2 & 1 \\ 0 & 3 & 0 \\ 0 & 0 & \frac{49}{9} \end{array}\right)
We add -9/49 times row 3 to the row 1
(32003000499)\displaystyle \left(\begin{array}{rrr} 3 & -2 & 0 \\ 0 & 3 & 0 \\ 0 & 0 & \frac{49}{9} \end{array}\right)
We add 2/3 times row 2 to the row 1
(30003000499)\displaystyle \left(\begin{array}{rrr} 3 & 0 & 0 \\ 0 & 3 & 0 \\ 0 & 0 & \frac{49}{9} \end{array}\right)
We divide row 1 by 3
(10003000499)\displaystyle \left(\begin{array}{rrr} 1 & 0 & 0 \\ 0 & 3 & 0 \\ 0 & 0 & \frac{49}{9} \end{array}\right)
We divide row 2 by 3
(10001000499)\displaystyle \left(\begin{array}{rrr} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & \frac{49}{9} \end{array}\right)
We divide row 3 by 49/9
(100010001)\displaystyle \left(\begin{array}{rrr} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{array}\right)
## Solving linear equations(using solve command) var('x,y,z') eq1 = 2*x-3*y+z==7 eq2 = 3*x+5*y+3*z==17 eq3 = 5*x+2*y+4*z==24 solve([eq1,eq2,eq3],[x,y,z])
(x, y, z) [[x == -14/19*r1 + 86/19, y == -3/19*r1 + 13/19, z == r1]]
## Solving Ax = b A = matrix(QQ,[[2,0,4],[-5,8,7],[9,15,12]]) b = vector([10,-15,20])
show(A, b)
(20458791512)\displaystyle \left(\begin{array}{rrr} 2 & 0 & 4 \\ -5 & 8 & 7 \\ 9 & 15 & 12 \end{array}\right) (10,15,20)\displaystyle \left(10,\,-15,\,20\right)
A.solve_right(b) # A\b
(815/303, -365/303, 350/303)
aug = A.augment(b,subdivide=True)
aug.rref()
[ 1 0 0| 815/303] [ 0 1 0|-365/303] [ 0 0 1| 350/303]
## Defining Vector spaces V = QQ^4 V
Vector space of dimension 4 over Rational Field
v1 = vector(QQ, [1,-5,3,7])
v1 in V
True
r = V.random_element() r
(1/6, 0, 0, 4/43)
v1 = vector(QQ, [1,-5,3,7]) v2 = vector(QQ, [2,-15,13,17]) v3 = vector(QQ, [13,-20,16,24]) v4 = vector(QQ, [4,-30,26,34])
B = [v1,v2,v3,v4]
V.span(B)
Vector space of degree 4 and dimension 3 over Rational Field Basis matrix: [ 1 0 0 0] [ 0 1 0 -2] [ 0 0 1 -1]
V.linear_dependence(B)
[ (0, 1, 0, -1/2) ]
D = [V.random_element() for i in range(4)] D
[(1, 1/3, -1, 1/10), (0, -14/23, 0, -4/3), (1/5, 0, 2, -1), (0, 1/4, 1/8, 3)]
V.linear_dependence(D)
[ ]
H = column_matrix([v1,v2,v3,v4]) H
[ 1 2 13 4] [ -5 -15 -20 -30] [ 3 13 16 26] [ 7 17 24 34]
H.rref()
[1 0 0 0] [0 1 0 2] [0 0 1 0] [0 0 0 0]
u1 = vector(QQ, [1,2,3,4]) u2 = vector(QQ, [2,1,-2,0]) u3 = vector(QQ, [11,12,13,4]) u4 = vector(QQ, [7,21,31,41]) B1 = [u1,u2,u3,u4] V.linear_dependence(B1)==[]
True
W = V.submodule_with_basis(B1) W
Vector space of degree 4 and dimension 4 over Rational Field User basis matrix: [ 1 2 3 4] [ 2 1 -2 0] [11 12 13 4] [ 7 21 31 41]
u = vector(QQ,[3,5,7,13])
d = W.coordinates(u) d
[651/115, 10/23, -21/115, -5/23]
d[0]*u1+d[1]*u2+d[2]*u3+d[3]*u4
(3, 5, 7, 13)
sum([d[i]*B1[i] for i in range(4)])==u
True
M = column_matrix(B1) aug = M.augment(u) aug.rref()
[ 1 0 0 0 651/115] [ 0 1 0 0 10/23] [ 0 0 1 0 -21/115] [ 0 0 0 1 -5/23]
w1 = vector(QQ, [1,-2,0,2]) w2 = vector(QQ,[-1,1,0,-2])
A1 = column_matrix([w1,w2]) A1
[ 1 -1] [-2 1] [ 0 0] [ 2 -2]
A2 =A1.augment(identity_matrix(4)) A2.rref()
[ 1 0 0 -1 0 -1/2] [ 0 1 0 -1 0 -1] [ 0 0 1 0 0 -1/2] [ 0 0 0 0 1 0]
A2.pivots()
(0, 1, 2, 4)
B1 = [V.random_element() for i in range(3)] B2 = [V.random_element() for i in range(3)] W1 = V.span(B1) W2 = V.span(B2)
W1.intersection(W2)
Vector space of degree 4 and dimension 2 over Rational Field Basis matrix: [ 1 0 4/15 -4/3] [ 0 1 -1307/30 -1229/12]
W1+W2
Vector space of degree 4 and dimension 4 over Rational Field Basis matrix: [1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]
dimension(W1+W2)==dimension(W1)+dimension(W2)-dimension(W1.intersection(W2))
True