Matrix functions
A matrix is a square array, that is an array that has two dimensions of the same length. It can also have other dimensions, not necessarily of the same length. Matrix functions perform a variety of operations of matrices and other arrays.
Standard mathematical notation for matrix algebra, and most computer languages that work with matrices, distinguish rows and columns. In Analytica, rows and columns are not basic to an array: They are just ways you can choose to display an array in a table. Instead, it uses a named index to label each dimension. So, when using a matrix function, like any Analytica function that work with arrays, you specify the dimensions you want it to operate over by naming the index(es) as a parameter(s). For example:
Transpose(X, I, J)
This exchanges indexes I and J for matrix X. You don’t need to remember or worry about which dimension is the rows and which the columns. X can also have other indexes, and the function generalizes appropriately.
Dot product of two matrices
The dot product (i.e., matrix multiplication) of MatrixA and MatrixB is equal to:
Sum(MatrixA * MatrixB, i)
where i is the common index.
Example:
Variable MatrixA :=
i ▶ | |||
---|---|---|---|
j ▼ | 1 | 2 | 3 |
a | 4 | 1 | 2 |
b | 2 | 5 | 3 |
c | 3 | 2 | 7 |
Variable MatrixB :=
i ▶ | |||
---|---|---|---|
k ▼ | 1 | 2 | 3 |
l | 3 | 2 | 1 |
m | 2 | 5 | 3 |
n | 4 | 1 | 2 |
Sum(MatrixA*MatrixB, i) →
j ▶ | |||
---|---|---|---|
k ▼ | 1 | 2 | 3 |
l | 16 | 19 | 20 |
m | 19 | 38 | 37 |
n | 21 | 19 | 28 |
MatrixMultiply(a, aRow, aCol, b, bRow, bCol)
Performs a matrix multiplication on matrix a, having indexes aRow and aCol, and matrix b, having indexes bRow and bCol. The result is indexed by aRow and bCol. a and b must have the specified two indexes, and can also have other indexes. aCol and bRow must have the same length or it flags an error. If aRow and bCol are the same index, it returns only the diagonal of the result.
Library: Matrix
Example: Matrices
C
index2 ▶ | ||
---|---|---|
index1 ▼ | 1 | 2 |
1 | 1 | 2 |
2 | 1 | 0 |
x
D
index3 ▶ | |||
---|---|---|---|
index2 ▼ | a | b | c |
1 | 3 | 0 | 1 |
2 | 0 | 1 | 1 |
MatrixMultiply(C, index1, index2, D, index2, index3) →
index3 ▶ | |||
---|---|---|---|
index1 ▼ | a | b | c |
1 | 3 | 2 | 3 |
2 | 3 | 0 | 1 |
When the inner index is shared by C and D, the expression Sum(C*D, index2)
is equivalent to their dot product.
MatrixMultiply(A, I, J, Transpose(A, I, J), I, J)
It does not work to use MatrixMultiply(A, I, J, A, J, I)
because the result would have
to be doubly indexed by I
.
Transpose(c, i, j)
Returns the transpose of matrix c exchanging dimensions i and j, which must be indexes of the same size.
Library: Matrix
Example:
Transpose(MatrixA, i, j) →
i ▶ | |||
---|---|---|---|
j ▼ | 1 | 2 | 3 |
a | 4 | 2 | 3 |
b | 1 | 5 | 2 |
c | 2 | 3 | 7 |
The conjugate transpose of a complex matrix is obtained using
Transpose(Conj(c), i, j)
Identity matrix
Given two indexes of equal length, the identity matrix is obtained by the expression (@i = @j).
Example:
@i = @j →
Transpose(MatrixA, i, j) →
i ▶ | |||
---|---|---|---|
j ▼ | 1 | 2 | 3 |
a | 1 | 0 | 0 |
b | 0 | 1 | 0 |
c | 0 | 0 | 1 |
Unit vector
A unit vector on index i is obtained by the expression Array(i,1)
. There is no need to differentiate between a row vector and column vector, since it is the index that determines its orientation.
Example:
Array(i,1) →
i ▶ | ||
---|---|---|
1 | 2 | 3 |
1 | 1 | 1 |
Invert(c, i, j)
Returns the inversion of matrix c along dimensions i and j.
Library: Matrix
Example: Set number format to fixed point, 3 decimal digits.
Invert(MatrixA, i, j) →
i ▶ | |||
---|---|---|---|
j ▼ | 1 | 2 | 3 |
a | 0.326 | -0.034 | -0.079 |
b | -0.056 | 0.247 | -0.090 |
c | -0.124 | -0.056 | 0.202 |
Determinant(c, i, j)
Returns the determinant of matrix c along dimensions i and j.
Library: Matrix
Example:
Determinant(MatrixA, i, j) → 89
MatrixA
is defined above.
Decompose(c, i, j)
Returns the Cholesky decomposition (square root) matrix of matrix c along dimensions i and j. Matrix c must be symmetric and positive-definite. (Positive-definite means that v*C*v > 0
, for all vectors v
.)
Cholesky decomposition computes a lower diagonal matrix L such that L*L' = C
, where L'
is the transpose of L
.
Library: Matrix
Example:
Matrix
M ▶ | |||||
---|---|---|---|---|---|
L ▼ | 1 | 2 | 3 | 4 | 5 |
1 | 2.4495 | 0 | 0 | 0 | 0 |
2 | 0.8165 | 1.8257 | 0 | 0 | 0 |
3 | 2.4495 | 0.5477 | 1.6432 | 0 | 0 |
4 | 1.2247 | -0 | -0 | 2.5495 | 0 |
5 | 0.4082 | 1.4606 | 1.3389 | 1.3728 | 1.0113 |
Note: The computed result includes values that are nearly zero (-1.2e-16 and -2.7e-16),
possibly as a result of floating point round-off error. When displayed with Fixed Point
number format to four digits, these display as -0.
Enable comment auto-refresher