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}
Use \pmatrix instead of \matrix, if you want to have a matrix with round parantheses instead of square brackets

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.2 Matrix Operations

Supported Matrix Operations are:

  • Scalar multiplication
  • Matrix multiplication
  • Hadamard product (element-wise product)
  • Hadamard division (element-wise division)
  • Addition and substraction
  • Inverse
  • Transpose
  • Adjoint
  • Determinant
  • Trace
  • Norm
  • Minimum and maximum (gives the smallest or the biggest entry of a matrix as result)
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
Use \pmatrix instead of \matrix, if you want to have a matrix with round parantheses instead of square brackets
\begin{variables}
...
\matrix{C}{[ [1;2];[3;4] ]} % 2x2 Matrix
\matrix{D}{2*C}          % Scalar multiplication
\matrix{E}{C*D}          % Matrix multiplication
\matrix{F}{C.*D}         % Hadamard product
\matrix{G}{C./D}         % Hadamard division
\matrix{H}{D+E-C}        % addition and substraction
\matrix{I}{inv(C)}       % inverse of matrix, will throw exception if matrix is not invertible
\matrix{J}{transpose(C)} % adjoint of matrix
\matrix{K}{adjoint(C)}   % adjoint of matrix
\matrix{L}{det(C)}       % determinant of C, result is 1x1 matrix
\matrix{M}{trace(C)}     % trace of C, result is 1x1 matrix
\matrix{N}{norm(C)}      % norm 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
\function{fl}{norm(C)}   % norm of C but as a function
\function{fm}{min(C)}    % smalles entry of C
\function{fn}{max(C)}    % biggest entry of C
...
\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}              % result is [ [2*1;2*2];[2*3;2*4] ]
\matrix[calculate]{Dcalc}{D} % result is [ [2;4] ; [6;8] ]
\end{variables}
...

See Matrix Algebra Example in Webmiau, or learn how to create input.matrix questions here

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 0
  • 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:3]}              % result is [ [2;3] ; [5;6] ]
\matrix{E}{C[1,0:3]}                % result is [ [4;5;6] ]
\matrix{F]{C[:,0]}                  % result is the first column of all rows [ [1+x] ; [4] ]
\matrix{G}{C[1:]}                   % result is [ [4;5;6] ]
\matrix{H}{C[:,0:3:2]}              % result is [ [1+x;3] ; [4;6] ]
...
\end{variables}

See Indexing and Slicing Example in Webmiau, or learn how to create input.matrix questions here