8#ifndef META_OCEAN_MATH_SQUARE_MATRIX_2_H
9#define META_OCEAN_MATH_SQUARE_MATRIX_2_H
22template <
typename T>
class SquareMatrixT2;
102 template <
typename U>
122 template <
typename U>
137 template <
typename U>
138 SquareMatrixT2(
const U* arrayValues,
const bool valuesRowAligned);
145 SquareMatrixT2(
const T* arrayValues,
const bool valuesRowAligned);
154 inline SquareMatrixT2(
const T& m00,
const T& m10,
const T& m01,
const T& m11);
213 inline T
trace()
const;
258 inline T
norm()
const;
264 inline bool isNull()
const;
308 inline const T*
data()
const;
322 template <
typename U>
323 inline void copyElements(U* arrayValues,
const bool columnAligned =
true)
const;
330 inline void copyElements(T* arrayValues,
const bool columnAligned =
true)
const;
427 inline T
operator[](
const unsigned int index)
const;
435 inline T&
operator[](
const unsigned int index);
444 inline T
operator()(
const unsigned int row,
const unsigned int column)
const;
453 inline T&
operator()(
const unsigned int row,
const unsigned int column);
461 inline T
operator()(
const unsigned int index)
const;
469 inline T&
operator()(
const unsigned int index);
502 template <
typename U>
512 template <
typename U>
531 values_[0] = T(matrix.
values_[0]);
532 values_[1] = T(matrix.
values_[1]);
533 values_[2] = T(matrix.
values_[2]);
534 values_[3] = T(matrix.
values_[3]);
559 values_[0] = diagonal[0];
562 values_[3] = diagonal[1];
569 ocean_assert(arrayValues);
571 for (
unsigned int n = 0u; n < 4u; ++n)
573 values_[n] = T(arrayValues[n]);
580 ocean_assert(arrayValues);
581 memcpy(values_, arrayValues,
sizeof(T) * 4);
588 ocean_assert(arrayValues);
590 if (valuesRowAligned)
592 values_[0] = T(arrayValues[0]);
593 values_[1] = T(arrayValues[2]);
594 values_[2] = T(arrayValues[1]);
595 values_[3] = T(arrayValues[3]);
600 for (
unsigned int n = 0u; n < 4u; ++n)
602 values_[n] = T(arrayValues[n]);
610 ocean_assert(arrayValues);
612 if (valuesRowAligned)
614 values_[0] = arrayValues[0];
615 values_[1] = arrayValues[2];
616 values_[2] = arrayValues[1];
617 values_[3] = arrayValues[3];
621 memcpy(values_, arrayValues,
sizeof(T) * 4);
641 const T det = eigenVector0.
x() * eigenVector1.
y() - eigenVector1.
x() * eigenVector0.
y();
644 values_[0] = (eigenVector0.
x() * eigenValue0 * eigenVector1.
y() - eigenVector0.
y() * eigenValue1 * eigenVector1.
x()) / det;
645 values_[1] = (eigenValue0 - eigenValue1) * eigenVector0.
y() * eigenVector1.
y() / det;
646 values_[2] = (eigenValue1 - eigenValue0) * eigenVector0.
x() * eigenVector1.
x() / det;
647 values_[3] = (eigenVector0.
x() * eigenValue1 * eigenVector1.
y() - eigenVector0.
y() * eigenValue0 * eigenVector1.
x()) / det;
650 if (!std::is_same<Scalar, float>::value)
652 T debugEigenValue0, debugEigenValue1;
654 ocean_assert(eigenSystem(debugEigenValue0, debugEigenValue1, debugEigenVector0, debugEigenVector1));
673 const T tmp = values_[1];
674 values_[1] = values_[2];
685 ocean_assert(
false &&
"Could not invert the matrix.");
695 const T det = determinant();
701 const T factor = T(1.0) / det;
703 *
this =
SquareMatrixT2<T>(values_[3] * factor, -values_[1] * factor, -values_[2] * factor, values_[0] * factor);
711 const T det = determinant();
717 const T factor = T(1.0) / det;
719 invertedMatrix =
SquareMatrixT2<T>(values_[3] * factor, -values_[1] * factor, -values_[2] * factor, values_[0] * factor);
721#ifdef OCEAN_INTENSIVE_DEBUG
722 if (!std::is_same<T, float>::value)
728 for (
unsigned int n = 0; n < 4u; ++n)
737 T absolusteAverageEnergy = 0;
738 for (
unsigned int n = 0u; n < 4u; ++n)
742 absolusteAverageEnergy *= T(0.25);
746 if (absolusteAverageEnergy <= 1)
748 ocean_assert_accuracy(!
"This should never happen!");
752 const T adjustedDistance = distance / absolusteAverageEnergy;
765 return values_[0] * values_[3] - values_[1] * values_[2];
771 return values_[0] + values_[3];
777 const T determinant = (values_[0] * values_[3]) - (values_[1] * values_[2]);
781 x[0] = (values_[3] * b[0] - values_[2] * b[1]) / determinant;
782 x[1] = (values_[0] * b[1] - values_[1] * b[0]) / determinant;
853 ocean_assert(epsilon >= T(0));
881 ocean_assert(arrayValues);
885 arrayValues[0] = U(values_[0]);
886 arrayValues[1] = U(values_[1]);
887 arrayValues[2] = U(values_[2]);
888 arrayValues[3] = U(values_[3]);
892 arrayValues[0] = U(values_[0]);
893 arrayValues[1] = U(values_[2]);
894 arrayValues[2] = U(values_[1]);
895 arrayValues[3] = U(values_[3]);
902 ocean_assert(arrayValues);
906 arrayValues[0] = values_[0];
907 arrayValues[1] = values_[1];
908 arrayValues[2] = values_[2];
909 arrayValues[3] = values_[3];
913 arrayValues[0] = values_[0];
914 arrayValues[1] = values_[2];
915 arrayValues[2] = values_[1];
916 arrayValues[3] = values_[3];
923 return isEqual(matrix);
929 return !(*
this == matrix);
941 values_[0] += matrix.
values_[0];
942 values_[1] += matrix.
values_[1];
943 values_[2] += matrix.
values_[2];
944 values_[3] += matrix.
values_[3];
958 values_[0] -= matrix.
values_[0];
959 values_[1] -= matrix.
values_[1];
960 values_[2] -= matrix.
values_[2];
961 values_[3] -= matrix.
values_[3];
971 result.
values_[ 0] = -values_[ 0];
972 result.
values_[ 1] = -values_[ 1];
973 result.
values_[ 2] = -values_[ 2];
974 result.
values_[ 3] = -values_[ 3];
991 *
this = *
this * matrix;
998 return VectorT2<T>(values_[0] * vector[0] + values_[2] * vector[1],
999 values_[1] * vector[0] + values_[3] * vector[1]);
1002template <
typename T>
1005 return SquareMatrixT2<T>(values_[0] * value, values_[1] * value, values_[2] * value, values_[3] * value);
1008template <
typename T>
1011 values_[0] *= value;
1012 values_[1] *= value;
1013 values_[2] *= value;
1014 values_[3] *= value;
1019template <
typename T>
1022 ocean_assert(index < 4u);
1023 return values_[index];
1026template <
typename T>
1029 ocean_assert(index < 4u);
1030 return values_[index];
1033template <
typename T>
1036 ocean_assert(row < 2u && column < 2u);
1037 return values_[column * 2u + row];
1040template <
typename T>
1043 ocean_assert(row < 2u && column < 2u);
1044 return values_[column * 2u + row];
1047template <
typename T>
1050 ocean_assert(index < 4u);
1051 return values_[index];
1054template <
typename T>
1057 ocean_assert(index < 4u);
1058 return values_[index];
1061template <
typename T>
1067template <
typename T>
1073template <
typename T>
1076 size_t seed = std::hash<T>{}(matrix.
values_[0]);
1078 for (
unsigned int n = 1u; n < 4u; ++n)
1080 seed ^= std::hash<T>{}(matrix.
values_[n]) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
1086template <
typename T>
1092template <
typename T>
1100 const T tr = trace();
1101 const T det = determinant();
1108 if (eigenValue0 < eigenValue1)
1110 std::swap(eigenValue0, eigenValue1);
1115 const T factor = T(1.0) / values_[2];
1116 eigenVector0 =
VectorT2<T>(1, (eigenValue0 - values_[0]) * factor);
1117 eigenVector1 =
VectorT2<T>(1, (eigenValue1 - values_[0]) * factor);
1121 const T factor = T(1.0) / values_[1];
1123 eigenVector0 =
VectorT2<T>((eigenValue0 - values_[3]) * factor, T(1.0));
1124 eigenVector1 =
VectorT2<T>((eigenValue1 - values_[3]) * factor, T(1.0));
1130 eigenVector0 =
VectorT2<T>(1, values_[1] / (eigenValue0 - values_[3]));
1140 eigenVector0 =
VectorT2<T>(values_[2] / (eigenValue0 - values_[0]), T(1.0));
1146 eigenVector1 =
VectorT2<T>(1, values_[1] / (eigenValue1 - values_[3]));
1156 eigenVector1 =
VectorT2<T>(values_[2] / (eigenValue1 - values_[0]), T(1.0));
1164 ocean_assert((std::is_same<T, float>::value) || (*
this * eigenVector0).isEqual(eigenVector0 * eigenValue0,
NumericT<T>::weakEps()));
1165 ocean_assert((std::is_same<T, float>::value) || (*
this * eigenVector1).isEqual(eigenVector1 * eigenValue1,
NumericT<T>::weakEps()));
1170template <
typename T>
1171template <
typename U>
1174 std::vector< SquareMatrixT2<T> > result;
1175 result.reserve(matrices.size());
1177 for (
typename std::vector<
SquareMatrixT2<U> >::const_iterator i = matrices.begin(); i != matrices.end(); ++i)
1199template <
typename T>
1200template <
typename U>
1203 std::vector< SquareMatrixT2<T> > result;
1204 result.reserve(size);
1206 for (
size_t n = 0; n < size; ++n)
1214template <
typename T>
1217 stream <<
"|" << matrix(0, 0) <<
", " << matrix(0, 1) <<
"|" << std::endl;
1218 stream <<
"|" << matrix(1, 0) <<
", " << matrix(1, 1) <<
"|";
1223template <
bool tActive,
typename T>
1224MessageObject<tActive>&
operator<<(MessageObject<tActive>& messageObject,
const SquareMatrixT2<T>& matrix)
1226 return messageObject <<
"|" << matrix(0, 0) <<
", " << matrix(0, 1) <<
"|\n|" << matrix(1, 0) <<
", " << matrix(1, 1) <<
"|";
1229template <
bool tActive,
typename T>
1230MessageObject<tActive>&
operator<<(MessageObject<tActive>&& messageObject,
const SquareMatrixT2<T>& matrix)
1232 return messageObject <<
"|" << matrix(0, 0) <<
", " << matrix(0, 1) <<
"|\n|" << matrix(1, 0) <<
", " << matrix(1, 1) <<
"|";
This class provides several functions to solve equations with different degree using floating point v...
Definition Equation.h:53
This class provides basic numeric functionalities.
Definition Numeric.h:57
static T abs(const T value)
Returns the absolute value of a given value.
Definition Numeric.h:1220
static T sqrt(const T value)
Returns the square root of a given value.
Definition Numeric.h:1533
static bool isEqual(const T first, const T second)
Returns whether two values are equal up to a small epsilon.
Definition Numeric.h:2389
static constexpr T sqr(const T value)
Returns the square of a given value.
Definition Numeric.h:1495
static constexpr bool isEqualEps(const T value)
Returns whether a value is smaller than or equal to a small epsilon.
Definition Numeric.h:2090
This class implements a 2x2 square matrix.
Definition SquareMatrix2.h:73
static size_t elements()
Returns the number of elements this matrix has.
Definition SquareMatrix2.h:1087
SquareMatrixT2< T > & operator=(const SquareMatrixT2< T > &)=default
Default copy assignment operator.
bool isIdentity() const
Returns whether this matrix is the identity matrix.
Definition SquareMatrix2.h:839
bool isSingular() const
Returns whether this matrix is singular (and thus cannot be inverted).
Definition SquareMatrix2.h:845
SquareMatrixT2< T > & operator+=(const SquareMatrixT2< T > &matrix)
Adds and assigns two matrices.
Definition SquareMatrix2.h:939
T norm() const
Returns the norm of this matrix that is the sum of the absolute matrix elements.
Definition SquareMatrix2.h:827
VectorT2< T > xAxis() const
Returns the x axis which is the first column of the matrix.
Definition SquareMatrix2.h:809
SquareMatrixT2< T > transposed() const
Returns the transposed of this matrix.
Definition SquareMatrix2.h:665
bool operator!=(const SquareMatrixT2< T > &matrix) const
Returns whether two matrices are not identical up to a small epsilon.
Definition SquareMatrix2.h:927
static std::vector< SquareMatrixT2< T > > matrices2matrices(const std::vector< SquareMatrixT2< U > > &matrices)
Converts matrices with specific data type to matrices with different data type.
Definition SquareMatrix2.h:1172
bool isEqual(const SquareMatrixT2< T > &matrix, const T eps=NumericT< T >::eps()) const
Returns whether two matrices are almost identical up to a specified epsilon.
Definition SquareMatrix2.h:859
T operator[](const unsigned int index) const
Element operator.
Definition SquareMatrix2.h:1020
bool isNull() const
Returns whether this matrix is a null matrix.
Definition SquareMatrix2.h:833
friend class SquareMatrixT2
Definition SquareMatrix2.h:74
VectorT2< T > yAxis() const
Returns the y axis which is the middle column of the matrix.
Definition SquareMatrix2.h:815
T trace() const
Returns the trace of the matrix which is the sum of the diagonal elements.
Definition SquareMatrix2.h:769
bool operator==(const SquareMatrixT2< T > &matrix) const
Returns whether two matrices are identical up to a small epsilon.
Definition SquareMatrix2.h:921
bool solve(const VectorT2< T > &b, VectorT2< T > &x) const
Solve a simple 2x2 system of linear equations: Ax = b Beware: The system of linear equations is assum...
Definition SquareMatrix2.h:775
void toIdentity()
Sets the matrix to the identity matrix.
Definition SquareMatrix2.h:791
SquareMatrixT2< T > operator*(const SquareMatrixT2< T > &matrix) const
Multiplies two matrices.
Definition SquareMatrix2.h:980
SquareMatrixT2< T > inverted() const
Returns the inverted matrix of this matrix.
Definition SquareMatrix2.h:679
const T * data() const
Returns a pointer to the internal values.
Definition SquareMatrix2.h:866
SquareMatrixT2< T > & operator-=(const SquareMatrixT2< T > &matrix)
Subtracts and assigns two matrices.
Definition SquareMatrix2.h:956
bool isSymmetric(const T epsilon=NumericT< T >::eps()) const
Returns whether this matrix is symmetric.
Definition SquareMatrix2.h:851
T determinant() const
Returns the determinant of the matrix.
Definition SquareMatrix2.h:763
T Type
Definition of the used data type.
Definition SquareMatrix2.h:81
SquareMatrixT2< T > operator-() const
Returns the negative matrix of this matrix (all matrix elements are multiplied by -1).
Definition SquareMatrix2.h:967
bool invert()
Inverts this matrix in place.
Definition SquareMatrix2.h:693
void copyElements(U *arrayValues, const bool columnAligned=true) const
Copies the elements of this matrix to an array with floating point values of type U.
Definition SquareMatrix2.h:879
T values_[4]
The four values of the matrix.
Definition SquareMatrix2.h:518
SquareMatrixT2< T > operator+(const SquareMatrixT2< T > &matrix) const
Adds two matrices.
Definition SquareMatrix2.h:933
SquareMatrixT2(const SquareMatrixT2< T > &matrix)=default
Copy constructor.
void transpose()
Transposes the matrix.
Definition SquareMatrix2.h:671
VectorT2< T > diagonal() const
Returns a 2D vector with values of the matrix diagonal.
Definition SquareMatrix2.h:821
const T * operator()() const
Access operator.
Definition SquareMatrix2.h:1062
SquareMatrixT2< T > & operator*=(const SquareMatrixT2< T > &matrix)
Multiplies and assigns two matrices.
Definition SquareMatrix2.h:989
bool eigenSystem(T &eigenValue0, T &eigenValue1, VectorT2< T > &eigenVector0, VectorT2< T > &eigenVector1) const
Performs an eigen value analysis.
Definition SquareMatrix2.h:1093
void toNull()
Sets the matrix to a zero matrix.
Definition SquareMatrix2.h:800
This class implements a vector with two elements.
Definition Vector2.h:96
const T & x() const noexcept
Returns the x value.
Definition Vector2.h:710
const T & y() const noexcept
Returns the y value.
Definition Vector2.h:722
bool isEqual(const VectorT2< T > &vector, const T eps) const
Returns whether two vectors are equal up to a specified epsilon.
Definition Vector2.h:758
T length() const
Returns the length of the vector.
Definition Vector2.h:627
bool normalize()
Normalizes this vector.
Definition Vector2.h:612
unsigned int sqrDistance(const char first, const char second)
Returns the square distance between two values.
Definition base/Utilities.h:1113
std::vector< SquareMatrixT2< T > > SquareMatricesT2
Definition of a typename alias for vectors with SquareMatrixT2 objects.
Definition SquareMatrix2.h:51
SquareMatrixT2< float > SquareMatrixF2
Instantiation of the SquareMatrixT2 template class using a single precision float data type.
Definition SquareMatrix2.h:43
SquareMatrixT2< Scalar > SquareMatrix2
Definition of the SquareMatrix2 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with ...
Definition SquareMatrix2.h:29
SquareMatrixT2< double > SquareMatrixD2
Instantiation of the SquareMatrixT2 template class using a double precision float data type.
Definition SquareMatrix2.h:36
std::vector< SquareMatrix2 > SquareMatrices2
Definition of a vector holding SquareMatrix2 objects.
Definition SquareMatrix2.h:58
The namespace covering the entire Ocean framework.
Definition Accessor.h:15
std::ostream & operator<<(std::ostream &stream, const HighPerformanceStatistic &highPerformanceStatistic)
Definition HighPerformanceTimer.h:963