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);
638 ocean_assert(eigenVector0.
isUnit());
639 ocean_assert(eigenVector1.
isUnit());
641 const T determinant = eigenVector0.
x() * eigenVector1.
y() - eigenVector1.
x() * eigenVector0.
y();
644 values_[0] = (eigenVector0.
x() * eigenValue0 * eigenVector1.
y() - eigenVector0.
y() * eigenValue1 * eigenVector1.
x()) / determinant;
645 values_[1] = (eigenValue0 - eigenValue1) * eigenVector0.
y() * eigenVector1.
y() / determinant;
646 values_[2] = (eigenValue1 - eigenValue0) * eigenVector0.
x() * eigenVector1.
x() / determinant;
647 values_[3] = (eigenVector0.
x() * eigenValue1 * eigenVector1.
y() - eigenVector0.
y() * eigenValue0 * eigenVector1.
x()) / determinant;
659 const T tmp = values_[1];
660 values_[1] = values_[2];
671 ocean_assert(
false &&
"Could not invert the matrix.");
681 const T det = determinant();
687 const T factor = T(1.0) / det;
689 *
this =
SquareMatrixT2<T>(values_[3] * factor, -values_[1] * factor, -values_[2] * factor, values_[0] * factor);
697 const T det = determinant();
703 const T factor = T(1.0) / det;
705 invertedMatrix =
SquareMatrixT2<T>(values_[3] * factor, -values_[1] * factor, -values_[2] * factor, values_[0] * factor);
707#ifdef OCEAN_INTENSIVE_DEBUG
708 if (!std::is_same<T, float>::value)
714 for (
unsigned int n = 0; n < 4u; ++n)
723 T absolusteAverageEnergy = 0;
724 for (
unsigned int n = 0u; n < 4u; ++n)
728 absolusteAverageEnergy *= T(0.25);
732 if (absolusteAverageEnergy <= 1)
734 ocean_assert_accuracy(!
"This should never happen!");
738 const T adjustedDistance = distance / absolusteAverageEnergy;
751 return values_[0] * values_[3] - values_[1] * values_[2];
757 return values_[0] + values_[3];
763 const T determinant = (values_[0] * values_[3]) - (values_[1] * values_[2]);
767 x[0] = (values_[3] * b[0] - values_[2] * b[1]) / determinant;
768 x[1] = (values_[0] * b[1] - values_[1] * b[0]) / determinant;
839 ocean_assert(epsilon >= T(0));
867 ocean_assert(arrayValues);
871 arrayValues[0] = U(values_[0]);
872 arrayValues[1] = U(values_[1]);
873 arrayValues[2] = U(values_[2]);
874 arrayValues[3] = U(values_[3]);
878 arrayValues[0] = U(values_[0]);
879 arrayValues[1] = U(values_[2]);
880 arrayValues[2] = U(values_[1]);
881 arrayValues[3] = U(values_[3]);
888 ocean_assert(arrayValues);
892 arrayValues[0] = values_[0];
893 arrayValues[1] = values_[1];
894 arrayValues[2] = values_[2];
895 arrayValues[3] = values_[3];
899 arrayValues[0] = values_[0];
900 arrayValues[1] = values_[2];
901 arrayValues[2] = values_[1];
902 arrayValues[3] = values_[3];
909 return isEqual(matrix);
915 return !(*
this == matrix);
927 values_[0] += matrix.
values_[0];
928 values_[1] += matrix.
values_[1];
929 values_[2] += matrix.
values_[2];
930 values_[3] += matrix.
values_[3];
944 values_[0] -= matrix.
values_[0];
945 values_[1] -= matrix.
values_[1];
946 values_[2] -= matrix.
values_[2];
947 values_[3] -= matrix.
values_[3];
957 result.
values_[ 0] = -values_[ 0];
958 result.
values_[ 1] = -values_[ 1];
959 result.
values_[ 2] = -values_[ 2];
960 result.
values_[ 3] = -values_[ 3];
977 *
this = *
this * matrix;
984 return VectorT2<T>(values_[0] * vector[0] + values_[2] * vector[1],
985 values_[1] * vector[0] + values_[3] * vector[1]);
991 return SquareMatrixT2<T>(values_[0] * value, values_[1] * value, values_[2] * value, values_[3] * value);
1000 values_[3] *= value;
1005template <
typename T>
1008 ocean_assert(index < 4u);
1009 return values_[index];
1012template <
typename T>
1015 ocean_assert(index < 4u);
1016 return values_[index];
1019template <
typename T>
1022 ocean_assert(row < 2u && column < 2u);
1023 return values_[column * 2u + row];
1026template <
typename T>
1029 ocean_assert(row < 2u && column < 2u);
1030 return values_[column * 2u + row];
1033template <
typename T>
1036 ocean_assert(index < 4u);
1037 return values_[index];
1040template <
typename T>
1043 ocean_assert(index < 4u);
1044 return values_[index];
1047template <
typename T>
1053template <
typename T>
1059template <
typename T>
1062 size_t seed = std::hash<T>{}(matrix.
values_[0]);
1064 for (
unsigned int n = 1u; n < 4u; ++n)
1066 seed ^= std::hash<T>{}(matrix.
values_[n]) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
1072template <
typename T>
1078template <
typename T>
1086 const T tr = trace();
1087 const T det = determinant();
1094 if (eigenValue0 < eigenValue1)
1096 std::swap(eigenValue0, eigenValue1);
1101 const T factor = T(1.0) / values_[2];
1102 eigenVector0 =
VectorT2<T>(1, (eigenValue0 - values_[0]) * factor);
1103 eigenVector1 =
VectorT2<T>(1, (eigenValue1 - values_[0]) * factor);
1107 const T factor = T(1.0) / values_[1];
1109 eigenVector0 =
VectorT2<T>((eigenValue0 - values_[3]) * factor, T(1.0));
1110 eigenVector1 =
VectorT2<T>((eigenValue1 - values_[3]) * factor, T(1.0));
1116 eigenVector0 =
VectorT2<T>(1, values_[1] / (eigenValue0 - values_[3]));
1126 eigenVector0 =
VectorT2<T>(values_[2] / (eigenValue0 - values_[0]), T(1.0));
1132 eigenVector1 =
VectorT2<T>(1, values_[1] / (eigenValue1 - values_[3]));
1142 eigenVector1 =
VectorT2<T>(values_[2] / (eigenValue1 - values_[0]), T(1.0));
1150 ocean_assert((std::is_same<T, float>::value) || (*
this * eigenVector0).isEqual(eigenVector0 * eigenValue0,
NumericT<T>::weakEps()));
1151 ocean_assert((std::is_same<T, float>::value) || (*
this * eigenVector1).isEqual(eigenVector1 * eigenValue1,
NumericT<T>::weakEps()));
1156template <
typename T>
1157template <
typename U>
1160 std::vector< SquareMatrixT2<T> > result;
1161 result.reserve(matrices.size());
1163 for (
typename std::vector<
SquareMatrixT2<U> >::const_iterator i = matrices.begin(); i != matrices.end(); ++i)
1185template <
typename T>
1186template <
typename U>
1189 std::vector< SquareMatrixT2<T> > result;
1190 result.reserve(size);
1192 for (
size_t n = 0; n < size; ++n)
1200template <
typename T>
1203 stream <<
"|" << matrix(0, 0) <<
", " << matrix(0, 1) <<
"|" << std::endl;
1204 stream <<
"|" << matrix(1, 0) <<
", " << matrix(1, 1) <<
"|";
1209template <
bool tActive,
typename T>
1210MessageObject<tActive>&
operator<<(MessageObject<tActive>& messageObject,
const SquareMatrixT2<T>& matrix)
1212 return messageObject <<
"|" << matrix(0, 0) <<
", " << matrix(0, 1) <<
"|\n|" << matrix(1, 0) <<
", " << matrix(1, 1) <<
"|";
1215template <
bool tActive,
typename T>
1216MessageObject<tActive>&
operator<<(MessageObject<tActive>&& messageObject,
const SquareMatrixT2<T>& matrix)
1218 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:1537
static bool isEqual(const T first, const T second)
Returns whether two values are equal up to a small epsilon.
Definition Numeric.h:2395
static constexpr T sqr(const T value)
Returns the square of a given value.
Definition Numeric.h:1499
static constexpr bool isEqualEps(const T value)
Returns whether a value is smaller than or equal to a small epsilon.
Definition Numeric.h:2096
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:1073
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:825
bool isSingular() const
Returns whether this matrix is singular (and thus cannot be inverted).
Definition SquareMatrix2.h:831
SquareMatrixT2< T > & operator+=(const SquareMatrixT2< T > &matrix)
Adds and assigns two matrices.
Definition SquareMatrix2.h:925
T norm() const
Returns the norm of this matrix that is the sum of the absolute matrix elements.
Definition SquareMatrix2.h:813
VectorT2< T > xAxis() const
Returns the x axis which is the first column of the matrix.
Definition SquareMatrix2.h:795
SquareMatrixT2< T > transposed() const
Returns the transposed of this matrix.
Definition SquareMatrix2.h:651
T Type
Definition of the used data type.
Definition SquareMatrix2.h:81
bool operator!=(const SquareMatrixT2< T > &matrix) const
Returns whether two matrices are not identical up to a small epsilon.
Definition SquareMatrix2.h:913
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:1158
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:845
T operator[](const unsigned int index) const
Element operator.
Definition SquareMatrix2.h:1006
bool isNull() const
Returns whether this matrix is a null matrix.
Definition SquareMatrix2.h:819
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:801
T trace() const
Returns the trace of the matrix which is the sum of the diagonal elements.
Definition SquareMatrix2.h:755
bool operator==(const SquareMatrixT2< T > &matrix) const
Returns whether two matrices are identical up to a small epsilon.
Definition SquareMatrix2.h:907
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:761
void toIdentity()
Sets the matrix to the identity matrix.
Definition SquareMatrix2.h:777
SquareMatrixT2< T > operator*(const SquareMatrixT2< T > &matrix) const
Multiplies two matrices.
Definition SquareMatrix2.h:966
SquareMatrixT2< T > inverted() const
Returns the inverted matrix of this matrix.
Definition SquareMatrix2.h:665
const T * data() const
Returns a pointer to the internal values.
Definition SquareMatrix2.h:852
SquareMatrixT2< T > & operator-=(const SquareMatrixT2< T > &matrix)
Subtracts and assigns two matrices.
Definition SquareMatrix2.h:942
bool isSymmetric(const T epsilon=NumericT< T >::eps()) const
Returns whether this matrix is symmetric.
Definition SquareMatrix2.h:837
T determinant() const
Returns the determinant of the matrix.
Definition SquareMatrix2.h:749
SquareMatrixT2< T > operator-() const
Returns the negative matrix of this matrix (all matrix elements are multiplied by -1).
Definition SquareMatrix2.h:953
bool invert()
Inverts this matrix in place.
Definition SquareMatrix2.h:679
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:865
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:919
SquareMatrixT2(const SquareMatrixT2< T > &matrix)=default
Copy constructor.
void transpose()
Transposes the matrix.
Definition SquareMatrix2.h:657
VectorT2< T > diagonal() const
Returns a 2D vector with values of the matrix diagonal.
Definition SquareMatrix2.h:807
const T * operator()() const
Access operator.
Definition SquareMatrix2.h:1048
SquareMatrixT2< T > & operator*=(const SquareMatrixT2< T > &matrix)
Multiplies and assigns two matrices.
Definition SquareMatrix2.h:975
bool eigenSystem(T &eigenValue0, T &eigenValue1, VectorT2< T > &eigenVector0, VectorT2< T > &eigenVector1) const
Performs an eigen value analysis.
Definition SquareMatrix2.h:1079
void toNull()
Sets the matrix to a zero matrix.
Definition SquareMatrix2.h:786
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 isUnit(const T eps=NumericT< T >::eps()) const
Returns whether this vector is a unit vector (whether the vector has the length 1).
Definition Vector2.h:752
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:1159
std::vector< SquareMatrixT2< T > > SquareMatricesT2
Definition of a typename alias for vectors with SquareMatrixT2 objects.
Definition SquareMatrix2.h:51
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