Skip to content

soroush-msd/euclidean-vector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

euclidean-vector

A Euclidean Vector Class Library in C++

Interface is in include/euclidean_vector.h and the implementation is in source/euclidean_vector.cpp.

1. Constructors

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.
(1) auto a = comp6771::euclidean_vector();
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.
(1) auto a = comp6771::euclidean_vector(1);
(2) int i {3};
    auto b = comp6771::euclidean_vector(i);
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.
(1) auto a = comp6771::euclidean_vector(2, 4.0);
(2) auto x = int{3};
    auto y = double{3.24};
    auto b = comp6771::euclidean_vector(x, y);
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.
std::vector<double> v;
auto b = comp6771::euclidean_vector(v.begin(),v.end());
N/A
Constructor euclidean_vector(std::initializer_list<double>) A constructor that takes an initialiser list of doubles to populate vector magnitudes. You will have to do your own research to implement this one.
auto b = comp6771::euclidean_vector{1.0, 2.0, 3.0};
N/A
Copy Constructor euclidean_vector(euclidean_vector const&)
auto a = comp6771::euclidean_vector(a);
N/A
Move Constructor euclidean_vector(euclidean_vector&&)
auto aMove = comp6771::euclidean_vector(std::move(a));
N/A

Example Usage

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

Notes

  • You may assume that all arguments supplied by the user are valid. No error checking on constructors is required.

2. Destructor

You must explicitly declare the destructor as default.

3. Operations

Name Operator Description Examples Exception: Why thrown & what message
Copy Assignment euclidean_vector& operator=(euclidean_vector const&) A copy assignment operator overload
a = b;
N/A
Move Assignment euclidean_vector& operator=(euclidean_vector&&) A move assignment operator
a = std::move(b);
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.
double a {b[1]};
b[2] = 3.0;
N/A
Unary plus euclidean_vector operator+() Returns a copy of the current object.
+a
N/A
Negation euclidean_vector operator-() Returns a copy of the current object, where each scalar value has its sign negated.
auto const actual = comp6771::euclidean_vector{-6, 1};
auto const expected = comp6771::euclidean_vector{6, -1};
CHECK(expected == -actual);
N/A
Compound Addition euclidean_vector& operator+=(euclidean_vector const&) For adding vectors of the same dimension.
a += b;
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.
a -= b;
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]
a *= 3;
N/A
Compound Division euclidean_vector& operator/=(double) For scalar division, e.g. [3 6] / 2 = [1.5 3]
a /= 4;
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
auto const a = comp6771::euclidean_vector{0.0, 1.0, 2.0};
auto const vf = static_cast<std::vector<double>>(a);
N/A
List Type Conversion
explicit operator std::list<double>() Operators for type casting to a std::list
auto const a = comp6771::euclidean_vector{0.0, 1.0, 2.0};
auto lf = static_cast<std::list<double>>(a);
N/A

4. Member Functions

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

5. Friends

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.
a == b;
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.
a != b;
N/A
Addition euclidean_vector operator+(euclidean_vector const&, euclidean_vector const&) For adding vectors of the same dimension.
a = b + c;
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.
a = b - c;
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.
(1) a = b * 3;
(2) a = 3 * b;
N/A
Divide euclidean_vector operator/(euclidean_vector const&, double) For scalar division, e.g. [3 6] / 2 = [1.5 3]
auto b = comp6771::euclidean_vector(3, 3.0);
auto c = double{2.0};
auto a = b / c;
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.
std::cout << a;
N/A

6. Utility functions

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.
comp6771::euclidean_norm(a);
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.
comp6771::unit(a);
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 xy; returns a double. E.g., [1 2] ⋅ [3 4] = 1 * 3 + 2 * 4 = 11
auto c = double{comp6771::dot(a, b)};
Given: X = a.dimensions(), Y = b.dimensions() When: X != Y
Throw: "Dimensions of LHS(X) and RHS(Y) do not match"

Testing

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.

About

A Euclidean Vector Class Library in C++

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published