diff options
Diffstat (limited to 'meowpp/math/Vector.h')
-rw-r--r-- | meowpp/math/Vector.h | 276 |
1 files changed, 276 insertions, 0 deletions
diff --git a/meowpp/math/Vector.h b/meowpp/math/Vector.h new file mode 100644 index 0000000..d387c2b --- /dev/null +++ b/meowpp/math/Vector.h @@ -0,0 +1,276 @@ +#ifndef math_Vector_H__ +#define math_Vector_H__ + +#include "../Self.h" +#include "Matrix.h" +#include "utility.h" + +#include <vector> + +#include <cmath> + +namespace meow { + +/*! + * @brief \b vector + * + * @author cat_leopard + */ +template<class Scalar> +class Vector { +private: + Matrix<Scalar> matrix_; +public: + /*! + * @brief constructor + * + * With \b dimension=0, which means \b invalid. + */ + Vector(){ + } + + /*! + * @brief constructor + * + * Copy from another vector + * + * @param [in] v another vector + */ + Vector(Vector const& v) { + matrix_.copyFrom(v.matrix_); + } + + /*! + * @brief constructor + * + * From matrix's first column + * + * @param [in] m matrix + */ + Vector(Matrix<Scalar> const& m) { + matrix_.copyFrom(m.col(0)); + } + + /*! + * @brief constructor + * + * From matrix's \a i-th column + * + * @param [in] m matrix + * @param [in] i i-th + */ + Vector(Matrix<Scalar> const& m, size_t i) { + matrix_.copyFrom(m.col(i)); + } + + /*! + * @brief constructor + * + * Copy from another std::vector + * + * @param [in] v vector + */ + Vector(std::vector<Scalar> const& v) { + matrix_.size(v.size(), 1, Scalar(0)); + for (size_t i = 0, I = v.size(); i < I; i++) { + matrix_.entry(i, 0, v[i]); + } + } + + /*! + * @brief constructor + * + * setup dimension and inital value + * + * @param [in] d dimension + * @param [in] e inital value + */ + Vector(size_t d, Scalar const& e) { + matrix_.reset(d, 1, e); + } + + //! @brief destructor + ~Vector(){ + } + + //! @brief copy from ... + Vector& copyFrom(Vector const& v) { + matrix_.copyFrom(v.matrix_); + return *this; + } + + //! @brief reference from ... + Vector& referenceFrom(Vector const& v) { + matrix_.referenceFrom(v.matrix_); + return *this; + } + + //! @brief Return a \a dimension x 1 matrix form of it + Matrix<Scalar> const& matrix() const { + return matrix_; + } + + //! @brief return dimension + size_t dimension() const { + return matrix_.rows(); + } + + /*! + * @brief resize the dimension + * + * @param [in] d new dimension + * @param [in] s inital entry + * @return new dimension + */ + size_t dimension(size_t d, Scalar const& s) { + matrix_.rows(d, s); + return dimension(); + } + + /*! + * @brief Return whether \c dimension>0 is true or not + * @return \c true/false + */ + bool valid() const { + return (dimension() > 0); + } + + //! @brief return \a i -th entry + Scalar entry(size_t i) const { + return matrix_.entry(i, 0); + } + + /*! + * @brief change \a i -th entry + * + * @param [in] i i-th + * @param [in] s new value + */ + Scalar entry(size_t i, Scalar const& s) { + matrix_.entry(i, 0, s); + return entry(i); + } + + /*! + * @brief change \a i -th to \a j -th entries + * + * @param [in] i i-th + * @param [in] j j-th + * @param [in] s new value + */ + void entries(size_t i, size_t j, Scalar const& s) { + for (size_t it = i; it <= j; it++) { + matrix_.entry(it, 0, s); + } + } + + //! @brief subvector form i-th to j-th + Vector subVector(size_t i, size_t j) { + return Vector(matrix_.subMatrix(i, 0, j, 0)); + } + + //! @brief return +\a (*this) + Vector positive() const { + return *this; + } + + //! @brief return -\a (*this) + Vector negative() const { + return Vector(matrix_.negative()); + } + + //! @brief return \a (*this)+v + Vector add(Vector const& v) const { + return Vector(matrix_.add(v.matrix_)); + } + + //! @brief return \a (*this)-v + Vector sub(Vector const& v) const { + return Vector(matrix_.sub(v.matrix_)); + } + + //! @brief return \a (*this)*s , where s is a scalar + Vector mul(Scalar const& s) const { + return Vector(matrix_.mul(s)); + } + + //! @brief return \a (*this)/s , where s is a scalar + Vector div(Scalar const& s) const { + return Vector(matrix_.div(s)); + } + + //! @brief dot + Scalar dot(Vector const& v) const { + return matrix_.transpose().mul(v.matrix_).entry(0, 0); + } + + //! @brief sqrt of \a length2 + Scalar length() const { + return Scalar(sqrt((double)length2())); + } + + //! @brief same as \a (*this).dot(*this) + Scalar length2() const { + return dot(*this); + } + + //! @brief return a normalize form of itself + Vector normalize() const { + return div(length()); + } + + //! @brief Let itself be normalize form + Vector& normalized() { + copyFrom(normalize()); + return *this; + } + + //! @brief same as copyFrom + Vector& operator=(Vector const& v) { + return copyFrom(v); + } + + //! @brief same as entry(i) + Scalar operator()(size_t i) const { + return entry(i); + } + + //! @brief same as positive() + Vector operator+() const { + return positive(); + } + + //! @brief same as negative() + Vector operator-() const { + return negative(); + } + + //! @brief same as add(v) + Vector operator+(Vector const& v) const { + return add(v); + } + + //! @brief same as sub(v) + Vector operator-(Vector const& v) const { + return sub(v); + } + + //! @brief same as dot(v) + Scalar operator*(Vector const& v) const { + return dot(v); + } + + //! @brief same as mul(s) + Vector operator*(Scalar const& s) const { + return mul(s); + } + + //! @brief same as div(s) + Vector operator/(Scalar const& s) const { + return div(s); + } +}; + +} + +#endif // math_Vector_H__ |