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