Class: MDS::Matrix

Inherits:
Object
  • Object
show all
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

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matrix) ⇒ Matrix

Initialize from matrix instance and interface class.

Parameters:

  • matrix

    the matrix to be wrapped



36
37
38
# File 'lib/mds/matrix.rb', line 36

def initialize(matrix)
  @m = matrix
end

Instance Attribute Details

#mObject (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.

Returns:

  • (Matrix)

    the newly created matrix.



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.

Returns:

  • (Matrix)

    the newly created matrix.



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.

Returns:

  • (Matrix)

    the newly created 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.

Returns:

  • (Matrix)

    the newly created 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.

Returns:

  • (Matrix)

    the newly created matrix.



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.

Returns:

  • (Matrix)

    the newly created matrix.



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.

Parameters:

  • other (Matrix, Float)

    matrix to multiply with, or scalar

Returns:



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.

Parameters:

  • other (Matrix)

    matrix to add to this matrix

Returns:



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.

Parameters:

  • other (Matrix)

    matrix to subtract from this matrix.

Returns:



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.

Parameters:

  • i (Integer)

    the i-th row, zero-based indexing

  • j (Integer)

    the j-th column, zero-based indexing

Returns:

  • (Float)

    value of 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.

Parameters:

  • i (Integer)

    the i-th row, zero-based indexing

  • j (Integer)

    the j-th column, zero-based indexing

  • s (Float)

    scalar value to set



126
127
128
# File 'lib/mds/matrix.rb', line 126

def []=(i, j, s)
  Backend.active.set(@m, i, j, s)
end

#columnsArray<Array>

Returns matrix as array of columns.

Returns:

  • (Array<Array>)

    the array of columns where each column is an array



233
234
235
# File 'lib/mds/matrix.rb', line 233

def columns
  Backend.active.columns(@m) 
end

#diagonalsArray

Retrieve the diagonal elements as an array.

Returns:

  • (Array)

    diagonal elements as array.



202
203
204
# File 'lib/mds/matrix.rb', line 202

def diagonals
  Backend.active.diagonals(@m)
end

#edArray

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.

Returns:

  • (Array)

    the array containing the matrix of eigen-values and eigen-vector



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.

Parameters:

  • row_range (Range)

    row range

  • col_range (Range)

    column range

Returns:



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

#ncolsObject

Return the number of matrix columns

Returns:

  • number of columns in matrix



115
116
117
# File 'lib/mds/matrix.rb', line 115

def ncols
  Backend.active.ncols(@m)
end

#nrowsObject

Return the number of matrix rows

Returns:

  • number of rows in matrix



106
107
108
# File 'lib/mds/matrix.rb', line 106

def nrows
  Backend.active.nrows(@m)
end

#rowsArray<Array>

Returns matrix as array of rows.

Returns:

  • (Array<Array>)

    the array of rows where each row is an array



242
243
244
# File 'lib/mds/matrix.rb', line 242

def rows
  Backend.active.rows(@m) 
end

#tMatrix

Transpose a matrix.

Returns:

  • (Matrix)

    the transposed matrix.



180
181
182
# File 'lib/mds/matrix.rb', line 180

def t
  Matrix.new(Backend.active.t(@m))
end

#to_sObject

Convert to string.

Invokes #to_s from wrapped matrix.

Returns:

  • wrapped matrix as string.



253
254
255
# File 'lib/mds/matrix.rb', line 253

def to_s
  Backend.active.to_s(@m) 
end

#traceFloat

Calculate the sum of diagonal matrix elements.

Returns:

  • (Float)

    trace of matrix



211
212
213
# File 'lib/mds/matrix.rb', line 211

def trace
  Backend.active.trace(@m)
end