8 #ifndef META_OCEAN_MATH_SQUARE_MATRIX_2_H
9 #define META_OCEAN_MATH_SQUARE_MATRIX_2_H
22 template <
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>
521 template <
typename T>
527 template <
typename T>
528 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]);
537 template <
typename T>
556 template <
typename T>
559 values[0] = diagonal[0];
562 values[3] = diagonal[1];
565 template <
typename T>
566 template <
typename U>
569 ocean_assert(arrayValues);
571 for (
unsigned int n = 0u; n < 4u; ++n)
573 values[n] = T(arrayValues[n]);
577 template <
typename T>
580 ocean_assert(arrayValues);
581 memcpy(values, arrayValues,
sizeof(T) * 4);
584 template <
typename T>
585 template <
typename U>
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)
601 values[n] = T(arrayValues[n]);
605 template <
typename T>
608 ocean_assert(arrayValues);
610 if (valuesRowAligned)
612 values[0] = arrayValues[0];
613 values[1] = arrayValues[2];
614 values[2] = arrayValues[1];
615 values[3] = arrayValues[3];
619 memcpy(values, arrayValues,
sizeof(T) * 4);
623 template <
typename T>
633 template <
typename T>
639 const T det = eigenVector0.
x() * eigenVector1.
y() - eigenVector1.
x() * eigenVector0.
y();
642 values[0] = (eigenVector0.
x() * eigenValue0 * eigenVector1.
y() - eigenVector0.
y() * eigenValue1 * eigenVector1.
x()) / det;
643 values[1] = (eigenValue0 - eigenValue1) * eigenVector0.
y() * eigenVector1.
y() / det;
644 values[2] = (eigenValue1 - eigenValue0) * eigenVector0.
x() * eigenVector1.
x() / det;
645 values[3] = (eigenVector0.
x() * eigenValue1 * eigenVector1.
y() - eigenVector0.
y() * eigenValue0 * eigenVector1.
x()) / det;
648 if (!std::is_same<Scalar, float>::value)
650 T debugEigenValue0, debugEigenValue1;
652 ocean_assert(eigenSystem(debugEigenValue0, debugEigenValue1, debugEigenVector0, debugEigenVector1));
662 template <
typename T>
668 template <
typename T>
671 const T tmp = values[1];
672 values[1] = values[2];
676 template <
typename T>
683 ocean_assert(
false &&
"Could not invert the matrix.");
690 template <
typename T>
693 const T det = determinant();
699 const T factor = T(1.0) / det;
701 *
this =
SquareMatrixT2<T>(values[3] * factor, -values[1] * factor, -values[2] * factor, values[0] * factor);
706 template <
typename T>
709 const T det = determinant();
715 const T factor = T(1.0) / det;
717 invertedMatrix =
SquareMatrixT2<T>(values[3] * factor, -values[1] * factor, -values[2] * factor, values[0] * factor);
719 #ifdef OCEAN_INTENSIVE_DEBUG
720 if (!std::is_same<T, float>::value)
726 for (
unsigned int n = 0; n < 4u; ++n)
732 T absolusteAverageEnergy = 0;
733 for (
unsigned int n = 0u; n < 4u; ++n)
735 absolusteAverageEnergy *= T(0.25);
739 if (absolusteAverageEnergy <= 1)
740 ocean_assert_accuracy(!
"This should never happen!");
743 const T adjustedDistance = distance / absolusteAverageEnergy;
753 template <
typename T>
756 return values[0] * values[3] - values[1] * values[2];
759 template <
typename T>
762 return values[0] + values[3];
765 template <
typename T>
779 const T u0 = (values[0] * values[3]) - (values[1] * values[2]);
780 const T v0 = (values[0] * b[1]) - (values[1] * b[0]);
787 x[0] = (b[0] - (values[2] * x[1])) / values[0];
795 template <
typename T>
804 template <
typename T>
813 template <
typename T>
819 template <
typename T>
825 template <
typename T>
831 template <
typename T>
837 template <
typename T>
843 template <
typename T>
849 template <
typename T>
855 template <
typename T>
858 ocean_assert(epsilon >= T(0));
863 template <
typename T>
870 template <
typename T>
876 template <
typename T>
882 template <
typename T>
883 template <
typename U>
886 ocean_assert(arrayValues);
890 arrayValues[0] = U(values[0]);
891 arrayValues[1] = U(values[1]);
892 arrayValues[2] = U(values[2]);
893 arrayValues[3] = U(values[3]);
897 arrayValues[0] = U(values[0]);
898 arrayValues[1] = U(values[2]);
899 arrayValues[2] = U(values[1]);
900 arrayValues[3] = U(values[3]);
904 template <
typename T>
907 ocean_assert(arrayValues);
911 arrayValues[0] = values[0];
912 arrayValues[1] = values[1];
913 arrayValues[2] = values[2];
914 arrayValues[3] = values[3];
918 arrayValues[0] = values[0];
919 arrayValues[1] = values[2];
920 arrayValues[2] = values[1];
921 arrayValues[3] = values[3];
925 template <
typename T>
928 return isEqual(matrix);
931 template <
typename T>
934 return !(*
this == matrix);
937 template <
typename T>
943 template <
typename T>
946 values[0] += matrix.
values[0];
947 values[1] += matrix.
values[1];
948 values[2] += matrix.
values[2];
949 values[3] += matrix.
values[3];
954 template <
typename T>
960 template <
typename T>
963 values[0] -= matrix.
values[0];
964 values[1] -= matrix.
values[1];
965 values[2] -= matrix.
values[2];
966 values[3] -= matrix.
values[3];
971 template <
typename T>
976 result.
values[ 0] = -values[ 0];
977 result.
values[ 1] = -values[ 1];
978 result.
values[ 2] = -values[ 2];
979 result.
values[ 3] = -values[ 3];
984 template <
typename T>
988 values[1] * matrix.
values[0] + values[3] * matrix.
values[1],
989 values[0] * matrix.
values[2] + values[2] * matrix.
values[3],
990 values[1] * matrix.
values[2] + values[3] * matrix.
values[3]);
993 template <
typename T>
996 *
this = *
this * matrix;
1000 template <
typename T>
1003 return VectorT2<T>(values[0] * vector[0] + values[2] * vector[1],
1004 values[1] * vector[0] + values[3] * vector[1]);
1007 template <
typename T>
1010 return SquareMatrixT2<T>(values[0] * value, values[1] * value, values[2] * value, values[3] * value);
1013 template <
typename T>
1024 template <
typename T>
1027 ocean_assert(index < 4u);
1028 return values[index];
1031 template <
typename T>
1034 ocean_assert(index < 4u);
1035 return values[index];
1038 template <
typename T>
1041 ocean_assert(row < 2u && column < 2u);
1042 return values[column * 2u + row];
1045 template <
typename T>
1048 ocean_assert(row < 2u && column < 2u);
1049 return values[column * 2u + row];
1052 template <
typename T>
1055 ocean_assert(index < 4u);
1056 return values[index];
1059 template <
typename T>
1062 ocean_assert(index < 4u);
1063 return values[index];
1066 template <
typename T>
1072 template <
typename T>
1078 template <
typename T>
1081 size_t seed = std::hash<T>{}(matrix.
values[0]);
1083 for (
unsigned int n = 1u; n < 4u; ++n)
1085 seed ^= std::hash<T>{}(matrix.
values[n]) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
1091 template <
typename T>
1097 template <
typename T>
1105 const T tr = trace();
1106 const T det = determinant();
1113 if (eigenValue0 < eigenValue1)
1115 std::swap(eigenValue0, eigenValue1);
1120 const T factor = T(1.0) / values[2];
1121 eigenVector0 =
VectorT2<T>(1, (eigenValue0 - values[0]) * factor);
1122 eigenVector1 =
VectorT2<T>(1, (eigenValue1 - values[0]) * factor);
1126 const T factor = T(1.0) / values[1];
1128 eigenVector0 =
VectorT2<T>((eigenValue0 - values[3]) * factor, T(1.0));
1129 eigenVector1 =
VectorT2<T>((eigenValue1 - values[3]) * factor, T(1.0));
1135 eigenVector0 =
VectorT2<T>(1, values[1] / (eigenValue0 - values[3]));
1145 eigenVector0 =
VectorT2<T>(values[2] / (eigenValue0 - values[0]), T(1.0));
1151 eigenVector1 =
VectorT2<T>(1, values[1] / (eigenValue1 - values[3]));
1161 eigenVector1 =
VectorT2<T>(values[2] / (eigenValue1 - values[0]), T(1.0));
1169 ocean_assert((std::is_same<T, float>::value) || (*
this * eigenVector0).isEqual(eigenVector0 * eigenValue0,
NumericT<T>::weakEps()));
1170 ocean_assert((std::is_same<T, float>::value) || (*
this * eigenVector1).isEqual(eigenVector1 * eigenValue1,
NumericT<T>::weakEps()));
1175 template <
typename T>
1176 template <
typename U>
1179 std::vector< SquareMatrixT2<T> > result;
1180 result.reserve(matrices.size());
1182 for (
typename std::vector<
SquareMatrixT2<U> >::const_iterator i = matrices.begin(); i != matrices.end(); ++i)
1204 template <
typename T>
1205 template <
typename U>
1208 std::vector< SquareMatrixT2<T> > result;
1209 result.reserve(size);
1211 for (
size_t n = 0; n < size; ++n)
1219 template <
typename T>
1222 stream <<
"|" << matrix(0, 0) <<
", " << matrix(0, 1) <<
"|" << std::endl;
1223 stream <<
"|" << matrix(1, 0) <<
", " << matrix(1, 1) <<
"|";
1228 template <
bool tActive,
typename T>
1229 MessageObject<tActive>&
operator<<(MessageObject<tActive>& messageObject,
const SquareMatrixT2<T>& matrix)
1231 return messageObject <<
"|" << matrix(0, 0) <<
", " << matrix(0, 1) <<
"|\n|" << matrix(1, 0) <<
", " << matrix(1, 1) <<
"|";
1234 template <
bool tActive,
typename T>
1235 MessageObject<tActive>&
operator<<(MessageObject<tActive>&& messageObject,
const SquareMatrixT2<T>& matrix)
1237 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:2386
static constexpr bool isEqualEps(const T value)
Returns whether a value is smaller than or equal to a small epsilon.
Definition: Numeric.h:2087
static constexpr bool isNotEqualEps(const T value)
Returns whether a value is not smaller than or equal to a small epsilon.
Definition: Numeric.h:2237
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:1092
bool isIdentity() const
Returns whether this matrix is the identity matrix.
Definition: SquareMatrix2.h:844
bool isSingular() const
Returns whether this matrix is singular (and thus cannot be inverted).
Definition: SquareMatrix2.h:850
SquareMatrixT2< T > & operator+=(const SquareMatrixT2< T > &matrix)
Adds and assigns two matrices.
Definition: SquareMatrix2.h:944
T norm() const
Returns the norm of this matrix that is the sum of the absolute matrix elements.
Definition: SquareMatrix2.h:832
VectorT2< T > xAxis() const
Returns the x axis which is the first column of the matrix.
Definition: SquareMatrix2.h:814
SquareMatrixT2< T > transposed() const
Returns the transposed of this matrix.
Definition: SquareMatrix2.h:663
bool operator!=(const SquareMatrixT2< T > &matrix) const
Returns whether two matrices are not identical up to a small epsilon.
Definition: SquareMatrix2.h:932
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:1177
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:864
SquareMatrixT2< T > & operator=(const SquareMatrixT2< T > &)=default
Default copy assignment operator.
T operator[](const unsigned int index) const
Element operator.
Definition: SquareMatrix2.h:1025
bool isNull() const
Returns whether this matrix is a null matrix.
Definition: SquareMatrix2.h:838
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:820
T trace() const
Returns the trace of the matrix which is the sum of the diagonal elements.
Definition: SquareMatrix2.h:760
bool operator==(const SquareMatrixT2< T > &matrix) const
Returns whether two matrices are identical up to a small epsilon.
Definition: SquareMatrix2.h:926
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:766
void toIdentity()
Sets the matrix to the identity matrix.
Definition: SquareMatrix2.h:796
SquareMatrixT2< T > operator*(const SquareMatrixT2< T > &matrix) const
Multiplies two matrices.
Definition: SquareMatrix2.h:985
SquareMatrixT2< T > inverted() const
Returns the inverted matrix of this matrix.
Definition: SquareMatrix2.h:677
const T * data() const
Returns a pointer to the internal values.
Definition: SquareMatrix2.h:871
SquareMatrixT2< T > & operator-=(const SquareMatrixT2< T > &matrix)
Subtracts and assigns two matrices.
Definition: SquareMatrix2.h:961
bool isSymmetric(const T epsilon=NumericT< T >::eps()) const
Returns whether this matrix is symmetric.
Definition: SquareMatrix2.h:856
T determinant() const
Returns the determinant of the matrix.
Definition: SquareMatrix2.h:754
T values[4]
The four values of the matrix.
Definition: SquareMatrix2.h:518
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:972
bool invert()
Inverts this matrix in place.
Definition: SquareMatrix2.h:691
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:884
SquareMatrixT2< T > operator+(const SquareMatrixT2< T > &matrix) const
Adds two matrices.
Definition: SquareMatrix2.h:938
SquareMatrixT2(const SquareMatrixT2< T > &matrix)=default
Copy constructor.
void transpose()
Transposes the matrix.
Definition: SquareMatrix2.h:669
VectorT2< T > diagonal() const
Returns a 2D vector with values of the matrix diagonal.
Definition: SquareMatrix2.h:826
const T * operator()() const
Access operator.
Definition: SquareMatrix2.h:1067
SquareMatrixT2< T > & operator*=(const SquareMatrixT2< T > &matrix)
Multiplies and assigns two matrices.
Definition: SquareMatrix2.h:994
bool eigenSystem(T &eigenValue0, T &eigenValue1, VectorT2< T > &eigenVector0, VectorT2< T > &eigenVector1) const
Performs an eigen value analysis.
Definition: SquareMatrix2.h:1098
void toNull()
Sets the matrix to a zero matrix.
Definition: SquareMatrix2.h:805
This class implements a vector with two elements.
Definition: Vector2.h:96
const T & x() const noexcept
Returns the x value.
Definition: Vector2.h:698
const T & y() const noexcept
Returns the y value.
Definition: Vector2.h:710
bool isEqual(const VectorT2< T > &vector, const T eps) const
Returns whether two vectors are equal up to a specified epsilon.
Definition: Vector2.h:746
T length() const
Returns the length of the vector.
Definition: Vector2.h:615
bool normalize()
Normalizes this vector.
Definition: Vector2.h:600
unsigned int sqrDistance(const char first, const char second)
Returns the square distance between two values.
Definition: base/Utilities.h:1089
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:22
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