VectorIntro.mw

Create a vector A by assigning a list of elements in square brackets. Use the colon(:) followed by the equal(=) sign := 

 

A := [1, 2, 3]; 1 

[1, 2, 3] 

Create a second vector, B 

 

B := [3, 4, 5]; 1 

[3, 4, 5] 

Add the two vectors 

 

A+B; 1 

[4, 6, 8] 

Subtract them 

 

A-B; 1 

[-2, -2, -2] 

B-A; 1 

[2, 2, 2] 

Now load the linear algebra library  

 

with(linalg); 1 

[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp, QRdecomp, Wronskian, addcol, addrow, adj, adjoint, angle, augment, backsub, band, basis, bezout, blockmatrix, charmat, charpoly, cholesky, col, cold...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp, QRdecomp, Wronskian, addcol, addrow, adj, adjoint, angle, augment, backsub, band, basis, bezout, blockmatrix, charmat, charpoly, cholesky, col, cold...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp, QRdecomp, Wronskian, addcol, addrow, adj, adjoint, angle, augment, backsub, band, basis, bezout, blockmatrix, charmat, charpoly, cholesky, col, cold...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp, QRdecomp, Wronskian, addcol, addrow, adj, adjoint, angle, augment, backsub, band, basis, bezout, blockmatrix, charmat, charpoly, cholesky, col, cold...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp, QRdecomp, Wronskian, addcol, addrow, adj, adjoint, angle, augment, backsub, band, basis, bezout, blockmatrix, charmat, charpoly, cholesky, col, cold...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp, QRdecomp, Wronskian, addcol, addrow, adj, adjoint, angle, augment, backsub, band, basis, bezout, blockmatrix, charmat, charpoly, cholesky, col, cold...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp, QRdecomp, Wronskian, addcol, addrow, adj, adjoint, angle, augment, backsub, band, basis, bezout, blockmatrix, charmat, charpoly, cholesky, col, cold...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp, QRdecomp, Wronskian, addcol, addrow, adj, adjoint, angle, augment, backsub, band, basis, bezout, blockmatrix, charmat, charpoly, cholesky, col, cold...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp, QRdecomp, Wronskian, addcol, addrow, adj, adjoint, angle, augment, backsub, band, basis, bezout, blockmatrix, charmat, charpoly, cholesky, col, cold...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp, QRdecomp, Wronskian, addcol, addrow, adj, adjoint, angle, augment, backsub, band, basis, bezout, blockmatrix, charmat, charpoly, cholesky, col, cold...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp, QRdecomp, Wronskian, addcol, addrow, adj, adjoint, angle, augment, backsub, band, basis, bezout, blockmatrix, charmat, charpoly, cholesky, col, cold...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp, QRdecomp, Wronskian, addcol, addrow, adj, adjoint, angle, augment, backsub, band, basis, bezout, blockmatrix, charmat, charpoly, cholesky, col, cold...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp, QRdecomp, Wronskian, addcol, addrow, adj, adjoint, angle, augment, backsub, band, basis, bezout, blockmatrix, charmat, charpoly, cholesky, col, cold...
[BlockDiagonal, GramSchmidt, JordanBlock, LUdecomp, QRdecomp, Wronskian, addcol, addrow, adj, adjoint, angle, augment, backsub, band, basis, bezout, blockmatrix, charmat, charpoly, cholesky, col, cold...
 

Now take the dot or inner product using the innerprod(...) function 

 

innerprod(A, B); 1 

26 

Compute the Euclidean Norm of A using the norm(...,2) function. 2 signifies the Euclidean norm.  

 

norm(A, 2); 1 

14^(1/2) 

Now try the cross product using the crossprod(...) function 

 

crossprod(A, B); 1 

table( [( 1 ) = -2, ( 2 ) = 4, ( 3 ) = -2 ] ) 

The cross product is perpendicular to either vector so 

 

innerprod(A, crossprod(A, B)); 1 

0 

innerprod(B, crossprod(A, B)); 1 

0 

The angle between A and B can be computed using the angle(...) function 

 

angle(A, B); 1 

arccos(13/350*14^(1/2)*50^(1/2)) 

Or we can use the book definition  

 

arccos(innerprod(A, B)/(norm(A, 2)*norm(B, 2))); 1 

arccos(13/70*14^(1/2)*2^(1/2)) 

Are they the same? 

Use evalf to evaluate each one. 

 

 

evalf(angle(A, B)); 1 

.1862387665 

 

evalf(arccos(innerprod(A, B)/(norm(A, 2)*norm(B, 2)))); 1 

.1862387676 

Hmmm...pretty close. Just realize that the values within the arccos(...) are the same so the answers really are the same.  

Different algorithms produce different rounding results 

 

 

Clear A and B. Assigning a variable to itself in quotes, clears the variable 

 

A := 'A'; 1 

A 

B := 'B'; 1 

B 

Now make two generic vectors. (To create the subscript use shift hyphen to drop the into subscript mode) 

 

A := [x[1], y[1], z[1]]; 1 

[x[1], y[1], z[1]] 

B := [x[2], y[2], z[2]]; 1 

[x[2], y[2], z[2]] 

Now perform the dot product, norm and cross product to get the book formulas. 

 

innerprod(A, B); 1 

x[1]*x[2]+y[1]*y[2]+z[1]*z[2] 

norm(A, 2); 1 

(abs(x[1])^2+abs(y[1])^2+abs(z[1])^2)^(1/2) 

crossprod(A, B); 1 

table( [( 1 ) = y[1]*z[2]-z[1]*y[2], ( 2 ) = z[1]*x[2]-x[1]*z[2], ( 3 ) = x[1]*y[2]-y[1]*x[2] ] )