Firefly 3.1.0
Standalone library for vector calculations
Loading...
Searching...
No Matches
firefly::vector< T, Length > Class Template Reference

Represents a mathematical vector in n-dimensional space. More...

#include <vector.hpp>

Inheritance diagram for firefly::vector< T, Length >:

Public Types

using value_type = T
 

Public Member Functions

constexpr vector ()
 Default constructor that initialises all elements of the vector to zero.
 
constexpr vector (std::initializer_list< T > const &list)
 Constructor that initializes the vector using an initializer list.
 
constexpr vector (T const value)
 Constructor that initialises all elements of the vector to a given value.
 
template<vector_type U>
constexpr auto add (vector< U, Length > const &other) const
 Adds two vectors element-wise and returns the result.
 
template<typename U >
constexpr auto add (U const scalar) const
 Adds a scalar to each element of the vector and returns the result.
 
template<vector_type U>
constexpr auto operator+ (vector< U, Length > const &other) const
 Adds two vectors element-wise using the + operator.
 
template<vector_type U>
constexpr auto operator+ (U const scalar) const
 Adds a scalar to each element of the vector using the + operator.
 
template<vector_type U>
constexpr auto & operator+= (vector< U, Length > const &other)
 Performs element-wise addition of two vectors using the += operator.
 
template<vector_type U>
constexpr auto & operator+= (U const scalar)
 Adds a scalar to each element of the vector using the += operator.
 
template<vector_type U>
constexpr auto subtract (vector< U, Length > const &other) const
 Subtracts another vector from this vector.
 
template<vector_type U>
constexpr auto subtract (U const scalar) const
 Subtracts a scalar from each element of the vector.
 
template<vector_type U>
constexpr auto operator- (vector< U, Length > const &other) const
 Subtracts another vector from this vector using the - operator.
 
template<vector_type U>
constexpr auto operator- (U const scalar) const
 Subtracts a scalar from each element of the vector using the - operator.
 
template<vector_type U>
constexpr auto & operator-= (vector< U, Length > const &other)
 Performs element-wise subtraction of another vector using the -= operator.
 
template<vector_type U>
constexpr auto & operator-= (U const scalar)
 Subtracts a scalar from each element of the vector using the -= operator.
 
template<typename U >
constexpr auto dot (vector< U, Length > const &other) const
 Calculates the dot product of two vectors.
 
template<vector_type U>
constexpr auto cross (vector< U, Length > const &other) const
 Calculates the cross product of two 3D vectors.
 
template<vector_type U>
constexpr auto scale (U const scalar) const
 Scales the vector by a given scalar.
 
template<vector_type U>
constexpr auto operator* (vector< U, Length > const &other) const
 Calculates the dot product using the * operator.
 
template<vector_type U>
constexpr auto operator* (U const scalar) const
 Scales the vector using the * operator.
 
template<vector_type U>
constexpr auto & operator*= (U const scalar)
 Scales the vector in place using the *= operator.
 
template<vector_type U>
constexpr auto operator/ (U const scalar) const
 Scales the vector by the inverse of a scalar using the / operator.
 
template<vector_type U>
constexpr auto & operator/= (U const scalar)
 Scales the vector in place using the /= operator.
 
constexpr auto operator- () const
 Negates the vector.
 
constexpr auto norm () const
 Computes the Euclidean magnitude (Length) of the vector.
 
constexpr auto to_normalized () const
 Normalizes the vector.
 
constexpr auto is_equal (vector< T, Length > const &other) const
 Compares two vectors for equality.
 
constexpr auto operator== (vector< T, Length > const &other) const
 Equality operator for vectors.
 
template<vector_type AsType>
vector< AsType, Length > constexpr const as_type () const
 Converts the vector elements to a different type, handling complex numbers.
 
constexpr std::string view (int precision=20) const
 Converts the vector to a string representation.
 

Friends

template<vector_type U>
constexpr auto operator+ (U const scalar, vector< T, Length > const &vec)
 Adds a scalar to a vector.
 
template<vector_type U>
constexpr auto operator- (U const scalar, vector< T, Length > const &vec)
 Subtracts a scalar to a vector.
 
