]> git.tdb.fi Git - libs/math.git/blobdiff - source/linal/vector.h
Clearer API for slicing and dicing vectors, and also matrices
[libs/math.git] / source / linal / vector.h
index 369e3ff2435bf44e597e9e9750b4dc1a03d1c10b..6dfe6ca0167fb3e1d57e65217003e3d700f6a3b3 100644 (file)
@@ -82,6 +82,9 @@ public:
        template<typename U>
        Vector(const Vector<U, N> &);
 
+       template<unsigned M>
+       Vector<T, M> slice(unsigned) const;
+
        Vector &operator*=(T);
        Vector &operator/=(T);
        Vector &operator+=(const Vector &);
@@ -140,6 +143,47 @@ inline Vector<T, N>::Vector(const Vector<U, N> &v)
                (*this)[i] = v[i];
 }
 
+template<typename T, unsigned N>
+inline Vector<T, N+1> compose(const Vector<T, N> &v, T 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>
+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)
+               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>
 inline Vector<T, N> &Vector<T, N>::operator*=(T s)
 {
@@ -236,6 +280,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));
 }