Functions
The following functions are available globally.
-
Create a matrix of zeros.
Declaration
Swift
public func zeros(_ rows: Int, _ cols: Int) -> Matrix
Parameters
rows
number of rows
cols
number of columns
Return Value
zeros matrix of specified size
-
Create a matrix of ones.
Declaration
Swift
public func ones(_ rows: Int, _ cols: Int) -> Matrix
Parameters
rows
number of rows
cols
number of columns
Return Value
ones matrix of specified size
-
Create a matrix of uniformly distributed on [0, 1) interval random values.
Declaration
Swift
public func rand(_ rows: Int, _ cols: Int) -> Matrix
Parameters
rows
number of rows
cols
number of columns
Return Value
random values matrix of specified size
-
Create a matrix of normally distibuted random values.
Declaration
Swift
public func randn(_ rows: Int, _ cols: Int) -> Matrix
Parameters
rows
number of rows
cols
number of columns
Return Value
random values matrix of specified size
-
Create a matrix with ones on the main diagonal and zeros elsewhere.
Declaration
Swift
public func eye(_ rows: Int, _ cols: Int) -> Matrix
Parameters
rows
number of rows
cols
number of columns
Return Value
identity matrix of specified size
-
Create a matrix with specified values on the main diagonal and zeros elsewhere.
Parameters
rows
number of rows
cols
number of columns
v
matrix of values with one column
Return Value
diagonal matrix with specified values and size
-
Compute the trace of a matrix.
Declaration
Swift
public func trace(_ A: Matrix) -> Double
Parameters
A
matrix to compute trace of
Return Value
sum of the elements on the main diagonal
-
Raise matrix to specified power (integer value).
If power is 1, source matrix is returned If power is -1, inverted matrix is returned If power is > 1, continuous matrix product result is returned (eg
mpower(A, 2) = mtimes(A, A)
) If power is < -1, continuous matrix product of inverted matrix result is returned All other values are invalidAlternatively,
mpower(A, p)
can be executed withA ^ p
.Return Value
matrix A raised to power p
-
Raise matrix to specified power (integer value).
If power is 1, source matrix is returned If power is -1, inverted matrix is returned If power is > 1, continuous matrix product result is returned (eg
A ^ 2 = mtimes(A, A)
) If power is < -1, continuous matrix product of inverted matrix result is returned All other values are invalidAlternatively,
A ^ p
can be executed withmpower(A, p)
.Return Value
matrix A raised to power p
-
Compute eigen values and vectors of a given square matrix.
A precondition error is thrown if the algorithm fails to converge.
Parameters
A
square matrix to calculate eigen values and vectors of
Return Value
eigenvectors matrix (by rows) and diagonal matrix with eigenvalues on the main diagonal
-
Perform a generalized singular value decomposition of 2 given matrices.
Declaration
Parameters
A
first matrix
B
second matrix
Return Value
matrices U, V, and Q, plus vectors alpha and beta
-
Compute the Cholesky factorization of a real symmetric positive definite matrix.
A precondition error is thrown if the algorithm fails to converge.
Parameters
A
square matrix to compute Cholesky factorization of
t
Triangle value (.Upper, .Lower)
Return Value
upper triangular matrix U so that
A = U' * U
or lower triangular matrix L so thatA = L * L'
-
Compute the determinant of a given square matrix.
A precondition error is thrown if the given matrix is singular or algorithm fails to converge.
Declaration
Swift
public func det(_ A: Matrix) -> Double
Parameters
A
square matrix=
Return Value
determinant of A matrix
-
Return the result of convolving the given matrix with the provided kernel.
Declaration
Parameters
A
Matrix
K
Matrix (Needs to be smaller in dimension than A)
Return Value
Matrix result of the convolution
-
Perform matrix left division.
Alternatively,
A ./. B
can be executed withldivide(A, B)
.
-
Perform matrix and scalar addition.
Scalar value expands to matrix dimensions and elementwise matrix addition is performed.
Alternatively,
plus(A, b)
can be executed withA + b
.Return Value
elementwise sum of matrix A and scalar b
-
Perform scalar and matrix addition.
Scalar value expands to matrix dimensions and elementwise matrix addition is performed.
Alternatively,
plus(a, B)
can be executed witha + B
.Return Value
elementwise sum of scalar a and matrix B
-
Perform matrix and scalar substraction.
Scalar value expands to matrix dimensions and elementwise matrix substraction is performed.
Alternatively,
minus(A, b)
can be executed withA - b
.Return Value
elementwise difference of matrix A and scalar b
-
Perform matrix and scalar substraction.
Scalar value expands to matrix dimensions and elementwise matrix substraction is performed.
Alternatively,
A - b
can be executed withminus(A, b)
.Return Value
elementwise difference of matrix A and scalar b
-
Perform scalar and matrix substraction.
Scalar value expands to matrix dimensions and elementwise matrix addition is performed.
Alternatively,
minus(a, B)
can be executed witha - B
.Return Value
elementwise difference of scalar a and matrix B
-
Perform scalar and matrix substraction.
Scalar value expands to matrix dimensions and elementwise matrix addition is performed.
Alternatively,
a - B
can be executed withminus(a, B)
.Return Value
elementwise difference of scalar a and matrix B
-
Perform matrix and scalar multiplication.
Scalar value expands to matrix dimensions and elementwise matrix multiplication is performed.
Alternatively,
times(A, b)
can be executed withA .* b
.Return Value
elementwise product of matrix A and scalar b
-
Perform matrix and scalar multiplication.
Scalar value expands to matrix dimensions and elementwise matrix multiplication is performed.
Alternatively,
A .* b
can be executed withtimes(A, b)
.Return Value
elementwise product of matrix A and scalar b
-
Perform scalar and matrix multiplication.
Scalar value expands to matrix dimensions and elementwise matrix multiplication is performed.
Alternatively,
times(a, B)
can be executed witha .* B
.Return Value
elementwise product of scalar a and matrix B
-
Perform scalar and matrix multiplication.
Scalar value expands to matrix dimensions and elementwise matrix multiplication is performed.
Alternatively,
a .* B
can be executed withtimes(a, B)
.Return Value
elementwise product of scalar a and matrix B
-
Perform matrix and scalar right division.
Scalar value expands to matrix dimensions and elementwise matrix right division is performed.
Alternatively,
rdivide(A, b)
can be executed withA ./ b
.Return Value
result of elementwise division of matrix A by scalar b
-
Perform matrix and scalar right division.
Scalar value expands to matrix dimensions and elementwise matrix right division is performed.
Alternatively,
A ./ b
can be executed withrdivide(A, b)
.Return Value
result of elementwise division of matrix A by scalar b
-
Perform scalar and matrix right division.
Scalar value expands to matrix dimensions and elementwise matrix right division is performed.
Alternatively,
rdivide(a, B)
can be executed witha ./ B
.Return Value
result of elementwise division of scalar a by matrix B
-
Perform scalar and matrix right division.
Scalar value expands to matrix dimension and elementwise matrix right division is performed.
Alternatively,
a ./ B
can be executed withrdivide(a, B)
.Return Value
result of elementwise division of scalar a by matrix B
-
Perform matrix and scalar left division.
Scalar value expands to matrix dimensions and elementwise matrix left division is performed.
Alternatively,
ldivide(A, b)
can be executed withA ./. b
.Return Value
result of elementwise division of scalar b by matrix A
-
Perform scalar and matrix left division.
Scalar value expands to matrix dimensions and elementwise matrix left division is performed.
Alternatively,
ldivide(a, B)
can be executed witha ./. B
.Return Value
result of elementwise division of matrix B by scalar a
-
Perform matrix and vector addition.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
plus(A, b)
can be executed withA + b
.Return Value
elementwise sum of matrix A and vector b
-
Perform matrix and vector addition.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
A + b
can be executed withplus(A, b)
.Return Value
elementwise sum of matrix A and vector b
-
Perform vector and matrix addition.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
plus(a, B)
can be executed witha + B
.Return Value
elementwise sum of vector a and matrix B
-
Perform vector and matrix addition.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
a + B
can be executed withplus(a, B)
.Return Value
elementwise sum of vector a and matrix B
-
Perform matrix and vector substraction.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
minus(A, b)
can be executed withA - b
.Return Value
elementwise difference of matrix A and vector b
-
Perform matrix and vector substraction.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
A - b
can be executed withminus(A, b)
.Return Value
elementwise difference of matrix A and vector b
-
Perform vector and matrix substraction.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
minus(a, B)
can be executed witha - B
.Return Value
elementwise difference of vector a and matrix B
-
Perform vector and matrix substraction.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
a - B
can be executed withminus(a, B)
.Return Value
elementwise difference of vector a and matrix B
-
Perform matrix and vector multiplication.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
times(A, b)
can be executed withA .* b
.Return Value
elementwise product of matrix A and vector b
-
Perform matrix and vector multiplication.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
A .* b
can be executed withtimes(A, b)
.Return Value
elementwise product of matrix A and vector b
-
Perform vector and matrix multiplication.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
times(a, B)
can be executed witha .* B
.Return Value
elementwise product of vector a and matrix B
-
Perform vector and matrix multiplication.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
a .* B
can be executed withtimes(a, B)
.Return Value
elementwise product of vector a and matrix B
-
Perform matrix and vector right division.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
rdivide(A, b)
can be executed withA ./ b
.Return Value
result of elementwise division of matrix A by vector b
-
Perform matrix and vector right division.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
A ./ b
can be executed withrdivide(A, b)
.Return Value
result of elementwise division of matrix A by vector b
-
Perform vector and matrix right division.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
rdivide(a, B)
can be executed witha ./ B
.Return Value
result of elementwise division of vector a by matrix B
-
Perform vector and matrix right division.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
a ./ B
can be executed withrdivide(a, B)
.Return Value
result of elementwise division of vector a by matrix B
-
Perform matrix and vector left division.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
ldivide(A, b)
can be executed withA ./. b
.Return Value
result of elementwise division of vector b by matrix A
-
Perform vector and matrix left division.
Vector value expands to matrix dimensions (by rows) and elementwise matrix addition is performed.
Alternatively,
ldivide(a, B)
can be executed witha ./. B
.Return Value
result of elementwise division of matrix B by vector a
-
Exponentiation function, returning matrix raised to power.
Alternatively,
power(A, p)
can be executed withA .^ p
.Mathematically,
power
would return a complex number when base is negative and power is not an integral value.power
can’t do that, so instead it signals domain error (returns±NaN
).Return Value
elementwise matrix power of a raised to p
-
Exponentiation function, returning matrix raised to power.
Alternatively,
A .^ p
can be executed withpower(A, p)
.Mathematically,
power
would return a complex number when base is negative and power is not an integral value.power
can’t do that, so instead it signals domain error (returns±NaN
).Return Value
elementwise matrix power of a raised to p
-
Exponentiation function, returning square root of matrix.
Mathematically,
sqrt
would return a complex number when base is negative.sqrt
can’t do that, so instead it signals domain error (returns±NaN
).Return Value
elementwise square root of matrix A
-
Compute the natural logarithm of
A
whereexp(log(A))
equalsA
, exactly in mathematics and approximately in C.If x is negative, log signals a domain error (returns
NaN
). If x is zero, it returns negative infinity (-Inf
); if x is too close to zero, it may signal overflow.Return Value
elementwise natural logarithm of matrix A
-
Compute the least squares solution to the overdetermined linear system A*X = B with full rank matrix A.
A precondition errors throw for mismatched matrix dimensions or if algorithm fails.
Requires M (rows of A) >= N (cols of A).
Parameters
A
MxN matrix
B
MxK matrix
Return Value
- X: NxK solution matrix
- R: 1xK residuals
-
Return the tangent of
A
, whereA
is given in radians.Mathematically, the tangent function has singularities at odd multiples of pi/2. If the argument x is too close to one of these singularities, tan will return extremely large value.
Return Value
tangent of a matrix values
-
Create a vector of zeros.
Declaration
Swift
public func zeros(_ count: Int) -> Vector
Parameters
count
number of elements
Return Value
zeros vector of specified size
-
Create a vector of ones.
Declaration
Swift
public func ones(_ count: Int) -> Vector
Parameters
count
number of elements
Return Value
ones vector of specified size
-
Create a vector of uniformly distributed on [0, 1) interval random values.
Declaration
Swift
public func rand(_ count: Int) -> Vector
Parameters
count
number of elements
Return Value
random values vector of specified size
-
Create a vector of normally distributed random values.
Declaration
Swift
public func randn(_ count: Int) -> Vector
Parameters
count
number of elements
Return Value
random values vector of specified size
-
Perform vector and scalar addition.
Scalar value expands to vector dimension and elementwise vector addition is performed.
Alternatively,
plus(a, b)
can be executed witha + b
.Return Value
elementwise sum of vector a and scalar b
-
Perform scalar and vector addition.
Scalar value expands to vector dimension and elementwise vector addition is performed.
Alternatively,
plus(a, b)
can be executed witha + b
.Return Value
elementwise sum of scalar a and vector b
-
Perform vector and scalar substraction.
Scalar value expands to vector dimension and elementwise vector substraction is performed.
Alternatively,
minus(a, b)
can be executed witha - b
.Return Value
elementwise difference of vector a and scalar b
-
Perform vector and scalar substraction.
Scalar value expands to vector dimension and elementwise vector substraction is performed.
Alternatively,
a - b
can be executed withminus(a, b)
.Return Value
elementwise difference of vector a and scalar b
-
Perform scalar and vector substraction.
Scalar value expands to vector dimension and elementwise vector addition is performed.
Alternatively,
minus(a, b)
can be executed witha - b
.Return Value
elementwise difference of scalar a and vector b
-
Perform scalar and vector substraction.
Scalar value expands to vector dimension and elementwise vector addition is performed.
Alternatively,
a - b
can be executed withminus(a, b)
.Return Value
elementwise difference of scalar a and vector b
-
Perform vector and scalar multiplication.
Scalar value expands to vector dimension and elementwise vector multiplication is performed.
Alternatively,
times(a, b)
can be executed witha .* b
.Return Value
elementwise product of vector a and scalar b
-
Perform vector and scalar multiplication.
Scalar value expands to vector dimension and elementwise vector multiplication is performed.
Alternatively,
a .* b
can be executed withtimes(a, b)
.Return Value
elementwise product of vector a and scalar b
-
Perform scalar and vector multiplication.
Scalar value expands to vector dimension and elementwise vector multiplication is performed.
Alternatively,
times(a, b)
can be executed witha .* b
.Return Value
elementwise product of scalar a and vector b
-
Perform scalar and vector multiplication.
Scalar value expands to vector dimension and elementwise vector multiplication is performed.
Alternatively,
a .* b
can be executed withtimes(a, b)
.Return Value
elementwise product of scalar a and vector b
-
Perform vector and scalar right division.
Scalar value expands to vector dimension and elementwise vector right division is performed.
Alternatively,
rdivide(a, b)
can be executed witha ./ b
.Return Value
result of elementwise division of vector a by scalar b
-
Perform vector and scalar right division.
Scalar value expands to vector dimension and elementwise vector right division is performed.
Alternatively,
a ./ b
can be executed withrdivide(a, b)
.Return Value
result of elementwise division of vector a by scalar b
-
Perform scalar and vector right division.
Scalar value expands to vector dimension and elementwise vector right division is performed.
Alternatively,
rdivide(a, b)
can be executed witha ./ b
.Return Value
result of elementwise division of scalar a by vector b
-
Perform scalar and vector right division.
Scalar value expands to vector dimension and elementwise vector right division is performed.
Alternatively,
a ./ b
can be executed withrdivide(a, b)
.Return Value
result of elementwise division of scalar a by vector b
-
Perform vector and scalar left division.
Scalar value expands to vector dimension and elementwise vector left division is performed.
Alternatively,
ldivide(a, b)
can be executed witha ./. b
.Return Value
result of elementwise division of scalar b by vector a
-
Perform scalar and vector left division.
Scalar value expands to vector dimension and elementwise vector left division is performed.
Alternatively,
ldivide(a, b)
can be executed witha ./. b
.Return Value
result of elementwise division of vector b by scalar a
-
Exponentiation function, returning vector raised to power.
Alternatively,
power(a, p)
can be executed witha .^ p
.Mathematically,
power
would return a complex number when base is negative and power is not an integral value.power
can’t do that, so instead it signals domain error (returns±NaN
).Return Value
elementwise vector power of a raised to p
-
Exponentiation function, returning vector raised to power.
Alternatively,
a .^ p
can be executed withpower(a, p)
.Mathematically,
power
would return a complex number when base is negative and power is not an integral value.power
can’t do that, so instead it signals domain error (returns±NaN
).Return Value
elementwise vector power of a raised to p
-
Exponentiation function, returning square root of vector.
Mathematically,
sqrt
would return a complex number when base is negative.sqrt
can’t do that, so instead it signals domain error (returns±NaN
).Return Value
elementwise square root of vector a
-
Compute the natural logarithm of
a
whereexp(log(a))
equalsa
, exactly in mathematics and approximately in C.If x is negative, log signals a domain error (returns
NaN
). If x is zero, it returns negative infinity (-Inf
); if x is too close to zero, it may signal overflow.Return Value
elementwise natural logarithm of vector a
-
Return the largest element of vector.
Declaration
Swift
public func max(_ a: Vector) -> Double
Return Value
largest element of vector a
-
Return the index of largest element of vector.
Declaration
Swift
public func maxi(_ a: Vector) -> Int
Return Value
index of largest element of vector a
-
Return the smallest element of vector.
Declaration
Swift
public func min(_ a: Vector) -> Double
Return Value
smallest element of vector a
-
Return the index of smallest element of vector.
Declaration
Swift
public func mini(_ a: Vector) -> Int
Return Value
index of smallest element of vector a
-
Return mean (statistically average) value of vector.
Declaration
Swift
public func mean(_ a: Vector) -> Double
Return Value
mean value of vector a
-
Return standard deviation value of vector.
Declaration
Swift
public func std(_ a: Vector) -> Double
Return Value
standard deviation value of vector a
-
Return sum of vector’s elements.
Declaration
Swift
public func sum(_ a: Vector) -> Double
Return Value
sum of elements of vector a
-
Return sum of vector’s squared elements.
Declaration
Swift
public func sumsq(_ a: Vector) -> Double
Return Value
sum of squared elements of vector a
-
Return the tangent of
a
, wherea
is given in radians.Mathematically, the tangent function has singularities at odd multiples of pi/2. If the argument x is too close to one of these singularities, tan will return extremely large value.
Return Value
tangent of a vector values