Vectors

“Vector” is a funny word. It means a lot of things, to a lot of people. The JSL::Vector object, however, is unambiguously a vector on \(\mathbb{R}^n\), the real, Euclidean vector space of dimension \(n\). That means that it always contains objects of type double (though int castings in and out are more than welcome).

class JSL::Vector

A class implementing basic R^n vector mathematics. Mostly acts as an extention to the basic std::vector object, but with the implicit assumption that the objects should behave like members of a true vector space, can unambiguously overload some operators and add in additional functionality.

Public Functions

inline Vector()
inline Vector(int n)

Initialises the vector to a state of length n, populated by zeros.

Parameters:

n – The length of the vector to be created

inline Vector(std::vector<double> input)

Initialises the vector to contain the provided stl vector.

Parameters:

input – An std::vector which the new Vector will envelop.

inline Vector(std::vector<int> input)

Initialises the vector to contain the provided stl vector.

Parameters:

input – An std::vector which the new Vector will envelop.

inline int Size() const
inline double &operator[](int idx)

Overload access operator so can call Vector[0] etc as normal for a vector class. Performs checks on the size so that you cannot over/underflow the memory access.

inline const double &operator[](int idx) const

Replication of non-const version (annoying) but necessary for good access….

inline double Dot(const Vector &rhs) const

Provides a member alias for VectorDotProduct(), with the first argument being the current object.

Parameters:

rhs – The second object passed to VectorDotProduct

Returns:

The dot product of rhs and the object

inline Vector Cross(const Vector &rhs) const

Provides a member alias for VectorCrossProduct(), with the first argument being the current object (recall order does matter for cross products!)

Parameters:

rhs – The second object passed to VectorDotProduct

Returns:

The dot product (this x rhs)

inline double SqNorm() const

The squared-norm of the current object, calculated using Dot(). Probably not as much use as Norm(), but saves time sqrting and then squaring again!

Returns:

this.Dot(this)

inline double Norm() const

The norm of the current object,.

Returns:

The square-root of the SqNorm() function.

inline double AngleBetween(const Vector &rhs) const

A member-alias for AngleBetweenVectors(), with the first argument being the current object.

Parameters:

rhs – The second object passed to AngleBetweenVectors()

Returns:

The angle between this object and the provided vector

inline std::string to_string() const

Converts the vector into a human-readable string.

Returns:

A representation of the vector, such as (1,4.5,3)

inline std::string to_simple_string() const
inline std::string to_string_precision(const int n) const
inline operator std::vector<double>() const

Implicit conversion back to std::vector<double>

inline operator std::vector<int>() const

Implicit conversion back to std::vector<int>

inline Vector &operator+=(const Vector &rhs)

In-place addition of two vectors. Calls Vector operator+(const Vector & lhs, const Vector & rhs) using this object as lhs.

Parameters:

rhs – The vector to be accumulated into the current object. Must be the same nElements as the calling object.

Returns:

A reference to the now-modified calling object

inline Vector &operator-=(const Vector &rhs)

In-place subtraction of two vectors. Calls Vector operator-(const Vector & lhs, const Vector & rhs) using this object as lhs.

Parameters:

rhs – The vector to be subtracted from the current object. Must be the same nElements as the calling object.

Returns:

A reference to the now-modified calling object

inline Vector &operator+=(const double &scalar)

In-place addition of a scalar onto the callign object. Calls Vector operator+(const Vector & lhs, const double & scalar) using this object as lhs.

Parameters:

scalar – The double to be accumulated into the current object.

Returns:

A reference to the now-modified calling object

inline Vector &operator-=(const double &scalar)

In-place subtraction of a scalar onto the callign object. Calls Vector operator-(const Vector & lhs, const double & scalar) using this object as lhs.

Parameters:

scalar – The double to be subtracted from the current object.

Returns:

A reference to the now-modified calling object

inline Vector &operator*=(const double &scalar)

In-place multiplication of a scalar with the calling object. Calls Vector operator*(const Vector & lhs, const double & scalar) using this object as lhs.

Parameters:

scalar – The double to be accumulated into the current object.

Returns:

A reference to the now-modified calling object

inline Vector &operator/=(const double &scalar)

In-place division of the calling object with a scalar. Calls Vector operator/(const Vector & lhs, const double & scalar) using this object as lhs.

Parameters:

scalar – The double to be accumulated into the current object.

Returns:

A reference to the now-modified calling object

inline bool isnan()

Reports true if any members are NaN.

Public Static Functions

static inline Vector linspace(double start, double end, unsigned int length)

Constructs a vector of specified length with start and end points (inclusive) given by the user, with the intervening points linearly spaced apart.

Parameters:
  • start – The first element of the returned array

  • end – The final element of the array (included in the range!)

  • length – The length of the returned array

Returns:

The vector (start, start + x,start + 2x, …. end)

static inline Vector intspace(int start, int end, int step)

Similar to linspace(), but for integer values. Note that the step is specified here, not the length. It is possible that end is not included in the array.

Parameters:
  • start – The first element of the returned array

  • end – The maximum possible value for the final element in the array

  • step – The (integer) distance between each successive element in the array

Returns:

A vector, starting at #start, increasing by #step each time.

Overloaded Operators

