X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flinal%2Fvector.h;h=152c26e66482e943e5837c5ddecc54fc9e7c2fde;hb=7d43d93ac2d701bb3de87cdf81e44c03e64c35c3;hp=11292f7441dae7e7f69cefb58aadb73572f687e4;hpb=aa47cdf95914ae706b99a48d528260b3261f9882;p=libs%2Fmath.git diff --git a/source/linal/vector.h b/source/linal/vector.h index 11292f7..152c26e 100644 --- a/source/linal/vector.h +++ b/source/linal/vector.h @@ -53,6 +53,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 +74,21 @@ template class Vector: public VectorComponents { public: + typedef T ElementType; + Vector(); Vector(const T *); Vector(T, T); Vector(T, T, T); + Vector(T, T, T, T); template Vector(const Vector &); + unsigned size() const { return N; } + + template + Vector slice(unsigned) const; + Vector &operator*=(T); Vector &operator/=(T); Vector &operator+=(const Vector &); @@ -108,6 +130,15 @@ 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_; +} + template template inline Vector::Vector(const Vector &v) @@ -116,6 +147,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; + 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 +284,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)); }