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
......@@ -830,12 +830,12 @@ inline const Matrix2<Type> fabs( const Matrix2<Type>& m );
// \param matrix The right-hand-side matrix for the multiplication.
// \return The scaled result matrix.
*/
//template< typename Type, typename Other >
//inline const Matrix2<HIGH> operator*( Other scalar, const Matrix2<Type>& matrix )
//{
// static_assert( ! std::is_scalar<Other>::value, "Only scalar types allowed" );
// return matrix*scalar;
//}
template< typename Type, typename Other >
inline typename std::enable_if< std::is_fundamental< Other >::value, Matrix2< HIGH > >::type
operator*(Other scalar, const Matrix2< Type >& matrix)
{
return matrix * scalar;
}
//**********************************************************************************************************************
......
......@@ -1404,12 +1404,12 @@ inline const Matrix3<Type> fabs( const Matrix3<Type>& m );
// \param matrix The right-hand-side matrix for the multiplication.
// \return The scaled result matrix.
*/
//template< typename Type, typename Other >
//inline const Matrix3<HIGH> operator*( Other scalar, const Matrix3<Type>& matrix )
//{
// static_assert( ! std::is_scalar<Other>::value, "Only scalar types allowed" );
// return matrix*scalar;
//}
template< typename Type, typename Other >
inline typename std::enable_if< std::is_arithmetic< Other >::value, const Matrix3< HIGH > >::type
operator*(Other scalar, const Matrix3< Type >& matrix)
{
return matrix * scalar;
}
//**********************************************************************************************************************
//*************************************************************************************************
......
......@@ -19,67 +19,75 @@
//
//======================================================================================================================
#include "core/debug/TestSubsystem.h"
#include "core/math/Matrix3.h"
#include <iostream>
#include "core/debug/TestSubsystem.h"
#include <iostream>
using namespace walberla;
using walberla::uint8_t;
void rotationTest()
{
Matrix3<real_t> rotationMatrix (0.0);
rotationMatrix(0,0) = 1.0;
rotationMatrix(1,1) = 1.0;
rotationMatrix(2,2) = 1.0;
Matrix3< real_t > rotationMatrix(0.0);
rotationMatrix(0, 0) = 1.0;
rotationMatrix(1, 1) = 1.0;
rotationMatrix(2, 2) = 1.0;
Matrix3<real_t> diagonalMatrix ( 0.0 );
diagonalMatrix(0,0) = 2.0;
diagonalMatrix(1,1) = 4.0;
diagonalMatrix(2,2) = 6.0;
Matrix3< real_t > diagonalMatrix(0.0);
diagonalMatrix(0, 0) = 2.0;
diagonalMatrix(1, 1) = 4.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 << result << std::endl;
WALBERLA_CHECK_FLOAT_EQUAL( result(0,0), 2.0 );
WALBERLA_CHECK_FLOAT_EQUAL( result(1,1), 4.0 );
WALBERLA_CHECK_FLOAT_EQUAL( result(2,2), 6.0 );
WALBERLA_CHECK_FLOAT_EQUAL(result(0, 0), 2.0);
WALBERLA_CHECK_FLOAT_EQUAL(result(1, 1), 4.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)
for (uint_t j = 0; j < 3; ++j)
if (i != j) WALBERLA_CHECK_FLOAT_EQUAL(result(i, j), 0.0);
//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) );
// 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()
{
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 );
WALBERLA_CHECK_FLOAT_EQUAL( math::transformMatrixRART(R,A), R * A * R.getTranspose() );
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);
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);
const Matrix3< real_t > solution(5, 10, 15, 20, 25, 30, 35, 40, 45);
WALBERLA_CHECK_FLOAT_EQUAL(A * s, solution);
WALBERLA_CHECK_FLOAT_EQUAL(s * A, A * s);
}
Matrix3<real_t> m1 ( 1.0 );
Matrix3<real_t> m2 ( 2.0 );
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
m1 * m2;
// in an incorrect implementation of operator*(Other, Matrix3), the following line might give a compile error
m1* m2;
rotationTest();
RARTTest();
scalarMultiplicationTest();
return 0;
}
......@@ -25,46 +25,46 @@
#include <iostream>
using namespace walberla;
using walberla::uint8_t;
void matrix2Test()
{
Matrix2<real_t> m1 ( 2, 1, -1, 5 );
Matrix2<real_t> m2 ( 4, 2, -2, 1 );
Matrix2< real_t > m1(2, 1, -1, 5);
Matrix2< real_t > m2(4, 2, -2, 1);
WALBERLA_CHECK_EQUAL( m1 * m2 * m2.getInverse(), m1 );
WALBERLA_CHECK_EQUAL( m1 * m1.getInverse() * m2, m2 );
WALBERLA_CHECK_EQUAL(m1 * m2 * m2.getInverse(), m1);
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()
{
Vector2<real_t> v1( 1,2 );
Vector2<real_t> v2( 3,4 );
Vector2<uint_t> v3( 4,3 );
Vector2< real_t > v1(1, 2);
Vector2< real_t > v2(3, 4);
Vector2< uint_t > v3(4, 3);
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;
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.sqrLength(), 25.0);
WALBERLA_CHECK_FLOAT_EQUAL( v2.length(), 5.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()
{
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