class Apatite::LinearAlgebra::Vector
- Apatite::LinearAlgebra::Vector
- Reference
- Object
Overview
Represents a mathematical vector, and also constitutes a row or column
of a Matrix
Included Modules
- Comparable(Apatite::LinearAlgebra::Vector)
- Enumerable(Float64)
- Indexable(Float64)
Defined in:
apatite/linear_algebra/vector.crConstant Summary
-
I =
Vector.create([1.0, 0.0, 0.0])
-
Cartesian unit vector I
-
J =
Vector.create([0.0, 1.0, 0.0])
-
Cartesian unit vector J
-
K =
Vector.create([0.0, 0.0, 1.0])
-
Cartesian unit vector K
Constructors
-
.build(capacity : Int, &block) : self
Creates a new
Vector
, allocating an internal buffer with the given capacity, and yielding that buffer. -
.new(size : Int, value : Float64)
Creates a new
Vector
of the given size filled with the same value in each position. -
.new(initial_capacity : Int)
Creates a new empty
Vector
backed by a buffer that is initiallyinitial_capacity
big. -
.new
Create a new empty
Vector
. -
.new(size : Int, &block : Int32 -> Float64)
Creates a new
Vector
of the given size and invokes the given block once for each index ofself
, assigning the block's value in that index.
Class Method Summary
- .[](*array)
-
.basis(size, index)
Returns a standard basis n-vector.
-
.create(elements : Indexable(Number))
Creates a new
Vector
from the elements of anotherIndexable
collection. -
.ones(n)
Create a new vector of size
n
filled with ones. -
.random(n, range = nil)
Creates a new
Vector
of sizen
filled with random numbers. -
.zeros(n)
Create a new vector of size
n
filled with zeros.
Instance Method Summary
- #*(other)
- #+(other)
- #-(other)
- #/(other)
-
#<<(value : Float64)
Alias for
#push
-
#<=>(other)
Combined comparison operator.
- #==(other : Vector)
-
#[](start : Int, count : Int)
Returns count or less (if there aren't enough) elements starting at the given start index.
-
#[](range : Range(Int, Int))
Returns all elements that are within the given range.
-
#[]=(index : Int, count : Int, values : Vector)
Replaces a subrange with the elements of the given vector.
-
#[]=(index : Int, count : Int, value)
Replaces a subrange with a single value.
-
#[]=(index : Int, value)
Sets the given value at the given index.
-
#[]=(range : Range(Int, Int), value)
Replaces a subrange with a single value.
-
#add(value)
When the input is a number, this returns the result of adding it to all vector elements.
-
#angle_from(vector)
Returns the angle between this vector and another in radians.
-
#antiparallel_to?(vector)
Returns whether the vectors are antiparallel to each other.
-
#augment(elements)
Returns a new vector with the provided
elements
concatenated on the end. -
#chomp(n)
Returns a new vector with the first
n
elements removed from the beginning. -
#clear
Removes all elements from self.
- #clone
- #concat(other : Vector)
-
#covector
Creates a single-row matrix from this vector.
-
#cross(*vs)
Returns the cross product of this vector with the others.
-
#distance_from(obj)
Gets this vector's distance from the argument, when considered a point in space.
-
#divide(value)
When the input is a number, this returns the result of dividing it to all vector elements.
-
#dot(other)
Get the scalar (dot) product of this vector with
vector
. -
#e(i)
Returns the
ith
element of the vector. -
#independent?(*vs)
Returns true if all vectors are linearly independent.
-
#lies_in(plane)
Returns true if the vector is a point on the given plane.
-
#lies_on(line)
Returns true if the vector is a point on the given line
-
#log
Return a new
Vector
with the log of every item inself
. -
#magnitude
Returns the magnitude/euclidian norm of this vector.
-
#map(&block)
Invokes the given block for each element of
self
. -
#map!(&block)
Invokes the given block for each element of
self
, replacing the element with the value returned by the block. -
#map_with_index(&block)
Optimized version of
Enumerable#map_with_index
. -
#map_with_index!(&block)
Like
#map_with_index
, but mutatesself
instead of allocating a new object. -
#max
Returns the (absolute) largest element in this vector.
-
#max_index
Gets the index of the largest element in this vector.
-
#multiply(value)
When the input is a number, this returns the result of multiplying it to all vector elements.
-
#normalize
Returns a new vector created by normalizing this one to have a magnitude of
1
. -
#parallel_to?(vector)
Returns whether the vectors are parallel to each other.
-
#perpendicular_to?(vector)
Returns whether the vectors are perpendicular to each other.
- #pretty_print(pp) : Nil
-
#product
Get the product of all elements in this vector.
- #push(value : Float64)
- #push(*values : Float64)
-
#reflection_in(object)
Returns the result of reflecting the point in the given
object
(point, line, or plane). -
#rotate(t, object)
Rotates the vector about the given
object
. -
#round
Gets the result of rounding the elements of the vector.
- #set_elements(elements)
-
#sigmoid
Sums the numbers in the vector and returns a sigmoid value across the whole vector.
-
#similarity(other)
Compute the cosine similarity between this vector and another.
-
#size : Int32
Returns the number of elements in the vector
-
#snap_to(value)
Returns a copy of the vector with elements set to
value
if they differ from it by less thanApetite.precision
-
#subtract(value)
When the input is a number, this returns the result of subtracting it to all vector elements.
-
#sum
Returns the sum of all elements in the vector.
-
#to_3d
Utility to make sure vectors are 3D.
- #to_a
-
#to_diagonal_matrix
Returns a diagonal
Matrix
with the vectors elements as its diagonal elements. - #to_s(io)
-
#to_unit_vector
Returns a new vector created by normalizing this one to have a magnitude of
1
. -
#to_unsafe : Pointer(Float64)
Returns a pointer to the internal buffer where
self
's elements are stored. -
#top(n)
Returns a vector containing only the first
n
elements. -
#transpose
Transpose this vector into a 1xn
Matrix
- #unsafe_fetch(index : Int)
- #zero?
Constructor Detail
Creates a new Vector
, allocating an internal buffer with the given capacity,
and yielding that buffer. The given block must return the desired size of the vector.
This method is unsafe.
Vector.build(3) do |buffer|
LibSome.fill_buffer_and_return_number_of_elements_filled(buffer)
end
Creates a new Vector
of the given size filled with the same value in each position.
Vector.new(3, 1.0) # => Vector{1.0, 1.0, 1.0}
Creates a new empty Vector
backed by a buffer that is initially
initial_capacity
big.
The initial_capacity is useful to avoid unnecessary reallocations of the internal buffer in case of growth. If you have an estimate of the maximum number of elements an vector will hold, the vector should be initialized with that capacity for improved performance.
vec = Vector.new(5)
vec.size # => 0
Creates a new Vector
of the given size and invokes the given block once
for each index of self
, assigning the block's value in that index.
Vector.new(3) { |i| (i + 1.0) ** 2.0 } # => Vector{1.0, 4.0, 9.0}
vec = Vector.new(3) { 5.0 }
vec # => Vector{5.0, 5.0, 5.0}
Class Method Detail
Creates a new Vector
from the elements of another Indexable
collection.
Vector.create([1, 2, 3]) # Vector{1.0, 2.0, 3.0}
Creates a new Vector
of size n
filled with random numbers. A range
can optionally be passed in if you want to limit the random numbers
to a given range.
Instance Method Detail
Combined comparison operator. Returns 0 if self
equals other, 1 if
self
is greater than other and -1 if self
is smaller than other.
It compares the elements of both vectors in the same position using the
#<=>
operator. As soon as one of such comparisons returns a non-zero
value, that result is the return value of the comparison.
If all elements are equal, the comparison is based on the size of the vectors.
Vector{8.0} <=> Vector(1.0, 2.0, 3.0} # => 1
Vector{2.0} <=> Vector{4.0, 2.0, 3.0} # => -1
Vector{1.0, 2.0} <=> Vector{1.0, 2.0} # => 0
Returns count or less (if there aren't enough) elements starting at the given start index.
Negative indices count backward from the end of the vector (-1 is the last element). Additionally, an empty vector is returned when the starting index for an element range is at the end of the vector.
Raises IndexError
if the starting index is out of range.
v = Vector{1.0, 2.0, 3.0, 4.0, 5.0}
v[1, 3] # => Vector{2.0, 3.0, 4.0}
v[6, 10] # raise IndexError
Returns all elements that are within the given range.
Negative indices count backward from the end of the vector (-1 is the last element). Additionally, an empty vector is returned when the starting index for an element range is at the end of the vector.
Raises IndexError
if the starting index is out of range.
v = Vector{1.0, 2.0, 3.0, 4.0, 5.0}
v[1..3] # => Vector{2.0, 3.0, 4.0}
v[6..10] # raise IndexError
Replaces a subrange with the elements of the given vector.
vec = Vector{1.0, 2.0, 3.0, 4.0, 5.0}
vec[1, 3] = Vector{6.0, 7.0, 8.0}
vec # = Vector{1.0, 6.0, 7.0, 8.0, 5.0}
vec = Vector{1.0, 2.0, 3.0, 4.0, 5.0}
vec[1, 3] = Vector{6.0, 7.0}
vec # = Vector{1.0, 6.0, 7.0, 5.0}
Replaces a subrange with a single value. All elements in the range
index...index+count
are removed and replaced by a single element
value.
If count is zero, value is inserted at index.
Negative values of index count from the end of the vector.
vec = Vector{1.0, 2.0, 3.0, 4.0, 5.0}
vec[1, 3] = 6.0
vec # => Vector{1.0, 6.0, 5.0}
vec = Vector{1.0, 2.0, 3.0, 4.0, 5.0}
vec[1, 0] = 6.0
vec # => Vector{1.0, 6.0, 2.0, 3.0, 4.0, 5.0}
Sets the given value at the given index.
Negative indices can be used to start counting from the end of the array.
Raises IndexError
if trying to set an element outside the array's range.
vec = Vector{1.0, 2.0, 3.0}
vec[0] = 5.0
p vec # => Vec{5.0, 2.0, 3.0}
vec[3] = 5.0 # raises IndexError
Replaces a subrange with a single value.
vec = Vector{1.0, 2.0, 3.0, 4.0, 5.0}
vec[1..3] = 6.0
vec # = Vector{1.0, 6.0, 5.0}
vec = Vector{1.0, 2.0, 3.0, 4.0, 5.0}
vec[1...1] = 6
vec # = Vector{1.0, 6.0, 2.0, 3.0, 4.0, 5.0}
When the input is a number, this returns the result of adding it to all vector elements. When it's a vector, the vectors will be added together.
Returns the angle between this vector and another in radians.
If the vectors are mirrored across their axes this will return nil
.
Returns the cross product of this vector with the others.
v1 = Vector{1.0, 0.0, 0.0}
v2 = Vector{0.0, 1.0, 0.0}
v1.cross(v2) => Vector{0.0, 0.0, 1.0}
Gets this vector's distance from the argument, when considered a point in space.
When the input is a number, this returns the result of dividing it to all vector elements. When it's a vector, the vectors will be element-wise divided.
Returns the ith
element of the vector. Returns nil
if i
is out of bounds. Indexing starts from 1.
Invokes the given block for each element of self
, replacing the element
with the value returned by the block. Returns self
.
vec = Vector{1.0, 2.0, 3.0}
vec.map! { |x| x * x }
a # => Vector{1.0, 4.0, 9.0}
Like #map_with_index
, but mutates self
instead of allocating a new object.
When the input is a number, this returns the result of multiplying it to all vector elements. When it's a vector, the vectors will be element-wise multiplied.
Returns a new vector created by normalizing this one
to have a magnitude of 1
. If the vector is a zero
vector, it will not be modified.
Returns the result of reflecting the point in the given object
(point, line, or plane).
Rotates the vector about the given object
. The object should
be a point if the vector is 2D, and a line if it is 3D. Be
careful with line directions!
Returns a copy of the vector with elements set to value
if
they differ from it by less than Apetite.precision
When the input is a number, this returns the result of subtracting it to all vector elements. When it's a vector, the vectors will be subtracted.
Returns a diagonal Matrix
with the vectors elements as its
diagonal elements.
Returns a new vector created by normalizing this one
to have a magnitude of 1
. If the vector is a zero
vector, it will not be modified.
Returns a pointer to the internal buffer where self
's elements are stored.
This method is unsafe because it returns a pointer, and the pointed might eventually
not be that of self
if the array grows and its internal buffer is reallocated.
vec = Vector{1.0, 2.0, 3.0}
vec.to_unsafe[0] # => 1.0