MatrixAlgebra / MatrixAlgebra::Vector / MatrixAlgebra::Covector / GaussianElimination
(Class of Matrices)
This class expresses matrices. For creating an actual class, use the class method MatrixAlgebra.create or the function MatrixAlgebra, giving the ground ring and sizes.
That has MatrixAlgebra::Vector(column vectorj, MatrixAlgebra::Covector(row vector), MatrixAlgebra::SquareMatrix(square matrix) as subclass.
MatrixAlgebra(ground, m, n)
Same as MatrixAlgebra.create(ground, m, n).
MatrixAlgebra.create(ground, m, n)
Creates the class of matrix of type (m, n)
with
elements of the ring ground.
The return value of this method is a subclass of
MatrixAlgebra.
The subclass has class methods:
ground, rsize, csize and sizes,
which returns the ground ring, the size of rows( m ),
the size of columns( n ) and the array of [m, n]
respectively.
To create the actual matrix, use MatrixAlgebra.new, MatrixAlgebra.matrix or MatrixAlgebra.[].
MatrixAlgebra.new(array)
Returns the matrix of the elements designated by the array of arrays array.
Example:
M = MatrixAlgebra(Integer, 2, 3) a = M.new([[1, 2, 3], [4, 5, 6]]) a.display #=> [1, 2, 3] #=> [4, 5, 6]
MatrixAlgebra.matrix{|i, j| ... }
Returns the matrix which has the i-j
-th elements
evaluating ..., where i and j are the row
and the column indices
Example:
M = MatrixAlgebra(Integer, 2, 3) a = M.matrix{|i, j| 10*(i + 1) + j + 1} a.display #=> [11, 12, 13] #=> [21, 22, 23]
MatrixAlgebra.[array1, array2, ..., array]
Returns the matrix which has array1, array2, ..., array
as rows.
Example:
M = MatrixAlgebra(Integer, 2, 3) a = M[[1, 2, 3], [4, 5, 6]] a.display #=> [1, 2, 3] #=> [4, 5, 6]
MatrixAlgebra.collect_ij{|i, j| ... }
Returns the array of arrays with the value ... as the j-th element of the i-th array.
MatrixAlgebra.collect_row{|i| ... }
Returns the matrix whose i-th row is the array obtained by evaluating ....
Example:
M = MatrixAlgebra(Integer, 2, 3) A = M.collect_row{|i| [i*10 + 11, i*10 + 12, i*10 + 13]} A.display #=> [11, 12, 13] #=> [21, 22, 23]
MatrixAlgebra.collect_column{|j| ... }
Returns the matrix whose j-th column is the array obtained by evaluating ....
Example:
M = MatrixAlgebra(Integer, 2, 3) A = M.collect_column{|j| [11 + j, 21 + j]} A.display #=> [11, 12, 13] #=> [21, 22, 23]
MatrixAlgebra.*(otype)
Returns the class of matrix multiplicated by otype.
Example:
M = MatrixAlgebra(Integer, 2, 3) N = MatrixAlgebra(Integer, 3, 4) L = M * N p L.sizes #=> [3, 4]
MatrixAlgebra.vector
Returns the class of column-vector(Vector) which has the same size of rsize.
MatrixAlgebra.covector
Returns the class of row-vector(CoVector) which has the same size of csize.
MatrixAlgebra.transpose
Returns the transposed matrix
MatrixAlgebra.zero
Returns the zero matrix.
[i, j]
Returns the (i, j)
-th component.
[i, j] = x
Replaces the (i, j)
-th component with x.
rsize
Same as MatrixAlgebra.rsize.
csize
Same as MatrixAlgebra.csize.
sizes
Same as MatrixAlgebra.sizes.
rows
Returns the array of rows.
Example:
M = MatrixAlgebra(Integer, 2, 3) a = M.new([[1, 2, 3], [4, 5, 6]]) p a.rows #=> [[1, 2, 3], [4, 5, 6]] p a.row(1) #=> [4, 5, 6] a.set_row(1, [40, 50, 60]) a.display #=> [1, 2, 3] #=> [40, 50, 60]
row(i)
Returns the i-th row as an array.
set_row(i, array)
Replaces the i-th row with array.
columns
Returns the array of columns.
á:
M = MatrixAlgebra(Integer, 2, 3) a = M.new([[1, 2, 3], [4, 5, 6]]) p a.columns #=> [[1, 4], [2, 5], [3, 6]] p a.column(1) #=> [2, 5] a.set_column(1, [20, 50]) a.display #=> [1, 20, 3] #=> [4, 50, 6]
column(j)
Returns the j-th column as an array.
set_column(j, array)
Replaces the i-th column with array.
each{|row| ...}
Iterates with row.
each_index{|i, j| ...}
Iterates with indices (i, j)
.
each_i{|i| ...}
Iterates with the index i
of rows.
each_j{|j| ...}
Iterates with the index j
of columns.
each_row{|r| ... }
Iterates with the row r. Same as each.
each_column{|c| ... }
Iterates with the column c.
matrix{|i, j| ... }
Same as MatrixAlgebra.matrix.
collect_ij{|i, j| ... }
Same as MatrixAlgebra.collect_ij.
collect_row{|i| ... }
Same as MatrixAlgebra.collect_row.
collect_column{|j| ... }
Same as MatrixAlgebra.collect_column.
==(other)
Returns true if self is equal to other.
+(other)
Returns the sum of self and other.
-(other)
Returns the difference of self from other.
*(other)
Returns the product of self and other.
Example:
M = MatrixAlgebra(Integer, 2, 3) N = MatrixAlgebra(Integer, 3, 4) L = M * N a = M[[1, 2, 3], [4, 5, 6]] b = N[[-3, -2, -1, 0], [1, 2, 3, 4], [5, 6, 7, 8]] c = a * b p c.type #=> L c.display #=> [14, 20, 26, 32] #=> [23, 38, 53, 68]
**(n)
Returns the n-th power of self.
/(other)
Returns the quotient self by other.
transpose
Returns the transposed matrix.
Example:
M = MatrixAlgebra(Integer, 2, 3) a = M.new([[1, 2, 3], [4, 5, 6]]) Mt = M.transpose b = a.transpose p b.type #=> Mt b.display #=> [1, 4] #=> [2, 5] #=> [3, 6]
dup
Returns the duplication of self.
Example:
M = MatrixAlgebra(Integer, 2, 3) a = M.new([[1, 2, 3], [4, 5, 6]]) b = a.dup b[1, 1] = 50 a.display #=> [1, 2, 3] #=> [4, 5, 6] b.display #=> [1, 2, 3] #=> [4, 50, 6]
display([out])
Displays self to out. If out is omitted, out is $stdout.
(Class of Vector)
The class of column vectors.
none.
Vector(ground, n)
Same as MatrixAlgebra::Vector.create(ground, n).
MatrixAlgebra::Vector.create(ground, n)
Creates the class of the n-th dimensional (column) vector over the ring ground.
The return value of this is a subclass of Vector. This subclass has the class methods: ground and size, which returns ring and the size n respectively.
To get actual vectors, use MatrixAlgebra::Vector.new, MatrixAlgebra::Vector.matrix or MatrixAlgebra::Vector.[].
MatrixAlgebra::Vector is identified with MatrixAlgebra
of type [n, 1]
.
MatrixAlgebra::Vector.new(array)
Returns the vector of the array.
Example:
V = Vector(Integer, 3) a = V.new([1, 2, 3]) a.display #=> [1] #=> [2] #=> [3]
MatrixAlgebra::Vector.vector{|i| ... }
Returns the vector of ... as the i-th element.
Example:
V = Vector(Integer, 3) a = V.vector{|j| j + 1} a.display #=> [1] #=> [2] #=> [3]
MatrixAlgebra::Vector.matrix{|i, j| ... }
Returns the vector of ... as the i-th element. j is always 0.
size
Returns the dimension.
to_a
Returns the array of elements.
transpose
Transpose to the row vector MatrixAlgebra::Covector.
(Row Vector Class)
The class of row vectors.
none.
Covector(ground, n)
Same as MatrixAlgebra::Covector.create(ground, n).
MatrixAlgebra::Covector.create(ground, n)
Creates the class of the n-th dimensional (row) vector over the ring ground.
The return value of this is a subclass of CoVector. This subclass has the class methods: ground and size, which returns ring and the size n respectively.
To get actual vectors, use MatrixAlgebra::CoVector.new, MatrixAlgebra::CoVector.matrix or MatrixAlgebra::CoVector.[].
MatrixAlgebra::Covector is identified with [1, n]
-type
MatrixAlgebra.
MatrixAlgebra::Covector.new(array)
Returns the row vector of the array.
Example:
V = Covector(Integer, 3) a = V.new([1, 2, 3]) a.display #=> [1, 2, 3]
MatrixAlgebra::Covector.covector{|j| ... }
Returns the vector of ... as the j-th element.
Example:
V = Covector(Integer, 3) a = V.covector{|j| j + 1} a.display #=> [1, 2, 3]
MatrixAlgebra::Covector.matrix{|i, j| ... }
Returns the vector of ... as the j-th element. i is always 0.
size
Returns the dimension.
to_a
Returns the array of elements.
transpose
Transpose to the column vector MatrixAlgebra::Vector.
(Class of SquareMatrix)
The Ring of Square Matrices over a ring.
none.
SquareMatrix(ground, size)
Same as MatrixAlgebra::SquareMatrix.create(ground, n).
MatrixAlgebra::SquareMatrix.create(ground, n)
Creates the class of square matrices.
The return value of this is the subclass of SquareMatrix. This sub class has the class methods ground and size which returns ring and the size n respectively.
SquareMatrix is identified with MatrixAlgebra of type
[n, n]
.
To get the actual matrices, use the class methods MatrixAlgebra.new, MatrixAlgebra.matrix or MatrixAlgebra.[].
MatrixAlgebra::SquareMatrix.unity
Returns the unity.
MatrixAlgebra::SquareMatrix.zero
Returns the zero.
MatrixAlgebra::SquareMatrix.const(x)
Returns the scalar matrix with by the diagonal components x.
size
Returns the dimension.
const(x)
Returns the scalar matrix with the diagonal components x.
determinant
Returns the determinant.
char_polynomial(ring)
Returns the characteristic polynomial over ring.
(Module of Gaussian Elimination)
Module of the elimination method of Gauss.
gaussian-elimination.rb
none.
none.
swap_r!(i, j)
Swaps i-th row and j-th row.
swap_r(i, j)
Returns the new matrix with i-th row and j-th row swapped.
swap_c!(i, j)
Swaps i-th column and j-th column.
swap_c(i, j)
Returns the new matrix with i-th column and j-th column swapped.
multiply_r!(i, c)
Multiplys the i-th row by c.
multiply_r(i, c)
Returns the new Matrix with the i-th row multiplied by c.
multiply_c!(j, c)
Multiplys the j-th column by c.
multiply_c(j, c)
Returns the new Matrix with the j-th column multiplied by c.
mix_r!(i, j, c)
Adds the j-th row multiplied by c to the i-th row.
mix_r(i, j, c)
Returns the new matrix such that the j-th row multiplied by c is added to the i-th row.
mix_c!(i, j, c)
Adds the j-th column multiplied by c to the i-th column.
mix_c(i, j, c)
Returns the new matrix such that the j-th column multiplied by c is added to the i-th column.
left_eliminate!
Transform to the step matrix by the left fundamental transformation.
The return value is the array of the square matrix which used to transform and its determinant.
Example:
require "matrix-algebra" require "mathn" class Rational < Numeric def inspect; to_s; end end M = MatrixAlgebra(Rational, 4, 3) a = M.matrix{|i, j| i*10 + j} b = a.dup c, d = b.left_eliminate! b.display #=> [1, 0, -1] #=> [0, 1, 2] #=> [0, 0, 0] #=> [0, 0, 0] c.display #=> [-11/10, 1/10, 0, 0] #=> [1, 0, 0, 0] #=> [1, -2, 1, 0] #=> [2, -3, 0, 1] p c*a == b#=> true p d #=> 1/10
left_inverse
The general inverse matrix obtained by the left fundamental transformation.
left_sweep
Returns the step matrix by the left fundamental transformation.
step_matrix?
Returns the pivots if self is a step matrix, otherwise returns nil.
kernel_basis
Returns the array of vector( MatrixAlgebra::Vector ) such that the right multiplication of it is null.
Example:
require "matrix-algebra" require "mathn" M = MatrixAlgebra(Rational, 5, 4) a = M.matrix{|i, j| i + j} a.display #=> #[0, 1, 2, 3] #[1, 2, 3, 4] #[2, 3, 4, 5] #[3, 4, 5, 6] #[4, 5, 6, 7] a.kernel_basis.each do |v| puts "a * #{v} = #{a * v}" #=> a * [1, -2, 1, 0] = [0, 0, 0, 0, 0] #=> a * [2, -3, 0, 1] = [0, 0, 0, 0, 0] end
determinant_by_elimination
Calculate the determinant by elimination.