Vector.h

template<typename T, size_t DIM>
class Cubism::Core::Vector

Vector class with support for common operations.

Wraps around std::array. The data type T must follow the rules of aggregate initialization. The vector dimension DIM should be low-dimensional when used for automatic variables on the stack.

Template Parameters
  • T: Data type

  • DIM: Dimension

Public Functions

inline Vector()

Default constructor.

All elements are initialized to 0.

inline Vector(const Vector &c) noexcept

Default copy constructor.

inline Vector(Vector &&c) noexcept

Default move constructor (linear complexity)

inline Vector(const ArrayType &ary)

Copy constructor.

Initialize data from std::array of same type

inline Vector(ArrayType &&ary) noexcept

Move constructor.

Initialize data from std::array of same type (linear complexity)

template<typename U, size_t DIMU>
inline Vector(const Vector<U, DIMU> &c)

Constructor for arbitrary Vector types.

The type U must define a cast to DataType.

Template Parameters
  • U: Scalar type

  • DIMU: Vector dimension

Parameters
  • c: Initial values

template<typename U>
inline explicit Vector(const U scalar)

Constructor for any scalar type U.

The type U must define a cast to DataType.

Template Parameters
  • U: Scalar type

Parameters
  • scalar: Initialization value

template<typename U>
inline Vector(std::initializer_list<U> ilist)

Constructor for arbitrary list initialization.

The type U must define a cast to DataType.

Template Parameters
  • U: Scalar type

Parameters
  • ilist: Initializer list

template<typename U>
inline Vector(const std::vector<U> &vec)

Constructor to initialize data from std::vector

The type U must define a cast to DataType.

Template Parameters
  • U: Scalar type

Parameters
  • vec: Initializer vector

template<typename U, size_t DIMU>
inline Vector(const std::array<U, DIMU> &ary)

Constructor to initialize data from arbitrary std::array

The type U must define a cast to DataType. If DIMU > DIM only the first DIM elements will be copied. If DIMU < DIM the missing elements will be initialized to zero.

Template Parameters
  • U: Scalar type

  • DIMU: Array dimension

Parameters
  • ary: Initializer array

template<typename U>
inline explicit Vector(const U *ptr, size_t n)

Constructor to initialize data from arbitrary pointer.

The type U must define a cast to DataType. If n > DIM only the first DIM elements will be copied. If n < DIM the missing elements will be initialized to zero.

Template Parameters
  • U: Scalar type

Parameters
  • ptr: Pointer to first element

  • n: Number of elements to copy

~Vector() = default

Default destructor.

inline Vector &operator=(const Vector &c)

Default copy assignment operator.

inline Vector &operator=(Vector &&c)

Default move assignment operator (linear complexity)

inline Vector &operator=(const ArrayType &ary)

Copy assignment operator.

inline Vector &operator=(ArrayType &&ary)

Move assignment operator (linear complexity)

inline Vector &operator=(const DataType c)

Copy assignment operator for scalar c.

template<typename U, size_t DIMU>
inline Vector &operator=(const Vector<U, DIMU> &c)

Copy assignment operator for any other Vector type.

The type U must define a cast to DataType. If DIMU > DIM only the first DIM elements will be copied. If DIMU < DIM the missing elements in this vector will not be modified.

Template Parameters
  • U: Scalar type

  • DIMU: Vector dimension

Parameters
  • c: Source vector

inline void swap(Vector &other) noexcept

Swap this vector with other vector.

inline size_t size() const

Size of vector.

Return

Size of vector (static)

inline DataType *data()

Get pointer to raw data.

Return

Pointer to first element

inline const DataType *data() const

Get pointer to raw data.

Return

const pointer to first element

inline ArrayType &getArray()

Get underlying array.

Return

std::array

inline const ArrayType &getArray() const

Get underlying array.

Return

const std::array

inline DataType &operator[](const size_t i)

Data access interface.

Return

Reference to element

Parameters
  • i: Vector element

inline const DataType &operator[](const size_t i) const

Data access interface.

Return

const reference to element

Parameters
  • i: Vector element

inline explicit operator ArrayType() const

Cast operator.

Cast to std::array

inline explicit operator DataType*()

Cast operator.

Cast to pointer pointing to the first array element

inline bool operator==(const Vector &other) const

Equality operator.

A Vector is equal to another if they are equal component-wise.

inline bool operator<(const Vector &other) const

Less than operator.

A Vector is smaller than another if all components are less than the corresponding components of the other Vector.

inline bool operator>(const Vector &other) const

Greater than operator.

A Vector is larger than another if all components are greater than the corresponding components of the other Vector.

inline bool operator<=(const Vector &other) const

Less-equal than operator.

A Vector is smaller or equal than another if all components are less or equal than the corresponding components of the other Vector.

inline bool operator>=(const Vector &other) const

Greater-equal than operator.

A Vector is larger or equal than another if all components are greater or equal than the corresponding components of the other Vector.

inline bool lexLT(const Vector &other) const

Lexicographic less-than.

Identical to std::array

inline bool lexLE(const Vector &other) const

Lexicographic less-equal.

Identical to std::array

inline bool lexGT(const Vector &other) const

Lexicographic greater-than.

Identical to std::array

inline bool lexGE(const Vector &other) const

Lexicographic greater-equal.

Identical to std::array

inline DataType normsq() const

Squared Euclidean vector norm.

inline DataType norm() const

Euclidean vector norm (L2)

inline DataType normL2() const

Vector L2 norm (alias for norm())

inline DataType normL1() const

Vector L1 norm.

inline DataType normLinf() const

Vector maximum norm.

inline Vector unit() const

Unit vector.

inline DataType dot(const Vector &other) const

Vector dot product.

inline Vector cross(const Vector &other) const

Vector cross product.

inline DataType getCrossThird(const Vector &other) const

Third component of vector cross-product.

inline DataType distsq(Vector other) const

Squared distance between this and other vector.

inline DataType dist(Vector other) const

Distance between this and other vector.

inline DataType sum() const

Sum of vector components.

inline DataType prod() const

Product of vector components.

inline DataType min() const

Minimum vector component.

inline DataType max() const

Maximum vector component.

inline size_t argmin() const

Index of minimum component.

inline size_t argmax() const

Index of maximum component.

inline Vector abs() const

Absolute value.

Return

Absolute copy of this vector