Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Markus Holzer
waLBerla
Commits
ed4c3ec5
Commit
ed4c3ec5
authored
Jul 01, 2021
by
Christoph Schwarzmeier
Browse files
Implement operator*(scalar, matrix) for Matrix2 and Matrix3
parent
acd9a0bc
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/core/math/Matrix2.h
View file @
ed4c3ec5
//======================================================================================================================
//
// 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
// 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.
//
// waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
//
// waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
//
// 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/>.
//
...
...
@@ -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
;
}
//**********************************************************************************************************************
...
...
src/core/math/Matrix3.h
View file @
ed4c3ec5
//======================================================================================================================
//
// 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
// 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.
//
// waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
//
// waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
//
// 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/>.
//
...
...
@@ -441,7 +441,7 @@ Matrix3<Type> Matrix3<Type>::makeDiagonalMatrix( const Type d )
//**********************************************************************************************************************
/*!\fn Matrix3<Type> Matrix3<Type>::makeIdentityMatrix()
// \brief Named constructor to create the identity matrix.
//
//
// All diagonal elements are initialized to one, alls others to zero.
//
*/
...
...
@@ -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
;
}
//**********************************************************************************************************************
//*************************************************************************************************
...
...
tests/core/math/Matrix3Test.cpp
View file @
ed4c3ec5
//======================================================================================================================
//
// 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
// 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.
//
// waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
//
// waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
//
// 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/>.
//
...
...
@@ -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
<<
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
);
Matrix3
<
real_t
>
m1
(
1.0
);
Matrix3
<
real_t
>
m2
(
2.0
);
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
);
}
int
main
()
{
Matrix3
<
real_t
>
m1
(
1.0
);
Matrix3
<
real_t
>
m2
(
2.0
);
//
the following line gives a co
mp
i
le
error when the
operator*(Other, Matrix3)
is commented in
m1
*
m2
;
//
in an incorrect i
mple
mentation of
operator*(Other, Matrix3)
, the following line might give a compile error
m1
*
m2
;
rotationTest
();
RARTTest
();
scalarMultiplicationTest
();
return
0
;
}
tests/core/math/MatrixVector2Test.cpp
View file @
ed4c3ec5
//======================================================================================================================
//
// 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
// 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.
//
// waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
//
// waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
//
// 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/>.
//
...
...
@@ -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_EQUAL
(
v3
.
indexOfMin
(),
1
);
WALBERLA_CHECK_EQUAL
(
v3
.
indexOfMax
(),
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
);
}
int
main
()
{
vector2Test
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment