Firefly 3.1.0
Standalone library for vector calculations
|
Represents a mathematical vector in n-dimensional space. More...
#include <vector.hpp>
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. | |
Represents a mathematical vector in n-dimensional space.
using firefly::vector< T, Length >::value_type = T |
|
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.
|
inlineconstexpr |
Constructor that initializes the vector using an initializer list.
list | An initializer list containing the elements to initialize the vector. |
std::out_of_range | if the initializer list size exceeds the vector Length. |
|
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
.
value | The value used to initialise all elements of the vector. |
|
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.
U | The type of the scalar value being added. |
scalar | The scalar value to add to each element of the vector. |
|
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.
U | The type of elements in the other vector being added. |
other | The vector to add to the current vector. |
|
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.
AsType | The type to which the elements will be cast. |
|
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.
U | The type of elements in the other vector. |
other | The vector to compute the cross product with. |
std::logic_error | if the Length of the vector is not 3. |
|
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.
U | The type of the elements in the other vector. |
other | The vector with which the dot product is computed. This vector must have the same Length as the current vector. |
common_type_t<T, U>
.std::invalid_argument | if the vectors have different sizes (if this condition is checked elsewhere). |
|
inlineconstexpr |
Compares two vectors for equality.
This function checks if the current vector is equal to another vector by comparing their elements.
other | The vector to compare with. |
true
if the vectors are equal, otherwise false
.
|
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.
|
inlineconstexpr |
Scales the vector using the *
operator.
This operator overload allows the use of the *
operator to scale the vector by a scalar value.
U | The type of the scalar value. |
scalar | The scalar value to scale the vector by. |
|
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.
U | The type of elements in the other vector. |
other | The vector to compute the dot product with. |
|
inlineconstexpr |
Scales the vector in place using the *=
operator.
This operator overload allows in-place scaling of the vector by a scalar value.
U | The type of the scalar value. |
scalar | The scalar value to scale the vector by. |
|
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.
U | The type of the scalar value being added. |
scalar | The scalar value to add to each element of the vector. |
|
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.
U | The type of elements in the other vector being added. |
other | The vector to add to the current vector. |
|
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.
U | The type of the scalar value being added. |
scalar | The scalar value to add to each element of the vector. |
|
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.
U | The type of elements in the other vector being added. |
other | The vector to add to the current vector. |
|
inlineconstexpr |
Negates the vector.
This function scales the vector by -1, effectively returning the negated vector.
|
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.
U | The type of the scalar value being subtracted. |
scalar | The scalar value to subtract from each element of the vector. |
|
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.
U | The type of elements in the other vector being subtracted. |
other | The vector to subtract from the current vector. |
|
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.
U | The type of the scalar value being subtracted. |
scalar | The scalar value to subtract from each element of the vector. |
|
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.
U | The type of elements in the other vector being subtracted. |
other | The vector to subtract from the current vector. |
|
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.
U | The type of the scalar value. |
scalar | The scalar value to scale the vector by. |
|
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.
U | The type of the scalar value. |
scalar | The scalar value to scale the vector by. |
|
inlineconstexpr |
Equality operator for vectors.
This operator overload allows the use of the ==
operator to compare two vectors for equality.
other | The vector to compare with. |
true
if the vectors are equal, otherwise false
.
|
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.
U | The type of the scalar value. |
scalar | The scalar value to scale the vector by. |
|
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.
U | The type of the scalar value being subtracted. |
scalar | The scalar value to subtract from each element of the vector. |
|
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.
U | The type of elements in the other vector being subtracted. |
other | The vector to subtract from the current vector. |
|
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.
std::logic_error | when norm is zero. This usually happens when zero vector is passed |
|
inlineconstexpr |
Converts the vector to a string representation.
This function creates a string representation of the vector in the format "[el1, el2, ..., elN]".
|
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.
U | The type of the scalar. |
scalar | The scalar value to scale by. |
vec | The vector to scale. |
|
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.
U | The type of the scalar. |
scalar | The scalar value to add. |
vec | The vector to add the scalar to. |
|
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.
U | The type of the scalar. |
scalar | The scalar value to subtract. |
vec | The vector to add the scalar to. |
|
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.
U | The type of the scalar. |
scalar | The scalar value to inversely scale by. |
vec | The vector to scale. |
|
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.
os | The output stream. |
other | The vector to be output. |