Vector

template<Number ElemType, DataLocation Location, template<typename, typename> typename Storage>
class Vector : public HighFM::MatrixBase<Vector<ElemType, Location, Storage>>, public Storage<ElemType, Location>

An object representing a vector in Linear Algebra. Depending on the Storage parameter, this object will be referred to either as VectorMap (when Storage is set to DataMap) or as just Vector (when Storage is set to Array).

Creating a new object:

using namespace HighFM;
Vector<int> vec1;                                                                   // Creates an empty column vector
Vector<double> vec2(10);                                                            // Creates a column vector with 10 doubles initialized at 0
Vector<double> vec3(10, 3.4);                                                       // Creates a row vector with 10 doubles initialized at 3.4
Vector<double> vec4 = vec2;                                                         // Creates a copy of the vec2
VectorMap<double> vec_map1(vec4.mat_data());                                        // Creates a data map to vec4
VectorMap<double> vec_map2({.buffer = vec4.data(), .size = vec4.size()});           // Creates a data map to vec4 (is equivalent to the above)

Accessing the elements:

Vector<double> vec(10);
vec(1) = 10;                    // Access element at position (1)
vec[2] = 20;                    // Access element at position (1) and (2)
vec({1, 6}) = 5;                // Creates a slice (entries: 1 → 6) from the vector

Template Parameters:
  • ElemType – Data types of the entries. Must be either a double or float.

  • Location – Location of the data

  • Storage – The container used for storing the data (either Array or DataMap).

Constructor

Vector()

Creates an empty Vector (i.e., with size 0).

template<ResizableContainer S = storage>
Vector(index_t size, ElemType val = ElemType(0))

Creates a Vector and fills with val.

Warning

This constructor is only available when using Array as Storage.

Parameters:
  • size[in] vector size

  • val[in] value to initialize the Vector with.

Vector(VectorData<ElemType> data)

Maps the Vector to an external container when using DataMap as Storage. Otherwise, creates a new Vector and then copies the data from the external container.

Parameters:

data[in] struct containing the information about the container

template<DataLocation OtherLoc, template<typename, typename> typename OtherStorage>
Vector(const Vector<ElemType, OtherLoc, OtherStorage> &other)

Creates a Vector from an other Vector. When using DataMap as Storage, this will just duplicate the map without copying the data.

Parameters:

other[in] another Vector to use as the data source.

template<template<typename, typename> typename OtherStorage, ResizableContainer S = storage>
Vector(const SparseVector<ElemType, Location, OtherStorage> &sparse_vec)

Creates a new Vector from a SparseVector.

Warning

This constructor is only available when using Array as Storage.

Parameters:

sparse_vec[in] another Vector to use as the data source

Returns:

*this.

Vector(const Vector &other)

Creates a Vector from an other Vector. When using DataMap as Storage, this will just duplicate the map without copying the data.

Parameters:

other[in] another Vector to use as the data source.

template<ResizableContainer S = storage>
Vector(Vector &&other) noexcept

Creates a new Vector and then moves the content from an other Vector.

Warning

This constructor is only available when using Array as Storage.

Parameters:

other[in] another Vector to use as the data source.

Destructor

virtual ~Vector() = default

Default destructor.

Assignment

template<Number T>
Vector &operator=(T scalar)

Sets all elements in the container to scalar.

Parameters:

scalar[in] scalar to use as the data source

Returns:

*this.

template<DataLocation OtherLoc, template<typename, typename> typename OtherStorage>
Vector &operator=(const Vector<ElemType, OtherLoc, OtherStorage> &other)

Copies the content from an other Vector to this container.

Parameters:

other[in] another Vector to use as the data source

Returns:

*this.

template<template<typename, typename> typename OtherStorage>
Vector &operator=(const SparseVector<ElemType, Location, OtherStorage> &sparse_vec)

Copies the content from an other Vector to this container.

Parameters:

sparse_vec[in] another Vector to use as the data source

Returns:

*this.

Vector &operator=(const Vector &other)

Copies the content from an other Vector to this container.

Parameters:

other[in] another Vector to use as the data source.

Returns:

*this.

template<ResizableContainer S = storage>
Vector &operator=(Vector &&other) noexcept

Moves the content from an other Vector to this container.

Warning

This constructor is only available when using Array as Storage.

Parameters:

other[in] another Vector to use as the data source.

Returns:

*this.

Element Access

ElemType &operator()(index_t idx)

Accesses element at idx position.

Parameters:

idx[in] position of the element to return

Returns:

A reference to requested element

ElemType operator()(index_t idx) const

Accesses element at idx position.

Parameters:

idx[in] position of the element to return

Returns:

The requested element

VectorMap<ElemType, Location> operator()(Range idx_range) const
Parameters:

idx_range[in] the indexes of beginning and end of the range

Returns:

A view of the vector that corresponds to the specified range.

Capacity

index_t size() const noexcept
Returns:

The number of elements in the Vector.

index_t rows() const noexcept
Returns:

The number of rows in the Vector.

index_t cols() const noexcept
Returns:

The number of columns in the Vector.

template<ResizableContainer S = storage>
void resize(index_t new_size)

Resizes the Vector. The previous content in the vector is destroyed.

Note

This routine is only available when using Array as Storage.

Parameters:

new_size[in] new size

Throws:

LengthError – if `new_size < 1`.

Modifiers

internal::ConjugateOp<Vector> conj() const
Returns:

An abstract expression representing the conjugate of the Vector. If the matrix is real, this is a noop.

void swap(Vector &other) noexcept

Swaps the Vector content with an other Vector, including any memory allocation (if applicable).

Parameters:

other[in] another Vector to exchange content with.

template<RemappableContainer S = storage>
void map_to(VectorData<ElemType> data)

Replaces the managed container.

Note

This routine is only available when using DataMap as Storage.

Parameters:

data[in] a struct containing the pointer and size of the container

template<template<typename, typename> typename OtherStorage, RemappableContainer S = storage>
void map_to(const Vector<ElemType, Location, OtherStorage> &other)

Replaces the managed container.

Note

This routine is only available when using DataMap as Storage.

Parameters:

other[in] another Vector

VectorData<ElemType> mat_data() const
Returns:

a simple struct with a pointer to the container and its dimensions.

template<typename T>
struct VectorData

A simple struct storing the data of a dense vector.

Public Members

T *buffer

A contiguous array with elements of the vector.

index_t size

The size of the vector.