Matrices are handled in a similar way, only with two indices for row number (first index) and column number (second index). Rows are separated by either a semicolon or a linefeed during input.
All operators be applied to vectors and matrices. If scalar, per-element operation is desired, some operators (* / ^) must be preceded by a point to distinguish them from the quite different linear-algebra versions of these operations (see chapter 2.9). Further useful functions are sum(vector) and prod(vector) which return the sum and product of the vectors elements.
The following matrix operations are available:
These operations are also applicable for vectors. These matrix operations apply to scalars (1-by-1 matrices) as well. If the sizes of the matrices are incompatible for the matrix operation, an error message will result, except in the case of scalar-matrix operations (for addition, subtraction, and division as well as for multiplication) in which case each entry of the matrix is operated on by the scalar.
The colon notation can be used to access sub-matrices of a matrix. For example,
is the column vector consisting of the first four entries of the third column of A. A colon by itself denotes an entire row or column:
is the third column of A, and A(1:4,:) is the first four rows. Arbitrary integral vectors can be used as subscripts:
contains as columns, columns 2 and 4 of A. Such subscripting can be used on both sides of an assignment statement:
A(:,[2 4 5]) = B(:,1:3)
replaces columns 2,4,5 of b with the first three columns of B. Note that the entire altered matrix A is printed and assigned. Try it.
Columns 2 and 4 of A can be multiplied on the right by the 2-by-2 matrix [1 2;3 4]:
A(:,[2,4]) = A(:,[2,4])*[1 2;3 4]
Once again, the entire altered matrix is printed and assigned.
Here is the list of matrix and vector operations:
|linspace($var_1$,$var_2$,COUNT)||vector with COUNT numbers ranging from $var_1$ to $var_2$|
|length($vector$)||number of elements in $vector$|
|zeros(ROWS[,COLUMNS])||matrix of zeros|
|ones(ROWS[,COLUMNS])||matrix of ones|
|eye(ROWS[,COLUMNS])||matrix with diagonal one|
|rand(ROWS[,COLUMNS])||matrix of random numbers|
|size($matrix$)||number of rows and columns|
|sum($var$)||if $var$ is a vector: sum of elements, if $var$ is a matrix: sum of columns|
|find($var$)||indices of non-vanishing elements|
|max($var$)||largest element in $var$|
|min($var$)||smallest element in $var$|
|diag($var$,[OFFSET])||if $var$ is a vector: matrix with $var$ as diagonal, if $var$ is matrix: diagonal as vector|
|svd($matrix$)||singular value decomposition (Lapack)|
For example, zeros(m,n) produces an m-by-n matrix of zeros and zeros(n) produces an n-by-n one.
Several standard matrices are created by means of functions without specifying individual elements: ones(n,m), zeros(n,m), rand(n,m) return matrices with elements 1, 0 or random numbers between 0 and 1. eye(n,m) has diagonal elements 1, else 0, and hilb(n) creates the n-th degree Hilbert-matrix.
The following functions are provided for matrix calculations: diag(x) (extracts diagonal elements), det(x) (determinante), eig(x) (eigenvalues), inv(x) (inverse), pinv(x) (pseudo-inverse). The adjunct matrix is created using the operator'.
The nontrivial functions are all based on the LU-decomposition, which is also accessible as a function call lu(x). It has 2 or 3 return values, therefor the left side of the equation must provide multiple variables, see example below:
Without preceding point the arithmetic operators function as matrix operators, e.g. * corresponds to matrix and vector multiplication.
If one of the arguments is a scalar datatype, the operation is repeated for each element of the other argument:
Matrix division corresponds to multiplication by the pseudo-inverse. Using the operator backslash leads to left-division, which can be used to solve systems of linear equations:
Systems of linear equations can (and should) be solved directly with the function linsolve(A,b).
The application contains JLAPACK , the Java-port of the LAPACK -routines with extended and better algorithms for matrix calculations. However, these are limited to matrices with real coefficients in floating point format. The LAPACK routines are accessed by the following functions:
svd(A) Singular value decomposition of A (1 or 3 return values).
qr(A) QR-decomposition of A (2 return values).
linsolve2( A, b)
Solves $A\cdot x=b$ (1 return value). Example in chapter 2.13.1.
linlstsq(A, b) Solves $A\cdot x=b$, overdetermined (1 return value).
eigen(A) Eigenvalues of A (1 return value).
We calculate the 4-th degree regression polynomial for the following x,y-data:
The coefficients p(1),p(2),p(3) should vanish since x and y represent a perfect straight line. This is an unstable problem, and it can be easily extended to make Jasymca completely fail. In our second attempt we use the Lapack-routine linlstsq:
The coefficients p(1),p(2),p(3) are now significantly smaller. This particular problem can be solved exactly using Jasymca-routines and exact numbers, which avoids any rounding errors: