Class

Matrix

Inheritance
< Object
Included Modules
ExceptionForMatrix

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:

  • regular?
  • singular?
  • square?

Matrix arithmetic:

Matrix functions:

Conversion to other data types:

String representations:

Aliases

Method Alias Description
identity → unit
identity → I

Methods

Class

Visibility Signature
public [] (*rows)
public column_vector (column)
public columns (columns)
public diagonal (*values)
public identity (n)
public new (init_method, *argv)
public row_vector (row)
public rows (rows, copy = true)
public scalar (n, value)
public zero (n)

Instance

Visibility Signature
public * (m)
public ** (other)
public + (m)
public - (m)
public / (other)
public == (other)
public [] (i, j)
public clone ()
public coerce (other)
public collect ( {|e| ...}
public column (j) {|e| ...}
public column_size ()
public column_vectors ()
public compare_by_row_vectors (rows)
public det ()
public determinant ()
public eql? (other)
public hash ()
public inspect ()
public inv ()
public inverse ()
public inverse_from (src)
public map (
public minor (*param)
public rank ()
public regular? ()
public row (i) {|e| ...}
public row_size ()
public row_vectors ()
public singular? ()
public square? ()
public t ()
public to_a ()
public to_s ()
public tr ()
public trace ()
public transpose ()

Class Method Detail

[](*rows)

Creates a matrix where each argument is a row.

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

column_vector(column)

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

columns(columns)

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

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

diagonal(*values)

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

identity(n)

Creates an n by n identity matrix.

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

new(init_method, *argv)

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

row_vector(row)

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

rows(rows, copy = true)

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

scalar(n, value)

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

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

zero(n)

Creates an n by n zero matrix.

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

Instance Method Detail

*(m)

Matrix multiplication.

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

**(other)

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

+(m)

Matrix addition.

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

-(m)

Matrix subtraction.

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

/(other)

Matrix division (multiplication by the inverse).

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

==(other)

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

[](i, j)

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

clone()

Returns a clone of the matrix, so that the contents of each do not reference identical objects.

coerce(other)

FIXME: describe coerce.

collect( {|e| ...}

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

column(j) {|e| ...}

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.

column_size()

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.

column_vectors()

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

compare_by_row_vectors(rows)

Not really intended for general consumption.

det()

Alias for determinant

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 #==

hash()

Returns a hash-code for the matrix.

inspect()

Overrides Object#inspect

inv()

Alias for inverse

inverse()

Returns the inverse of the matrix.

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

inverse_from(src)

Not for public consumption?

map(

Alias for collect

minor(*param)

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

rank()

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

regular?()

Returns true if this is a regular matrix.

row(i) {|e| ...}

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.

row_size()

Returns the number of rows.

row_vectors()

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

singular?()

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

square?()

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

t()

Alias for transpose

to_a()

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

to_s()

Overrides Object#to_s

tr()

Alias for trace

trace()

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

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

transpose()

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