#include <algorithm>
#include <cmath>
+#include <ostream>
namespace Msp {
namespace LinAl {
class Vector: public VectorComponents<T, N>
{
public:
+ typedef T ElementType;
+
Vector();
Vector(const T *);
- Vector(T, T);
- Vector(T, T, T);
- Vector(T, T, T, T);
+
+ /** Constructs a vector from an array of interleaved values. Intended for
+ use by Matrix row accessor. */
+ Vector(const T *, unsigned);
+
+ template<typename... Args>
+ Vector(T, Args...);
+
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);
(*this)[i] = d[i];
}
-/* 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>
-inline Vector<T, N>::Vector(T x_, T y_)
-{
- this->VectorComponents<T, 2>::x = x_;
- this->VectorComponents<T, 2>::y = y_;
-}
-
template<typename T, unsigned N>
-inline Vector<T, N>::Vector(T x_, T y_, T z_)
+inline Vector<T, N>::Vector(const T *d, unsigned stride)
{
- this->VectorComponents<T, 3>::x = x_;
- this->VectorComponents<T, 3>::y = y_;
- this->VectorComponents<T, 3>::z = z_;
+ for(unsigned i=0; i<N; ++i)
+ (*this)[i] = d[i*stride];
}
template<typename T, unsigned N>
-inline Vector<T, N>::Vector(T x_, T y_, T z_, T w_)
+template<typename... Args>
+inline Vector<T, N>::Vector(T x_, Args... v)
{
- this->VectorComponents<T, 4>::x = x_;
- this->VectorComponents<T, 4>::y = y_;
- this->VectorComponents<T, 4>::z = z_;
- this->VectorComponents<T, 4>::w = w_;
+ static_assert(1+sizeof...(v)==N, "Incorrect number of arguments in Vector constructor");
+ (*this)[0] = x_;
+ unsigned i = 1;
+ for(auto c: std::initializer_list<T> { static_cast<T>(v)... })
+ (*this)[i++] = c;
}
template<typename T, unsigned N>
}
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;
+ r[0] = s;
for(unsigned i=0; i<N; ++i)
- (*this)[i] = v[i];
+ r[i+1] = v[i];
+ 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>
template<typename T, unsigned N>
inline T Vector<T, N>::norm() const
{
+ using std::sqrt;
return sqrt(inner_product(*this, *this));
}
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