Class Matrix
In: lib/backports/1.9.2/stdlib/matrix/lup_decomposition.rb
lib/backports/1.9.2/stdlib/matrix/eigenvalue_decomposition.rb
lib/backports/1.9.2/stdlib/matrix.rb
Parent: Object

Methods

*   **   +   -   /   ==   []   []   []=   build   clone   coerce   collect   column   column_vector   column_vectors   columns   component   conj   conjugate   det   det_e   determinant   determinant_e   diagonal   diagonal?   each   each_with_index   eigen   eigensystem   element   elements_to_f   elements_to_i   elements_to_r   empty   empty?   eql?   find_index   hash   hermitian?   identity   imag   imaginary   index   inspect   inv   inverse   lower_triangular?   lup   lup_decomposition   map   minor   new   normal?   orthogonal?   permutation?   rank   rank_e   real   real?   rect   rectangular   regular?   round   row   row_size   row_vector   row_vectors   rows   scalar   set_component   set_element   singular?   square?   symmetric?   t   to_a   to_s   tr   trace   transpose   unitary?   upper_triangular?   zero   zero?  

Included Modules

Enumerable ExceptionForMatrix CoercionHelper

Classes and Modules

Class Matrix::EigenvalueDecomposition
Class Matrix::LUPDecomposition

Constants

SELECTORS = {:all => true, :diagonal => true, :off_diagonal => true, :lower => true, :strict_lower => true, :strict_upper => true, :upper => true}.freeze unless const_defined?(:SELECTORS)

External Aliases

identity -> unit
identity -> I

Attributes

column_size  [R]  Returns the number of columns.
rows  [R]  instance creations

Public Class methods

Creates a matrix where each argument is a row.

  Matrix[ [25, 93], [-1, 66] ]
     =>  25 93
         -1 66

Creates a matrix of size row_size x column_size. It fills the values by calling the given block, passing the current row and column. Returns an enumerator if no block is given.

  m = Matrix.build(2, 4) {|row, col| col - row }
    => Matrix[[0, 1, 2, 3], [-1, 0, 1, 2]]
  m = Matrix.build(3) { rand }
    => a 3x3 matrix with random elements

Creates a single-column matrix where the values of that column are as given in column.

  Matrix.column_vector([4,5,6])
    => 4
       5
       6

Creates a matrix using columns as an array of column vectors.

  Matrix.columns([[25, 93], [-1, 66]])
     =>  25 -1
         93 66

Creates a matrix where the diagonal elements are composed of values.

  Matrix.diagonal(9, 5, -3)
    =>  9  0  0
        0  5  0
        0  0 -3

Creates a empty matrix of row_size x column_size. At least one of row_size or column_size must be 0.

  m = Matrix.empty(2, 0)
  m == Matrix[ [], [] ]
    => true
  n = Matrix.empty(0, 3)
  n == Matrix.columns([ [], [], [] ])
    => true
  m * n
    => Matrix[[0, 0, 0], [0, 0, 0]]

Creates an n by n identity matrix.

  Matrix.identity(2)
    => 1 0
       0 1

Creates a single-row matrix where the values of that row are as given in row.

  Matrix.row_vector([4,5,6])
    => 4 5 6

Creates a matrix where rows is an array of arrays, each of which is a row of the matrix. If the optional argument copy is false, use the given arrays as the internal structure of the matrix without copying.

  Matrix.rows([[25, 93], [-1, 66]])
     =>  25 93
         -1 66

Creates an n by n diagonal matrix where each diagonal element is value.

  Matrix.scalar(2, 5)
    => 5 0
       0 5

Creates a zero matrix.

  Matrix.zero(2)
    => 0 0
       0 0

Public Instance methods

Matrix multiplication.

  Matrix[[2,4], [6,8]] * Matrix.identity(2)
    => 2 4
       6 8

Matrix exponentiation. Equivalent to multiplying the matrix by itself N times. Non integer exponents will be handled by diagonalizing the matrix.

  Matrix[[7,6], [3,9]] ** 2
    => 67 96
       48 99

Matrix addition.

  Matrix.scalar(2,5) + Matrix[[1,0], [-4,7]]
    =>  6  0
       -4 12

Matrix subtraction.

  Matrix[[1,5], [4,2]] - Matrix[[9,3], [-4,1]]
    => -8  2
        8  1

Matrix division (multiplication by the inverse).

  Matrix[[7,6], [3,9]] / Matrix[[2,9], [3,1]]
    => -7  1
       -3 -6

Returns true if and only if the two matrices contain equal elements.

Returns element (i,j) of the matrix. That is: row i, column j.

Returns a clone of the matrix, so that the contents of each do not reference identical objects. There should be no good reason to do this since Matrices are immutable.

The coerce method provides support for Ruby type coercion. This coercion mechanism is used by Ruby to handle mixed-type numeric operations: it is intended to find a compatible common type between the two operands of the operator. See also Numeric#coerce.

Returns a matrix that is the result of iteration of the given block over all elements of the matrix.

  Matrix[ [1,2], [3,4] ].collect { |e| e**2 }
    => 1  4
       9 16

Returns column vector number j of the matrix as a Vector (starting at 0 like an array). When a block is given, the elements of that vector are iterated.

Returns an array of the column vectors of the matrix. See Vector.

component(i, j)

Alias for #[]

conj()

Alias for conjugate

Returns the conjugate of the matrix.

  Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]]
    => 1+2i   i  0
          1   2  3
  Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]].conjugate
    => 1-2i  -i  0
          1   2  3
det()

Alias for determinant

det_e()

Alias for determinant_e

Returns the determinant of the matrix.

Beware that using Float values can yield erroneous results because of their lack of precision. Consider using exact types like Rational or BigDecimal instead.

  Matrix[[7,6], [3,9]].determinant
    => 45

Returns true is this is a diagonal matrix. Raises an error if matrix is not square.

Yields all elements of the matrix, starting with those of the first row, or returns an Enumerator is no block given. Elements can be restricted by passing an argument:

  • :all (default): yields all elements
  • :diagonal: yields only elements on the diagonal
  • :off_diagonal: yields all elements except on the diagonal
  • :lower: yields only elements on or below the diagonal
  • :strict_lower: yields only elements below the diagonal
  • :strict_upper: yields only elements above the diagonal
  • :upper: yields only elements on or above the diagonal

    Matrix[ [1,2], [3,4] ].each { |e| puts e }

      # => prints the numbers 1 to 4
    

    Matrix[ [1,2], [3,4] ].each(:strict_lower).to_a # => [3]

Same as each, but the row index and column index in addition to the element

  Matrix[ [1,2], [3,4] ].each_with_index do |e, row, col|
    puts "#{e} at #{row}, #{col}"
  end
    # => Prints:
    #    1 at 0, 0
    #    2 at 0, 1
    #    3 at 1, 0
    #    4 at 1, 1
eigen()

Alias for eigensystem

Returns the Eigensystem of the matrix; see EigenvalueDecomposition.

  m = Matrix[[1, 2], [3, 4]]
  v, d, v_inv = m.eigensystem
  d.diagonal? # => true
  v.inv == v_inv # => true
  (v * d * v_inv).round(5) == m # => true
element(i, j)

Alias for #[]

Returns true if this is an empty matrix, i.e. if the number of rows or the number of columns is 0.

find_index(*args)

Alias for index

Returns a hash-code for the matrix.

Returns true is this is an hermitian matrix. Raises an error if matrix is not square.

imag()

Alias for imaginary

Returns the imaginary part of the matrix.

  Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]]
    => 1+2i  i  0
          1  2  3
  Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]].imaginary
    =>   2i  i  0
          0  0  0

The index method is specialized to return the index as [row, column] It also accepts an optional selector argument, see each for details.

  Matrix[ [1,2], [3,4] ].index(&:even?) # => [0, 1]
  Matrix[ [1,1], [1,1] ].index(1, :strict_lower) # => [1, 0]

Overrides Object#inspect

inv()

Alias for inverse

Returns the inverse of the matrix.

  Matrix[[-1, -1], [0, -1]].inverse
    => -1  1
        0 -1

Returns true is this is a lower triangular matrix.

Returns the LUP decomposition of the matrix; see LUPDecomposition.

  a = Matrix[[1, 2], [3, 4]]
  l, u, p = a.lup
  l.lower_triangular? # => true
  u.upper_triangular? # => true
  p.permutation?      # => true
  l * u == a * p      # => true
  a.lup.solve([2, 5]) # => Vector[(1/1), (1/2)]
lup_decomposition()

Alias for lup

map()

Alias for collect

Returns a section of the matrix. The parameters are either:

  • start_row, nrows, start_col, ncols; OR
  • row_range, col_range
  Matrix.diagonal(9, 5, -3).minor(0..1, 0..2)
    => 9 0 0
       0 5 0

Like Array#[], negative indices count backward from the end of the row or column (-1 is the last element). Returns nil if the starting row or column is greater than row_size or column_size respectively.

Returns true is this is a normal matrix. Raises an error if matrix is not square.

Returns true is this is an orthogonal matrix Raises an error if matrix is not square.

Returns true is this is a permutation matrix Raises an error if matrix is not square.

Returns the rank of the matrix. Beware that using Float values can yield erroneous results because of their lack of precision. Consider using exact types like Rational or BigDecimal instead.

  Matrix[[7,6], [3,9]].rank
    => 2

deprecated; use Matrix#rank

Returns the real part of the matrix.

  Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]]
    => 1+2i  i  0
          1  2  3
  Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]].real
    =>    1  0  0
          1  2  3

Returns true if all entries of the matrix are real.

Returns an array containing matrices corresponding to the real and imaginary parts of the matrix

m.rect == [m.real, m.imag] # ==> true for all matrices m

rectangular()

Alias for rect

Returns true if this is a regular (i.e. non-singular) matrix.

Returns a matrix with entries rounded to the given precision (see Float#round)

Returns row vector number i of the matrix as a Vector (starting at 0 like an array). When a block is given, the elements of that vector are iterated.

Returns the number of rows.

Returns an array of the row vectors of the matrix. See Vector.

set_component(i, j, v)

Alias for #[]=

set_element(i, j, v)

Alias for #[]=

Returns true is this is a singular matrix.

Returns true is this is a square matrix.

Returns true is this is a symmetric matrix. Raises an error if matrix is not square.

t()

Alias for transpose

Returns an array of arrays that describe the rows of the matrix.

Overrides Object#to_s

tr()

Alias for trace

Returns the trace (sum of diagonal elements) of the matrix.

  Matrix[[7,6], [3,9]].trace
    => 16

Returns the transpose of the matrix.

  Matrix[[1,2], [3,4], [5,6]]
    => 1 2
       3 4
       5 6
  Matrix[[1,2], [3,4], [5,6]].transpose
    => 1 3 5
       2 4 6

Returns true is this is a unitary matrix Raises an error if matrix is not square.

Returns true is this is an upper triangular matrix.

Returns true is this is a matrix with only zero elements

[Validate]