]> git.tdb.fi Git - libs/math.git/blobdiff - source/linal/vector.h
Add formatted output operators for vector and matrix classes
[libs/math.git] / source / linal / vector.h
index 152c26e66482e943e5837c5ddecc54fc9e7c2fde..c9b2a179fe5fd0ce595e4dda796b3c2d7b13ab5d 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <algorithm>
 #include <cmath>
+#include <ostream>
 
 namespace Msp {
 namespace LinAl {
@@ -78,6 +79,11 @@ public:
 
        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);
@@ -113,6 +119,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>
@@ -313,6 +326,20 @@ inline Vector<T, 3> cross(const Vector<T, 3> &v1, const Vector<T, 3> &v2)
        return Vector<T, 3>(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<typename T, unsigned N>
+inline std::ostream &operator<<(std::ostream &s, const Vector<T, N> &v)
+{
+       s << "Vector" << N << '(';
+       for(unsigned i=0; i<N; ++i)
+       {
+               if(i)
+                       s << ", ";
+               s << v[i];
+       }
+       s << ')';
+       return s;
+}
+
 } // namespace LinAl
 } // namespace Msp