Added slice function for matrix.

This commit is contained in:
James Goppert
2016-02-16 12:22:08 -05:00
parent 1ae114b3d2
commit 5a01e6c939
4 changed files with 50 additions and 1 deletions
+16
View File
@@ -32,6 +32,8 @@ A simple and efficient template based matrix library.
## Example
See the test directory for detailed examples. Some simple examples are included below:
```c++
// define an euler angle (Body 3(yaw)-2(pitch)-1(roll) rotation)
float roll = 0.1f;
@@ -91,4 +93,18 @@ A simple and efficient template based matrix library.
// correction
x += K*r;
// slicing
float data[9] = {0, 2, 3,
4, 5, 6,
7, 8, 10
};
SquareMatrix<float, 3> A(data);
// Slice a 3,3 matrix starting at row 1, col 0,
// with size 2 x 3, warning, no size checking
Matrix<float, 2, 3> B(A.slice<2, 3>(1, 0));
// this results in:
// 4, 5, 6
// 7, 8, 10
```
+7
View File
@@ -317,6 +317,13 @@ public:
return transpose();
}
template<size_t P, size_t Q>
Matrix<Type, P, Q> slice(size_t x0, size_t y0) const
{
Matrix<Type, P, Q> res(&(_data[x0][y0]));
return res;
}
void setZero()
{
memset(_data, 0, sizeof(_data));
+2 -1
View File
@@ -1,6 +1,7 @@
set(tests
setIdentity
inverse
slice
matrixMult
vectorAssignment
matrixAssignment
@@ -33,4 +34,4 @@ if (${CMAKE_BUILD_TYPE} STREQUAL "Profile")
WORKING_DIRECTORY ${CMAKE_BUILD_DIR}
DEPENDS test_build
)
endif()
endif()
+25
View File
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <matrix/math.hpp>
#include "test_macros.hpp"
using namespace matrix;
int main()
{
float data[9] = {0, 2, 3,
4, 5, 6,
7, 8, 10
};
float data_check[6] = {
4, 5, 6,
7, 8, 10
};
SquareMatrix<float, 3> A(data);
Matrix<float, 2, 3> B_check(data_check);
Matrix<float, 2, 3> B(A.slice<2, 3>(1, 0));
TEST(isEqual(B, B_check));
return 0;
}
/* vim: set et fenc=utf-8 ff=unix sts=0 sw=4 ts=4 : */