Matrices may be created as follows:
If the rest of values in a row are zero, you may use a ;
and begin entering the next row. Zeros will be added:
The following functions also create matrices:
matrix(r,c,v)
returns a new matrix with r
rows, c
columns, and all entries are equal to value v
.
matrixd(n,v)
returns a new diagonal matrix of size n
and all diagonal entries have a value v
.
matrixd(n)
. If n
is an integer, it returns a new identity matrix of size n
.
matrixd(a)
. If a
is an array, it returns a diagonal matrix such that the diagonal terms are the entries of array a
.
Matrices can also be created by creating any entry:
You may use functions joinright()
and joinbottom()
to augment matrices. A typical session follows.
Use function submatrix()
to build a smaller matrix from an existing one. Functions row()
and column()
are special cases of function submatrix()
.
Also note that column matrices and row matrices are treated like arrays too.
Use the +
and -
operators provided the matrices have the same dimensions.
Operator *
(matrix multiplication) works as long as the inner dimensions of the matrices are the same.
The following functions can be used with matrix arguments:
b=inverse(a)
returns the inverse of matrix a
.
b=pseudoinv(a)
returns the pseudo inverse of matrix a
.
r=determinant(a)
returns the determinant of matrix a
.
r=trace(a)
returns the trace of matrix a
(sum of diagonal terms).
transpose(v)
returns the transpose of matrix v
.
x = solve(a,b)
solves the linear set of equations A*x=B.
apply(a, f)
transforms matrix a
by applying function f
to each of the entries.
Use function solve()
to solve a set of linear equations of the form:
where both A and B are matrices with equal number of rows.
There are three cases to consider. Assuming matrix A has r
rows and c
columns, we show each case with an example.
Given A
and B
:
We solve this systems as follows:
Calcugator uses an LU Decomposition to solve the system of equations. Matrices A
and B
can be real or complex.
Let's create a matrix with 3 rows and 5 columns with random integers between 0 and 10:
The systems is solved using the same function as above:
To solve the system Calcugator uses the pseudo inverse of matrix A
computed with a Singular Value Decomposition. Both matrices A
and B
must be real.
Given A
and B
:
Again, use function solve
:
In this case, Calcugator finds the least squares solution. This solution minimizes the norm of A*X-B. Both matrices A
and B
must be real.
Three types of matrix decompositions are supported.
A matrix A (square or rectangular matrix with more rows than columns) that has full rank, can be decomposed into the product:
where Q is an orthonormal matrix and R is upper triangular. Use function qrgetQ
or function qrgetR
to compute those matrices.
Example:
For properties of matrices Q
and R
, consult a book on linear Algebra.
A square matrix A (or rectangular with more rows than columns), can be decomposed into the product:
where U and V are orthonormal and S is diagonal. Use functions svdgetU()
, svdgetS()
and svdgetV()
to compute those matrices.
Example:
The norm returned by function normf()
returns the Frobenius norm of the matrix.
A square matrix A can be decomposed into the following:
where S is a matrix such that its columns are the eigenvectors and L is a diagonal matrix containing the eigenvalues. Use functions eigenmatrix()
and eigenvalues()
to compute those matrices.
Example:
Notice all imaginary values are small; let's take the real part of the matrix:
Any matrix A can be decomposed as the multiplication of a lower triangular matrix L times and upper triangular matrix U. If row permutations were required, then a permutation matrix P needs to premultiply the product L*U in order to retrieve A. To obtain matrices L, U and P use functions lugetL()
, lugetU()
and lugetP()
respectively.
To find the echelon form of a matrix, use function echelon()
.
To find the reduced echelon form of a matrix, use function echelonr()
Function nullspace
returns a matrix that spans the null space of a matrix.
Function solven()
solves exactly for a matrix equation A*X=B for the case that the number of columns of matrix A is bigger than the number of rows. Function solven()
returns a solution such that adding a linear combination of columns of the null space, the equality still hods.
The following functions are implemented:
m=minor(a,i,j)
returns the minor of matrix a
corresponding to the entry at row i
and column j
.
m=cofactor(a,i,j)
returns the cofactor of matrix a
corresponding to the entry at row i
and column j
. The cofactor is the minor times -1
to the power i+j
.
m=minormatrix(a,i,j)
returns the matrix obtained by deleting row i
and column j
of matrix a
. Note that minor(a,i,j)
is equal to the determinant of minormatrix(a,i,j)
.
L=eigenvalues(a)
returns a diagonal matrix with all eigenvalues of matrix a
. Matrix a
must be a real matrix.
V=eigenmatrix(a)
returns a square matrix with all eigenvectors of matrix a
. The eigenvectors are the columns of the returned matrix. Matrix a
must be a real matrix.
cond(a)
returns the condition number of matrix a
.
rank(a)
returns the rank of matrix a
.
norm1(a)
returns the norm1 of matrix a
.
norm2(a)
returns the norm2 of matrix a
.
normf(a)
returns the Frobenius norm of matrix a
.
norminf(a)
returns the infinity norm of matrix a
.
mexp(a)
returns a matrix equal to ea
. Matrix a
must be square.
msqrt(a)
returns the square root of matrix a
. Matrix a
must be square.
mlog(a)
returns the logarithm of matrix a
. Matrix a
must be square.
mpow(a, p)
returns the matrix equal to a
to the power p
. Matrix a
must be square.
When testing for singularity while finding an inverse, Calcugator uses a default tolerance equal to 1.e-40
.
To change this value do:
set("matrixSingularityTolerance", tol)
Where tol
is the new tolerance to be used in the check for singularity.
Example:
sets the tolerance to 1e-20
.
Other functions to use with matrices are:
c=row(a,i)
returns a row matrix (a matrix with only one row) equal to the ith
row of matrix a
.
c=column(a,i)
returns a column matrix (a matrix with only one column) equal to the ith
column of matrix a
.
m=submatrix(a, r1,c1, r2,c2)
returns a matrix obtained from row r1
to row r2
and from column c1
to column c2
of matrix a
.
isHermitian(a)
returns true
if matrix a
is Hermitian.
rowadd(a,i,j)
is an educational function. It adds row i
to row j
of matrix a
.
rowtimes(a,i,c)
is an educational function. It multiplies row i
of matrix a
times values c
.
Matrices with random values can be created using the randommatrix()
function.
a=randommatrix( rws, cls )
returns a matrix with rws
rows and cls
columns with real random values between 0.
and 1.0
.
a=randommatrix( rws, cls, value )
returns a matrix with rws
rows and cls
columns with random values. If value
is real, all values are real between 0.
and value
. If value
is integer, all values are integers between 0
and value
inclusive.
a=randommatrix( rws, cls, value1, value2 )
returns a matrix with rws
rows and cls
columns with random values. If any of the values is real, all values are real between value1
and value2
. If both values are integer, all values are integer between value1
and value2
inclusive.
Example:
A random matrix with real values between 0. and 5.:
A random matrix with integer values between 3 and 7: