LA::GeneralMatrix(T)
Inherits LA::Matrix / LA::LapackHelper / Enumerable / Reference / Object
generic matrix, heap-allocated
Data are stored in column-major as this is a storage used by LAPACK
See SUPPORTED_TYPES for supported types
Constructors
Creates matrix with given size and populate elements from values
if col_major is true, values content is just copied to #raw,
otherwise a conversion from row-major form is performed
Example:
values = [1, 2, 3, 4]
a = GMat.new(2, 2, values)
a.to_aa # => [[1,2],[3,4]]
b = GMat.new(2, 2, values, col_major: true)
b.to_aa # => [[1,3],[2,4]]
Creates zero-initialized matrix of given size
Example: LA::GMat.new(4,4)
Creates matrix of given size and then call block to initialize each element
Example: LA::GMat.new(4,4){|i,j| i+j }
Creates matrix from any Indexable of Indexables
Example:
m = GMat.new([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12],
])
Class methods
Creates matrix from a number of columns
Example:
a = GMat.columns([1, 2, 3, 4], [5, 6, 7, 8])
a.to_aa # => [[1, 5], [2, 6], [3, 7], [4, 8]]
Returns diagonal matrix of given size with diagonal elements taken from array values
Returns diagonal matrix of given size with diagonal elements equal to block value
Creates matrix from a number of rows
Example:
a = GMat.rows([1, 2, 3, 4], [5, 6, 7, 8])
a.to_aa # => [[1,2,3,4], [5,6,7,8]]
Instance methods
Concatenate matrix adding another matrix by dimension Axis::Rows (horizontal) or Axis::Columns (vertical)
Conjurgate transposes matrix inplace
Currently, transpose of non-square matrix still allocates temporary buffer
Returns a matrix with different nrows and ncolumns but same elements (total number of elements must not change)
if col_major is true, just nrows and ncolumns are changed, data kept the same
Otherwise, elements are reordered to emulate row-major storage
Example:
a = GMat[[1, 2, 3], [4, 5, 6]]
a.reshape(2, 3).to_aa # => [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]
b = GMat[[1, 2, 3], [4, 5, 6]]
this is because in-memory order of values is (1, 4, 2, 5, 3, 6)
b.reshape(2, 3, col_major: true).to_aa # => [[1.0, 5.0], [4.0, 3.0], [2.0, 6.0]]
Changes nrows and ncolumns of matrix (total number of elements must not change)
if col_major is true, just nrows and ncolumns are changed, data kept the same
Otherwise, elements are reordered to emulate row-major storage
Example:
a = GMat[[1, 2, 3], [4, 5, 6]]
a.reshape!(2, 3)
a.to_aa # => [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]
b = GMat[[1, 2, 3], [4, 5, 6]]
b.reshape!(2, 3, col_major: true)
this is because in-memory order of values is (1, 4, 2, 5, 3, 6)
b.to_aa # => [[1.0, 5.0], [4.0, 3.0], [2.0, 6.0]]
Change number of rows and columns in matrix.
if new number is higher zero elements are added, if new number is lower, exceeding elements are lost
Converts matrix to plain array of elements
if col_major is true, elements are returned as stored inplace,
otherwise row-major storage is emulated
Converts matrix to array of array of elements
if col_major is true, elements are returned as stored inplace,
otherwise row-major storage is emulated
Example:
a = GMat[[1, 2], [3, 4]]
a.to_aa # => [[1.0, 2.0],[3.0, 4.0]]
a.to_aa(col_major: true) # => [[1.0, 3.0],[2.0, 4.0]]
transposes matrix inplace
Currently, transpose of non-square matrix still allocates temporary buffer
sets element at row i and column j to value, without performing any checks