Matrices en ruby

Páginas: 20 (4781 palabras) Publicado: 17 de enero de 2012
Matrix

The Matrix class represents a mathematical matrix. It provides methods for creating matrices, operating on them arithmetically and algebraically, and determining their mathematical properties (trace, rank, inverse, determinant).

1 Method Catalogue

To create a matrix:

Matrix

Matrix.[](*rows)

Matrix.rows(rows, copy = true)

Matrix.columns(columns)Matrix.build(row_size, column_size, &block)

Matrix.diagonal(*values)

Matrix.scalar(n, value)

Matrix.identity(n)

Matrix.unit(n)

Matrix.I(n)

Matrix.zero(n)

Matrix.row_vector(row)

Matrix.column_vector(column)

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

[](i, j)

row_size

column_size

row(i)

column(j)

collect

map

each

each_with_index

find_indexminor(*param)

Properties of a matrix:

diagonal?

empty?

hermitian?

lower_triangular?

normal?

orthogonal?

permutation?

real?

regular?

singular?

square?

symmetric?

unitary?

upper_triangular?

zero?

Matrix arithmetic:

*(m)

+(m)

-(m)

#/(m)

inverse

inv

**

Matrix functions:

determinant

det

rank

round

trace

trtranspose

t

Matrix decompositions:

eigen

eigensystem

lup

lup_decomposition

Complex arithmetic:

conj

conjugate

imag

imaginary

real

rect

rectangular

Conversion to other data types:

coerce(other)

row_vectors

column_vectors

to_a

String representations:

to_s

inspect

1 Constants

SELECTORS

2 Attributes

column_size[R]

Returnsthe number of columns.

rows[R]

instance creations

3 Public Class Methods

I(n) click to toggle source
Alias for: identity
[](*rows) click to toggle source

Creates a matrix where each argument is a row.

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

# File matrix.rb, line 139
def Matrix.[](*rows)
Matrix.rows(rows, false)
end

build(row_size, column_size =row_size) click to toggle source

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

# File matrix.rb, line 184def Matrix.build(row_size, column_size = row_size)
row_size = CoercionHelper.coerce_to_int(row_size)
column_size = CoercionHelper.coerce_to_int(column_size)
raise ArgumentError if row_size < 0 || column_size < 0
return to_enum :build, row_size, column_size unless block_given?
rows = Array.new(row_size) do |i|
Array.new(column_size) do |j|
yield i, j
end
endnew rows, column_size
end

column_vector(column) click to toggle source

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

# File matrix.rb, line 269
def Matrix.column_vector(column)
column = convert_to_array(column)
new [column].transpose, 1
end

columns(columns) click to toggle sourceCreates a matrix using columns as an array of column vectors.

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

# File matrix.rb, line 169
def Matrix.columns(columns)
Matrix.rows(columns, false).transpose
end

diagonal(*values) click to toggle source

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

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

# File matrix.rb, line 204
def Matrix.diagonal(*values)
size = values.size
rows = Array.new(size) {|j|
row = Array.new(size, 0)
row[j] = values[j]
row
}
new rows
end

empty(row_size = 0, column_size = 0) click to toggle source

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

m =...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • ruby
  • Ruby
  • Ruby
  • ruby
  • ruby
  • RUBY
  • Ruby
  • Ruby

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS