X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flinal%2Fvector.h;h=31597d4282410e2606590eaea2faa6d3c22c2cd9;hb=0a8781509bafe347b9c32f1106891eac18354965;hp=11292f7441dae7e7f69cefb58aadb73572f687e4;hpb=aa47cdf95914ae706b99a48d528260b3261f9882;p=libs%2Fmath.git diff --git a/source/linal/vector.h b/source/linal/vector.h index 11292f7..31597d4 100644 --- a/source/linal/vector.h +++ b/source/linal/vector.h @@ -3,6 +3,7 @@ #include #include +#include namespace Msp { namespace LinAl { @@ -53,6 +54,20 @@ public: const T &operator[](unsigned i) const { return *(&x+i); } }; +template +class VectorComponents +{ +public: + T x, y, z, w; + +protected: + VectorComponents() { } + +public: + T &operator[](unsigned i) { return *(&x+i); } + const T &operator[](unsigned i) const { return *(&x+i); } +}; + /** A general mathematical vector. */ @@ -60,13 +75,31 @@ template class Vector: public VectorComponents { public: + typedef T ElementType; + Vector(); Vector(const T *); + + /** Constructs a vector from an array of interleaved values. Intended for + use by Matrix row accessor. */ + Vector(const T *, unsigned); + +#if __cplusplus >= 201103L + template + Vector(T, Args...); +#else Vector(T, T); Vector(T, T, T); + Vector(T, T, T, T); +#endif template Vector(const Vector &); + unsigned size() const { return N; } + + template + Vector slice(unsigned) const; + Vector &operator*=(T); Vector &operator/=(T); Vector &operator+=(const Vector &); @@ -91,6 +124,25 @@ inline Vector::Vector(const T *d) (*this)[i] = d[i]; } +template +inline Vector::Vector(const T *d, unsigned stride) +{ + for(unsigned i=0; i= 201103L +template +template +inline Vector::Vector(T x_, Args... v) +{ + static_assert(1+sizeof...(v)==N, "Incorrect number of arguments in Vector constructor"); + (*this)[0] = x_; + unsigned i = 1; + for(auto c: std::initializer_list { static_cast(v)... }) + (*this)[i++] = c; +} +#else /* The compiler won't instantiate these unless they are used. Trying to use them on the wrong class results in an error. */ template @@ -108,6 +160,16 @@ inline Vector::Vector(T x_, T y_, T z_) this->VectorComponents::z = z_; } +template +inline Vector::Vector(T x_, T y_, T z_, T w_) +{ + this->VectorComponents::x = x_; + this->VectorComponents::y = y_; + this->VectorComponents::z = z_; + this->VectorComponents::w = w_; +} +#endif + template template inline Vector::Vector(const Vector &v) @@ -116,6 +178,47 @@ inline Vector::Vector(const Vector &v) (*this)[i] = v[i]; } +template +inline Vector compose(const Vector &v, T s) +{ + Vector r; + for(unsigned i=0; i +inline Vector compose(T s, const Vector &v) +{ + Vector r; + r[0] = s; + for(unsigned i=0; i +inline Vector compose(const Vector &v1, const Vector &v2) +{ + Vector r; + for(unsigned i=0; i +template +inline Vector Vector::slice(unsigned j) const +{ + Vector r; + for(unsigned i=0; i inline Vector &Vector::operator*=(T s) { @@ -212,6 +315,7 @@ inline T inner_product(const Vector &v1, const Vector &v2) template inline T Vector::norm() const { + using std::sqrt; return sqrt(inner_product(*this, *this)); } @@ -240,6 +344,20 @@ inline Vector cross(const Vector &v1, const Vector &v2) return Vector(v1.y*v2.z-v1.z*v2.y, v1.z*v2.x-v1.x*v2.z, v1.x*v2.y-v1.y*v2.x); } +template +inline std::ostream &operator<<(std::ostream &s, const Vector &v) +{ + s << "Vector" << N << '('; + for(unsigned i=0; i