Vectors

Page Contents

Vectors

Whereas a matrix is a two-dimensional array of numbers, a vector is a one-dimensional array. In they are defined as mathyma.Vector objects, and can be defined in a similar way to matricies, either as text or arrays:

```< script >
mathyma.Matrix_Precision = 0;
var vecA = new mathyma.Vector ("1,2,3");
document.write("< p > #~a _ _ = _ _  " + vecA.McPrint() + "< /p > ");
var arrB = new Array(3,2,1);
var vecB = new mathyma.Vector (arrB);
document.write("< p > #~b _ _ = _ _  " + vecB.McPrint() + "< /p > ");
< /script >
```

Note that McPrint() can also be used to display vectors.

Scalar Multiplication of a Vector

```< script >
document.write("< p > #~a _ _ = _ _  " + vecA.McPrint() + "< /p > ");
document.write("< p > 3#~a _ _ = _ _  " + vecA.ScalarMult(3).McPrint() + "< /p > ");
< /script >
```

```< script >
document.write("< p > #~a _ _ = _ _  " + vecA.McPrint() + "< /p > ");
document.write("< p > #~b _ _ = _ _  " + vecB.McPrint() + "< /p > ");
document.write("< p > #~a - #~b _ _ = _ _  " + vecA.Minus(vecB).McPrint() + "< /p > ");
document.write("< p > #~a + #~b _ _ = _ _  " + vecA.Plus(vecB).McPrint() + "< /p > ");
< /script >
```

The two vectors must have the same length.

Note that you can use .Subtract( ) or .Difference( ) instead of .Minus( ), and .Add( ) or .Sum( ) instead of .Plus( ).

Scalar or Dot Product of Vectors

```< script >
document.write("< p > #~a _ _ = _ _  " + vecA.McPrint() + "< /p > ");
document.write("< p > #~b _ _ = _ _  " + vecB.McPrint() + "< /p > ");
document.write("< p > #~a &dot. #~b _ _ = _ _  " + vecA.Dot(vecB) + "< /p > ");
< /script >
```

The two vectors must have the same length.

The dot product is the sum of the product of the respective coordinates of the two vectors: _ #~a &dot. #~b _ = _ sum{~a_~i~b_~i,~i = 1,~n}

Norm of a Vector

```< script >
document.write("< p > #~a _ _ = _ _  " + vecA.McPrint() + "< /p > ");
document.write("< p > || #~a || _ _ = _ _  " + formatDec(vecA.Norm(),2) + "< /p > ");
< /script >
```

The norm of a vector is the square root of the dot product of the vector with itself: _ | #~a | _ = _ &sqrt.\${ #~a &dot. #~a }

Angle Between Two Vectors

```< script >
document.write("< p > #~a _ _ = _ _  " + vecA.McPrint() + "< /p > ");
document.write("< p > #~b _ _ = _ _  " + vecB.McPrint() + "< /p > ");
document.write("< p > angle between #~a and #~b  _ _ = _ _  " + formatDec(vecA.Angle(vecB),2) + " radians" +
"  _ _ = _ _  " + formatDec(vecA.Angle(vecB)*180/Math.PI,2) + "&deg.< /p > ");
< /script >
```

The angle between #~a and #~b is defined as: _ cos^{-1} rndb{fract{ #~a &dot. #~b , | #~a | | #~b | }}

For two and three dimensional vectors, this will be the physical angle between the two vectors. The value can vary between 0 and 180 degrees.

Vector or Cross Product of Vectors

```< script >
document.write("< p > #~a _ _ = _ _  " + vecA.McPrint() + "< /p > ");
document.write("< p > #~b _ _ = _ _  " + vecB.McPrint() + "< /p > ");
document.write("< p > #~a # #~b _ _ = _ _  " + vecA.Cross(vecB).McPrint() + "< /p > ");
< /script >
```

The cross product is always a three-dimensional vector. Vectors of any dimension above three can be used in cross products, but only the first three coordinates are used:

#~a # #~b _ = _ ( ( ~a_2 ~b_3 - ~a_3 ~b_2 ) , ( ~a_3 ~b_1 - ~a_1 ~b_3 ) , ( ~a_1 ~b_2 - ~a_2 ~b_1 ) )

Row and Column Vectors

An n-dimensional row vector is a 1×n matrix, while an n-dimensional column vector is a n×1 matrix.

Although all vector operations can be performed as matrix operations, (for example if #~a is a 3-dimensional column vector then #~a &dot. #~a = #~a^T#~a), you may still want to consider the rows or columns of a matrix as vectors, and perform vector operations on these. Two mathyma.Matrix methods, Row and Col, convert rows and columns to vectors.

```< script >
mathyma.Matrix_Precision = 0;
var wMatA = new mathyma.Matrix ("1,2/4,5,6/7,8,9");
document.write("< p > #A _ _ = _ _ " + wMatA.McPrint() + "< /p > ");
document.write("< p > row 2 _ = _ " + wMatA.Row(2).McPrint() + "< /p > ");
document.write("< p > column 3 _ = _ " + wMatA.Col(3).McPrint() + "< /p > ");
document.write("< p > row 2 - column 3 _ = _ " + wMatA.Row(2).Minus(wMatA.Col(3)).McPrint() + "< /p > ");
< /script >
```