class Linalg::Matrix(T)
- Linalg::Matrix(T)
- Reference
- Object
Defined in:
linalg/matrix.crConstructors
-
.new(rows : Int, columns : Int)
Creates a new zero matrix with dimensions row x columns.
-
.new(elements : Array(Array(T)))
Creates a new matrix filled with vectors of the given elements.
-
.new(elements : Array(Linalg::Vector(T)))
Creates a new matrix filled with the given elements.
-
.new(size : Int)
Creates a new identity matrix of the given size.
-
.new
Creates a new empty matrix.
Instance Method Summary
-
#*(other : Linalg::Vector(U)) forall U
Matrix-vector multiplication
-
#*(other : Linalg::Matrix)
Returns the product of
self
and other. -
#*(scalar : U) forall U
Scales all the vectors in
self
by the given scalar and return a newLinalg::Matrix
. -
#+(other : Linalg::Matrix(U)) forall U
Matrix addition.
-
#-(other : Linalg::Matrix(U)) forall U
Matrix subtraction.
-
#- : self
Unary operator.
-
#<<(value : Array(T))
Append.
-
#<<(value : Linalg::Vector(T))
Appends the given vector to
self
. -
#==(other : Linalg::Matrix)
Returns
true
if each element inself
is equal to each corresponding element in other. -
#[](row : Int, col : Int)
Returns the element at the given position (row, col).
-
#[](index : Int)
Returns the element at the given index.
-
#[]=(row : Int, col : Int, value : T)
Sets the given value at the given position (row, col).
-
#append_column(column : Array(T))
Appends the given column to
self
. -
#append_column(column : Linalg::Vector(T))
Appends the given column to
self
. -
#columns : Int32
Returns the number of columns in the matrix.
-
#each(&block : Linalg::Vector(T) -> _)
Iterates over the collection, yielding the elements.
-
#each_with_index(offset = 0, &)
Iterates over the collection, yielding both the elements and their index.
-
#empty?
Returns
true
ifself
is empty,false
otherwise. -
#get_column(index : Int) : Linalg::Vector(T)
Returns the column at the given index.
-
#identity?
Return
true
ifself
is an identity matrix, otherwisefalse
. -
#product(other : Linalg::Matrix(U)) forall U
Returns the product of
self
and other. -
#rows : Int32
Returns the number of rows in the matrix.
-
#size
Returns the number of elements in the vector.
-
#to_s(io : IO) : Nil
Appends a short String representation of this object which includes its class name and its object address.
-
#transpose
Transposes the rows and columns of
self
. -
#zero?
Returns
true
ifself
is a zero matrix, otherwisefalse
.
Constructor Detail
Creates a new zero matrix with dimensions row x columns.
Linalg::Matrix(Float64).new(2, 2) # => [[0.0, 0.0], [0.0, 0.0]]
Linalg::Matrix(Int32).new(2, 3) # => [[0, 0, 0], [0, 0, 0]]
Creates a new matrix filled with vectors of the given elements.
matrix = Linalg::Matrix.new([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
matrix # => [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
An ArgumentError
is raised if the elements in the given array have
differents sizes.
Creates a new matrix filled with the given elements.
rows = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
vectors = Array(Linalg::Vector(Int32)).new
rows.each { |r| vectors << Linalg::Vector.new(r) }
mat = Linalg::Matrix.new(vectors)
mat # => [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
An ArgumentError
is raised if the given vectors have different sizes.
Creates a new identity matrix of the given size.
Linalg::Matrix(Int32).new(1) # => [[1]]
Linalg::Matrix(Int32).new(2) # => [[1, 0], [0, 1]]
Linalg::Matrix(Float64).new(3) # => [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
An ArgumentError
is raised if the given size is less than 1.
Instance Method Detail
Matrix-vector multiplication
vec = Linalg::Vector.new([2.0, 3.0, 4.0])
mat = Linalg::Matrix.new([[5, 2, 6], [7, 2, 5], [1, 4, 2]])
mat * vec # => [40.0, 40.0, 22.0]
Returns the product of self
and other.
If self
is a m x n matrix and other is a n x p matrix then the product
will be a m x p matrix.
mat1 = Linalg::Matrix.new([[2, 1, 4], [-1, 0, 3]])
mat2 = Linalg::Matrix.new([[1, 6], [5, 1], [2, -2]])
product = mat1.product(mat2)
product # => [[15, 5], [5, -12]]
NOTE Same as Linalg::Matrix#product
Scales all the vectors in self
by the given scalar and return a new Linalg::Matrix
.
mat = Linalg::Matrix.new([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
scaled_mat = mat * 2.0
scaled_mat # => [[2.0, 4.0, 6.0], [8.0, 10.0, 12.0], [14.0, 16.0, 18.0]]
Matrix addition. Adds self
and other together and returns a new Linalg::Matrix
.
mat1 = Linalg::Matrix.new([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])
mat2 = Linalg::Matrix.new([[4, -1, 6], [2, 9, -3], [-4, 5, 1]])
mat3 = mat1 + mat2
mat3 # => [[5.0, 1.0, 9.0], [6.0, 14.0, 3.0], [3.0, 13.0, 10.0]]
Matrix subtraction. Subtracts other from self
and returns a new Linalg::Matrix
.
mat1 = Linalg::Matrix.new([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])
mat2 = Linalg::Matrix.new([[4, -1, 6], [2, 9, -3], [-4, 5, 1]])
mat3 = mat1 - mat2
mat3 # => [[-3.0, 3.0, -3.0], [2.0, -4.0, 9.0], [11.0, 3.0, 8.0]]
Unary operator. Returns the inverted matrix to self
.
mat = Linalg::Matrix.new([[-1, 2, -3], [-4, -5, -6], [7, -8, 9]])
-mat # => [[1, -2, 3], [4, 5, 6], [-7, 8, -9]]
Append. Converts the given array into a Linalg::Vector
and appends it to self
.
mat = Linalg::Matrix(Int32).new
mat << [1, 2, 3]
mat # => [1, 2, 3]
An ArgumentError
is raised if the given array doesn't have the same
size as the vectors already in the matrix.
Appends the given vector to self
.
mat = Linalg::Matrix(Int32).new
mat << Linalg::Vector.new([1, 2, 3])
mat # => [1, 2, 3]
An ArgumentError
is raised if the given vector doesn't have the same
size as the vectors already in the matrix.
Returns true
if each element in self
is equal to each
corresponding element in other.
Returns the element at the given position (row, col).
mat = Linalg::Matrix.new([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
mat[1, 0] # => 4
Returns the element at the given index.
mat = Linalg::Matrix.new([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
mat[2] # => [7, 8, 9]
Sets the given value at the given position (row, col).
mat = Linalg::Matrix.new([[1, 0, 0], [0, 0, 0], [0, 0, 1]])
mat[1, 1] = 1
mat # => [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
Appends the given column to self
.
mat = Linalg::Matrix.new([[1, 2], [4, 5], [7, 8]])
mat.append_column([3, 6, 9])
mat # => [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Appends the given column to self
.
mat = Linalg::Matrix.new([[1, 2], [4, 5], [7, 8]])
mat.append_column([3, 6, 9])
mat # => [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Iterates over the collection, yielding both the elements and their index.
See Enumerable#each_with_index
for more details.
Returns the column at the given index.
Assumes that self
is a non-empty Linalg::Matrix
with at least 1 column.
mat = Linalg::Matrix.new([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
mat.get_column(1) # => [2, 5, 8]
Returns the product of self
and other.
If self
is a m x n matrix and other is a n x p matrix then the product
will be a m x p matrix.
mat1 = Linalg::Matrix.new([[2, 1, 4], [-1, 0, 3]])
mat2 = Linalg::Matrix.new([[1, 6], [5, 1], [2, -2]])
product = mat1.product(mat2)
product # => [[15, 5], [5, -12]]
Appends a short String representation of this object which includes its class name and its object address.
class Person
def initialize(@name : String, @age : Int32)
end
end
Person.new("John", 32).to_s # => #<Person:0x10a199f20>
Transposes the rows and columns of self
.
mat = Linalg::Matrix.new([[1, 2], [3, 4], [5, 6]])
mat.transpose # => [[1, 3, 5], [2, 4, 6]]
mat # => [[1, 2], [3, 4], [5, 6]]
Returns true
if self
is a zero matrix, otherwise false
.
NOTE An empty matrix is not considered a zero matrix.