Theoretica
A C++ numerical and automatic mathematical library
theoretica::vec< Type, N > Class Template Reference

A statically allocated N-dimensional vector with elements of the given type. More...

#include <vec.h>

Public Types

using iterator = vec_iterator< vec< Type, N >, Type & >
 Sequential iterator for statically allocated vectors.
 
using const_iterator = vec_iterator< const vec< Type, N >, const Type & >
 Const sequential iterator for statically allocated vectors.
 

Public Member Functions

 vec ()
 Construct a vector with all elements equal to zero.
 
 vec (Type val)
 Construct a vector with all elements equal to the given value.
 
 vec (unsigned int size, Type val)
 Construct a vector with all elements equal to the given value, checking that the given size matches that of the vector type.
 
template<unsigned int M>
 vec (const vec< Type, M > &other)
 Copy constructor.
 
template<typename Vector >
vec< Type, N > & operator= (const Vector &other)
 Copy from other.
 
 vec (std::initializer_list< Type > l)
 Initialize from a list, e.g. {1, 2, 3}.
 
vec< Type, N > operator+ () const
 Identity.
 
vec< Type, N > operator+ (const vec< Type, N > &other) const
 Vector sum (v + w = (v.x + w.x, ...))
 
vec< Type, N > operator- () const
 Opposite vector.
 
vec< Type, N > operator- (const vec< Type, N > &other) const
 Vector subtraction.
 
vec< Type, N > operator* (Type scalar) const
 Scalar multiplication (av = (v.x * a, ...))
 
vec< Type, N > operator/ (Type scalar) const
 Scalar division (v / a = (v.x / a, ...))
 
template<typename Vector >
Type dot (const Vector &other) const
 Dot product between vectors (v * w = v.x * w.x + ...)
 
template<typename Vector >
Type operator* (const Vector &other) const
 Dot product between vectors (v * w = v.x * w.x + ...)
 
vec< Type, N > cross (const vec< Type, N > &other) const
 Cross product between vectors.
 
template<typename Vector >
vec< Type, N > cross (const Vector &other) const
 Cross product between vectors.
 
template<typename Vector >
vec< Type, N > & operator+= (const Vector &other)
 Sum a vector the the vector itself.
 
template<typename Vector >
vec< Type, N > & operator-= (const Vector &other)
 Subtract a vector the the vector itself.
 
vec< Type, N > & operator*= (Type scalar)
 Multiply the vector itself by a scalar.
 
vec< Type, N > & operator/= (Type scalar)
 Divide the vector itself by a scalar.
 
Type norm () const
 Compute the norm of the vector (sqrt(v * v))
 
Type sqr_norm () const
 Compute the square norm of the vector (v * v)
 
Type & operator[] (unsigned int i)
 Access i-th component.
 
const Type & operator[] (unsigned int i) const
 Get the i-th component.
 
Type & at (unsigned int i)
 Access i-th element.
 
Type get (unsigned int i) const
 Getters and setters.
 
void set (unsigned int i, Type x)
 Set the i-th element.
 
auto begin ()
 Get an iterator to the first element of the vector.
 
auto end ()
 Get an iterator to one plus the last element of the vector.
 
auto begin () const
 Get a const iterator to the first element of the vector.
 
auto end () const
 Get a const iterator to one plus the last element of the vector.
 
void normalize ()
 Vector normalization (v / |v|)
 
vec< Type, N > normalized () const
 Return the normalized vector (v / |v|)
 
template<typename Vector >
bool operator== (const Vector &other) const
 Check whether all elements of both vectors are equal.
 
template<typename Vector >
bool operator!= (const Vector &other) const
 Check whether all elements of both vectors are unequal.
 
TH_CONSTEXPR unsigned int size () const
 Returns the size of the vector (N)
 
void resize (size_t n) const
 Compatibility function to allow for allocation or resizing of dynamic vectors. More...
 
std::string to_string (const std::string &separator=", ", bool parenthesis=true) const
 Convert the vector to string representation.
 
 operator std::string ()
 Convert the vector to string representation.
 

Static Public Member Functions

static vec< Type, N > euclidean_base (unsigned int i, unsigned int n=N)
 Returns an N-dimensional euclidean base unit vector with the i-th element set to 1.
 

Static Public Attributes

static constexpr size_t size_argument = N
 

Friends

vec< Type, N > operator* (Type a, const vec< Type, N > &v)
 Friend operator to enable equations of the form (Type) * (vec)
 
std::ostream & operator<< (std::ostream &out, const vec< Type, N > &obj)
 Stream the vector in string representation to an output stream (std::ostream)
 

Detailed Description

template<typename Type = real, unsigned int N = 0>
class theoretica::vec< Type, N >

A statically allocated N-dimensional vector with elements of the given type.

A dynamically allocated vector with elements of the given type.

Member Function Documentation

◆ resize()

template<typename Type = real, unsigned int N = 0>
void theoretica::vec< Type, N >::resize ( size_t  n) const
inline

Compatibility function to allow for allocation or resizing of dynamic vectors.

Since statically allocated vectors cannot change size, this function only checks whether the target size is the same as the vector's.


The documentation for this class was generated from the following file: