class

LA::Matrix(T)

Inherits LA::LapackHelper / Enumerable / Reference / Object

class that provide all utility matrix functions

Class methods

arange(start_val : T, end_val : T, delta = 1.0)

Create row from start_val...end_val with step of delta between

Source
block_diag(*args)

Create a block diagonal matrix from provided matrices

Given the inputs A, B and C, the output will have these matrices arranged on the diagonal:

Example:

m = Mat.block_diag(a, b, c)

m will have following structure:

[[a, 0, 0],
 [0, b, 0],
 [0, 0, c]]
Source
circulant(c)

Construct a Circulant matrix

c - first column of matrix

Example:

a = circulant([1, 2, 3])
a.to_aa # => [[1, 3, 2],[2, 1, 3],[3, 2, 1]])

Behaviour copied from scipy

Source
column(values)

Returns single column matrix with elements taken from array values

Source
companion(a)

Create a companion matrix associated with the polynomial whose coefficients are given in a

Behaviour copied from scipy

Example:

Mat.companion([1, -10, 31, -30]) # =>
# LA::GeneralMatrix(Float64) (3x3, None):
# [10.0, -31.0, 30.0]
# [1.0, 0.0, 0.0]
# [0.0, 1.0, 0.0]
Source
dft(n, scale : DFTScale = DFTScale::None)
Source
diag(nrows, ncolumns, value : Number | Complex)

Returns diagonal matrix of given size with all diagonal elements equal to value

Source
diag(nrows, ncolumns, values)

Returns diagonal matrix of given size with diagonal elements taken from array values

Source
diag(values)

Returns square diagonal matrix with diagonal elements taken from array values

Source
diag(nrows, ncolumns, &)

Returns diagonal matrix of given size with diagonal elements equal to block value

Source
eye(n)

returns identity matrix of size n

Source
fiedler(values)

Returns a symmetric Fiedler matrix

Given an sequence of numbers values, Fiedler matrices have the structure f[i, j] = (values[i] - values[j]).abs, and hence zero diagonals and nonnegative entries. A Fiedler matrix has a dominant positive eigenvalue and other eigenvalues are negative.

Behaviour copied from scipy

Example:

Mat.fiedler([1, 4, 12, 45, 77]) # =>
# LA::GeneralMatrix(Float64) (5x5, Symmetric | Hermitian):
# [0.0, 3.0, 11.0, 44.0, 76.0]
# [3.0, 0.0, 8.0, 41.0, 73.0]
# [11.0, 8.0, 0.0, 33.0, 65.0]
# [44.0, 41.0, 33.0, 0.0, 32.0]
# [76.0, 73.0, 65.0, 32.0, 0.0]
Source
from_custom(str : String, prefix, columns_separator, rows_separator, postfix)

Converts a string of given format to matrix Example:

str = "( 1,2,3 | 4,5,6 | 7,8,9 )"
LA::GMat.from_custom(str, "(", ",", "|", ")")
Source
from_custom(io, prefix, columns_separator, rows_separator, postfix)

Converts a string of given format to matrix Example:

str = "( 1,2,3 | 4,5,6 | 7,8,9 )"
LA::GMat.from_custom(IO::Memory.new(str), prefix: "(", columns_separator: ",", rows_separator: "|", postfix: ")")
Source
from_matlab(s)

Converts a string from matlab format to matrix Example:

str = "[1,2,3; 4,5,6; 7,8,9]"
LA::GMat.from_matlab(str)
Source
hadamard(n)

Constructs an n-by-n Hadamard matrix, n must be power of 2

Behaviour copied from scipy

Example:

Mat.hadamard(4) # =>
# LA::GeneralMatrix(Float64) (4x4,  Symmetric | Hermitian):
# [1.0, 1.0, 1.0, 1.0]
# [1.0, -1.0, 1.0, -1.0]
# [1.0, 1.0, -1.0, -1.0]
# [1.0, -1.0, -1.0, 1.0]
Source
hankel(column : Indexable | Matrix, row : Indexable | Matrix | Nil = nil)

