class Linalg::Matrix(T)

Defined in:

linalg/matrix.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(rows : Int, columns : Int) #

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]]

[View source]
def self.new(elements : Array(Array(T))) #

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.


[View source]
def self.new(elements : Array(Linalg::Vector(T))) #

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.


[View source]
def self.new(size : Int) #

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.


[View source]
def self.new #

Creates a new empty matrix.

matrix = Linalg::Matrix(Float64).new
matrix # => []

[View source]

Instance Method Detail

def *(other : Linalg::Vector(U)) forall U #

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]

[View source]
def *(other : Linalg::Matrix) #

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


[View source]
def *(scalar : U) forall U #

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]]

[View source]
def +(other : Linalg::Matrix(U)) forall U #

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]]

[View source]
def -(other : Linalg::Matrix(U)) forall U #

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]]

[View source]
def - : self #

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]]

[View source]
def <<(value : Array(T)) #

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.


[View source]
def <<(value : Linalg::Vector(T)) #

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.


[View source]
def ==(other : Linalg::Matrix) #

Returns true if each element in self is equal to each corresponding element in other.


[View source]
def [](row : Int, col : Int) #

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

[View source]
def [](index : Int) #

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]

[View source]
def []=(row : Int, col : Int, value : T) #

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]]

[View source]
def append_column(column : Array(T)) #

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]]

[View source]
def append_column(column : Linalg::Vector(T)) #

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]]

[View source]
def columns : Int32 #

Returns the number of columns in the matrix.


[View source]
def each(&block : Linalg::Vector(T) -> _) #

Iterates over the collection, yielding the elements.


[View source]
def each_with_index(offset = 0, &) #

Iterates over the collection, yielding both the elements and their index.

See Enumerable#each_with_index for more details.


[View source]
def empty? #

Returns true if self is empty, false otherwise.


[View source]
def get_column(index : Int) : Linalg::Vector(T) #

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]

[View source]
def identity? #

Return true if self is an identity matrix, otherwise false.


[View source]
def product(other : Linalg::Matrix(U)) forall U #

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]]

[View source]
def rows : Int32 #

Returns the number of rows in the matrix.

NOTE Same as Linalg::Matrix#size.


[View source]
def size #

Returns the number of elements in the vector.

NOTE Same as Linalg::Matrix#rows.


[View source]
def to_s(io : IO) : Nil #
Description copied from class Reference

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>

[View source]
def transpose #

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]]

[View source]
def zero? #

Returns true if self is a zero matrix, otherwise false.

NOTE An empty matrix is not considered a zero matrix.


[View source]