template<vector_type U>
constexpr auto operator* (U const scalar, vector< T, Length > const &vec)
 Scales a vector by a scalar.
 
template<vector_type U>
constexpr auto operator/ (U const scalar, vector< T, Length > const &vec)
 Performs inverse scaling of a vector by a scalar.
 
std::ostream & operator<< (std::ostream &os, vector const &other)
 Stream insertion operator for vectors.
 

Detailed Description

template<vector_type T, std::size_t Length>
class firefly::vector< T, Length >

Represents a mathematical vector in n-dimensional space.

Member Typedef Documentation

◆ value_type

template<vector_type T, std::size_t Length>
using firefly::vector< T, Length >::value_type = T

Constructor & Destructor Documentation

◆ vector() [1/3]

template<vector_type T, std::size_t Length>
constexpr firefly::vector< T, Length >::vector ( )
inlineconstexpr

Default constructor that initialises all elements of the vector to zero.

This constructor initialises the vector by calling the constructor with a value of 0.

◆ vector() [2/3]

template<vector_type T, std::size_t Length>
constexpr firefly::vector< T, Length >::vector ( std::initializer_list< T > const &  list)
inlineconstexpr

Constructor that initializes the vector using an initializer list.

Parameters
listAn initializer list containing the elements to initialize the vector.
Exceptions
std::out_of_rangeif the initializer list size exceeds the vector Length.

◆ vector() [3/3]

template<vector_type T, std::size_t Length>
constexpr firefly::vector< T, Length >::vector ( T const  value)
inlineconstexpr

Constructor that initialises all elements of the vector to a given value.

This constructor fills the vector with the specified value for all elements. It leverages std::fill to ensure that all entries in the array are initialised with value.

Parameters
valueThe value used to initialise all elements of the vector.

Member Function Documentation

◆ add() [1/2]

template<vector_type T, std::size_t Length>
template<typename U >
constexpr auto firefly::vector< T, Length >::add ( U const  scalar) const
inlineconstexpr

Adds a scalar to each element of the vector and returns the result.

This function adds a scalar value to every element in the vector and returns a new vector containing the result. The result's type is determined using std::common_type to ensure compatibility between the vector's element type and the scalar type.

Template Parameters
UThe type of the scalar value being added.
Parameters
scalarThe scalar value to add to each element of the vector.
Returns
A new vector where each element is the result of adding the scalar to the corresponding element.

◆ add() [2/2]

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto firefly::vector< T, Length >::add ( vector< U, Length > const &  other) const
inlineconstexpr

Adds two vectors element-wise and returns the result.

This function performs element-wise addition of two vectors and returns a new vector of the result. The result's type is deduced using std::common_type to ensure compatibility between different types of vectors.

Template Parameters
UThe type of elements in the other vector being added.
Parameters
otherThe vector to add to the current vector.
Returns
A new vector containing the element-wise sum of the two vectors.

◆ as_type()

template<vector_type T, std::size_t Length>
template<vector_type AsType>
vector< AsType, Length > constexpr const firefly::vector< T, Length >::as_type ( ) const
inlineconstexpr

Converts the vector elements to a different type, handling complex numbers.

If the elements are of type std::complex, it multiplies the element by its conjugate and then casts the result to the specified type. For other types, it performs a direct cast.

Template Parameters
AsTypeThe type to which the elements will be cast.
Returns
A new vector with elements of the specified type.

◆ cross()

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto firefly::vector< T, Length >::cross ( vector< U, Length > const &  other) const
inlineconstexpr

Calculates the cross product of two 3D vectors.

This function computes the cross product of the current vector with another vector. It is only valid for 3D vectors, and a static assertion is used to enforce this.

Template Parameters
UThe type of elements in the other vector.
Parameters
otherThe vector to compute the cross product with.
Returns
A new vector representing the cross product.
Exceptions
std::logic_errorif the Length of the vector is not 3.

◆ dot()

template<vector_type T, std::size_t Length>
template<typename U >
constexpr auto firefly::vector< T, Length >::dot ( vector< U, Length > const &  other) const
inlineconstexpr

