A Euclidean Vector Class Library in C++
Interface is in include/euclidean_vector.h
and the implementation is in source/euclidean_vector.cpp
.
You may have to scroll horizontally to view these tables
Name | Constructor | Description and Hints | Examples | Exception: Why thrown & what message |
---|---|---|---|---|
Default Constructor | euclidean_vector() |
A constructor that generates a euclidean vector with a dimension of 1 and magnitude of 0.0. You can assume the integer input will always be non-negative. |
|
N/A |
Single-argument Constructor | explicit euclidean_vector(int) |
A constructor that takes the number of dimensions (as a int) but no magnitudes, sets the magnitude in each dimension as 0.0. You can assume the integer input will always be non-negative. |
|
N/A |
Constructor | euclidean_vector(int, double); |
A constructor that takes the number of dimensions (as an int ) and initialises the
magnitude in each dimension as the second argument (a double ). You can assume the
integer input will always be non-negative.
|
|
N/A |
Constructor | euclidean_vector(std::vector<double>::const_iterator, std::vector<double>::const_iterator) |
A constructor (or constructors) that takes the start and end of an iterator to a
std:vector<double> and works out the required dimensions, and sets the
magnitude in each dimension according to the iterated values.
|
|
N/A |
Constructor | euclidean_vector(std::initializer_list<double>) |
A constructor that takes an initialiser list of double s to populate vector
magnitudes. You will have to do your own research to implement this one.
|
|
N/A |
Copy Constructor | euclidean_vector(euclidean_vector const&) |
|
N/A | |
Move Constructor | euclidean_vector(euclidean_vector&&) |
|
N/A |
auto a = comp6771::euclidean_vector(1); // a Euclidean Vector in 1 dimension, with default magnitude 0.0.
auto b = comp6771::euclidean_vector(2, 4.0); // a Euclidean Vector in 2 dimensions with magnitude 4.0 in both dimensions
auto v = std::vector<double>{5.0, 6.5, 7.0};
auto c = comp6771::euclidean_vector(v.begin(), v.end()); // a Euclidean Vector in 3 dimensions constructed from a vector of magnitudes
- You may assume that all arguments supplied by the user are valid. No error checking on constructors is required.
You must explicitly declare the destructor as default.
Name | Operator | Description | Examples | Exception: Why thrown & what message |
---|---|---|---|---|
Copy Assignment | euclidean_vector& operator=(euclidean_vector const&) |
A copy assignment operator overload |
|
N/A |
Move Assignment | euclidean_vector& operator=(euclidean_vector&&) |
A move assignment operator |
|
N/A |
Subscript |
operator[]
A const and non-const declaration is needed |
Allows to get and set the value in a given dimension of the Euclidean vector. Hint: you may
need two overloadeds to achieve this requirement. Note: It's a requirement you use asserts to ensure the index passed is valid. |
|
N/A |
Unary plus | euclidean_vector operator+() |
Returns a copy of the current object. |
|
N/A |
Negation | euclidean_vector operator-() |
Returns a copy of the current object, where each scalar value has its sign negated. |
|
N/A |
Compound Addition | euclidean_vector& operator+=(euclidean_vector const&) |
For adding vectors of the same dimension. |
|
Given: X = a.dimensions(), Y = b.dimensions()
When: X != Y Throw: "Dimensions of LHS(X) and RHS(Y) do not match" |
Compound Subtraction | euclidean_vector& operator-=(euclidean_vector const&)
|
For subtracting vectors of the same dimension. |
|
Given: X = a.dimensions(), Y = b.dimensions()
When: X != Y Throw: "Dimensions of LHS(X) and RHS(Y) do not match" |
Compound Multiplication | euclidean_vector& operator*=(double) |
For scalar multiplication, e.g. [1 2] * 3 = [3 6] |
|
N/A |
Compound Division | euclidean_vector& operator/=(double) |
For scalar division, e.g. [3 6] / 2 = [1.5 3] |
|
When: b == 0 Throw: "Invalid vector division by 0" |
Vector Type Conversion |
explicit operator std::vector<double>() |
Operators for type casting to a std::vector |
|
N/A |
List Type Conversion |
explicit operator std::list<double>() |
Operators for type casting to a std::list |
|
N/A |
Prototype | Description | Usage | Exception: Why thrown & what message |
---|---|---|---|
double at(int) const |
Returns the value of the magnitude in the dimension given as the function parameter | a.at(1); |
When: For Input X: when X is < 0 or X is >= number of dimensions Throw: "Index X is not valid for this euclidean_vector object" |
double& at(int) |
Returns the reference of the magnitude in the dimension given as the function parameter | a.at(1); |
When: For Input X: when X is < 0 or X is >= number of dimensions Throw: "Index X is not valid for this euclidean_vector object" |
int dimensions() |
Return the number of dimensions in a particular euclidean_vector | a.dimensions(); |
N/A |
In addition to the operations indicated earlier, the following operations should be supported as friend functions. Note that these friend operations don't modify any of the given operands.
Name | Operator | Description | Usage | Exception: Why thrown & what message |
---|---|---|---|---|
Equal | bool operator==(euclidean_vector const&, euclidean_vector const&) |
True if the two vectors are equal in the number of dimensions and the magnitude in each dimension is equal. |
|
N/A |
Not Equal | bool operator!=(euclidean_vector const&, euclidean_vector const&) |
True if the two vectors are not equal in the number of dimensions or the magnitude in each dimension is not equal. |
|
N/A |
Addition | euclidean_vector operator+(euclidean_vector const&, euclidean_vector const&) |
For adding vectors of the same dimension. |
|
Given: X = b.dimensions() , Y = c.dimensions()
When: X != Y Throw: "Dimensions of LHS(X) and RHS(Y) do not match" |
Subtraction | euclidean_vector operator-(euclidean_vector const&, euclidean_vector const&) |
For substracting vectors of the same dimension. |
|
Given: X = b.dimensions() , Y = c.dimensions()
When: X != Y Throw: "Dimensions of LHS(X) and RHS(Y) do not match" |
Multiply | euclidean_vector operator*(euclidean_vector const&, double) |
For scalar multiplication, e.g. [1 2] * 3 = 3 * [1 2] = [3 6] .
Hint: you'll need two operators, as the scalar can be either side of the vector.
|
|
N/A |
Divide | euclidean_vector operator/(euclidean_vector const&, double) |
For scalar division, e.g. [3 6] / 2 = [1.5 3] |
|
When: c == 0 Throw: "Invalid vector division by 0" |
Output Stream | std::ostream& operator<<(std::ostream&, euclidean_vector const&) |
Prints out the magnitude in each dimension of the Euclidean vector (surrounded by
[ and ] ), e.g. for a 3-dimensional vector: [1 2 3] . Note: When printing the magnitude, simple use the double << operator.
|
|
N/A |
The following are functions that operate on a Euclidean vector, but shouldn't be a part of its interface. They may be friends, if you need access to the implementation, but you should avoid friendship if you can.
Name | Description | Usage | Exception: Why thrown & what message |
---|---|---|---|
auto euclidean_norm(euclidean_vector const& v) -> double; |
Returns the Euclidean norm of the vector as a double . The Euclidean norm is the
square root of the sum of the squares of the magnitudes in each dimension. E.g, for the vector
[1 2 3] the Euclidean norm is sqrt(1*1 + 2*2 + 3*3) = 3.74 .
If v.dimensions() == 0 , the result is 0.
|
|
N/A |
auto unit(euclidean_vector const& v) -> euclidean_vector; |
Returns a Euclidean vector that is the unit vector of v . The magnitude for each
dimension in the unit vector is the original vector's magnitude divided by the Euclidean norm.
|
|
When: v.dimensions() == 0 Throw: "euclidean_vector with no dimensions does not have a unit vector" When: comp6771::euclidean_norm(v) == 0 Throw: "euclidean_vector with zero euclidean normal does not have a unit vector" |
auto dot(euclidean_vector const& x, euclidean_vector const& y) -> double
|
Computes the dot product of x ⋅ y ; returns a
double . E.g., [1 2] ⋅ [3 4] = 1 * 3 + 2 * 4 = 11
|
|
Given: X = a.dimensions() , Y = b.dimensions()
When: X != Y Throw: "Dimensions of LHS(X) and RHS(Y) do not match" |
Here is a sample and example of Catch2 tests:
TEST_CASE("Creation of unit vectors") {
SECTION("You have two identical vectors") {
auto a = comp6771::euclidean_vector(2);
a[0] = 1;
a[1] = 2;
auto b = comp6771::euclidean_vector(2);
b[0] = 1;
b[1] = 2;
auto c = comp6771::unit(a);
auto d = comp6771::unit(b);
REQUIRE(c == d);
}
}
To test exceptions, functions like
REQUIRE_THROWS_WITH
can be useful.