Skip to content

Commit

Permalink
vectorND
Browse files Browse the repository at this point in the history
  • Loading branch information
cdelv committed Dec 12, 2023
1 parent 3255797 commit fbf8d52
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 10 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ If this code was helpful to you, I'll be very happy if you cite it:
# Whats New?
* Vector2D: operates in the same manner as the Vector3D, but with only two components. The cross product of the Vector2D results in a scalar instead of a vector.

* VectorND: is a vector with N components whose size is defined at compile time. It uses std::numerics for some of the operations. (comming soon)
* VectorND: is a vector with N components whose size is defined at compile time.

* The library utilizes C++20 concepts to restrict the templates for improved compilation time, safety, and error messages. The templated vectors can support any arithmetic type or std::complex of an arithmetic type.

Expand All @@ -30,7 +30,7 @@ vector2D<float> u;
vectorND<bool, 10> w;
```

* The vector components are public for the vector2D and vector3D types.
* The vector components are public for the vector2D and vector3D types `(v.x, v.y, v.z)`.

# Compilation

Expand Down Expand Up @@ -69,7 +69,12 @@ v[0] = 1.2; v[1] = -1.2; v[2] = 22;

For N-dimensional vectors, the only way of accessing the components is with the [] operator.

# vectorND ussage (comming soon)
```
vectorND<int, 5> v(1,2,3,4,5);
vectorND<int, 4> u = {1,2,3,4};
vectorND<int, 6> w(1); // vector of 1's
w.load(1,2,3,4,5,6); // gets checked at compile time for the size of the vector and arguments
```

Print the vector to the screen with `std::cout`. You can also calculate the norm of the vector with `v.norm()` and `norm(v)` or the square norm with `v.norm2()` and `norm2(v)`. Calculate the angle between two vectors (in radians) with the functions `angle(v1,v2)` and in degrees with `angled(v1,v2)`.

Expand Down
105 changes: 98 additions & 7 deletions vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include <iostream>
#include <complex>
#include <cmath>
#include <vector>
#include <algorithm>

/*
* This file is part of the Vector3D distribution (https://github.com/cdelv/Vector3D).
* Copyright (c) 2022 Carlos Andres del Valle.
Expand Down Expand Up @@ -58,7 +61,12 @@ std::ostream& operator<<(std::ostream& os, const __VecExpression<E, N>& vec) {
*/
// Sumation of all elements
template <typename E1, std::size_t N>
inline constexpr auto sum(const __VecExpression<E1, N> &expr) noexcept;
inline constexpr auto sum(const __VecExpression<E1, N> &expr) noexcept {
auto Sum = expr[0];
for (std::size_t i = 1; i < N; ++i)
Sum += expr[i];
return Sum;
}
// Specialization for N = 3
template <typename E1>
inline constexpr auto sum(const __VecExpression<E1, 3> &expr) noexcept {
Expand Down Expand Up @@ -342,15 +350,15 @@ class vector3D : public __VecExpression<vector3D<T>, 3> {
x = expr[0]; y = expr[1]; z = expr[2];
}
inline constexpr const T& operator[](const std::size_t i) const {
if(i == 0) return x;
else if(i == 1) return y;
else if(i == 2) return z;
if (i == 0) return x;
else if (i == 1) return y;
else if (i == 2) return z;
else throw std::out_of_range("vector3D: Index out of range");
}
inline constexpr T& operator[](const std::size_t i) {
if(i == 0) return x;
else if(i == 1) return y;
else if(i == 2) return z;
if (i == 0) return x;
else if (i == 1) return y;
else if (i == 2) return z;
else throw std::out_of_range("vector3D: Index out of range");
}
/*
Expand Down Expand Up @@ -492,4 +500,87 @@ class vector2D : public __VecExpression<vector2D<T>, 2> {
*this /= norm();
return *this;
}
};
template <__Number T, std::size_t N>
class vectorND : public __VecExpression<vectorND<T, N>, N> {
private:
std::vector<T> data;
public:
static inline constexpr const std::size_t size() {
return N;
}
std::vector<T>::iterator begin() { return data.begin();}
std::vector<T>::iterator end() { return data.end();}

constexpr vectorND() noexcept = default;
constexpr vectorND(const vectorND& other) noexcept = default;
constexpr vectorND(vectorND&& other) noexcept = default;
constexpr vectorND(const T value) noexcept : data(N, value) {}
template <typename... Args>
constexpr vectorND(const Args&... args) noexcept : data {args...} {
static_assert(sizeof...(args) == N, "vectorND: Number of arguments does not match the size of the vector.");
}
constexpr vectorND& operator=(const vectorND& other) noexcept = default;
constexpr vectorND& operator=(vectorND&& other) noexcept = default;

template <typename... Args>
inline constexpr void load(const Args&... args) noexcept {
static_assert(sizeof...(args) == N, "vectorND: Number of arguments does not match the size of the vector.");
data = {args...};
}

template <typename E>
inline constexpr vectorND(const __VecExpression<E, N> &expr) noexcept {
for (std::size_t i = 0; i < N; ++i)
data[i] = expr[i];
}
inline constexpr const T& operator[](const std::size_t i) const {
return data[i];
}
inline constexpr T& operator[](const std::size_t i) {
return data[i];
}
/*
* OPERATORS
*/
template <typename E>
inline constexpr vectorND<T, N>& operator+=(const __VecExpression<E, N>& expr) noexcept {
for (std::size_t i = 0; i < N; ++i)
data[i] += expr[i];
return *this;
}
template <typename E>
inline constexpr vectorND<T, N>& operator-=(const __VecExpression<E, N>& expr) noexcept {
for (std::size_t i = 0; i < N; ++i)
data[i] -= expr[i];
return *this;
}
template <__Number E>
inline constexpr vectorND<T, N>& operator*=(const E& a) noexcept {
for (std::size_t i = 0; i < N; ++i)
data[i] *= a;
return *this;
}
template <__Number E>
inline constexpr vectorND<T, N>& operator/=(const E& a) noexcept {
for (std::size_t i = 0; i < N; ++i)
data[i] /= a;
return *this;
}
template <typename E>
inline constexpr vectorND<T, N>& operator/=(const __VecExpression<E, N>& expr) noexcept {
for (std::size_t i = 0; i < N; ++i)
data[i] /= expr[i];
return *this;
}
inline constexpr const T norm2() const noexcept {
return dot(*this, *this);
}
inline constexpr const T norm() const noexcept {
return std::sqrt(norm2());
}
inline constexpr const vectorND<T, N>& unit() noexcept {
*this /= norm();
return *this;
}
};

0 comments on commit fbf8d52

Please sign in to comment.