diff options
author | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2019-01-14 15:47:59 -0500 |
---|---|---|
committer | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2019-01-14 15:47:59 -0500 |
commit | 49ac78a6c04e215950bc9c0f97368605e63da15b (patch) | |
tree | 64b770c543a0c90bc7dcbc06ceaaa31e96e541ce /lib/wolff_models/vector.hpp | |
parent | 994cbf1a3b611ff4c94ced3b1630e51fd249d7ed (diff) | |
download | c++-49ac78a6c04e215950bc9c0f97368605e63da15b.tar.gz c++-49ac78a6c04e215950bc9c0f97368605e63da15b.tar.bz2 c++-49ac78a6c04e215950bc9c0f97368605e63da15b.zip |
Large refactoring around changes in the graph class.
- Graphs now use lists of references instead of vectors of indicies.
- Vertices and edges have associated classes that can be given arbitrary properties via template specification.
- All essential library headers have been combined into one, wolff.hpp.
Diffstat (limited to 'lib/wolff_models/vector.hpp')
-rw-r--r-- | lib/wolff_models/vector.hpp | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/lib/wolff_models/vector.hpp b/lib/wolff_models/vector.hpp new file mode 100644 index 0000000..4106e62 --- /dev/null +++ b/lib/wolff_models/vector.hpp @@ -0,0 +1,113 @@ + +#ifndef WOLFF_MODELS_VECTOR_H +#define WOLFF_MODELS_VECTOR_H + +#include <cmath> +#include <array> +#include <iostream> + +namespace wolff { + + template <unsigned q, class T> + class vector_t : public std::array<T, q> { + public: + + vector_t() { + this->fill((T)0); + (*this)[0] = (T)1; + } + + vector_t(const T *x) { + for (unsigned i = 0; i < q; i++) { + (*this)[i] = x[i]; + } + } + + typedef vector_t <q, T> M_t; + typedef vector_t <q, double> F_t; + + template <class U> + inline vector_t<q, T>& operator+=(const vector_t<q, U> &v) { + for (unsigned i = 0; i < q; i++) { + (*this)[i] += (T)v[i]; + } + return *this; + } + + template <class U> + inline vector_t<q, T>& operator-=(const vector_t<q, U> &v) { + for (unsigned i = 0; i < q; i++) { + (*this)[i] -= (T)v[i]; + } + return *this; + } + + inline vector_t<q, T> operator*(unsigned x) const { + vector_t<q, T> result; + for (unsigned i = 0; i < q; i++) { + result[i] = x * (*this)[i]; + } + + return result; + } + + inline vector_t<q, double> operator*(double x) const { + vector_t<q, double> result; + for (unsigned i = 0; i < q; i++) { + result[i] = x * (*this)[i]; + } + + return result; + } + + inline vector_t<q, T> operator-(const vector_t<q, T>& v) const { + vector_t<q, T> diff = *this; + diff -= v; + return diff; + } + + inline T operator*(const vector_t<q, T>& v) const { + double prod = 0; + + for (unsigned i = 0; i < q; i++) { + prod += v[i] * (*this)[i]; + } + + return prod; + } + + template <class U> + inline vector_t<q, T> operator/(U a) const { + vector_t<q, T> result; + for (unsigned i = 0; i < q; i++) { + result[i] = (*this)[i] / a; + } + + return result; + } + }; + + template<unsigned q, class T> + inline vector_t<q, T> operator*(unsigned a, const vector_t<q, T>&v) { + return v * a; + } + + template<unsigned q, class T> + inline vector_t<q, double> operator*(double a, const vector_t<q, T>&v) { + return v * a; + } + + template<unsigned q, class T> + std::ostream& operator<<(std::ostream& os, const vector_t<q, T>&v) { + os << "( "; + for (T vi : v) { + os << vi << " "; + } + os << ")"; + return os; + } + +} + +#endif + |