You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
1.5 KiB

9 years ago
/**
* @file Vector.hpp
*
* Vector class.
*
* @author James Goppert <james.goppert@gmail.com>
*/
#pragma once
9 years ago
#include <cmath>
#include "math.hpp"
9 years ago
namespace matrix
{
template <typename Type, size_t M, size_t N>
class Matrix;
template<typename Type, size_t M>
class Vector : public Matrix<Type, M, 1>
9 years ago
{
public:
virtual ~Vector() {};
9 years ago
typedef Matrix<Type, M, 1> MatrixM1;
9 years ago
Vector() : MatrixM1()
{
}
9 years ago
Vector(const MatrixM1 & other) :
MatrixM1(other)
{
}
Vector(const Type *data_) :
9 years ago
MatrixM1(data_)
{
}
inline Type operator()(size_t i) const
{
9 years ago
const MatrixM1 &v = *this;
return v(i, 0);
}
inline Type &operator()(size_t i)
{
9 years ago
MatrixM1 &v = *this;
return v(i, 0);
}
9 years ago
Type dot(const MatrixM1 & b) const {
9 years ago
const Vector &a(*this);
Type r = 0;
for (size_t i = 0; i<M; i++) {
9 years ago
r += a(i)*b(i,0);
}
return r;
}
9 years ago
Type norm() const {
const Vector &a(*this);
return Type(sqrt(a.dot(a)));
}
inline void normalize() {
(*this) /= norm();
}
Vector unit() const {
return (*this) / norm();
9 years ago
}
9 years ago
9 years ago
Vector pow(Type v) const {
const Vector &a(*this);
Vector r;
for (size_t i = 0; i<M; i++) {
r(i) = Type(::pow(a(i), v));
}
return r;
}
9 years ago
};
} // namespace matrix
/* vim: set et fenc=utf-8 ff=unix sts=0 sw=4 ts=4 : */