X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flinal%2Fvector.h;h=c9b2a179fe5fd0ce595e4dda796b3c2d7b13ab5d;hb=9ab061034720c1c0db4e8bff5dae4191ccd2a26f;hp=369e3ff2435bf44e597e9e9750b4dc1a03d1c10b;hpb=959f6f9d250985e7324d4a07623bee5a5b00faca;p=libs%2Fmath.git diff --git a/source/linal/vector.h b/source/linal/vector.h index 369e3ff..c9b2a17 100644 --- a/source/linal/vector.h +++ b/source/linal/vector.h @@ -3,6 +3,7 @@ #include #include +#include namespace Msp { namespace LinAl { @@ -74,14 +75,26 @@ 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); + 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 &); @@ -106,6 +119,13 @@ inline Vector::Vector(const T *d) (*this)[i] = d[i]; } +template +inline Vector::Vector(const T *d, unsigned stride) +{ + for(unsigned i=0; i @@ -140,6 +160,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) { @@ -236,6 +297,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)); } @@ -264,6 +326,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