class Linalg::Vector(T)

Defined in:

linalg/vector.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(elements : Array(T)) #

Creates a new vector filled with the given elements.

vec = Linalg::Vector.new([1, 2, 3])
vec # => [1, 2, 3]

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

Creates a new zero vector of the given size.

vec = Linalg::Vector(Int32).new(3)
vec # => [0, 0, 0]
vec2 = Linalg::Vector(Float64).new(3)
vec2 # => [0.0, 0.0, 0.0]

[View source]
def self.new #

Creates a new empty vector.

vec = Linalg::Vector(Int32).new
vec # => []

[View source]

Instance Method Detail

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

Vector-matrix multiplication.

vec = Linalg::Vector.new([2.0, 3.0, 4.0])
mat = Linalg::Matrix.new([[5, 2, 6], [7, 2, 5], [1, 4, 2]])
vec * mat # => [35.0, 26.0, 35.0]

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

Dot product. Returns the dot product of self and other.

NOTE Same as Linalg::Vector#dot.


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

Scaling. Scales self by the given scalar and returns a new Linalg::Vector.

vec = Linalg::Vector.new([1, 2, 3])
scalar = 2

scaled_vec = vec * scalar
scaled_vec # => [2, 4, 6]

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

Vector addition. Adds self and other together and returns a new Linalg::Vector.

vec1 = Linalg::Vector.new([1, 2, 3])
vec2 = Linalg::Vector.new([2.1, 3.2, 4.3])

vec3 = vec1 + vec2
vec3 # => [3.1, 5.2, 7.3]

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

Vector subtraction. Subtracts other from self and returns a new Linalg::Vector.

vec1 = Linalg::Vector.new([2, 3, 4])
vec2 = Linalg::Vector.new([1, 2, 3])

vec3 = vec1 - vec2
vec3 # => [1, 1, 1]

[View source]
def - : self #

Unary operator. Returns the inverted vector to self.

vec = Linalg::Vector.new([1, 2, 3, 4, 5])
-vec # => [-1, -2, -3, -4, -5]

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

Appends the given value to the end of the vector.


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

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


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

Returns the element at the given index.


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

Sets the given value at the given index.


[View source]
def dot(other : Linalg::Vector(U)) forall U #

Returns the dot product of self and other.


[View source]
def each(&block : 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 size #

Returns the number of elements in the vector.


[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 zero? #

Return true if self is a zero vector, otherwise false.

NOTE An empty vector is not considered a zero vector.


[View source]