Calculates the dot product of two vectors.

This function computes the dot product of the current vector with another vector. The dot product is the sum of the products of corresponding elements from both vectors. It returns a single scalar value representing the result.

The function uses std::transform_reduce to efficiently calculate the dot product, applying element-wise multiplication and accumulating the result.

Template Parameters
UThe type of the elements in the other vector.
Parameters
otherThe vector with which the dot product is computed. This vector must have the same Length as the current vector.
Returns
The scalar result of the dot product, with type common_type_t<T, U>.
Exceptions
std::invalid_argumentif the vectors have different sizes (if this condition is checked elsewhere).

◆ is_equal()

template<vector_type T, std::size_t Length>
constexpr auto firefly::vector< T, Length >::is_equal ( vector< T, Length > const &  other) const
inlineconstexpr

Compares two vectors for equality.

This function checks if the current vector is equal to another vector by comparing their elements.

Parameters
otherThe vector to compare with.
Returns
true if the vectors are equal, otherwise false.

◆ norm()

template<vector_type T, std::size_t Length>
constexpr auto firefly::vector< T, Length >::norm ( ) const
inlineconstexpr

Computes the Euclidean magnitude (Length) of the vector.

This function calculates the Euclidean magnitude of the vector. For real number vectors, the magnitude is computed as the square root of the dot product of the vector with itself. For complex number vectors, the magnitude is calculated as the square root of the sum of the squared magnitudes of each element (using std::norm for the squared modulus).

If the vector contains complex numbers, the function uses std::transform_reduce to compute the sum of the squared magnitudes of each element. For real numbers, the function uses the dot product of the vector with itself and returns the square root of that result.

Returns
The magnitude of the vector as a scalar value.

◆ operator*() [1/2]

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto firefly::vector< T, Length >::operator* ( U const  scalar) const
inlineconstexpr

Scales the vector using the * operator.

This operator overload allows the use of the * operator to scale the vector by a scalar value.

Template Parameters
UThe type of the scalar value.
Parameters
scalarThe scalar value to scale the vector by.
Returns
A new vector where each element is scaled by the scalar.

◆ operator*() [2/2]

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto firefly::vector< T, Length >::operator* ( vector< U, Length > const &  other) const
inlineconstexpr

Calculates the dot product using the * operator.

This operator overload allows the use of the * operator to calculate the dot product of two vectors.

Template Parameters
UThe type of elements in the other vector.
Parameters
otherThe vector to compute the dot product with.
Returns
The dot product as a scalar value.

◆ operator*=()

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto & firefly::vector< T, Length >::operator*= ( U const  scalar)
inlineconstexpr

Scales the vector in place using the *= operator.

This operator overload allows in-place scaling of the vector by a scalar value.

Template Parameters
UThe type of the scalar value.
Parameters
scalarThe scalar value to scale the vector by.
Returns
A reference to the current vector after scaling.

◆ operator+() [1/2]

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto firefly::vector< T, Length >::operator+ ( U const  scalar) const
inlineconstexpr

Adds a scalar to each element of the vector using the + operator.

This operator overload adds a scalar to every element of the vector using the + operator. The result is calculated by calling the add function.

Template Parameters
UThe type of the scalar value being added.
Parameters
scalarThe scalar value to add to each element of the vector.
Returns
A new vector where each element is the result of adding the scalar to the corresponding element.

◆ operator+() [2/2]

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto firefly::vector< T, Length >::operator+ ( vector< U, Length > const &  other) const
inlineconstexpr

Adds two vectors element-wise using the + operator.

This operator overload allows element-wise addition of two vectors using the + operator. The result is calculated by calling the add function.

Template Parameters
UThe type of elements in the other vector being added.
Parameters
otherThe vector to add to the current vector.
Returns
A new vector containing the element-wise sum of the two vectors.

◆ operator+=() [1/2]

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto & firefly::vector< T, Length >::operator+= ( U const  scalar)
inlineconstexpr

Adds a scalar to each element of the vector using the += operator.

This operator overload allows a scalar to be added to every element of the vector, updating the current vector with the result of the addition.

