Class Matrix
In: lib/matrix.rb
Parent: Object

The Matrix class represents a mathematical matrix, and provides methods for creating special-case matrices (zero, identity, diagonal, singular, vector), operating on them arithmetically and algebraically, and determining their mathematical properties (trace, rank, inverse, determinant).

Note that although matrices should theoretically be rectangular, this is not enforced by the class.

Also note that the determinant of integer matrices may be incorrectly calculated unless you also require ‘mathn’. This may be fixed in the future.

Method Catalogue

To create a matrix:

To access Matrix elements/columns/rows/submatrices/properties:

Properties of a matrix:

Matrix arithmetic:

Matrix functions:

Conversion to other data types:

String representations:

Methods

*   **   +   -   /   ==   []   []   clone   coerce   collect   column   column_size   column_vector   column_vectors   columns   compare_by_row_vectors   det   determinant   diagonal   eql?   hash   identity   inspect   inv   inverse   inverse_from   map   minor   new   rank   regular?   row   row_size   row_vector   row_vectors   rows   scalar   singular?   square?   t   to_a   to_s   tr   trace   transpose   zero  

Included Modules

ExceptionForMatrix

External Aliases

identity -> unit
identity -> I

Public Class methods

Creates a matrix where each argument is a row.

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

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 an n by n identity matrix.

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

This method is used by the other methods that create matrices, and is of no use to general users.

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 to 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 an n by n 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. Defined for integer powers only. Equivalent to multiplying the matrix by itself N times.

  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.

FIXME: describe 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 { |i| i**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 the number of columns. Note that it is possible to construct a matrix with uneven columns (e.g. Matrix[ [1,2,3], [4,5] ]), but this is mathematically unsound. This method uses the first row to determine the result.

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

Not really intended for general consumption.

det()

Alias for determinant

Returns the determinant of the matrix. If the matrix is not square, the result is 0.

  Matrix[[7,6], [3,9]].determinant
    => 63
eql?(other)

Alias for #==

Returns a hash-code for the matrix.

inv()

Alias for inverse

Returns the inverse of the matrix.

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

Not for public consumption?

map(

Alias for collect

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

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

Returns the rank of the matrix. Beware that using Float values, with their usual lack of precision, can affect the value returned by this method. Use Rational values instead if this is important to you.

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

Returns true if this is a regular matrix.

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.

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

Returns true is this is a square matrix. See note in column_size about this being unreliable, though.

t()

Alias for transpose

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

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
To view or add comments on this documentation, please go to the API wiki.

[Validate]