#2345 Add + and * operators to the tensor class, to make averaging possible.

This commit is contained in:
Jacob Støren committed 2018-02-07 13:10:36 +01:00
1 parent bd14c65678
commit bb53b926bf
3 files changed
+67 -1

No files matched your search

@@ -124,7 +124,7 @@ TEST(OffshoreSphericalCoords, OffshoreSphericalCoords)
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST(TensorRotation, TensorRotation)
TEST(Tensor, TensorRotation)
{
{
@@ -232,4 +232,32 @@ TEST(TensorRotation, TensorRotation)
EXPECT_NEAR(0.0f, rotT[caf::Ten3f::SYZ], 1e-4);
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST(Tensor, TensorAddScale)
{
caf::Ten3f orgT1(1.1f, 1.2f, 1.3f, 1.4f, 1.5f, 1.6f);
caf::Ten3f orgT2(1.6f, 1.5f, 1.4f, 1.3f, 1.2f, 1.1f);
caf::Ten3f newT = orgT1 + orgT2;
EXPECT_NEAR(2.7f, newT[caf::Ten3f::SXX], 1e-4);
EXPECT_NEAR(2.7f, newT[caf::Ten3f::SYY], 1e-4);
EXPECT_NEAR(2.7f, newT[caf::Ten3f::SZZ], 1e-4);
EXPECT_NEAR(2.7f, newT[caf::Ten3f::SXY], 1e-4);
EXPECT_NEAR(2.7f, newT[caf::Ten3f::SZX], 1e-4);
EXPECT_NEAR(2.7f, newT[caf::Ten3f::SYZ], 1e-4);
newT = newT*0.5;
EXPECT_NEAR(1.35f, newT[caf::Ten3f::SXX], 1e-4);
EXPECT_NEAR(1.35f, newT[caf::Ten3f::SYY], 1e-4);
EXPECT_NEAR(1.35f, newT[caf::Ten3f::SZZ], 1e-4);
EXPECT_NEAR(1.35f, newT[caf::Ten3f::SXY], 1e-4);
EXPECT_NEAR(1.35f, newT[caf::Ten3f::SZX], 1e-4);
EXPECT_NEAR(1.35f, newT[caf::Ten3f::SYZ], 1e-4);
}
+2
View File
@@ -34,6 +34,8 @@ public:
Tensor3(const Tensor3& other);
inline Tensor3& operator=(const Tensor3& rhs);
inline Tensor3 operator+(const Tensor3& rhs) const;
inline Tensor3 operator*(S scale) const;
bool equals(const Tensor3& mat) const;
bool operator==(const Tensor3& rhs) const;
+36
View File
@@ -62,6 +62,42 @@ inline Tensor3<S>& Tensor3<S>::operator=(const Tensor3& obj)
return *this;
}
//--------------------------------------------------------------------------------------------------
/// Component-wise addition
//--------------------------------------------------------------------------------------------------
template< typename S>
Tensor3<S> caf::Tensor3<S>::operator+(const Tensor3& rhs) const
{
Tensor3<S> result(*this);
result.m_tensor[0] += rhs.m_tensor[0];
result.m_tensor[1] += rhs.m_tensor[1];
result.m_tensor[2] += rhs.m_tensor[2];
result.m_tensor[3] += rhs.m_tensor[3];
result.m_tensor[4] += rhs.m_tensor[4];
result.m_tensor[5] += rhs.m_tensor[5];
return result;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
template< typename S>
Tensor3<S> caf::Tensor3<S>::operator*(S scale) const
{
Tensor3<S> result(*this);
result.m_tensor[0] *= scale;
result.m_tensor[1] *= scale;
result.m_tensor[2] *= scale;
result.m_tensor[3] *= scale;
result.m_tensor[4] *= scale;
result.m_tensor[5] *= scale;
return result;
}
//----------------------------------------------------------------------------------------------------
/// Check if matrices are equal using exact comparisons.