Template Parameters
UThe type of the scalar value being added.
Parameters
scalarThe scalar value to add to each element of the vector.
Returns
A reference to the current vector after the addition.

◆ operator+=() [2/2]

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto & firefly::vector< T, Length >::operator+= ( vector< U, Length > const &  other)
inlineconstexpr

Performs element-wise addition of two vectors using the += operator.

This operator overload allows element-wise addition of two vectors, updating the current vector with the result of the addition.

Template Parameters
UThe type of elements in the other vector being added.
Parameters
otherThe vector to add to the current vector.
Returns
A reference to the current vector after the addition.

◆ operator-() [1/3]

template<vector_type T, std::size_t Length>
constexpr auto firefly::vector< T, Length >::operator- ( ) const
inlineconstexpr

Negates the vector.

This function scales the vector by -1, effectively returning the negated vector.

Returns
A new vector representing the negated value of the current vector.

◆ operator-() [2/3]

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto firefly::vector< T, Length >::operator- ( U const  scalar) const
inlineconstexpr

Subtracts a scalar from each element of the vector using the - operator.

This operator overload allows a scalar to be subtracted from every element of the vector using the - operator.

Template Parameters
UThe type of the scalar value being subtracted.
Parameters
scalarThe scalar value to subtract from each element of the vector.
Returns
A new vector where each element is the result of subtracting the scalar from the corresponding element.

◆ operator-() [3/3]

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto firefly::vector< T, Length >::operator- ( vector< U, Length > const &  other) const
inlineconstexpr

Subtracts another vector from this vector using the - operator.

This operator overload allows element-wise subtraction of another vector from the current vector using the - operator.

Template Parameters
UThe type of elements in the other vector being subtracted.
Parameters
otherThe vector to subtract from the current vector.
Returns
A new vector containing the result of the element-wise subtraction.

◆ operator-=() [1/2]

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto & firefly::vector< T, Length >::operator-= ( U const  scalar)
inlineconstexpr

Subtracts a scalar from each element of the vector using the -= operator.

This operator overload allows a scalar to be subtracted from every element of the vector, updating the current vector with the result.

Template Parameters
UThe type of the scalar value being subtracted.
Parameters
scalarThe scalar value to subtract from each element of the vector.
Returns
A reference to the current vector after the subtraction.

◆ operator-=() [2/2]

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto & firefly::vector< T, Length >::operator-= ( vector< U, Length > const &  other)
inlineconstexpr

Performs element-wise subtraction of another vector using the -= operator.

This operator overload allows element-wise subtraction of another vector from the current vector, updating the current vector with the result.

Template Parameters
UThe type of elements in the other vector being subtracted.
Parameters
otherThe vector to subtract from the current vector.
Returns
A reference to the current vector after the subtraction.

◆ operator/()

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto firefly::vector< T, Length >::operator/ ( U const  scalar) const
inlineconstexpr

Scales the vector by the inverse of a scalar using the / operator.

This operator overload allows scaling the vector by the reciprocal of a scalar value.

Template Parameters
UThe type of the scalar value.
Parameters
scalarThe scalar value to scale the vector by.
Returns
A new vector where each element is scaled by the reciprocal of the scalar.

◆ operator/=()

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto & firefly::vector< T, Length >::operator/= ( U const  scalar)
inlineconstexpr

Scales the vector in place using the /= operator.

This operator overload allows in-place scaling of the vector by the reciprocal of a scalar value.

Template Parameters
UThe type of the scalar value.
Parameters
scalarThe scalar value to scale the vector by.
Returns
A reference to the current vector after scaling.

◆ operator==()

template<vector_type T, std::size_t Length>
constexpr auto firefly::vector< T, Length >::operator== ( vector< T, Length > const &  other) const
inlineconstexpr

Equality operator for vectors.

This operator overload allows the use of the == operator to compare two vectors for equality.

Parameters
otherThe vector to compare with.
Returns
true if the vectors are equal, otherwise false.

◆ scale()

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto firefly::vector< T, Length >::scale ( U const  scalar) const
inlineconstexpr

Scales the vector by a given scalar.

This function multiplies each element of the vector by a scalar value and returns a new vector containing the result.

Template Parameters
UThe type of the scalar value.
Parameters
scalarThe scalar value to scale the vector by.
Returns
A new vector where each element is scaled by the scalar.

◆ subtract() [1/2]

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto firefly::vector< T, Length >::subtract ( U const  scalar) const
inlineconstexpr

Subtracts a scalar from each element of the vector.

This function subtracts a scalar value from every element in the vector and returns a new vector containing the result.

Template Parameters
UThe type of the scalar value being subtracted.
Parameters
scalarThe scalar value to subtract from each element of the vector.
Returns
A new vector where each element is the result of subtracting the scalar from the corresponding element.

◆ subtract() [2/2]

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto firefly::vector< T, Length >::subtract ( vector< U, Length > const &  other) const
inlineconstexpr

Subtracts another vector from this vector.

This function performs element-wise subtraction of another vector from the current vector and returns the result by adding the negative of the other vector.

Template Parameters
UThe type of elements in the other vector being subtracted.
Parameters
otherThe vector to subtract from the current vector.
Returns
A new vector containing the result of the element-wise subtraction.

◆ to_normalized()

template<vector_type T, std::size_t Length>
constexpr auto firefly::vector< T, Length >::to_normalized ( ) const
inlineconstexpr

Normalizes the vector.

This function returns a new vector that is the normalized version of the current vector, which has a magnitude of 1. This is done by scaling the vector by the inverse of its magnitude.

Exceptions
std::logic_errorwhen norm is zero. This usually happens when zero vector is passed
Returns
A new vector that is the normalized form of the current vector.

◆ view()

template<vector_type T, std::size_t Length>
constexpr std::string firefly::vector< T, Length >::view ( int  precision = 20) const
inlineconstexpr

Converts the vector to a string representation.

This function creates a string representation of the vector in the format "[el1, el2, ..., elN]".

Returns
A string representation of the vector.

Friends And Related Symbol Documentation

◆ operator*

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto operator* ( U const  scalar,
vector< T, Length > const &  vec 
)
friend

Scales a vector by a scalar.

This friend function allows scaling a vector by a scalar, returning a new vector with each element of the original vector multiplied by the scalar.

Template Parameters
UThe type of the scalar.
Parameters
scalarThe scalar value to scale by.
vecThe vector to scale.
Returns
A new vector with the result of the scaling.

◆ operator+

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto operator+ ( U const  scalar,
vector< T, Length > const &  vec 
)
friend

Adds a scalar to a vector.

This friend function allows adding a scalar to a vector, returning a new vector with the scalar added to each element of the original vector.

Template Parameters
UThe type of the scalar.
Parameters
scalarThe scalar value to add.
vecThe vector to add the scalar to.
Returns
A new vector with the result of the addition.

◆ operator-

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto operator- ( U const  scalar,
vector< T, Length > const &  vec 
)
friend

Subtracts a scalar to a vector.

This friend function allows subtracting a scalar to a vector, returning a new vector with the scalar subtracted from each element of the original vector.

Template Parameters
UThe type of the scalar.
Parameters
scalarThe scalar value to subtract.
vecThe vector to add the scalar to.
Returns
A new vector with the result of the subtraction.

◆ operator/

template<vector_type T, std::size_t Length>
template<vector_type U>
constexpr auto operator/ ( U const  scalar,
vector< T, Length > const &  vec 
)
friend

Performs inverse scaling of a vector by a scalar.

This friend function allows inverse scaling of a vector by a scalar, returning a new vector with each element of the original vector divided by the scalar.

Template Parameters
UThe type of the scalar.
Parameters
scalarThe scalar value to inversely scale by.
vecThe vector to scale.
Returns
A new vector with the result of the inverse scaling.

◆ operator<<

template<vector_type T, std::size_t Length>
std::ostream & operator<< ( std::ostream &  os,
vector< T, Length > const &  other 
)
friend

Stream insertion operator for vectors.

This operator overload allows a vector to be inserted into an output stream, outputting the vector in its string representation format.

Parameters
osThe output stream.
otherThe vector to be output.
Returns
The output stream with the vector representation.

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