#include #define tmpl template tmpl VectorXD::VectorXD() { for (unsigned i = 0; i < X; ++i) { _scalars[i] = 0; } } tmpl VectorXD::VectorXD(const double scalars[X]) { for (unsigned i = 0; i < X; ++i) { _scalars[i] = scalars[i]; } } tmpl VectorXD::VectorXD(const VectorXD& other) { for (unsigned i = 0; i < X; ++i) { _scalars[i] = other._scalars[i]; } } tmpl VectorXD& VectorXD::setScalar(unsigned i, double value) { _scalars[i] = value; return *this; } tmpl double VectorXD::getScalar(unsigned i) { return _scalars[i]; } tmpl bool VectorXD::isNull() const { return equal(VectorXD()); } tmpl bool VectorXD::equal(const VectorXD &other) const { for (unsigned i = 0; i < X; ++i) { if (_scalars[i] != other._scalars[i]) { return false; } } return true; } tmpl VectorXD &VectorXD::add(double k) { double scalars[X]; for (unsigned i = 0; i < X; ++i) { scalars[i] = k; } return add(VectorXD(scalars)); } tmpl VectorXD &VectorXD::add(double scalars[X]) { return add(VectorXD(scalars)); } tmpl VectorXD& VectorXD::add(const VectorXD &other) { for (unsigned i = 0; i < X; ++i) { _scalars[i] += other._scalars[i]; } return *this; } tmpl VectorXD &VectorXD::sub(double k) { double scalars[X]; for (unsigned i = 0; i < X; ++i) { scalars[i] = k; } return sub(VectorXD(scalars)); } tmpl VectorXD &VectorXD::sub(double scalars[X]) { return sub(VectorXD(scalars)); } tmpl VectorXD& VectorXD::sub(const VectorXD &other) { for (unsigned i = 0; i < X; ++i) { _scalars[i] -= other._scalars[i]; } return *this; } tmpl VectorXD &VectorXD::mult(double k) { for (unsigned i = 0; i < X; ++i) { _scalars[i] *= k; } return *this; } tmpl VectorXD &VectorXD::mult(double scalars[X]) { return mult(VectorXD(scalars)); } tmpl VectorXD &VectorXD::mult(const VectorXD &other) { for (unsigned i = 0; i < X; ++i) { _scalars[i] *= other._scalars[i]; } return *this; } tmpl VectorXD &VectorXD::div(double k) { double scalars[X]; for (unsigned i = 0; i < X; ++i) { scalars[i] = k; } return div(VectorXD(scalars)); } tmpl VectorXD &VectorXD::div(double scalars[X]) { return div(VectorXD(scalars)); } tmpl VectorXD &VectorXD::div(const VectorXD &other) { for (unsigned i = 0; i < X; ++i) { _scalars[i] /= other._scalars[i]; } return *this; } tmpl double VectorXD::dotProduct(const VectorXD &other) const { double total = 0; for (unsigned i = 0; i < X; ++i) { total += _scalars[i] * other._scalars[i]; } return total; } tmpl VectorXD& VectorXD::crossProduct(const VectorXD& other) { VectorXD t = *this; for (unsigned i = 0; i < X; ++i) { unsigned j = (i + 1) % X; unsigned k = (i + 2) % X; _scalars[i] = (t._scalars[j] * other._scalars[k]) - (t._scalars[k] * other._scalars[j]); } return *this; } tmpl VectorXD VectorXD::crossProduct(const VectorXD &v1, const VectorXD &v2) { return VectorXD(v1).crossProduct(v2); } tmpl double VectorXD::norm() const { double total = 0; for (unsigned i = 0; i < X; ++i) { total += _scalars[i] * _scalars[i]; } return sqrt(total); } tmpl VectorXD VectorXD::operator+() const { return *this; } tmpl VectorXD VectorXD::operator+(const double &k) const { return VectorXD(*this).add(k); } tmpl VectorXD &VectorXD::operator+=(const double &k) { return add(k); } tmpl VectorXD VectorXD::operator+(const VectorXD &other) const { return VectorXD(*this).add(other); } tmpl VectorXD &VectorXD::operator+=(const VectorXD &other) { return add(other); } tmpl VectorXD VectorXD::operator-() const { double scalars[X]; for (unsigned i = 0; i < X; ++i) { scalars[i] = -_scalars[i]; } return VectorXD(scalars); } tmpl VectorXD VectorXD::operator-(const double &k) const { return VectorXD(*this).sub(k); } tmpl VectorXD &VectorXD::operator-=(const double &k) { return sub(k); } tmpl VectorXD VectorXD::operator-(const VectorXD &other) const { return VectorXD(*this).sub(other); } tmpl VectorXD &VectorXD::operator-=(const VectorXD &other) { return sub(other); } tmpl VectorXD VectorXD::operator*(const double &k) const { return VectorXD(*this).mult(k); } tmpl VectorXD &VectorXD::operator*=(const double &k) { return mult(k); } tmpl VectorXD VectorXD::operator*(const VectorXD &other) const { return VectorXD(*this).mult(other); } tmpl VectorXD &VectorXD::operator*=(const VectorXD &other) { dotProduct(other); return *this; } tmpl VectorXD VectorXD::operator/(const double &k) const { return VectorXD(*this).div(k); } tmpl VectorXD &VectorXD::operator/=(const double &k) { return div(k); } tmpl bool VectorXD::operator==(const VectorXD &other) const { return equal(other); } tmpl bool VectorXD::operator!=(const VectorXD &other) const { return !equal(other); } tmpl bool VectorXD::operator!() const { return isNull(); } tmpl VectorXD::operator bool() const { return !isNull(); } tmpl QDebug operator<<(QDebug dbg, const VectorXD &v) { return dbg.nospace() << "(" << v.getX() << ", " << v.getY() << ", " << v.getZ() << ")"; }