Class Linalg::DMatrix
In: lib/linalg/dmatrix/main.rb
lib/linalg/dmatrix/cholesky.rb
lib/linalg/dmatrix/cond.rb
lib/linalg/dmatrix/det.rb
lib/linalg/dmatrix/eigen.rb
lib/linalg/dmatrix/fit.rb
lib/linalg/dmatrix/inverse.rb
lib/linalg/dmatrix/lu.rb
lib/linalg/dmatrix/norms.rb
lib/linalg/dmatrix/nullspace.rb
lib/linalg/dmatrix/qr.rb
lib/linalg/dmatrix/schur.rb
lib/linalg/dmatrix/solve.rb
lib/linalg/dmatrix/svd.rb
ext/linalg/dmatrix.c
lib/linalg/dmatrix/alias.rb
Parent: Object

Methods

*   +   +@   -   -@   /   ==   =~   []   []   []=   _dump   _load   chol   cholesky   clone   coerce   col   column   column_norm   columns   cond   cond_est_1   cond_est_inf   det   determinant   diagonal   dimensions   dot   eigensystem   eigenvalues   exchange_columns   exchange_rows   fill   fit   frobenius_norm   hsize   identity   inspect   inspect2   inv   inverse   inverse!   join_columns   join_rows   lu   lu!   maxabs   minor   minus!   mul!   ncol   negate!   new   norm   norm_1   norm_2   norm_f   norm_inf   nrow   nullity   nullspace   num_columns   num_rows   orth   pinv   plus!   postmul!   premul!   pseudo_inverse   qr   qr!   rand   rank   rankspace   regular?   replace_column   replace_minor   replace_row   reserve   row   row_norm   rows   schur   singleton_class   singular?   singular_value_decomposition   singular_values   solve   solve!   square?   sv   svd   symmetric!   symmetric?   t   to_a   to_doublereal_ptr   to_f   to_s   trace   transpose   transpose!   vsize   within  

Included Modules

Iterators XData Linalg::Exception

Attributes

default_epsilon  [RW] 

Public Class methods

calls DMatrix.rows with a splat (*)

  DMatrix[[1,2,3],[4,5,6]]

is equivalent to

  DMatrix.rows [[1,2,3],[4,5,6]]

For Marshal. Uses native data format.

Create a matrix using the elements of array as columns.

  DMatrix.columns(array)

is equivalent to

  DMatrix.new(array[0].size, array.size) { |i,j| array[j][i] }

DimensionError is raised if array is not rectangular.

In the first form, create a diagonal matrix with the elements of array along the diagonal. The second form is like the first but using elements of the column vector vector. In the third form, create an n x n diagonal matrix with diagonal elements of value scalar. In the fourth form, each element m[i,i] gets the block result.

Least squares minimization using QR factorization.

Find x for which

  (a*x - b).norm_2

is a minimum.

Assumes a matrix of full rank.

Create an n x n identity matrix.

Create a matrix by joining together a list of column vectors

  m == DMatrix.join_columns(m.columns.map { |x| x })

Create a matrix by joining together a list of row vectors

  m == DMatrix.join_rows(m.rows.map { |x| x })

Returns a new matrix with dimensions [vsize, hsize].

In the first form, the matrix is filled with zero. In the second form, the matrix is filled with scalar. In the third form, each index pair is passed to the block and the respective matrix element is assigned the block result.

  DMatrix.new(4,4) { |i,j| i == j ? 1 : 0 }

is the 4 x 4 identity matrix.

Create a matrix filled with random numbers between -1.0 and 1.0.

Similar to

  DMatrix.new(vsize, hsize) { 2*rand - 1 }

except uses rand() from stdlib.h

Create a matrix using the elements of array as rows.

  DMatrix.rows(array)

is equivalent to

  DMatrix.new(array.size, array[0].size) { |i,j| array[i][j] }

DimensionError is raised if array is not rectangular.

Solve the linear equation

  a * x == b

Returns the solution x, or raises SingularMatrix if a was singular.

Solve the linear equation

  a * x == b

a and b are both overwritten.

If a unique solution is found, a contains both L and U factors while b holds the solution x above.

Returns the solution x, or raises SingularMatrix if a was singular.

  x = DMatrix.solve!(a, b)
  x.object_id == b.object_id

