class Linalg::Vector(T)
- Linalg::Vector(T)
- Reference
- Object
Defined in:
linalg/vector.crConstructors
-
.new(elements : Array(T))
Creates a new vector filled with the given elements.
-
.new(size : Int)
Creates a new zero vector of the given size.
-
.new
Creates a new empty vector.
Instance Method Summary
-
#*(other : Linalg::Matrix(U)) forall U
Vector-matrix multiplication.
-
#*(other : Linalg::Vector)
Dot product.
-
#*(scalar : U) forall U
Scaling.
-
#+(other : Linalg::Vector(U)) forall U
Vector addition.
-
#-(other : Linalg::Vector(U)) forall U
Vector subtraction.
-
#- : self
Unary operator.
-
#<<(value : T)
Appends the given value to the end of the vector.
-
#==(other : Linalg::Vector)
Returns
true
if each element inself
is equal to each corresponding element in other. -
#[](index : Int)
Returns the element at the given index.
-
#[]=(index : Int, value : T)
Sets the given value at the given index.
-
#dot(other : Linalg::Vector(U)) forall U
Returns the dot product of
self
and other. -
#each(&block : 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. -
#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.
-
#zero?
Return
true
ifself
is a zero vector, otherwisefalse
.
Constructor Detail
Creates a new vector filled with the given elements.
vec = Linalg::Vector.new([1, 2, 3])
vec # => [1, 2, 3]
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]
Instance Method Detail
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]
Dot product. Returns the dot product of self
and other.
NOTE Same as Linalg::Vector#dot
.
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]
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]
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]
Unary operator. Returns the inverted vector to self
.
vec = Linalg::Vector.new([1, 2, 3, 4, 5])
-vec # => [-1, -2, -3, -4, -5]
Returns true
if each element in self
is equal to each
corresponding element in other.
Iterates over the collection, yielding both the elements and their index.
See Enumerable#each_with_index
for more details.
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>
Return true
if self
is a zero vector, otherwise false
.
NOTE An empty vector is not considered a zero vector.