# struct Geode::Vector(T, N)

## Overview

Generic vector type. Provides a collection of scalars of the same type.

T is the scalar type. N is a positive integer indicating the number of components.

## Defined in:

geode/vectors/vector.cr

## Constructor Detail

def self.new(vector : CommonVector(T, N)) #

Copies the contents of another vector.

def self.new(array : StaticArray(T, N)) #

Constructs the vector with pre-existing values.

The memory allocated for components must match the size of the vector.

def self.new(components : Indexable(T)) #

Creates a new matrix from a collection of components.

The size of components must be equal to N.

``Vector(Int32, 5).new([1, 2, 3, 4, 5]) # => (1, 2, 3, 4, 5)``

def self.new(&) #

Constructs the vector by yielding for each component.

The value of each component should be returned from the block. The block will be given the index of each component as an argument.

``Vector(Int32, 3).new { |i| i * 5 } # => (0, 5, 10)``

def self.zero : self #

Constructs a zero-vector.

Each component will have a scalar value equal to the type's zero value. This is done by calling `T.zero` for each component. The type T must have a `.zero` class method.

``Vector(Float32, 3).zero # => (0.0, 0.0, 0.0)``

## Class Method Detail

def self.[](*elements) #

Constructs a vector with existing components.

The type of the components is specified by the type parameter. Each value is cast to the type T.

``Vector(Float32, 5)[1, 2, 3, 4, 5] # => (1.0, 2.0, 3.0, 4.0, 5.0)``

## Instance Method Detail

def cross(other : CommonVector(U, 3)) : CommonVector forall U #

Computes the cross-product of this and another vector.

Raises a compilation error if the vector size (N) isn't 3.

``Vector[1, 3, 4].cross(Vector[2, -5, 8]) # => (44, 0, -11)``

def map(& : T -> U) : CommonVector forall U #

Returns a new vector where components are mapped by the given block.

``````vector = Vector[1, 2, 3]
vector.map { |v| v * 2 } # => (2, 4, 6)``````

def to_slice : Slice(T) #

Returns a slice that points to the components in this vector.

def to_unsafe : Pointer(T) #

Returns a pointer to the data for this vector.

The components are tightly packed and ordered consecutively in memory.

def unsafe_fetch(index : Int) #

Retrieves the scalar value of the component at the given index, without checking size boundaries.

End-users should never invoke this method directly. Instead, methods like `#[]` and `#[]?` should be used.

This method should only be directly invoked if the index is certain to be in bounds.

def w : T #

Retrieves the w component.

Raises a compilation error if the vector size (N) is less than 4.

def x : T #

Retrieves the x component.

Raises a compilation error if the vector size (N) is less than 1.

def y : T #

Retrieves the y component.

Raises a compilation error if the vector size (N) is less than 2.

def z : T #

Retrieves the z component.

Raises a compilation error if the vector size (N) is less than 3.

## Macro Detail

macro [](*components) #

Constructs the vector with existing components.

The type of the components is derived from the type of each argument. The size of the vector is determined by the number of components.

``Vector[1, 2, 3] # => (1, 2, 3)``