Public Instance methods

Matrix multiply

  c = a * b

DimensionError is raised unless

  a.hsize == b.vsize

Matrix addition.

  a + b

DimensionError is raised unless

  a.dimensions == b.dimensions

Unary plus. Does nothing.

Matrix subtraction

  a - b

DimensionError is raised unless

  a.dimensions == b.dimensions

Unary minus

Division by scalar s.

True if element-wise identical values.

Implemented as

  m.within(epsilon, other)

where

  epsilon =
     self.singleton_class.default_epsilon ||
     self.class.default_epsilon

Element read. Column vectors are permitted to omit the column index.

Element write. Column vectors are permitted to omit the column index.

For Marshal. Uses native data format.

chol(

Alias for cholesky

Given a symmetric positive definite matrix m, gives an upper-triangular matrix u such that

  u.t * u

is equal to m.

Only the upper-triangular portion of m is considered for the algorithm.

Raises Diverged if m was not positive definite.

Deep copy

Internal call from Numeric#coerce.

col(p1)

Alias for column

Returns the j-th column of the matrix.

column_norm()

Alias for norm_1

The condition number. Ratio of the largest singular value to the smallest.

Estimate the condition number using the 1-norm. Seems to be within an order of magnitude.

Estimate the condition number using the infinity-norm. Seems to be within an order of magnitude.

det()

Alias for determinant

Returns the determinant of the matrix. Raises DimensionError if the matrix is not square.

Returns the pair

  [a.vsize, a.hsize]

Column vector dot product

  a.dot(b)

DimensionError is raised unless

  a.hsize == b.hsize and
  b.hsize == 1 and
  a.vsize == b.vsize

Find the eigenvectors and eigenvalues of the matrix.

The columns of eigvs hold the eigenvectors; the column vectors real and imag hold the eigenvalues.

Stepping through the eigenvalues in succession starting from the beginning, one will encounter either a real eigenvalue or a complex conjugate pair. That is, either the imaginary part will be exactly equal to zero

  imag[n] == 0.0

or there will be a conjugate pair with the following exactly equal,

  real[n] == real[n+1]
  imag[n] == -imag[n+1]

In the former case, the real eigenvector

  eigvs.column(n)

corresponds to the real eigenvalue

  real[n]

In the latter case, the complex eigenvector

  eigvs.column(n) + i*eigvs.column(n+1)

corresponds to the complex eigenvalue

  real[n] + i*imag[n]

and the conjugate eigenvector

  eigvs.column(n) - i*eigvs.column(n+1)

corresponds to the conjugate eigenvalue

  real[n] - i*imag[n]

for i = sqrt(-1).

The eigenvectors are each given unit length with largest component real.

Raises DimensionError if the given matrix is not square. May raise Diverged.

Exchange the p-th column with the q-th column

Exchange the p-th row with the q-th row

Fill the matrix with value.

frobenius_norm()

Alias for norm_f

Horizontal size of the matrix (number of columns)

Same as to_s but prepended with a newline for nice irb output

Dump to an eval-able string

inv()

Alias for inverse

Matrix inverse. Raises SingularMatrix if the matrix is singular.

In-place matrix inverse. Raises SingularMatrix if the matrix is singular.

  a.object_id == a.inverse!.object_id

Generalized LU decomposition for rectangular matrices. The matrix is decomposed into

  p * l * u

where p is a permutation matrix, l lower-triangular (or lower-trapezoidal), and u is upper-triangular (or upper-trapezoidal).

In-place LU decomposition. The matrix is overwritten with both L and U factors (the diagonal 1‘s of L are discarded). Returns the vector p of pivots.

The maximum absolute value of the individual matrix elements. For matrix m, equivalent to

  m.elems.map { |e| e.abs }.to_a.flatten.sort[-1]

Note maxabs is not a matrix norm.

Return the minor with upper-left position [i, j] and dimensions [vsize, hsize].

In-place matrix subtraction.

  c = a - b

is equivalent to

  c = a.minus!(b)

with

  a.object_id == c.object_id
mul!(p1)

Alias for postmul!

ncol()

Alias for hsize

In-place unary minus.

norm()

Alias for norm_2

The 1-norm. Maximum column absolute sum. For matrix m, equivalent to

  m.columns.map { |col|
     col.elems.inject(0.0) { |acc, e| acc + e.abs }
  }.sort[-1]

The 2-norm. The largest singular value.

Frobenius norm. Square root of the sum of the squares of the elements.

For matrix m, equivalent to

  Math.sqrt(m.elems.inject(0.0) { |acc, e| acc + e*e })

The infinity-norm. Maximum row absolute sum. For matrix m, equivalent to

  m.rows.map { |row|
     row.elems.inject(0.0) { |acc, e| acc + e.abs }
  }.sort[-1]
nrow()

Alias for vsize

Returns the nullity of the matrix (the dimension of the nullspace).

  hsize == rank + nullity

see DMatrix#nullspace

Returns a matrix whose columns form an orthogonal basis for the nullspace. (This is simply taken from vt.t in DMatrix#svd.) Returns nil if the matrix is of full rank.

For matrix m, a singular value less than or equal to m.norm*epsilon is considered a rank deficiency.

num_columns()

Alias for hsize

num_rows()

Alias for vsize

orth(epsilon = (self.singleton_class.default_epsilon ||self.class.default_epsilon))

Alias for rankspace

pinv(epsilon = nil)

Alias for pseudo_inverse

In-place matrix addition.

  a.object_id == a.plus!(b).object_id

In-place multiply

  c = a * b

is equivalent to

  c = a.postmul!(b)

with

  a.object_id == c.object_id

Note this implies b must be square. DimensionError is raised otherwise.

In-place multiply

  c = a * b

is equivalent to

  c = b.premul!(a)

with

  b.object_id == c.object_id

Note this implies a must be square. DimensionError is raised otherwise.

Pseudo-inverse.

For matrix m, find x which minimizes

  (m*x - DMatrix.identity(m.vsize)).norm

QR factorization. Decompose the matrix into

  q * r

where q is orthogonal and r is upper-triangular.

In-place QR factorization. The contents of the matrix are left in the state returned by dgeqrf().

Returns a matrix whose columns form an orthonormal basis for the span of the matrix.

This is just the complement of the nullspace. See DMatrix#nullspace.

replace the j-th column with the column vector v.

Insert minor into the matrix, where [i,j] is the location of the upper-left corner of the minor.

replace the i-th row with the row vector v.

Returns the i-th row of the matrix.

row_norm()

Alias for norm_inf

Decompose into

  z * t * z.t

where z is orthogonal and t is quasi upper-diagonal. real and imag contain the respective eigenvalues in the format described in DMatrix#eigensystem.

Returns the singleton class of the object. Convenient for setting the default_epsilon on a per-object basis.

  b = DMatrix.rand(4, 4)
  a = b.map { |e| e + 0.0001 }
  a.class.default_epsilon  # => 1e-8
  a =~ b  # => false
  a.singleton_class.default_epsilon = 0.001
  a =~ b  # => true

Decompose the m x n matrix into

  u * s * vt

where u and vt are orthogonal matrices and s is an m x n matrix whose non-diagonal elements are all zero. The diagonal elements of s are called the singular values of the matrix. If called with

  m.singular_value_decomposition(:diagonalize => false)

then s is a vector of singular values rather than a diagonal matrix.

May raise Diverged.

Returns a vector containing the singular values of the matrix. See DMatrix#singular_value_decomposition.

Returns

  self.vsize == self.hsize
sv(&b)

Alias for singular_values

svd(opts = nil, &b)

Force the matrix to be symmetric, reflecting the upper triangle onto the lower triangle.

t()

Alias for transpose

Create an array from the matrix

  a = m.to_a

is equivalent to

  a = Array.new(m.vsize) { Array.new(m.hsize) }
  m.each_index { |i,j|
     a[i][j] = m[i,j]
  }

Actual pointer value of the matrix data. Subject to change after a bang (!) operation. This is called by the Lapack module.

Returns m[0,0] when m is a 1x1 matrix, otherwise a TypeError is raised.

Dump to a readable string

Sum of diagonal elements.

In-place matrix transpose. Guaranteed O(1) trivial cost for column and row vectors. Note that transpose flags are not implemented; thus for non-vectors the cost is a temporary copy and assignment.

Vertical size of the matrix (number of rows)

Test if each matrix element is within epsilon of the respective matrix element of other.

[Validate]