You're not reading the latest revision of this page, which is here.

Matrix Variables

How to create matrix variables

1. tex like syntax

To make a simple matrix variable you can use

\begin{variables}
...
\matrix{A}{
 1 & 2 \\ %1st row of 2x2 matrix
 3 & 4    %2nd row of 2x2 matrix
}
...
\end{variables}

you can also use number or function variables for the cells

\begin{variables}
\randint{a}{-5}{5}
\randint{b}{-5}{5}
\function[normalize]{c}{a+b}
\function[normalize]{d}{a-b}
\matrix{B}{
 a & b \\ %1st row of 2x2 matrix
 c & d    %2nd row of 2x2 matrix
}
...
\end{variables}

2. python like syntax

The python like syntax is created to make create more complex matrices.
It supports matrix functions such as addition, multiplication, inverse and so on.

2.1 creating a simple matrix

a matrix in the python like syntax is a collection of row vectors with same number of columns, separated by semicolon.

\begin{variables}
...
\matrix{C}{[ [1;2] ; [3;4] ]} % 2x2 Matrix
...
\end{variables}

you can also create row vector variables and use it in your matrix

\begin{variables}
...
\randint{a}{-5}{5}
\randint{b}{-5}{5}
\rowVector{row1}{[a;b]}
\matrix{C}{[ row1 ; [3;4] ]} % 2x2 Matrix
...
\end{variables}

2.1 Matrix Operations

Supported Matrix Operations are:

  • Scalar multiplication
  • Matrix multiplication
  • Addition and substraction
  • Inverse
  • Transpose
  • Adjoint
  • Determinant
  • Trace
An exception will be thrown if the matrix is not suitable for the operations (e.g inverse on not invertible matrices, unsuitable format on matrix multiplication)
The operations trace and determinant will result in a 1x1 matrix. You can also use them in a function variable
\begin{variables}
...
\matrix{C}{[[1;2];[3;4]} % 2x2 Matrix
\matrix{D}{2*C} % Scalar multiplication
\matrix{E}{C*D} % Matrix multiplication
\matrix{F}{D+E-C} % addition and substraction
\matrix{G}{inv(C)} % inverse of matrix, will throw exception if matrix is not invertible
\matrix{H}{transpose(C)} % adjoint of matrix
\matrix{I}{adjoint(C)} % adjoint of matrix
\matrix{J}{det(C)} % determinant of C, result is 1x1 matrix
\matrix{K}{trace(C)} % trace of C, result is 1x1 matrix
\function{fj}{det(C)} % determinant of C but as a function
\function{fk}{trace(C)} % trace of C but as a function
...
\end{variables}

The new syntax also supports function actions such as normalize, calculate, expand

\begin{variables}
...
\matrix{C}{[[1;2];[3;4]} % 2x2 Matrix
\matrix{D}{C*2} % [ [2*1;2*2];[2*3;2*4] ]
\matrix[calculate]{Dcalc}{D} % [ [2;4] ; [6;8] ]
\end{variables}
...

Indexing and Slicing

Indexing and slicing is zero based as in numpy. So the first row and the first column in a matrix have the index 0.

If the result is a 1x1 matrix, you can also use it as a function variable

\begin{variables}_
...
\matrix{C}{[[1+x;2;3];[4;5;6]} % 2x3 Matrix
\matrix{D}{C[0,0]} % entry in the first row and first column as a 1x1 matrix
\function{fd}{C[0,0]} % entry in the first row and first column as a function object
...
\end{variables}

The Basic syntax for slicing is i:j:k, with

  • i = the starting index, default is 1
  • j = the stopping index (not included), default is the number of rows/columns
  • k = the step, k!=0, default is 1
    k is optional, you can also only specify i:j, or just i:, or :j
    For 2 dimensional matrix you can specify up to 2 slicing arguments separated by comma (e.g D[row_i:row_j:row_k, col_i:col_j:col_k])
    if the second is not defined, then all columns will be selected
    \begin{variables}
    ...
    \matrix{C}{[ [1+x;2;3] ; [4;5;6] ]} % 2x3 Matrix
    \matrix{D}{C[0:1,1:2]} % result is [ [2;3] ; [5;6] ]
    \matrix{E}{C[1,0:3]} % result is [ [4;5;6] ]
    \matrix{F]{C[:,0]} % result is first column of all rows [ [1+x] ; [4] ]
    \matrix{G}{C[1:]} % [ [4;5;6] ]
    \matrix{H}{C[:,0:3:2] % [ [1+x;3] ; [4;6] ]
    ...
    \end{variables}