Matrix and vector operations

The operations on vectors includes scalar multiplication, implemented by * and / (where the second operand must be the scalar) and vector addition + and -. The unary operations + and - also work, where the second negates each entry in the vector.

The operations on matrices include all the operations on vectors, but also includes a new operator for matrix-vector and matrix-matrix multiplication. That new operator is the . operator, so to multiply two matrices $A$ and $B$, you use A.B and to multiply a matrix by a vector $\textbf{v}$, you use A.v.

For square matrices, there is also integer exponentiation, so you can calculate A^n where n is any integer, and where A^(-1) calculates the inverse.

[> A := <<1.2, 3.5>|<3.5, 4.6>>;

$A := \begin{pmatrix} 1.2 & 3.5 \\ 3.5 & 4.6 \end{pmatrix}$

[> A^2;

$\begin{pmatrix} 13.6900000000000 & 20.3000000000000 \\ 20.3000000000000 & 33.4100000000000 \end{pmatrix}$

[> A^(-1);

$\begin{pmatrix} -0.683506686478454 & 0.520059435364042 \\ 0.520059435364042 & -0.178306092124814 \end{pmatrix}$

[> B := <<4.3, 2.1>|<3.9, 0.7>>;

$B := \begin{pmatrix} 4.3 & 3.9 \\ 2.1 & 0.7 \end{pmatrix}$

[> A.B;

$\begin{pmatrix} 12.5100000000000 & 7.13000000000000 \\ 24.7100000000000 & 16.8700000000000 \end{pmatrix}$

[> B.A;

$\begin{pmatrix} 18.8100000000000 & 32.9900000000000 \\ 4.97000000000000 & 10.5700000000000 \end{pmatrix}$

[> A + B;

$\begin{pmatrix} 5.50000000000000 & 7.40000000000000 \\ [5.60000000000000 & 5.30000000000000 \end{pmatrix}$

[> 3.2*A - 2.7*B;

$\begin{pmatrix} -7.77000000000000 & 0.670000000000000 \\ 5.53000000000000 & 12.8300000000000 \end{pmatrix}$

One item that sets Maple apart is that when a scalar is added to a matrix, that scalar is interpreted as the scalar times the identity matrix. In this case, the characteristic polynomial of $A$ is $\lambda^2 - 5.8\lambda - 6.73$, so we see here that this characteristic polynomial evaluated at $A$ gives a matrix that is very close to the zero matrix:

[> A^2 - 5.8*A - 6.73;

$\begin{pmatrix} -0.888178419700125 10^{-15} & -0.355271367880050 10^{-14} \\ -0.355271367880050 10^{-14} & 0 \end{pmatrix}$

Maple can also do this with algebraic matrices:

[> C := <<x + y, y>|<x, 0>>;

$C := \begin{pmatrix} x + y & x \\ y & 0 \end{pmatrix}$

In this case, the characteristc polynomial is $\lambda^2 - (x + y)\lambda - xy$, so we see that:

[> C^2 - (x + y)*C - x*y;

$-xy + \begin{pmatrix} xy & 0 \\ 0 & xy \end{pmatrix}$

The issue here is that Maple does not know if $x$ and $y$ are scalars (after all, they could be matrices), but simplify(...) assumes that they are scalars:

[> simplify( % );

$\begin{pmatrix} 0 & 0 \\ 0 & 0 \end{pmatrix}$

The justification for introducing . for matrix-matrix and matrix-vector multiplication is that Maple assumes * is a commutative operation, so if you enter A*B - B*A, this will automatically simplify to 0, even if the matrices do not commute:

[> A.B - B.A;

$\begin{pmatrix} -6.30000000000000 & -25.8600000000000 \\ 19.7400000000000 & 6.30000000000000 \end{pmatrix}$

As for vectors, recall that u is likely aliased to the unit step function, so:

[> v := <5.9, 4.1>>;

$v := \begin{pmatrix} 5.9 \\ 4.1 \end{pmatrix}$

[> w := <-3.8, 0.6>>;

$w := \begin{pmatrix} -3.8 \\ 0.6 \end{pmatrix}$

[> -v + 2.7*w;

$\begin{pmatrix} -16.1600000000000 \\ -2.48000000000000 \end{pmatrix}$

[> A.%;

$\begin{pmatrix} -28.0720000000000 \\ -67.9680000000000 \end{pmatrix}$

[> 4.9*A.v - 3.4*B.w;

$\begin{pmatrix} 152.607000000000 \\ 219.303000000000 \end{pmatrix}$

One last operation is the inner product, and for this, the . is also used. The result is, of course, a scalar:

[> v.w;

$-19.96$