new feature p-norm

This commit is contained in:
Marc Wäckerlin
2017-01-10 10:31:58 +00:00
parent 1e4898d396
commit 3c2c78367f
3 changed files with 194 additions and 52 deletions

View File

@@ -199,6 +199,21 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
Matrix<T,3,3> o(m);
CPPUNIT_ASSERT_EQUAL((T)3*o, m.apply([](T& t){t*=3;}));
}
template<typename T>
void norm() {
Matrix<T,3,3> m1(1, -1, 1,
-1, 1, -1,
1, 1, -1);
Matrix<T,3,3> m2(-1, 3, 2,
-2, 0, -3,
2, 1, -2);
Matrix<T,3,3> m3(0, 3, 0,
-1, 0, 2,
0, -3, 1);
CPPUNIT_ASSERT_DOUBLES_EQUAL(3.0, m1.norm(), 0.00001);
CPPUNIT_ASSERT_DOUBLES_EQUAL(6.0, m2.norm(), 0.00001);
CPPUNIT_ASSERT_DOUBLES_EQUAL(4.0, m3.norm(3), 0.00001);
}
template<typename T>
void gauss() {
Matrix<T,3,3> m(2, -2, 4,
@@ -332,92 +347,113 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
CPPUNIT_TEST(initFromArray1<unsigned long>);
CPPUNIT_TEST(initFromArray1<float>);
CPPUNIT_TEST(initFromArray1<double>);
CPPUNIT_TEST(initFromArray1<long double>);
CPPUNIT_TEST(initFromArray2<int>);
CPPUNIT_TEST(initFromArray2<long>);
CPPUNIT_TEST(initFromArray2<unsigned>);
CPPUNIT_TEST(initFromArray2<unsigned long>);
CPPUNIT_TEST(initFromArray2<float>);
CPPUNIT_TEST(initFromArray2<double>);
CPPUNIT_TEST(initFromArray2<long double>);
CPPUNIT_TEST(initFromArray3<int>);
CPPUNIT_TEST(initFromArray3<long>);
CPPUNIT_TEST(initFromArray3<unsigned>);
CPPUNIT_TEST(initFromArray3<unsigned long>);
CPPUNIT_TEST(initFromArray3<float>);
CPPUNIT_TEST(initFromArray3<double>);
CPPUNIT_TEST(initFromArray3<long double>);
CPPUNIT_TEST(initFromOtherMatrix<int>);
CPPUNIT_TEST(initFromOtherMatrix<long>);
CPPUNIT_TEST(initFromOtherMatrix<unsigned>);
CPPUNIT_TEST(initFromOtherMatrix<unsigned long>);
CPPUNIT_TEST(initFromOtherMatrix<float>);
CPPUNIT_TEST(initFromOtherMatrix<double>);
CPPUNIT_TEST(initFromOtherMatrix<long double>);
CPPUNIT_TEST(access<int>);
CPPUNIT_TEST(access<long>);
CPPUNIT_TEST(access<unsigned>);
CPPUNIT_TEST(access<unsigned long>);
CPPUNIT_TEST(access<float>);
CPPUNIT_TEST(access<double>);
CPPUNIT_TEST(access<long double>);
CPPUNIT_TEST(equality<int>);
CPPUNIT_TEST(equality<long>);
CPPUNIT_TEST(equality<unsigned>);
CPPUNIT_TEST(equality<unsigned long>);
CPPUNIT_TEST(equality<float>);
CPPUNIT_TEST(equality<double>);
CPPUNIT_TEST(equality<long double>);
CPPUNIT_TEST(operator_plus<int>);
CPPUNIT_TEST(operator_plus<long>);
CPPUNIT_TEST(operator_plus<unsigned>);
CPPUNIT_TEST(operator_plus<unsigned long>);
CPPUNIT_TEST(operator_plus<float>);
CPPUNIT_TEST(operator_plus<double>);
CPPUNIT_TEST(operator_plus<long double>);
CPPUNIT_TEST(operator_minus<int>);
CPPUNIT_TEST(operator_minus<long>);
CPPUNIT_TEST(operator_minus<unsigned>);
CPPUNIT_TEST(operator_minus<unsigned long>);
CPPUNIT_TEST(operator_minus<float>);
CPPUNIT_TEST(operator_minus<double>);
CPPUNIT_TEST(operator_minus<long double>);
CPPUNIT_TEST(scalar_mult<int>);
CPPUNIT_TEST(scalar_mult<long>);
CPPUNIT_TEST(scalar_mult<unsigned>);
CPPUNIT_TEST(scalar_mult<unsigned long>);
CPPUNIT_TEST(scalar_mult<float>);
CPPUNIT_TEST(scalar_mult<double>);
CPPUNIT_TEST(scalar_mult<long double>);
CPPUNIT_TEST(scalar_div<int>);
CPPUNIT_TEST(scalar_div<long>);
CPPUNIT_TEST(scalar_div<unsigned>);
CPPUNIT_TEST(scalar_div<unsigned long>);
CPPUNIT_TEST(scalar_div<float>);
CPPUNIT_TEST(scalar_div<double>);
CPPUNIT_TEST(scalar_div<long double>);
CPPUNIT_TEST(matrix_mult<int>);
CPPUNIT_TEST(matrix_mult<long>);
CPPUNIT_TEST(matrix_mult<unsigned>);
CPPUNIT_TEST(matrix_mult<unsigned long>);
CPPUNIT_TEST(matrix_mult<float>);
CPPUNIT_TEST(matrix_mult<double>);
CPPUNIT_TEST(matrix_mult<long double>);
CPPUNIT_TEST(transpose<int>);
CPPUNIT_TEST(transpose<long>);
CPPUNIT_TEST(transpose<unsigned>);
CPPUNIT_TEST(transpose<unsigned long>);
CPPUNIT_TEST(transpose<float>);
CPPUNIT_TEST(transpose<double>);
CPPUNIT_TEST(transpose<long double>);
CPPUNIT_TEST(apply<int>);
CPPUNIT_TEST(apply<long>);
CPPUNIT_TEST(apply<unsigned>);
CPPUNIT_TEST(apply<unsigned long>);
CPPUNIT_TEST(apply<float>);
CPPUNIT_TEST(apply<double>);
CPPUNIT_TEST(apply<long double>);
CPPUNIT_TEST(norm<int>);
CPPUNIT_TEST(norm<long>);
CPPUNIT_TEST(norm<float>);
CPPUNIT_TEST(norm<double>);
CPPUNIT_TEST(norm<long double>);
CPPUNIT_TEST(gauss<int>);
CPPUNIT_TEST(gauss<long>);
CPPUNIT_TEST(gauss<float>);
CPPUNIT_TEST(gauss<double>);
CPPUNIT_TEST(gauss<long double>);
CPPUNIT_TEST(det<int>);
CPPUNIT_TEST(det<long>);
CPPUNIT_TEST(det<float>);
CPPUNIT_TEST(det<double>);
CPPUNIT_TEST(det<long double>);
CPPUNIT_TEST(i<int>);
CPPUNIT_TEST(i<long>);
CPPUNIT_TEST(i<unsigned>);
CPPUNIT_TEST(i<unsigned long>);
CPPUNIT_TEST(i<float>);
CPPUNIT_TEST(i<double>);
CPPUNIT_TEST(i<long double>);
CPPUNIT_TEST(inv<float>);
CPPUNIT_TEST(inv<double>);
CPPUNIT_TEST(inv<long double>);
@@ -427,6 +463,7 @@ class TemplateMatrixTest: public CppUnit::TestFixture {
CPPUNIT_TEST(stream<unsigned long>);
CPPUNIT_TEST(stream<float>);
CPPUNIT_TEST(stream<double>);
CPPUNIT_TEST(stream<long double>);
CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION(TemplateMatrixTest);
@@ -626,6 +663,24 @@ class VariableMatrixTest: public CppUnit::TestFixture {
Matrix<T> o(m);
CPPUNIT_ASSERT_EQUAL((T)3*o, m.apply([](T& t){t*=3;}));
}
template<typename T>
void norm() {
Matrix<T> m1(3, 3,
1, -1, 1,
-1, 1, -1,
1, 1, -1);
Matrix<T> m2(3, 3,
-1, 3, 2,
-2, 0, -3,
2, 1, -2);
Matrix<T> m3(3, 3,
0, 3, 0,
-1, 0, 2,
0, -3, 1);
CPPUNIT_ASSERT_DOUBLES_EQUAL(3.0, m1.norm(), 0.00001);
CPPUNIT_ASSERT_DOUBLES_EQUAL(6.0, m2.norm(), 0.00001);
CPPUNIT_ASSERT_DOUBLES_EQUAL(4.0, m3.norm(3), 0.00001);
}
template<typename T>
void gauss() {
Matrix<T> m(3, 3,
@@ -763,48 +818,56 @@ class VariableMatrixTest: public CppUnit::TestFixture {
CPPUNIT_TEST(initFromArray1<unsigned long>);
CPPUNIT_TEST(initFromArray1<float>);
CPPUNIT_TEST(initFromArray1<double>);
CPPUNIT_TEST(initFromArray1<long double>);
CPPUNIT_TEST(initFromArray2<int>);
CPPUNIT_TEST(initFromArray2<long>);
CPPUNIT_TEST(initFromArray2<unsigned>);
CPPUNIT_TEST(initFromArray2<unsigned long>);
CPPUNIT_TEST(initFromArray2<float>);
CPPUNIT_TEST(initFromArray2<double>);
CPPUNIT_TEST(initFromArray2<long double>);
CPPUNIT_TEST(initFromOtherMatrix<int>);
CPPUNIT_TEST(initFromOtherMatrix<long>);
CPPUNIT_TEST(initFromOtherMatrix<unsigned>);
CPPUNIT_TEST(initFromOtherMatrix<unsigned long>);
CPPUNIT_TEST(initFromOtherMatrix<float>);
CPPUNIT_TEST(initFromOtherMatrix<double>);
CPPUNIT_TEST(initFromOtherMatrix<long double>);
CPPUNIT_TEST(access<int>);
CPPUNIT_TEST(access<long>);
CPPUNIT_TEST(access<unsigned>);
CPPUNIT_TEST(access<unsigned long>);
CPPUNIT_TEST(access<float>);
CPPUNIT_TEST(access<double>);
CPPUNIT_TEST(access<long double>);
CPPUNIT_TEST(equality<int>);
CPPUNIT_TEST(equality<long>);
CPPUNIT_TEST(equality<unsigned>);
CPPUNIT_TEST(equality<unsigned long>);
CPPUNIT_TEST(equality<float>);
CPPUNIT_TEST(equality<double>);
CPPUNIT_TEST(equality<long double>);
CPPUNIT_TEST(operator_plus<int>);
CPPUNIT_TEST(operator_plus<long>);
CPPUNIT_TEST(operator_plus<unsigned>);
CPPUNIT_TEST(operator_plus<unsigned long>);
CPPUNIT_TEST(operator_plus<float>);
CPPUNIT_TEST(operator_plus<double>);
CPPUNIT_TEST(operator_plus<long double>);
CPPUNIT_TEST(operator_minus<int>);
CPPUNIT_TEST(operator_minus<long>);
CPPUNIT_TEST(operator_minus<unsigned>);
CPPUNIT_TEST(operator_minus<unsigned long>);
CPPUNIT_TEST(operator_minus<float>);
CPPUNIT_TEST(operator_minus<double>);
CPPUNIT_TEST(operator_minus<long double>);
CPPUNIT_TEST(scalar_mult<int>);
CPPUNIT_TEST(scalar_mult<long>);
CPPUNIT_TEST(scalar_mult<unsigned>);
CPPUNIT_TEST(scalar_mult<unsigned long>);
CPPUNIT_TEST(scalar_mult<float>);
CPPUNIT_TEST(scalar_mult<double>);
CPPUNIT_TEST(scalar_mult<long double>);
CPPUNIT_TEST(scalar_mult<int>);
CPPUNIT_TEST(scalar_div<int>);
CPPUNIT_TEST(scalar_div<long>);
@@ -812,38 +875,50 @@ class VariableMatrixTest: public CppUnit::TestFixture {
CPPUNIT_TEST(scalar_div<unsigned long>);
CPPUNIT_TEST(scalar_div<float>);
CPPUNIT_TEST(scalar_div<double>);
CPPUNIT_TEST(scalar_div<long double>);
CPPUNIT_TEST(matrix_mult<int>);
CPPUNIT_TEST(matrix_mult<long>);
CPPUNIT_TEST(matrix_mult<unsigned>);
CPPUNIT_TEST(matrix_mult<unsigned long>);
CPPUNIT_TEST(matrix_mult<float>);
CPPUNIT_TEST(matrix_mult<double>);
CPPUNIT_TEST(matrix_mult<long double>);
CPPUNIT_TEST(transpose<int>);
CPPUNIT_TEST(transpose<long>);
CPPUNIT_TEST(transpose<unsigned>);
CPPUNIT_TEST(transpose<unsigned long>);
CPPUNIT_TEST(transpose<float>);
CPPUNIT_TEST(transpose<double>);
CPPUNIT_TEST(transpose<long double>);
CPPUNIT_TEST(apply<int>);
CPPUNIT_TEST(apply<long>);
CPPUNIT_TEST(apply<unsigned>);
CPPUNIT_TEST(apply<unsigned long>);
CPPUNIT_TEST(apply<float>);
CPPUNIT_TEST(apply<double>);
CPPUNIT_TEST(apply<long double>);
CPPUNIT_TEST(norm<int>);
CPPUNIT_TEST(norm<long>);
CPPUNIT_TEST(norm<float>);
CPPUNIT_TEST(norm<double>);
CPPUNIT_TEST(norm<long double>);
CPPUNIT_TEST(gauss<int>);
CPPUNIT_TEST(gauss<long>);
CPPUNIT_TEST(gauss<float>);
CPPUNIT_TEST(gauss<double>);
CPPUNIT_TEST(gauss<long double>);
CPPUNIT_TEST(det<int>);
CPPUNIT_TEST(det<long>);
CPPUNIT_TEST(det<float>);
CPPUNIT_TEST(det<double>);
CPPUNIT_TEST(det<long double>);
CPPUNIT_TEST(i<int>);
CPPUNIT_TEST(i<long>);
CPPUNIT_TEST(i<unsigned>);
CPPUNIT_TEST(i<unsigned long>);
CPPUNIT_TEST(i<float>);
CPPUNIT_TEST(i<double>);
CPPUNIT_TEST(i<long double>);
CPPUNIT_TEST(inv<float>);
CPPUNIT_TEST(inv<double>);
CPPUNIT_TEST(inv<long double>);
@@ -853,6 +928,7 @@ class VariableMatrixTest: public CppUnit::TestFixture {
CPPUNIT_TEST(stream<unsigned long>);
CPPUNIT_TEST(stream<float>);
CPPUNIT_TEST(stream<double>);
CPPUNIT_TEST(stream<long double>);
CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION(VariableMatrixTest);