aboutsummaryrefslogtreecommitdiff
path: root/apps/systemlib/math/Matrix.cpp
diff options
context:
space:
mode:
authorjgoppert <james.goppert@gmail.com>2013-01-06 15:33:55 -0500
committerjgoppert <james.goppert@gmail.com>2013-01-06 15:33:55 -0500
commitd9491b20cc5fc8b683eb0f60a50da6b322b55e57 (patch)
tree48d44accf2ccff88766bca351c78be06bb9fa4a0 /apps/systemlib/math/Matrix.cpp
parent4f3b17f53b120cd54112097f4217a90863013c1f (diff)
downloadpx4-firmware-d9491b20cc5fc8b683eb0f60a50da6b322b55e57.tar.gz
px4-firmware-d9491b20cc5fc8b683eb0f60a50da6b322b55e57.tar.bz2
px4-firmware-d9491b20cc5fc8b683eb0f60a50da6b322b55e57.zip
Reformat of math library with astyle.
Diffstat (limited to 'apps/systemlib/math/Matrix.cpp')
-rw-r--r--apps/systemlib/math/Matrix.cpp223
1 files changed, 118 insertions, 105 deletions
diff --git a/apps/systemlib/math/Matrix.cpp b/apps/systemlib/math/Matrix.cpp
index 08796da89..5e536c3a8 100644
--- a/apps/systemlib/math/Matrix.cpp
+++ b/apps/systemlib/math/Matrix.cpp
@@ -45,136 +45,149 @@
namespace math
{
-static const float data_testA[] =
- {1,2,3,
- 4,5,6};
-static Matrix testA(2,3,data_testA);
-
-static const float data_testB[] =
- {0,1,3,
- 7,-1,2};
-static Matrix testB(2,3,data_testB);
-
-static const float data_testC[] =
- {0,1,
- 2,1,
- 3,2};
-static Matrix testC(3,2,data_testC);
-
-static const float data_testD[] =
- {0,1,2,
- 2,1,4,
- 5,2,0};
-static Matrix testD(3,3,data_testD);
-
-static const float data_testE[] =
- {1,-1,2,
- 0,2,3,
- 2,-1,1};
-static Matrix testE(3,3,data_testE);
-
-static const float data_testF[] =
- {3.777e006f, 2.915e007f, 0.000e000f,
- 2.938e007f, 2.267e008f, 0.000e000f,
- 0.000e000f, 0.000e000f, 6.033e008f};
-static Matrix testF(3,3,data_testF);
+static const float data_testA[] = {
+ 1, 2, 3,
+ 4, 5, 6
+};
+static Matrix testA(2, 3, data_testA);
+
+static const float data_testB[] = {
+ 0, 1, 3,
+ 7, -1, 2
+};
+static Matrix testB(2, 3, data_testB);
+
+static const float data_testC[] = {
+ 0, 1,
+ 2, 1,
+ 3, 2
+};
+static Matrix testC(3, 2, data_testC);
+
+static const float data_testD[] = {
+ 0, 1, 2,
+ 2, 1, 4,
+ 5, 2, 0
+};
+static Matrix testD(3, 3, data_testD);
+
+static const float data_testE[] = {
+ 1, -1, 2,
+ 0, 2, 3,
+ 2, -1, 1
+};
+static Matrix testE(3, 3, data_testE);
+
+static const float data_testF[] = {
+ 3.777e006f, 2.915e007f, 0.000e000f,
+ 2.938e007f, 2.267e008f, 0.000e000f,
+ 0.000e000f, 0.000e000f, 6.033e008f
+};
+static Matrix testF(3, 3, data_testF);
int __EXPORT matrixTest()
{
- matrixAddTest();
- matrixSubTest();
- matrixMultTest();
- matrixInvTest();
- matrixDivTest();
- return 0;
+ matrixAddTest();
+ matrixSubTest();
+ matrixMultTest();
+ matrixInvTest();
+ matrixDivTest();
+ return 0;
}
int matrixAddTest()
{
- printf("Test Matrix Add\t\t: ");
- Matrix r = testA + testB;
- float data_test[] =
- { 1.0f, 3.0f, 6.0f,
- 11.0f, 4.0f, 8.0f};
- ASSERT(matrixEqual(Matrix(2,3,data_test),r));
- printf("PASS\n");
- return 0;
+ printf("Test Matrix Add\t\t: ");
+ Matrix r = testA + testB;
+ float data_test[] = {
+ 1.0f, 3.0f, 6.0f,
+ 11.0f, 4.0f, 8.0f
+ };
+ ASSERT(matrixEqual(Matrix(2, 3, data_test), r));
+ printf("PASS\n");
+ return 0;
}
int matrixSubTest()
{
- printf("Test Matrix Sub\t\t: ");
- Matrix r = testA - testB;
- float data_test[] =
- { 1.0f, 1.0f, 0.0f,
- -3.0f, 6.0f, 4.0f};
- ASSERT(matrixEqual(Matrix(2,3,data_test),r));
- printf("PASS\n");
- return 0;
+ printf("Test Matrix Sub\t\t: ");
+ Matrix r = testA - testB;
+ float data_test[] = {
+ 1.0f, 1.0f, 0.0f,
+ -3.0f, 6.0f, 4.0f
+ };
+ ASSERT(matrixEqual(Matrix(2, 3, data_test), r));
+ printf("PASS\n");
+ return 0;
}
int matrixMultTest()
{
- printf("Test Matrix Mult\t: ");
- Matrix r = testC * testB;
- float data_test[] =
- { 7.0f, -1.0f, 2.0f,
- 7.0f, 1.0f, 8.0f,
- 14.0f, 1.0f, 13.0f};
- ASSERT(matrixEqual(Matrix(3,3,data_test),r));
- printf("PASS\n");
- return 0;
+ printf("Test Matrix Mult\t: ");
+ Matrix r = testC * testB;
+ float data_test[] = {
+ 7.0f, -1.0f, 2.0f,
+ 7.0f, 1.0f, 8.0f,
+ 14.0f, 1.0f, 13.0f
+ };
+ ASSERT(matrixEqual(Matrix(3, 3, data_test), r));
+ printf("PASS\n");
+ return 0;
}
int matrixInvTest()
{
- printf("Test Matrix Inv\t\t: ");
- Matrix origF = testF;
- Matrix r = testF.inverse();
- float data_test[] =
- { -0.0012518f, 0.0001610f, 0.0000000f,
- 0.0001622f, -0.0000209f, 0.0000000f,
- 0.0000000f, 0.0000000f, 1.6580e-9f};
- ASSERT(matrixEqual(Matrix(3,3,data_test),r));
- // make sure F in unchanged
- ASSERT(matrixEqual(origF,testF));
- printf("PASS\n");
- return 0;
+ printf("Test Matrix Inv\t\t: ");
+ Matrix origF = testF;
+ Matrix r = testF.inverse();
+ float data_test[] = {
+ -0.0012518f, 0.0001610f, 0.0000000f,
+ 0.0001622f, -0.0000209f, 0.0000000f,
+ 0.0000000f, 0.0000000f, 1.6580e-9f
+ };
+ ASSERT(matrixEqual(Matrix(3, 3, data_test), r));
+ // make sure F in unchanged
+ ASSERT(matrixEqual(origF, testF));
+ printf("PASS\n");
+ return 0;
}
int matrixDivTest()
{
- printf("Test Matrix Div\t\t: ");
- Matrix r = testD / testE;
- float data_test[] = {
- 0.2222222f, 0.5555556f, -0.1111111f,
- 0.0f, 1.0f, 1.0,
- -4.1111111f, 1.2222222f, 4.5555556f};
- ASSERT(matrixEqual(Matrix(3,3,data_test),r));
- printf("PASS\n");
- return 0;
+ printf("Test Matrix Div\t\t: ");
+ Matrix r = testD / testE;
+ float data_test[] = {
+ 0.2222222f, 0.5555556f, -0.1111111f,
+ 0.0f, 1.0f, 1.0,
+ -4.1111111f, 1.2222222f, 4.5555556f
+ };
+ ASSERT(matrixEqual(Matrix(3, 3, data_test), r));
+ printf("PASS\n");
+ return 0;
}
-bool matrixEqual(const Matrix & a, const Matrix & b, float eps)
+bool matrixEqual(const Matrix &a, const Matrix &b, float eps)
{
- if (a.getRows() != b.getRows()) {
- printf("row number not equal a: %d, b:%d\n", a.getRows(), b.getRows());
- return false;
- } else if (a.getCols() != b.getCols()) {
- printf("column number not equal a: %d, b:%d\n", a.getCols(), b.getCols());
- return false;
- }
- bool ret = true;
- for (size_t i=0;i<a.getRows();i++)
- for (size_t j =0;j<a.getCols();j++)
- {
- if (!equal(a(i,j),b(i,j),eps))
- {
- printf("element mismatch (%d, %d)\n", i, j);
- ret = false;
- }
- }
- return ret;
+ if (a.getRows() != b.getRows()) {
+ printf("row number not equal a: %d, b:%d\n", a.getRows(), b.getRows());
+ return false;
+
+ } else if (a.getCols() != b.getCols()) {
+ printf("column number not equal a: %d, b:%d\n", a.getCols(), b.getCols());
+ return false;
+ }
+
+ bool ret = true;
+
+ for (size_t i = 0; i < a.getRows(); i++)
+ for (size_t j = 0; j < a.getCols(); j++) {
+ if (!equal(a(i, j), b(i, j), eps)) {
+ printf("element mismatch (%d, %d)\n", i, j);
+ ret = false;
+ }
+ }
+
+ return ret;
}
-
+
} // namespace math