diff --git a/CMakeLists.txt b/CMakeLists.txt index fecbf88934..f22119ba5b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,7 +37,7 @@ set(CMAKE_CXX_FLAGS -Wcast-qual -Wconversion -Wcast-align - #-Werror + -Werror ) if (COVERAGE) diff --git a/matrix/Matrix.hpp b/matrix/Matrix.hpp index 2f10bcf46a..2e32fc29b2 100644 --- a/matrix/Matrix.hpp +++ b/matrix/Matrix.hpp @@ -152,6 +152,21 @@ public: return res; } + // unary minus + Matrix operator-() const + { + Matrix res; + const Matrix &self = *this; + + for (size_t i = 0; i < M; i++) { + for (size_t j = 0; j < N; j++) { + res(i , j) = -self(i, j); + } + } + + return res; + } + void operator+=(const Matrix &other) { Matrix &self = *this; @@ -287,6 +302,22 @@ public: memset(_data, 0, sizeof(_data)); } + void setAll(Type val) + { + Matrix &self = *this; + + for (size_t i = 0; i < M; i++) { + for (size_t j = 0; j < N; j++) { + self(i, j) = val; + } + } + } + + inline void setOne() + { + setAll(1); + } + void setIdentity() { setZero(); @@ -369,12 +400,19 @@ public: }; template -Matrix zero() { +Matrix zeros() { Matrix m; m.setZero(); return m; } +template +Matrix ones() { + Matrix m; + m.setOne(); + return m; +} + typedef Matrix Matrix3f; }; // namespace matrix diff --git a/matrix/Scalar.hpp b/matrix/Scalar.hpp index 628e8b10e7..022ab5f7de 100644 --- a/matrix/Scalar.hpp +++ b/matrix/Scalar.hpp @@ -20,24 +20,31 @@ namespace matrix { template -class Scalar : public Matrix +class Scalar { public: virtual ~Scalar() {}; - Scalar() : Matrix() + Scalar() : _value() { } Scalar(const Matrix & other) { - (*this)(0,0) = other(0,0); + _value = other(0,0); + } + + Scalar(float other) + { + _value = other; } operator Type() { - return (*this)(0,0); + return _value; } +private: + Type _value; }; diff --git a/matrix/Vector.hpp b/matrix/Vector.hpp index fb771ecc18..4c75ce5694 100644 --- a/matrix/Vector.hpp +++ b/matrix/Vector.hpp @@ -67,9 +67,6 @@ public: (*this) /= norm(); } - Type operator*(const MatrixM1 & b) const { - return (*this).dot(b); - } }; }; // namespace matrix diff --git a/matrix/integration.hpp b/matrix/integration.hpp new file mode 100644 index 0000000000..61f7574cca --- /dev/null +++ b/matrix/integration.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "math.hpp" + +namespace matrix { + +template +int integrate_rk4( + Vector (*f)(Type, Vector), + Vector & y, + Type & t, + Type h + ) +{ + // https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods + Vector k1, k2, k3, k4; + k1 = f(t, y); + k2 = f(t + h/2, y + k1*h/2); + k3 = f(t + h/2, y + k2*h/2); + k4 = f(t + h, y + k3*h); + y += (k1 + k2*2 + k3*2 + k4)*(h/6); + t += h; + return 0; +} + +}; // namespace matrix diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c7ae87b675..5f4557dd83 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -11,6 +11,7 @@ set(tests vector3 attitude filter + integration squareMatrix ) diff --git a/test/attitude.cpp b/test/attitude.cpp index 88faeaff69..8449287df5 100644 --- a/test/attitude.cpp +++ b/test/attitude.cpp @@ -29,7 +29,7 @@ int main() // euler default ctor Eulerf e; - Eulerf e_zero = zero(); + Eulerf e_zero = zeros(); assert(e == e_zero); assert(e == e); diff --git a/test/integration.cpp b/test/integration.cpp new file mode 100644 index 0000000000..e8b3889420 --- /dev/null +++ b/test/integration.cpp @@ -0,0 +1,26 @@ +#include +#include + +#include + +using namespace matrix; + +Vector f(float t, Vector y); + +Vector f(float t, Vector y) { + return ones(); +} + +int main() +{ + Vector y = ones(); + float h = 1; + float t = 1; + y.T().print(); + integrate_rk4(f, y, t, h); + y.T().print(); + assert(y == (ones()*2)); + return 0; +} + +/* vim: set et fenc=utf-8 ff=unix sts=0 sw=4 ts=4 : */ diff --git a/test/inverse.cpp b/test/inverse.cpp index d00fcd6cf2..743d877a9f 100644 --- a/test/inverse.cpp +++ b/test/inverse.cpp @@ -40,7 +40,7 @@ int main() assert(A_large == A_large_I); } - SquareMatrix zero_test = zero(); + SquareMatrix zero_test = zeros(); inv(zero_test); return 0; diff --git a/test/matrixAssignment.cpp b/test/matrixAssignment.cpp index 86c05527e5..cc1ad6945a 100644 --- a/test/matrixAssignment.cpp +++ b/test/matrixAssignment.cpp @@ -65,6 +65,7 @@ int main() Matrix3f m4(data); + assert(-m4 == m4*(-1)); m4.swapCols(0, 2); assert(m4 == Matrix3f(data_col_02_swap)); @@ -72,6 +73,11 @@ int main() m4.swapRows(0, 2); assert(m4 == Matrix3f(data_row_02_swap)); assert(fabs(m4.min() - 1) < 1e-5); + + Scalar s; + s = 1; + assert(fabs(s - 1) < 1e-5); + return 0; } diff --git a/test/vector.cpp b/test/vector.cpp index c340997f11..053e22dec8 100644 --- a/test/vector.cpp +++ b/test/vector.cpp @@ -15,7 +15,6 @@ int main() assert(fabs(v1.norm() - 7.416198487095663f) < 1e-5); Vector v2(data2); assert(fabs(v1.dot(v2) - 130.0f) < 1e-5); - assert(fabs(v1*v2 - 130.0f) < 1e-5); v2.normalize(); Vector v3(v2); assert(v2 == v3);