Create a Hankel matrix The Hankel matrix has constant anti-diagonals, with column as its first column and row as its last row. If `row is nil, then row with elements equal to zero is assumed. Behaviour copied from scipy Examples:

a = Mat.hankel([1, 17, 99])
a.to_aa # => [
# [ 1, 17, 99],
# [17, 99,  0],
# [99,  0,  0]]
a = Mat.hankel([1, 2, 3, 4], [4, 7, 7, 8, 9])
a.to_aa # => [
# [1, 2, 3, 4, 7],
# [2, 3, 4, 7, 7],
# [3, 4, 7, 7, 8],
# [4, 7, 7, 8, 9]]
Source
helmert(n, full = false)

Create an Helmert matrix of order n

If full is true the (n x n) matrix will be returned. Otherwise the submatrix that does not include the first row will be returned

Behaviour copied from scipy

Example:

Mat.helmert(5, full: true) # =>
# LA::GeneralMatrix(Float64) (5x5, Orthogonal):
# [0.4472135954999579, 0.4472135954999579, 0.4472135954999579, 0.4472135954999579, 0.4472135954999579]
# [0.7071067811865476, -0.7071067811865476, 0.0, 0.0, 0.0]
# [0.408248290463863, 0.408248290463863, -0.816496580927726, 0.0, 0.0]
# [0.28867513459481287, 0.28867513459481287, 0.28867513459481287, -0.8660254037844386, 0.0]
# [0.22360679774997896, 0.22360679774997896, 0.22360679774997896, 0.22360679774997896, -0.8944271909999159]
Source
hilbert(n)

Create a Hilbert matrix of order n.

Returns the n by n matrix with entries h[i,j] = 1 / (i + j + 1).

Behaviour copied from scipy

Example:

Mat.hilbert(3) # =>
# LA::GeneralMatrix(Float64) (3x3, Symmetric | Hermitian | PositiveDefinite):
# [1.0, 0.5, 0.3333333333333333]
# [0.5, 0.3333333333333333, 0.25]
# [0.3333333333333333, 0.25, 0.2]
Source
identity(n)

returns identity matrix of size n

Source
invhilbert(n)

Compute the inverse of the Hilbert matrix of order n.

The entries in the inverse of a Hilbert matrix are integers.

Behaviour copied from scipy

Example:

Mat.invhilbert(4) # => LA::GeneralMatrix(Float64) (4x4, Symmetric | Hermitian | PositiveDefinite):
# [16.0, -120.0, 240.0, -140.0]
# [-120.0, 1200.0, -2700.0, 1680.0]
# [240.0, -2700.0, 6480.0, -4200.0]
# [-140.0, 1680.0, -4200.0, 2800.0]

Mat.invhilbert(16)[7, 7] # => 4.247509952853739e+19
Source
invpascal(n, kind : PascalKind = PascalKind::Symmetric)

Returns the inverse of the n x n Pascal matrix

The Pascal matrix is a matrix containing the binomial coefficients as its elements.

kind : see PascalKind

Behaviour copied from scipy

Example:

Mat.invpascal(4) # =>
# LA::GeneralMatrix(Float64) (5x5, Symmetric | Hermitian | PositiveDefinite):
# [5.0, -10.0, 10.0, -5.0, 1.0]
# [-10.0, 30.0, -35.0, 19.0, -4.0]
# [10.0, -35.0, 46.0, -27.0, 6.0]
# [-5.0, 19.0, -27.0, 17.0, -4.0]
# [1.0, -4.0, 6.0, -4.0, 1.0]
Mat.invpascal(5, PascalKind::Lower) # =>
# LA::GeneralMatrix(Float64) (5x5, LowerTriangular):
# [1.0, 0.0, 0.0, 0.0, 0.0]
# [-1.0, 1.0, 0.0, 0.0, 0.0]
# [1.0, -2.0, 1.0, 0.0, 0.0]
# [-1.0, 3.0, -3.0, 1.0, 0.0]
# [1.0, -4.0, 6.0, -4.0, 1.0]
Source
kron(a, b)

Returns kroneker product of matrices

Resulting matrix size is {a.nrows*b.nrows, a.ncolumns*b.ncolumns}

Source
leslie(f, s)

Create a Leslie matrix

Given the length n array of fecundity coefficients f and the length n-1 array of survival coefficients s, return the associated Leslie matrix.

Behaviour copied from scipy

Example:

Mat.leslie([0.1, 2.0, 1.0, 0.1], [0.2, 0.8, 0.7]) # =>
# LA::GeneralMatrix(Float64) (4x4, None):
# [0.1, 2.0, 1.0, 0.1]
# [0.2, 0.0, 0.0, 0.0]
# [0.0, 0.8, 0.0, 0.0]
# [0.0, 0.0, 0.7, 0.0]
Source
load_csv(filename)

Loads a matrix from CSV (comma separated values) file Example:

LA::GMat.rand(30, 30).save_csv("./test.csv")
a = LA::GMat.load_csv("./test.csv")
Source
ones(nrows, ncolumns)

Generate matrix of given size with all elements equal to one

Source
pascal(n, kind : PascalKind = PascalKind::Symmetric)

Returns the n x n Pascal matrix.

The Pascal matrix is a matrix containing the binomial coefficients as its elements.

kind : see PascalKind

Behaviour copied from scipy

Example:

Mat.pascal(4) # =>
# LA::GeneralMatrix(Float64) (4x4, Symmetric | Hermitian | PositiveDefinite):
# [1.0, 1.0, 1.0, 1.0]
# [1.0, 2.0, 3.0, 4.0]
# [1.0, 3.0, 6.0, 10.0]
# [1.0, 4.0, 10.0, 20.0]
Mat.pascal(4, PascalKind::Lower) # =>
# LA::GeneralMatrix(Float64) (4x4, LowerTriangular):
# [1.0, 0.0, 0.0, 0.0]
# [1.0, 1.0, 0.0, 0.0]
# [1.0, 2.0, 1.0, 0.0]
# [1.0, 3.0, 3.0, 1.0]
Source
rand(nrows, ncolumns, rng = Random::DEFAULT)

Generate matrix of given size with elements randomly distributed from range 0.0..1.0

Source
repmat(a : Matrix(T), nrows, ncolumns)

Alias for #repmat

Source
row(values)

Returns single row matrix with elements taken from array values

Source
toeplitz(column : Indexable | Matrix, row : Indexable | Matrix | Nil = nil)

Create a Toeplitz matrix

column - first column of matrix

row - first row of matrix (if nil, it is assumed that row = column.map(&.conj))

Behaviour copied from scipy

Example:

MatComplex.toeplitz([1, 2, 3], [1, 4, 5, 6]) # =>
# LA::GeneralMatrix(Float64) (3x4, None):
# [1.0, 4.0, 5.0, 6.0]
# [2.0, 1.0, 4.0, 5.0]
# [3.0, 2.0, 1.0, 4.0]
MatComplex.toeplitz([1.0, 2 + 3.i, 4 - 1.i]) # =>
# LA::GeneralMatrix(Complex) (3x3, Hermitian):
# [1.0 + 0.0i, 2.0 - 3.0i, 4.0 + 1.0i]
# [2.0 + 3.0i, 1.0 + 0.0i, 2.0 - 3.0i]
# [4.0 - 1.0i, 2.0 + 3.0i, 1.0 + 0.0i]
Source
tri(nrows, ncolumns, k = 0)

Construct (nrows, ncolumns) matrix filled with ones at and below the kth diagonal.

The matrix has A[i,j] == 1 for j <= i + k

k - Number of subdiagonal below which matrix is filled with ones. k = 0 is the main diagonal, k < 0 subdiagonal and k > 0 superdiagonal.

Behaviour copied from scipy

Example:

Mat.tri(3, 5, 2).to_aa # => [[
# [1, 1, 1, 0, 0],
# [1, 1, 1, 1, 0],
# [1, 1, 1, 1, 1]]
Mat.tri(3, 5, -1).to_aa # => [[
# [0, 0, 0, 0, 0],
# [1, 0, 0, 0, 0],
# [1, 1, 0, 0, 0]]
Source
zeros(nrows, ncolumns)

Generate matrix of given size with all elements equal to zero

Source

Instance methods

*(k : Number)

Multiplies matrix to scalar

Source
*(k : Complex)

Multiplies matrix to scalar

Source
*(m : Matrix(T))

Matrix product to given m

Raises ArgumentError if inner dimensions do not match

This method automatically calls optimal function depending on MatrixFlags.

If one of the matrix is square and triangular - trmm is called

If one of the matrix is symmetric\hermitian - symm/hemm is called

Otherwise - gemm is called

Source
**(other : Int)

Raises the square matrix to the integer power other

Implementation taken from https://github.com/Exilor/matrix/

Source
+(k : Number)

Adds scalar value to every element

Source
+(k : Complex)

Adds scalar value to every element

Source
+(m : Matrix(T))

Returns element-wise sum

This method raises if another matrix doesn't have same size

Source
-(k : Number | Complex)

Substracts scalar value from every element

Source
-(m : Matrix(T))

Returns element-wise substract

This method raises if another matrix doesn't have same size

Source
-

Multiplies matrix to -1

Source
/(k : Number | Complex)

Divides matrix to scalar

Source
==(other)

Compare with another matrix

Returns True only if all element are exactly equal. Use #almost_eq If you need approximate equality

Source
[](i : Int32, j : Int32)

Return element of matrix on row i and column j

If i or j negative they are counted from end of matrix If i>=nrows or j>=ncolumns exception is raised Use #unsafe_fetch if you need to skip these checks

Source
[](arows : Range(Int32 | Nil, Int32 | Nil), acolumns : Range(Int32 | Nil, Int32 | Nil))

Return submatrix over given ranges.

See SubMatrix(T) for details on submatrices Example:

m = GMat32.new([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
m1 = m[1..3, 2..3] # => [[7.0, 8.0], [11.0, 12.0], [15.0, 16.0]]
m2 = m[..3, 2]     # => [[3.0], [7.0], [11.0], [15.0]]
Source
[](row : Int32, acolumns : Range(Int32 | Nil, Int32 | Nil))

Return submatrix over given ranges.

See SubMatrix(T) for details on submatrices Example:

m = GMat32.new([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
m1 = m[1..3, 2..3] # => [[7.0, 8.0], [11.0, 12.0], [15.0, 16.0]]
m2 = m[..3, 2]     # => [[3.0], [7.0], [11.0], [15.0]]
Source
[](arows : Range(Int32 | Nil, Int32 | Nil), column : Int32)

Return submatrix over given ranges.

See SubMatrix(T) for details on submatrices Example:

m = GMat32.new([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
m1 = m[1..3, 2..3] # => [[7.0, 8.0], [11.0, 12.0], [15.0, 16.0]]
m2 = m[..3, 2]     # => [[3.0], [7.0], [11.0], [15.0]]
Source
[]=(i : Int32, j : Int32, value)

Assign element of matrix on row i and column j

If i or j negative they are counted from end of matrix If i>=nrows or j>=ncolumns exception is raised Use #unsafe_set if you need to skip these checks Note this method reset all matrix flags

Source
[]=(arows : Range(Int32, Int32), acolumns : Range(Int32, Int32), value)

Assign value to a given submatrix

value can be a scalar or a matrix of same size as affected submatrix

Source
[]=(row : Int32, acolumns : Range(Int32, Int32), value)

Assign value to a given submatrix

value can be a scalar or a matrix of same size as affected submatrix

Source
[]=(nrows : Range(Int32, Int32), column : Int32, value)

Assign value to a given submatrix

value can be a scalar or a matrix of same size as affected submatrix

Source
abs(kind : MatrixNorm = MatrixNorm::Frobenius)

Alias for #norm

Source
add!(k : Number, m : Matrix)

Perform inplace addition with matrix m multiplied to scalar k

a.add!(k, b) is equal to a = a + k * b, but faster as no new matrix is allocated

Source
add!(m)

Performs inplace addition with matrix m

a.add!(b) is equal to a = a + b, but faster as no new matrix is allocated

Source
add_mult(a, b : Matrix(T), *, alpha = 1.0, beta = 1.0)

performs c = alphaab + beta*c (BLAS routines gemm/symm/hemm)

Source
almost_eq(other : Matrix(T), eps)

Approximately compare with other matrix

Returns true if all elements are within eps from corresponding elements of other matrix

Source
almost_eq(other : Matrix(T))

Approximately compare with other matrix

Returns true if all elements are within eps from corresponding elements of other matrix

Uses #tolerance as an eps by default

Source
assume!(flag : MatrixFlags, value : Bool = true)

Directly set or reset matrix flag without check See MatrixFlags for description of flags

Source
balance(*, permute = true, scale = true, separate = false)
Source
balance!(*, permute = true, scale = true, separate = false)
Source
cat(other : Matrix(T), axis : Axis)

Returns concatenation with another matrix by Axis::Rows (horizontal) or Axis::Columns (vertical)

Source
cho_solve(b : self, *, overwrite_b = false)
Source
cholesky(*, lower = false, dont_clean = false)
Source
cholesky!(*, lower = false, dont_clean = false)
Source
chop(eps = self.tolerance)

Converts complex matrix to real one if all imaginary parts are less then eps, returns nil otherwise

Returns just matrix if it is already real

Source
clear_flags

Reset matrix flags to None Usually is done automatically, but this method could be needed if internal content was changed using to_unsafe

Source
columns

Returns Indexable(SubMatrix(T)) that allows iterating over columns

Source
conjt

alias to #conjtranspose

Source
conjt!

alias to #conjtranspose!

Source
conjtranspose

Returns conjurgate transposed matrix

result is same as #transpose for real matrices

Source
coshm
Source
cosm

optimization idea for noncomplex matrix is from scipy

Source
det(*, overwrite_a = false)

Calculates determinant for a square matrix

if overwrite_a is true, a will be overriden in process of calculation

Uses getrf LAPACK routine

Source
detect(aflags : MatrixFlags = MatrixFlags::All, eps = tolerance)

Detect if given aflags are true or flase for a matrix with tolerance eps Update flags property See MatrixFlags for description of matrix flags Returns self for method chaining

Source
detect?(aflags : MatrixFlags = MatrixFlags::All, eps = tolerance)

Detect if given aflags are true or flase for a matrix with tolerance eps Update flags property See MatrixFlags for description of matrix flags Returns True if all given flags are set

Source
diag(offset = 0)

Returns Indexable(T) that allow iterating over k-th diagonal of matrix

Example:

m = GMat32.new([[-1, 2, 3, 4],
                [5, -6, 7, 8],
                [9, 10, -11, 12]])
m.diag(0).to_a.should eq [-1, -6, -11]
m.diag(1).to_a.should eq [2, 7, 12]
m.diag(2).to_a.should eq [3, 8]
m.diag(3).to_a.should eq [4]
expect_raises(ArgumentError) { m.diag(4) }
m.diag(-1).to_a.should eq [5, 10]
m.diag(-2).to_a.should eq [9]
expect_raises(ArgumentError) { m.diag(-3) }
expect_raises(ArgumentError) { m.diag(-4) }
Source
each(*, all = false, &)

Yields every element of matrix

all argument controls whether to yield all or non-empty elements for banded\sparse matrices Example: m.each { |v| raise "negative element found" if v < 0 }

Source
each_index(*, all = false, &)

Yields every index

all argument controls whether to yield all or non-empty elements for banded\sparse matrices Example: m.each_index { |i, j| m[i, j] = -m[i, j] }

Source
each_lower(*, diagonal = true, all = false, &)

For every element of matrix that is below main diagonal it yields a block with value and with corresponding row and column

This method is useful for symmetric matrices and similar cases include_diagonal argument controls whether to include elements on main diagonal all argument controls whether to yield all or non-empty elements for banded\sparse matrices

Source
each_upper(*, diagonal = true, all = false, &)

For every element of matrix that is above main diagonal it yields a block with value and with corresponding row and column

This method is useful for symmetric matrices and similar cases include_diagonal argument controls whether to include elements on main diagonal all argument controls whether to yield all or non-empty elements for banded\sparse matrices

Source
each_with_index(*, all = false, &)

Yields every element of matrix with corresponding row and column

all argument controls whether to yield all or non-empty elements for banded\sparse matrices Example: m.each_with_index { |v, i, j| m2[i, j] = v }

Source
eigs(*, left = false, overwrite_a = false)
Source
eigs(*, need_left : Bool, need_right : Bool, overwrite_a = false)
Source
eigs(*, b : Matrix(T), need_left : Bool, need_right : Bool, overwrite_a = false, overwrite_b = false)

generalized eigenvalues problem

Source
eigvals(*, overwrite_a = false)
Source
expm(*, schur_fact = false)

Computes matrix exponential

It exploits triangularity (if any) of A. if schur_fact is true, function uses an initial transformation to Schur form.

Reference: A. H. Al-Mohy and N. J. Higham, A New Scaling and Squaring Algorithm for the Matrix Exponential, SIAM J. Matrix Anal. Appl. 31(3): 970-989, 2009. Awad H. Al-Mohy and Nicholas J. Higham, April 20, 2010.

Source
flags

Returns flags of matrix (see MatrixFlags)

Source
hcat(other)

Returns horizontal concatenation with another matrix

Source
hessenberg
Source
hessenberg(*, calc_q = false)
Source
hessenberg!
Source
hessenberg!(*, calc_q = false)
Source
inspect(io)

Converts matrix to string for inspection

Output looks like:

GeneralMatrix(Float64) (10x10, MatrixFlags::None)
[1, 2, 3, .... 10]
[11, 12, 13, .... 20]
...
[91, 92, 93, .... 100]
Source
inv

Calculate matrix inversion See #inv! for details on algorithm

Source
inv!

Calculate matrix inversion inplace Method selects optimal algorithm depending on MatrixFlags transpose returned if matrix is orthogonal trtri is used if matrix is triangular potrf, potri are used if matrix is positive definite hetrf, hetri are used if matrix is hermitian sytrf, sytri are used if matrix is symmetric getrf, getri are used otherwise

Source
kron(b : Matrix(T))

Returns kroneker product with matrix b

Source
lq(*, overwrite_a = false)
Source
lq_r(*, overwrite_a = false)
Source
lstsq(b : self, method : LSMethod = LSMethod::Auto, *, overwrite_a = false, overwrite_b = false, cond = -1)
Source
lu(*, overwrite_a = false)

Compute pivoted LU decomposition of a matrix

If you call p,l,u = a.lu for a matrix m*n a then

  • p will be m*m permutation matrix
  • l will be m*k lower triangular matrix with unit diagonal. K = min(M, N)
  • u will be k*n upper triangular matrix
  • (p*l*u) will be equal to a (within calculation tolerance)

if overwrite_a is true, a will be overriden in process of calculation

Uses getrf LAPACK routine

Source
lu_factor

Compute pivoted LU decomposition of a matrix and returns it in a compact form, useful for solving linear equation systems.

Uses getrf LAPACK routine

Source
lu_factor!

Compute pivoted LU decomposition of a matrix and returns it in a compact form, useful for solving linear equation systems.

Overrides source matrix in a process of calculation

Uses getrf LAPACK routine

Source
map

Returns result of appliyng block to every element (without index)

Source
map!

Yields each element (without index) and replace it with returned value

Source
map_with_index

Returns result of appliyng block to every element with index

Source
map_with_index!

Yields each element with index and replace it with returned value

Source
max(axis : Axis)

Calculate maximum over given axis

Source
min(axis : Axis)

Calculate minimum over given axis

Source
ncolumns

Returns number of columns in matrix

Source
norm(kind : MatrixNorm = MatrixNorm::Frobenius)

returns matrix norm

kind defines type of norm

Uses LAPACK routines lantr, lanhe, lansy, lange depending on matrix flags

Source
nrows

Returns number of rows in matrix

Source
pinv

Calculate Moore–Penrose inverse of a matrix

https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse Implemented using an svd decomposition

Source
product(axis : Axis)

Perform product over given axis

Source
ql(*, overwrite_a = false)
Source
ql_r(*, overwrite_a = false)
Source
qr(*, overwrite_a = false, pivoting = false)
Source
qr_r(*, overwrite_a = false, pivoting = false)
Source
qr_raw(*, overwrite_a = false, pivoting = false)
Source
qz(b, overwrite_a = false, overwrite_b = false)
Source
rank(eps = self.tolerance, *, method : RankMethod = RankMethod::SVD, overwrite_a = false)

determine effective rank either by SVD method or QR-factorization with pivoting

if overwrite_a is true, a will be overriden in process of calculation

QR method is faster, but could fail to determine rank in some cases

Source
reduce(axis : Axis, initial, &)

Perform reduce from initial value over given axis

Source
repmat(arows, acolumns)

Return matrix repeated arows times by vertical and acolumns times by horizontal

Source
rows

Returns Indexable(SubMatrix(T)) that allows iterating over rows

Source
rq(*, overwrite_a = false)
Source
rq_r(*, overwrite_a = false)
Source
save_csv(filename)

Save a matrix to CSV (comma separated values) file Example:

LA::GMat.rand(30, 30).save_csv("./test.csv")
a = LA::GMat.load_csv("./test.csv")
Source
scale!(k : Number | Complex)

Perform inplace multiplication to scalar k

Source
schur(*, overwrite_a = false)
Source
shape

Returns shape of matrix in a form of tuple {nrows, ncolumns}

Source
sinhm
Source
sinm
Source
size

Returns shape of matrix in a form of tuple {nrows, ncolumns}

Source
solve(b : self, *, overwrite_a = false, overwrite_b = false)

Solves matrix equation self*x = b and returns x

Method returns matrix of same size as b

Matrix must be square, number of rows must match b

if overwrite_a is true, a will be overriden in process of calculation

if overwrite_b is true, b will be overriden in process of calculation

Uses LAPACK routines trtrs, posv, hesv, sysv, gesv depending on matrix flags

Source
solvels(b : self, *, overwrite_a = false, overwrite_b = false, cond = -1)
Source
square?

Returns True if matrix is square and False otherwise

Source
sum(axis : Axis)

Perform sum over given axis

Source
svd(*, overwrite_a = false)

Calculates singular value decomposition for a matrix

If you call u,s,vt = a.svd for a matrix m*n a then

  • u will be m*m matrix
  • s will be Array(T) with size equal to {m, n}.min
  • vt will be n*n matrix
  • (u*Mat.diag(a.nrows, a.ncolumns, s)*vt) will be equal to a (within calculation tolerance)

if overwrite_a is true, a will be overriden in process of calculation

Uses gesdd LAPACK routine

Source
svdvals(*, overwrite_a = false)

Calculates array of singular values for a matrix

if overwrite_a is true, a will be overriden in process of calculation

Uses gesdd LAPACK routine

Source
t

alias to #transpose

Source
t!

alias to #transpose!

Source
tanhm
Source
tanm
Source
to_custom(io, prefix, columns_separator, rows_separator, postfix)

to_custom(io, "[", ",", "],[", "]") Converts a matrix to string with custom format. Example:

a = LA::GMat[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
str = String.build do |io|
  a.to_custom(io, prefix: "(", columns_separator: ",", rows_separator: "|", postfix: ")")
end
str # => "(1,2,3|4,5,6|7,8,9)"
Source
to_custom(prefix, columns_separator, rows_separator, postfix)

to_custom(io, "[", ",", "],[", "]") Converts a matrix to string with custom format. Example:

a = LA::GMat[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
str = a.to_custom(prefix: "(", columns_separator: ",", rows_separator: "|", postfix: ")")
str # => "(1,2,3|4,5,6|7,8,9)"
Source
to_general

Creates general matrix with same content. Useful for banded\sparse matrices

Source
to_imag

Converts complex matrix to imaginary part

Source
to_matlab(io)

Converts a matrix to matlab format

Source
to_matlab

Converts a matrix to matlab format string Example:

LA::GMat[[1, 2, 3], [4, 5, 6], [7, 8, 9]].to_matlab # => "[1,2,3; 4,5,6; 7,8,9]"
Source
to_real

Converts complex matrix to real part

Source
to_s(io)

Converts matrix to string, with linefeeds before and after matrix:

Output looks like:

[1, 2, 3, .... 10]
[11, 12, 13, .... 20]
...
[91, 92, 93, .... 100]
Source
to_unsafe

Returns pointer to underlying data

Storage format depends of matrix type This method raises at runtime if matrix doesn't have raw pointer

Source
tolerance

Returns estimated tolerance of equality\inequality

This value is used by default in #almost_eq compare

Source
tr_mult!(a : Matrix(T), *, alpha = 1.0, left = false)

performs b = alphaab or b = alphaba (BLAS routine trmm)

a must be a square triangular GeneralMatrix(T)

if left is true, alpha*a*b is calculated, otherwise alpha*b*a

Source
trace

Returns sum of diagonal elements (trace of matrix)

Source
transpose

Returns transposed matrix

Source
tril(k = 0)

Same as tril in scipy - returns lower triangular or trapezoidal part of matrix

Returns a matrix with all elements above k-th diagonal zeroed

Source
tril!(k = 0)

Works like a tril in scipy - remove all elements above k-diagonal

Source
triu(k = 0)

Same as triu in scipy - returns upper triangular or trapezoidal part of matrix

Returns a matrix with all elements below k-th diagonal zeroed

Source
triu!(k = 0)

Works like a triu in scipy - remove all elements below k-diagonal

Source
vcat(other)

Returns vertical concatenation with another matrix

Source

Macros

lapack(name, *args, worksize = nil)

Complex utility macros that simplifies calling certain LAPACK functions It substitute first letter, allocate workareas, raise exception if return value is negative Check source to see supported functions Example:

# Calling *geev to calculate eigenvalues
lapack(geev, 'N'.ord.to_u8, 'N'.ord.to_u8, nrows, a, nrows,
  vals.to_unsafe.as(LibCBLAS::ComplexDouble*),
  Pointer(LibCBLAS::ComplexDouble).null, nrows,
  Pointer(LibCBLAS::ComplexDouble).null, nrows, worksize: [2*nrows])

Note that it should be called only from methods of Matrix or its descendants

Source
lapack_util(name, worksize, *args)

Utility macros that simplifies calling certain LAPACK functions Arguments that point to matrix should be passed as matrix(arg) Example:

# Calling *lange to calculate infinity-norm
lapack_util(lange, worksize, 'I', m.nrows, m.ncolumns, matrix(m), m.nrows)

Note that it should be called only from methods of Matrix or its descendants

Source