Commit 51045907 authored by Christoph Schwarzmeier's avatar Christoph Schwarzmeier
Browse files

Merge branch 'operator-multiplication-scalar-matrix' into 'master'

Implement operator*(scalar, matrix) for Matrix2 and Matrix3

See merge request !470
parents acd9a0bc ed4c3ec5
Pipeline #33077 passed with stages
in 487 minutes and 17 seconds
//====================================================================================================================== //======================================================================================================================
// //
// This file is part of waLBerla. waLBerla is free software: you can // This file is part of waLBerla. waLBerla is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public // redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of // License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version. // the License, or (at your option) any later version.
// //
// waLBerla is distributed in the hope that it will be useful, but WITHOUT // waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details. // for more details.
// //
// You should have received a copy of the GNU General Public License along // You should have received a copy of the GNU General Public License along
// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>. // with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
// //
...@@ -830,12 +830,12 @@ inline const Matrix2<Type> fabs( const Matrix2<Type>& m ); ...@@ -830,12 +830,12 @@ inline const Matrix2<Type> fabs( const Matrix2<Type>& m );
// \param matrix The right-hand-side matrix for the multiplication. // \param matrix The right-hand-side matrix for the multiplication.
// \return The scaled result matrix. // \return The scaled result matrix.
*/ */
//template< typename Type, typename Other > template< typename Type, typename Other >
//inline const Matrix2<HIGH> operator*( Other scalar, const Matrix2<Type>& matrix ) inline typename std::enable_if< std::is_fundamental< Other >::value, Matrix2< HIGH > >::type
//{ operator*(Other scalar, const Matrix2< Type >& matrix)
// static_assert( ! std::is_scalar<Other>::value, "Only scalar types allowed" ); {
// return matrix*scalar; return matrix * scalar;
//} }
//********************************************************************************************************************** //**********************************************************************************************************************
......
//====================================================================================================================== //======================================================================================================================
// //
// This file is part of waLBerla. waLBerla is free software: you can // This file is part of waLBerla. waLBerla is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public // redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of // License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version. // the License, or (at your option) any later version.
// //
// waLBerla is distributed in the hope that it will be useful, but WITHOUT // waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details. // for more details.
// //
// You should have received a copy of the GNU General Public License along // You should have received a copy of the GNU General Public License along
// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>. // with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
// //
...@@ -441,7 +441,7 @@ Matrix3<Type> Matrix3<Type>::makeDiagonalMatrix( const Type d ) ...@@ -441,7 +441,7 @@ Matrix3<Type> Matrix3<Type>::makeDiagonalMatrix( const Type d )
//********************************************************************************************************************** //**********************************************************************************************************************
/*!\fn Matrix3<Type> Matrix3<Type>::makeIdentityMatrix() /*!\fn Matrix3<Type> Matrix3<Type>::makeIdentityMatrix()
// \brief Named constructor to create the identity matrix. // \brief Named constructor to create the identity matrix.
// //
// All diagonal elements are initialized to one, alls others to zero. // All diagonal elements are initialized to one, alls others to zero.
// //
*/ */
...@@ -1404,12 +1404,12 @@ inline const Matrix3<Type> fabs( const Matrix3<Type>& m ); ...@@ -1404,12 +1404,12 @@ inline const Matrix3<Type> fabs( const Matrix3<Type>& m );
// \param matrix The right-hand-side matrix for the multiplication. // \param matrix The right-hand-side matrix for the multiplication.
// \return The scaled result matrix. // \return The scaled result matrix.
*/ */
//template< typename Type, typename Other > template< typename Type, typename Other >
//inline const Matrix3<HIGH> operator*( Other scalar, const Matrix3<Type>& matrix ) inline typename std::enable_if< std::is_arithmetic< Other >::value, const Matrix3< HIGH > >::type
//{ operator*(Other scalar, const Matrix3< Type >& matrix)
// static_assert( ! std::is_scalar<Other>::value, "Only scalar types allowed" ); {
// return matrix*scalar; return matrix * scalar;
//} }
//********************************************************************************************************************** //**********************************************************************************************************************
//************************************************************************************************* //*************************************************************************************************
......
//====================================================================================================================== //======================================================================================================================
// //
// This file is part of waLBerla. waLBerla is free software: you can // This file is part of waLBerla. waLBerla is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public // redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of // License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version. // the License, or (at your option) any later version.
// //
// waLBerla is distributed in the hope that it will be useful, but WITHOUT // waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details. // for more details.
// //
// You should have received a copy of the GNU General Public License along // You should have received a copy of the GNU General Public License along
// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>. // with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
// //
...@@ -19,67 +19,75 @@ ...@@ -19,67 +19,75 @@
// //
//====================================================================================================================== //======================================================================================================================
#include "core/debug/TestSubsystem.h"
#include "core/math/Matrix3.h" #include "core/math/Matrix3.h"
#include <iostream> #include "core/debug/TestSubsystem.h"
#include <iostream>
using namespace walberla; using namespace walberla;
using walberla::uint8_t; using walberla::uint8_t;
void rotationTest() void rotationTest()
{ {
Matrix3<real_t> rotationMatrix (0.0); Matrix3< real_t > rotationMatrix(0.0);
rotationMatrix(0,0) = 1.0; rotationMatrix(0, 0) = 1.0;
rotationMatrix(1,1) = 1.0; rotationMatrix(1, 1) = 1.0;
rotationMatrix(2,2) = 1.0; rotationMatrix(2, 2) = 1.0;
Matrix3<real_t> diagonalMatrix ( 0.0 ); Matrix3< real_t > diagonalMatrix(0.0);
diagonalMatrix(0,0) = 2.0; diagonalMatrix(0, 0) = 2.0;
diagonalMatrix(1,1) = 4.0; diagonalMatrix(1, 1) = 4.0;
diagonalMatrix(2,2) = 6.0; diagonalMatrix(2, 2) = 6.0;
Matrix3<real_t> result = rotationMatrix.rotate( diagonalMatrix ); Matrix3< real_t > result = rotationMatrix.rotate(diagonalMatrix);
std::cout << diagonalMatrix << std::endl; std::cout << diagonalMatrix << std::endl;
std::cout << result << std::endl; std::cout << result << std::endl;
WALBERLA_CHECK_FLOAT_EQUAL( result(0,0), 2.0 ); WALBERLA_CHECK_FLOAT_EQUAL(result(0, 0), 2.0);
WALBERLA_CHECK_FLOAT_EQUAL( result(1,1), 4.0 ); WALBERLA_CHECK_FLOAT_EQUAL(result(1, 1), 4.0);
WALBERLA_CHECK_FLOAT_EQUAL( result(2,2), 6.0 ); WALBERLA_CHECK_FLOAT_EQUAL(result(2, 2), 6.0);
for (uint_t i = 0; i < 3; ++i)
for (uint_t j = 0; j < 3; ++j)
if (i != j) WALBERLA_CHECK_FLOAT_EQUAL(result(i, j), 0.0);
for( uint_t i = 0; i < 3; ++i ) // also checking WALBERLA_CHECK_FLOAT_EQUAL for matrices
for( uint_t j = 0; j < 3; ++j ) Matrix3< real_t > cmp(2, 0, 0, 0, 4, 0, 0, 0, 6);
if ( i != j) WALBERLA_CHECK_FLOAT_EQUAL(result, cmp);
WALBERLA_CHECK_FLOAT_EQUAL( result(i,j), 0.0 ); WALBERLA_CHECK_FLOAT_EQUAL_EPSILON(result, cmp, real_t(1e-5));
//also checking WALBERLA_CHECK_FLOAT_EQUAL for matrices
Matrix3<real_t> cmp(2,0,0,0,4,0,0,0,6);
WALBERLA_CHECK_FLOAT_EQUAL( result, cmp );
WALBERLA_CHECK_FLOAT_EQUAL_EPSILON( result, cmp, real_t(1e-5) );
} }
void RARTTest() void RARTTest()
{ {
Matrix3<real_t> A ( 1,2,3,4,5,6,7,8,9 ); Matrix3< real_t > A(1, 2, 3, 4, 5, 6, 7, 8, 9);
Matrix3<real_t> R ( 2,3,4,5,6,7,8,9,1 ); Matrix3< real_t > R(2, 3, 4, 5, 6, 7, 8, 9, 1);
WALBERLA_CHECK_FLOAT_EQUAL( math::transformMatrixRART(R,A), R * A * R.getTranspose() ); WALBERLA_CHECK_FLOAT_EQUAL(math::transformMatrixRART(R, A), R * A * R.getTranspose());
} }
int main() void scalarMultiplicationTest()
{ {
const Matrix3< real_t > A(1, 2, 3, 4, 5, 6, 7, 8, 9);
const real_t s = real_c(5);
Matrix3<real_t> m1 ( 1.0 ); const Matrix3< real_t > solution(5, 10, 15, 20, 25, 30, 35, 40, 45);
Matrix3<real_t> m2 ( 2.0 );
WALBERLA_CHECK_FLOAT_EQUAL(A * s, solution);
WALBERLA_CHECK_FLOAT_EQUAL(s * A, A * s);
}
int main()
{
Matrix3< real_t > m1(1.0);
Matrix3< real_t > m2(2.0);
// the following line gives a compile error when the operator*(Other, Matrix3) is commented in // in an incorrect implementation of operator*(Other, Matrix3), the following line might give a compile error
m1 * m2; m1* m2;
rotationTest(); rotationTest();
RARTTest(); RARTTest();
scalarMultiplicationTest();
return 0; return 0;
} }
//====================================================================================================================== //======================================================================================================================
// //
// This file is part of waLBerla. waLBerla is free software: you can // This file is part of waLBerla. waLBerla is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public // redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of // License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version. // the License, or (at your option) any later version.
// //
// waLBerla is distributed in the hope that it will be useful, but WITHOUT // waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details. // for more details.
// //
// You should have received a copy of the GNU General Public License along // You should have received a copy of the GNU General Public License along
// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>. // with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
// //
...@@ -25,46 +25,46 @@ ...@@ -25,46 +25,46 @@
#include <iostream> #include <iostream>
using namespace walberla; using namespace walberla;
using walberla::uint8_t; using walberla::uint8_t;
void matrix2Test() void matrix2Test()
{ {
Matrix2<real_t> m1 ( 2, 1, -1, 5 ); Matrix2< real_t > m1(2, 1, -1, 5);
Matrix2<real_t> m2 ( 4, 2, -2, 1 ); Matrix2< real_t > m2(4, 2, -2, 1);
WALBERLA_CHECK_EQUAL( m1 * m2 * m2.getInverse(), m1 ); WALBERLA_CHECK_EQUAL(m1 * m2 * m2.getInverse(), m1);
WALBERLA_CHECK_EQUAL( m1 * m1.getInverse() * m2, m2 ); WALBERLA_CHECK_EQUAL(m1 * m1.getInverse() * m2, m2);
Vector2<real_t> v1 ( 5, 7 ); Vector2< real_t > v1(5, 7);
WALBERLA_CHECK_EQUAL( m2 * v1, Vector2<real_t> (34, -3) ); WALBERLA_CHECK_EQUAL(m2 * v1, Vector2< real_t >(34, -3));
}
const real_t s = real_c(5);
WALBERLA_CHECK_EQUAL(s * m1, Matrix2< real_t >(10, 5, -5, 25));
WALBERLA_CHECK_EQUAL(m1 * s, Matrix2< real_t >(10, 5, -5, 25));
}
void vector2Test() void vector2Test()
{ {
Vector2<real_t> v1( 1,2 ); Vector2< real_t > v1(1, 2);
Vector2<real_t> v2( 3,4 ); Vector2< real_t > v2(3, 4);
Vector2<uint_t> v3( 4,3 ); Vector2< uint_t > v3(4, 3);
auto sum = v1 + v2; auto sum = v1 + v2;
WALBERLA_CHECK_EQUAL( sum, Vector2<real_t>(4,6 ) ); WALBERLA_CHECK_EQUAL(sum, Vector2< real_t >(4, 6));
auto product = v1 * v2; auto product = v1 * v2;
WALBERLA_CHECK_FLOAT_EQUAL( product, 1.0*3 + 2.0*4 ); WALBERLA_CHECK_FLOAT_EQUAL(product, 1.0 * 3 + 2.0 * 4);
WALBERLA_CHECK_FLOAT_EQUAL( v2.length(), 5.0 ); WALBERLA_CHECK_FLOAT_EQUAL(v2.length(), 5.0);
WALBERLA_CHECK_FLOAT_EQUAL( v2.sqrLength(), 25.0 ); WALBERLA_CHECK_FLOAT_EQUAL(v2.sqrLength(), 25.0);
WALBERLA_CHECK_EQUAL( v3.indexOfMin(), 1 );
WALBERLA_CHECK_EQUAL( v3.indexOfMax(), 0 );
WALBERLA_CHECK_EQUAL(v3.indexOfMin(), 1);
WALBERLA_CHECK_EQUAL(v3.indexOfMax(), 0);
} }
int main() int main()
{ {
vector2Test(); vector2Test();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment