class Apatite::LinearAlgebra::Vector

Overview

Represents a mathematical vector, and also constitutes a row or column of a Matrix

Included Modules

Defined in:

apatite/linear_algebra/vector.cr

Constant 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

Class Method Summary

Instance Method Summary

Constructor Detail

def self.build(capacity : Int, &block) : self #

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

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

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}

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

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

[View source]
def self.new #

Create a new empty Vector.


[View source]
def self.new(size : Int, &block : Int32 -> Float64) #

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}

[View source]

Class Method Detail

def self.[](*array) #

[View source]
def self.basis(size, index) #

Returns a standard basis n-vector.


[View source]
def self.create(elements : Indexable(Number)) #

Creates a new Vector from the elements of another Indexable collection.

Vector.create([1, 2, 3]) # Vector{1.0, 2.0, 3.0}

[View source]
def self.ones(n) #

Create a new vector of size n filled with ones.


[View source]
def self.random(n, range = nil) #

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.


[View source]
def self.zeros(n) #

Create a new vector of size n filled with zeros.


[View source]

Instance Method Detail

def *(other) #

[View source]
def +(other) #

[View source]
def -(other) #

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

Alias for #push


[View source]
def <=>(other) #

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

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

[View source]
def [](start : Int, count : Int) #

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

[View source]
def [](range : Range(Int, Int)) #

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

[View source]
def []=(index : Int, count : Int, values : Vector) #

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}

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

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}

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

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

[View source]
def []=(range : Range(Int, Int), value) #

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}

[View source]
def add(value) #

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.


[View source]
def angle_from(vector) #

Returns the angle between this vector and another in radians. If the vectors are mirrored across their axes this will return nil.


[View source]
def antiparallel_to?(vector) #

Returns whether the vectors are antiparallel to each other.


[View source]
def augment(elements) #

Returns a new vector with the provided elements concatenated on the end.


[View source]
def chomp(n) #

Returns a new vector with the first n elements removed from the beginning.


[View source]
def clear #

Removes all elements from self.

vec = Vector{1.0, 2.0, 3.0}
vec.clear # => Vector{}

[View source]
def clone #

[View source]
def concat(other : Vector) #

[View source]
def covector #

Creates a single-row matrix from this vector.


[View source]
def cross(*vs) #

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}

[View source]
def distance_from(obj) #

Gets this vector's distance from the argument, when considered a point in space.


[View source]
def dot(other) #

Get the scalar (dot) product of this vector with vector.

https://en.wikipedia.org/wiki/Scalar_product


[View source]
def e(i) #

Returns the ith element of the vector. Returns nil if i is out of bounds. Indexing starts from 1.


[View source]
def independent?(*vs) #

Returns true if all vectors are linearly independent.


[View source]
def lies_in(plane) #

Returns true if the vector is a point on the given plane.


[View source]
def lies_on(line) #

Returns true if the vector is a point on the given line


[View source]
def log #

Return a new Vector with the log of every item in self.


[View source]
def magnitude #

Returns the magnitude/euclidian norm of this vector.

https://en.wikipedia.org/wiki/Euclidean_distance


[View source]
def map(&block) #

Invokes the given block for each element of self.


[View source]
def map!(&block) #

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}

[View source]
def map_with_index(&block) #

Optimized version of Enumerable#map_with_index.


[View source]
def map_with_index!(&block) #

Like #map_with_index, but mutates self instead of allocating a new object.


[View source]
def max #

Returns the (absolute) largest element in this vector.


[View source]
def max_index #

Gets the index of the largest element in this vector.


[View source]
def multiply(value) #

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.


[View source]
def parallel_to?(vector) #

Returns whether the vectors are parallel to each other.


[View source]
def perpendicular_to?(vector) #

Returns whether the vectors are perpendicular to each other.


[View source]
def pretty_print(pp) : Nil #

[View source]
def product #

Get the product of all elements in this vector.


[View source]
def push(*values : Float64) #

[View source]
def push(value : Float64) #

[View source]
def reflection_in(object) #

Returns the result of reflecting the point in the given object (point, line, or plane).


[View source]
def rotate(t, object) #

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!


[View source]
def round #

Gets the result of rounding the elements of the vector.


[View source]
def set_elements(elements) #

[View source]
def sigmoid #

Sums the numbers in the vector and returns a sigmoid value across the whole vector.


[View source]
def size : Int32 #

Returns the number of elements in the vector


[View source]
def snap_to(value) #

Returns a copy of the vector with elements set to value if they differ from it by less than Apetite.precision


[View source]
def subtract(value) #

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.


[View source]
def sum #

Returns the sum of all elements in the vector.


[View source]
def to_3d #

Utility to make sure vectors are 3D. If they are 2D, a zero z-component is added.


[View source]
def to_a #

[View source]
def to_diagonal_matrix #

Returns a diagonal Matrix with the vectors elements as its diagonal elements.


[View source]
def to_s(io) #

[View source]
def to_unit_vector #

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.


[View source]
def to_unsafe : Pointer(Float64) #

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

[View source]
def top(n) #

Returns a vector containing only the first n elements.


[View source]
def transpose #

Transpose this vector into a 1xn Matrix


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

[View source]
def zero? #

[View source]