The advantage of a custom vector class is that you can unambiguously overload some common operators, so that the vectors act as you suspect that they should under common understanding.

Addition

Addition and subtraction of vectors are defined exactly as one might expect:

\[\left(\vec{a} \pm \vec{b}\right)_i = a_i \pm b_i\]
inline Vector JSL::operator+(const Vector &lhs, const Vector &rhs)

Performs obvious vector addition (a+b)_i = a_i + b_i. Throws an error if the vectors are not the same size.

Parameters:
  • lhs – The first vector to be summed

  • rhs – The second vector to be summed (order is irrelevant)

Returns:

The vector lhs + rhs

inline Vector JSL::operator-(const Vector &lhs, const Vector &rhs)

Performs obvious vector subtraction (a-b)_i = a_i - b_i. Throws an error if the vectors are not the same size.

Parameters:
  • lhs – The base vector

  • rhs – The vector to be subtracted from the base vector (order does matter!)

Returns:

The vector lhs - rhs.

We also define the addition/subtraction of scalars from vectors, in which case we assume the following syntax:

\[\left( x \pm \vec{b} \right)_i = x \pm b_i\]
inline Vector JSL::operator+(const Vector &lhs, const double &scalar)

Adds the value of scalar to every element in the vector.

Parameters:
  • lhs – The vector to be summed

  • scalar – The scalar to be added element-wise

Returns:

The vector lhs + scalar

inline Vector JSL::operator+(const double &scalar, const Vector &rhs)

Exactly equivalent to JSL::operator+(const Vector &lhs, const double &scalar), just swapped around.

Parameters:
  • scalar – The scalar to be added element-wise

  • rhs – The vector to be summed

Returns:

The scalar + rhs

inline Vector JSL::operator-(const Vector &lhs, const double &scalar)

Subtracts the value of scalar to every element in the vector.

Parameters:
  • lhs – The base vector

  • scalar – The scalar to be subtracted from the base vector element wise

Returns:

The vector lhs - scalar.

inline Vector JSL::operator-(const double &scalar, const Vector &rhs)

A slightly odd operation (included for completeness) - adds the value of scalar to the negative of the elements of the vector.

Parameters:
  • scalar – The value which acts as a base

  • rhs – The vector which will be subtracted elementwise from the base scalar

Returns:

The vector scalar - rhs

Multiplication

In accordance with the definition of a Vector Space, we define scalar multiplication as:

\[\left( \alpha \vec{a} \right)_i = \left( \vec{a} \alpha \right)_i= \alpha a_i\]
inline Vector JSL::operator*(const double &scalar, const Vector &rhs)

Naive element-wise scalar multiplication.

Parameters:
  • scalar – The value to multiply elements by

  • rhs – The vector to multiply

Returns:

The pointwise product of the elements of rhs and the scalar

inline Vector JSL::operator*(const Vector &lhs, const double &scalar)

Alias of JSL::operator+(const double &scalar,const Vector &rhs) with the operation order swapped around.

Parameters:
  • lhs – The vector to multiply

  • scalar – The value to multiply elements by

Returns:

The pointwise product of the elements of lhs and the scalar

inline Vector JSL::operator/(const Vector &lhs, const double &scalar)

Essentially an alias for JSL::operator+(const double &scalar,const Vector &rhs) with the scalar set to one-over itself, i.e. pointwise division of the provided vector.

Parameters:
  • lhs – The vector to divide

  • scalar – The value to divide elements by

Returns:

The pointwise divisor of the elements of lhs and the scalar

We also define the following vector multiplication-adjacent operations.

inline Vector JSL::Hadamard(const Vector &lhs, const Vector &rhs)

Executes the pointwise (Hadamard) product of two vectors.

Parameters:
  • lhs – The first vector to be multiplied

  • rhs – The second vector to be multiplied (order is irrelevant)

Returns:

The vector (lhs * rhs)_i = lhs_i * rhs_i

inline double JSL::VectorDotProduct(const Vector &lhs, const Vector &rhs)

The standard dot product on R^n.

Parameters:
Returns:

The sum (lhs_i * rhs_i)

inline Vector JSL::VectorCrossProduct(const Vector &lhs, const Vector &rhs)

The standard cross product &#8212; only defined on R^3 (throws an error else)

Parameters:
Returns:

Vector cross product of inputs

inline double JSL::AngleBetweenVectors(const Vector &lhs, const Vector &rhs)

Uses Vector::Norm() and VectorDotProduct() to extract an angle between the vectors.

Parameters:
Returns:

The angle between the two vectors (between 0 and M_PI)

Misc.

inline std::ostream &JSL::operator<<(std::ostream &os, const Vector &obj)

Calls JSL::Vector::to_string() and then passes it to the provided stream, enabling sweet, smooth output such as std::cout << v1 << std::endl.

Parameters:
  • os – An output stream capable of parsing strings

  • obj – A vector object to be inserted into the stream for output

Returns:

A reference to the modified stream

inline Vector JSL::ElementWiseOperation(Vector input, double (*function)(double))

A nice way to perform a simple function on every element of a vector.

Parameters:
  • input – the vector on which the operation will be performed

  • function – A pointer to a funciton which accepts and returns a double. Can also be a lambda function.

Returns:

The vector y such that y_i = f(x_i)