]> git.tdb.fi Git - libs/math.git/blobdiff - source/linal/vector.h
Add row and column accessors to Matrix
[libs/math.git] / source / linal / vector.h
index a5c9a26cfc1e8152fd4cece0158aa553f3b97c8d..9a9d02822aebe554627178803b4c6bd4c2621ddc 100644 (file)
@@ -74,17 +74,25 @@ template<typename T, unsigned N>
 class Vector: public VectorComponents<T, N>
 {
 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<typename U>
        Vector(const Vector<U, N> &);
-       template<typename U>
-       Vector(const Vector<U, N-1> &, U);
-       template<typename U>
-       explicit Vector(const Vector<U, N+1> &);
+
+       unsigned size() const { return N; }
+
+       template<unsigned M>
+       Vector<T, M> slice(unsigned) const;
 
        Vector &operator*=(T);
        Vector &operator/=(T);
@@ -110,6 +118,13 @@ inline Vector<T, N>::Vector(const T *d)
                (*this)[i] = d[i];
 }
 
+template<typename T, unsigned N>
+inline Vector<T, N>::Vector(const T *d, unsigned stride)
+{
+       for(unsigned i=0; i<N; ++i)
+               (*this)[i] = d[i*stride];
+}
+
 /* The compiler won't instantiate these unless they are used.  Trying to use
 them on the wrong class results in an error. */
 template<typename T, unsigned N>
@@ -145,20 +160,44 @@ inline Vector<T, N>::Vector(const Vector<U, N> &v)
 }
 
 template<typename T, unsigned N>
-template<typename U>
-inline Vector<T, N>::Vector(const Vector<U, N-1> &v, U s)
+inline Vector<T, N+1> compose(const Vector<T, N> &v, T s)
 {
-       for(unsigned i=0; i<N-1; ++i)
-               (*this)[i] = v[i];
-       (*this)[N-1] = s;
+       Vector<T, N+1> r;
+       for(unsigned i=0; i<N; ++i)
+               r[i] = v[i];
+       r[N] = s;
+       return r;
 }
 
 template<typename T, unsigned N>
-template<typename U>
-inline Vector<T, N>::Vector(const Vector<U, N+1> &v)
+inline Vector<T, N+1> compose(T s, const Vector<T, N> &v)
 {
+       Vector<T, N+1> r;
        for(unsigned i=0; i<N; ++i)
-               (*this)[i] = v[i];
+               r[i] = v[i];
+       r[N] = s;
+       return r;
+}
+
+template<typename T, unsigned N, unsigned M>
+inline Vector<T, N+M> compose(const Vector<T, N> &v1, const Vector<T, M> &v2)
+{
+       Vector<T, N+M> r;
+       for(unsigned i=0; i<N; ++i)
+               r[i] = v1[i];
+       for(unsigned i=0; i<M; ++i)
+               r[N+i] = v2[i];
+       return r;
+}
+
+template<typename T, unsigned N>
+template<unsigned M>
+inline Vector<T, M> Vector<T, N>::slice(unsigned j) const
+{
+       Vector<T, M> r;
+       for(unsigned i=0; i<M; ++i)
+               r[i] = (*this)[j+i];
+       return r;
 }
 
 template<typename T, unsigned N>
@@ -257,6 +296,7 @@ inline T inner_product(const Vector<T, N> &v1, const Vector<T, N> &v2)
 template<typename T, unsigned N>
 inline T Vector<T, N>::norm() const
 {
+       using std::sqrt;
        return sqrt(inner_product(*this, *this));
 }