Class: MDS::StdlibInterface
- Inherits:
-
MatrixInterface
- Object
- MatrixInterface
- MDS::StdlibInterface
- Defined in:
- lib/mds/interfaces/stdlib_interface.rb
Overview
Matrix interface for Ruby’s standard library Matrix class.
To succesfully use this interface ‘extendmatrix’ is required. For more information and installation instructions see github.com/clbustos/extendmatrix
The algorithm/implementation of the eigen-decomposition is sub-optimal. It is therefore suitable only for small-scale problems. Timings are illustrated in the benchmark section of the README.
The reason this interface is still included in RMDS is the fact that it only depends on Ruby code and no native extensions.
Compatible with ‘extendmatrix >= 0.3.1’
Class Method Summary collapse
-
.add(m, n) ⇒ Object
Componentwise addition of two matrices.
-
.create(n, m, s) ⇒ Object
Create a new matrix with equal elements.
-
.ed(m) ⇒ Object
Compute the eigen-decomposition of a real symmetric matrix.
-
.get(m, i, j) ⇒ Object
Get matrix element.
-
.ncols(m) ⇒ Object
Return the number of matrix columns.
-
.nrows(m) ⇒ Object
Return the number of matrix rows.
-
.prod(m, n) ⇒ Object
Calculate the product of two matrices or the product of a matrix and a scalar.
-
.set(m, i, j, s) ⇒ Object
Set matrix element.
-
.sub(m, n) ⇒ Object
Componentwise subtraction of two matrices.
-
.t(m) ⇒ Object
Transpose a matrix.
Methods inherited from MatrixInterface
columns, create_block, create_diagonal, create_identity, create_random, create_rows, diagonals, inherited, minor, rows, #to_s, trace
Class Method Details
.add(m, n) ⇒ Object
Componentwise addition of two matrices.
114 115 116 |
# File 'lib/mds/interfaces/stdlib_interface.rb', line 114 def StdlibInterface.add(m, n) m + n end |
.create(n, m, s) ⇒ Object
Create a new matrix with equal elements.
44 45 46 |
# File 'lib/mds/interfaces/stdlib_interface.rb', line 44 def StdlibInterface.create(n, m, s) ::Matrix.build(n, m, s) end |
.ed(m) ⇒ Object
Compute the eigen-decomposition of a real symmetric matrix.
The Ruby version uses Jacobi iterations to calculate the eigen-decomposition of a matrix. Although comfortable as all third-party dependencies can be installed via gem, the method is not suited for matrices bigger than 10x10.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/mds/interfaces/stdlib_interface.rb', line 139 def StdlibInterface.ed(m) eigen_values = m.cJacobiA eigen_vectors = m.cJacobiV ranks = (0..(m.row_size-1)).sort{|i,j| eigen_values[j,j] <=> eigen_values[i,i]} s_eigen_values = [] s_eigen_vectors = [] ranks.each do |r| s_eigen_values << eigen_values[r,r] s_eigen_vectors << eigen_vectors.column(r) end [::Matrix.diagonal(*s_eigen_values), ::Matrix.columns(s_eigen_vectors)] end |
.get(m, i, j) ⇒ Object
Get matrix element.
83 84 85 |
# File 'lib/mds/interfaces/stdlib_interface.rb', line 83 def StdlibInterface.get(m, i, j) m[i,j] end |
.ncols(m) ⇒ Object
Return the number of matrix columns
64 65 66 |
# File 'lib/mds/interfaces/stdlib_interface.rb', line 64 def StdlibInterface.ncols(m) m.column_size end |
.nrows(m) ⇒ Object
Return the number of matrix rows
54 55 56 |
# File 'lib/mds/interfaces/stdlib_interface.rb', line 54 def StdlibInterface.nrows(m) m.row_size end |
.prod(m, n) ⇒ Object
Calculate the product of two matrices or the product of a matrix and a scalar.
94 95 96 |
# File 'lib/mds/interfaces/stdlib_interface.rb', line 94 def StdlibInterface.prod(m, n) m * n end |
.set(m, i, j, s) ⇒ Object
Set matrix element.
73 74 75 |
# File 'lib/mds/interfaces/stdlib_interface.rb', line 73 def StdlibInterface.set(m, i, j, s) m[i,j] = s end |
.sub(m, n) ⇒ Object
Componentwise subtraction of two matrices.
124 125 126 |
# File 'lib/mds/interfaces/stdlib_interface.rb', line 124 def StdlibInterface.sub(m, n) m - n end |
.t(m) ⇒ Object
Transpose a matrix.
104 105 106 |
# File 'lib/mds/interfaces/stdlib_interface.rb', line 104 def StdlibInterface.t(m) m.t end |