Class: MDS::Matrix
- Inherits:
-
Object
- Object
- MDS::Matrix
- Defined in:
- lib/mds/matrix.rb
Overview
A matrix adapter.
RMDS does not implement matrices or linear algebra routines itself, instead RMDS offers a non-intrusive adapter architecture to interop with third party linear algebra packages.
The RMDS adapter architecture consists of two layers
-
MatrixInterface defines a minimal common interface of required interop methods.
-
Matrix is a matrix adapter that binds to data provided by a specialized MatrixInterface class and carries out all computations through methods defined in MatrixInterface.
Instance Attribute Summary collapse
-
#m ⇒ Object
(also: #matrix)
readonly
Wrapped matrix.
Class Method Summary collapse
-
.create(n, m, s) ⇒ Matrix
Create a new matrix setting all elements to equal values.
-
.create_block(n, m, &block) ⇒ Matrix
Create a new matrix and assign each element as the result of invoking the given block.
-
.create_diagonal(*elements) ⇒ Matrix
Create a new diagonal matrix.
-
.create_identity(n) ⇒ Matrix
Create a new identity matrix.
-
.create_random(n, m, smin = -1.0,, smax = 1.0) ⇒ Matrix
Create a new matrix from uniform random distribution.
-
.create_rows(*rows) ⇒ Matrix
Create matrix from rows.
Instance Method Summary collapse
-
#*(other) ⇒ Matrix
Calculate the product of two matrices or the product of a matrix and a scalar.
-
#+(other) ⇒ Matrix
Componentwise addition of two matrices.
-
#-(other) ⇒ Matrix
Componentwise subtraction of two matrices.
-
#[](i, j) ⇒ Float
Get value of matrix element.
-
#[]=(i, j, s) ⇒ Object
Set value of matrix element.
-
#columns ⇒ Array<Array>
Returns matrix as array of columns.
-
#diagonals ⇒ Array
Retrieve the diagonal elements as an array.
-
#ed ⇒ Array
Compute the eigen-decomposition of a real symmetric matrix.
-
#initialize(matrix) ⇒ Matrix
constructor
Initialize from matrix instance and interface class.
-
#minor(row_range, col_range) ⇒ Matrix
Calculate minor matrix.
-
#ncols ⇒ Object
Return the number of matrix columns.
-
#nrows ⇒ Object
Return the number of matrix rows.
-
#rows ⇒ Array<Array>
Returns matrix as array of rows.
-
#t ⇒ Matrix
Transpose a matrix.
-
#to_s ⇒ Object
Convert to string.
-
#trace ⇒ Float
Calculate the sum of diagonal matrix elements.
Constructor Details
#initialize(matrix) ⇒ Matrix
Initialize from matrix instance and interface class.
36 37 38 |
# File 'lib/mds/matrix.rb', line 36 def initialize(matrix) @m = matrix end |
Instance Attribute Details
#m ⇒ Object (readonly) Also known as: matrix
Wrapped matrix
28 29 30 |
# File 'lib/mds/matrix.rb', line 28 def m @m end |
Class Method Details
.create(n, m, s) ⇒ Matrix
Create a new matrix setting all elements to equal values.
46 47 48 |
# File 'lib/mds/matrix.rb', line 46 def Matrix.create(n, m, s) Matrix.new(Backend.active.create(n, m, s)) end |
.create_block(n, m, &block) ⇒ Matrix
Create a new matrix and assign each element as the result of invoking the given block.
57 58 59 |
# File 'lib/mds/matrix.rb', line 57 def Matrix.create_block(n, m, &block) Matrix.new(Backend.active.create_block(n, m, &block)) end |
.create_diagonal(*elements) ⇒ Matrix
Create a new diagonal matrix.
87 88 89 |
# File 'lib/mds/matrix.rb', line 87 def Matrix.create_diagonal(*elements) Matrix.new(Backend.active.create_diagonal(*elements)) end |
.create_identity(n) ⇒ Matrix
Create a new identity matrix.
77 78 79 |
# File 'lib/mds/matrix.rb', line 77 def Matrix.create_identity(n) Matrix.new(Backend.active.create_identity(n)) end |
.create_random(n, m, smin = -1.0,, smax = 1.0) ⇒ Matrix
Create a new matrix from uniform random distribution.
67 68 69 |
# File 'lib/mds/matrix.rb', line 67 def Matrix.create_random(n, m, smin = -1.0, smax = 1.0) Matrix.new(Backend.active.create_random(n, m, smin, smax)) end |
.create_rows(*rows) ⇒ Matrix
Create matrix from rows.
97 98 99 |
# File 'lib/mds/matrix.rb', line 97 def Matrix.create_rows(*rows) Matrix.new(Backend.active.create_rows(*rows)) end |
Instance Method Details
#*(other) ⇒ Matrix
Calculate the product of two matrices or the product of a matrix and a scalar.
148 149 150 151 152 153 |
# File 'lib/mds/matrix.rb', line 148 def *(other) is_ma = other.instance_of?(Matrix) Matrix.new( Backend.active.prod(@m, is_ma ? other.matrix : other) ) end |
#+(other) ⇒ Matrix
Componentwise addition of two matrices.
161 162 163 |
# File 'lib/mds/matrix.rb', line 161 def +(other) Matrix.new(Backend.active.add(@m, other.m)) end |
#-(other) ⇒ Matrix
Componentwise subtraction of two matrices.
171 172 173 |
# File 'lib/mds/matrix.rb', line 171 def -(other) Matrix.new(Backend.active.sub(@m, other.m)) end |
#[](i, j) ⇒ Float
Get value of matrix element.
137 138 139 |
# File 'lib/mds/matrix.rb', line 137 def [](i,j) Backend.active.get(@m, i, j) end |
#[]=(i, j, s) ⇒ Object
Set value of matrix element.
126 127 128 |
# File 'lib/mds/matrix.rb', line 126 def []=(i, j, s) Backend.active.set(@m, i, j, s) end |
#columns ⇒ Array<Array>
Returns matrix as array of columns.
233 234 235 |
# File 'lib/mds/matrix.rb', line 233 def columns Backend.active.columns(@m) end |
#diagonals ⇒ Array
Retrieve the diagonal elements as an array.
202 203 204 |
# File 'lib/mds/matrix.rb', line 202 def diagonals Backend.active.diagonals(@m) end |
#ed ⇒ Array
Compute the eigen-decomposition of a real symmetric matrix.
The eigen-decomposition consists of a diagonal matrix D
containing the eigen values and a square matrix N
having the normalized eigenvectors in columns.
193 194 195 |
# File 'lib/mds/matrix.rb', line 193 def ed Backend.active.ed(@m).map {|m| Matrix.new(m) } end |
#minor(row_range, col_range) ⇒ Matrix
Calculate minor matrix.
222 223 224 225 226 |
# File 'lib/mds/matrix.rb', line 222 def minor(row_range, col_range) Matrix.new( Backend.active.minor(@m, row_range, col_range) ) end |
#ncols ⇒ Object
Return the number of matrix columns
115 116 117 |
# File 'lib/mds/matrix.rb', line 115 def ncols Backend.active.ncols(@m) end |
#nrows ⇒ Object
Return the number of matrix rows
106 107 108 |
# File 'lib/mds/matrix.rb', line 106 def nrows Backend.active.nrows(@m) end |
#rows ⇒ Array<Array>
Returns matrix as array of rows.
242 243 244 |
# File 'lib/mds/matrix.rb', line 242 def rows Backend.active.rows(@m) end |
#t ⇒ Matrix
Transpose a matrix.
180 181 182 |
# File 'lib/mds/matrix.rb', line 180 def t Matrix.new(Backend.active.t(@m)) end |
#to_s ⇒ Object
Convert to string.
Invokes #to_s from wrapped matrix.
253 254 255 |
# File 'lib/mds/matrix.rb', line 253 def to_s Backend.active.to_s(@m) end |
#trace ⇒ Float
Calculate the sum of diagonal matrix elements.
211 212 213 |
# File 'lib/mds/matrix.rb', line 211 def trace Backend.active.trace(@m) end |