aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/math/Vector.h
diff options
context:
space:
mode:
Diffstat (limited to 'meowpp/math/Vector.h')
-rw-r--r--meowpp/math/Vector.h89
1 files changed, 40 insertions, 49 deletions
diff --git a/meowpp/math/Vector.h b/meowpp/math/Vector.h
index d387c2b..caa64fd 100644
--- a/meowpp/math/Vector.h
+++ b/meowpp/math/Vector.h
@@ -3,7 +3,6 @@
#include "../Self.h"
#include "Matrix.h"
-#include "utility.h"
#include <vector>
@@ -18,6 +17,9 @@ namespace meow {
*/
template<class Scalar>
class Vector {
+public:
+ typedef typename Matrix<Scalar>::EntryRefK ScalarRefK;
+ typedef typename Matrix<Scalar>::EntryRef ScalarRef ;
private:
Matrix<Scalar> matrix_;
public:
@@ -26,9 +28,9 @@ public:
*
* With \b dimension=0, which means \b invalid.
*/
- Vector(){
+ Vector() {
}
-
+
/*!
* @brief constructor
*
@@ -36,10 +38,9 @@ public:
*
* @param [in] v another vector
*/
- Vector(Vector const& v) {
- matrix_.copyFrom(v.matrix_);
+ Vector(Vector const& v): matrix_(v.matrix_) {
}
-
+
/*!
* @brief constructor
*
@@ -47,22 +48,9 @@ public:
*
* @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));
+ Vector(Matrix<Scalar> const& m): matrix_(m.col(0)) {
}
-
+
/*!
* @brief constructor
*
@@ -70,27 +58,25 @@ public:
*
* @param [in] v vector
*/
- Vector(std::vector<Scalar> const& v) {
- matrix_.size(v.size(), 1, Scalar(0));
+ Vector(std::vector<Scalar> const& v): matrix_(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);
+ Vector(size_t d, Scalar const& e): matrix_(d, 1, e) {
}
//! @brief destructor
- ~Vector(){
+ ~Vector() {
}
//! @brief copy from ...
@@ -114,7 +100,7 @@ public:
size_t dimension() const {
return matrix_.rows();
}
-
+
/*!
* @brief resize the dimension
*
@@ -126,7 +112,7 @@ public:
matrix_.rows(d, s);
return dimension();
}
-
+
/*!
* @brief Return whether \c dimension>0 is true or not
* @return \c true/false
@@ -134,12 +120,12 @@ public:
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
*
@@ -151,6 +137,11 @@ public:
return entry(i);
}
+ //! @brief return \a i -th entry with non-constant type
+ ScalarRef entryGet(size_t i) {
+ return matrix_.entryGet(i);
+ }
+
/*!
* @brief change \a i -th to \a j -th entries
*
@@ -159,11 +150,11 @@ public:
* @param [in] s new value
*/
void entries(size_t i, size_t j, Scalar const& s) {
- for (size_t it = i; it <= j; it++) {
+ 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));
@@ -173,7 +164,7 @@ public:
Vector positive() const {
return *this;
}
-
+
//! @brief return -\a (*this)
Vector negative() const {
return Vector(matrix_.negative());
@@ -183,7 +174,7 @@ public:
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_));
@@ -193,7 +184,7 @@ public:
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));
@@ -208,17 +199,17 @@ public:
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());
@@ -229,48 +220,48 @@ public:
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);
}
};
-}
+} // meow
#endif // math_Vector_H__