8#ifndef META_OCEAN_MATH_SQUARE_MATRIX_2_H
9#define META_OCEAN_MATH_SQUARE_MATRIX_2_H
102 template <
typename U>
122 template <
typename U>
137 template <
typename U>
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;
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);
502 template <
typename U>
512 template <
typename U>
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)
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;
647 values_[3] = (eigenVector0.
x() * eigenValue1 * eigenVector1.
y() - eigenVector0.
y() * eigenValue0 * eigenVector1.
x()) /
determinant;
671 ocean_assert(
false &&
"Could not invert the matrix.");
687 const T factor = T(1.0) / det;
703 const T factor = T(1.0) / det;
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(
false &&
"This should never happen!");
738 const T adjustedDistance = distance / absolusteAverageEnergy;
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);
915 return !(*
this == matrix);
977 *
this = *
this * matrix;
1005template <
typename T>
1008 ocean_assert(index < 4u);
1012template <
typename T>
1015 ocean_assert(index < 4u);
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);
1040template <
typename T>
1043 ocean_assert(index < 4u);
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();
1094 if (eigenValue0 < eigenValue1)
1096 std::swap(eigenValue0, eigenValue1);
1101 const T factor = T(1.0) /
values_[2];
1107 const T factor = T(1.0) /
values_[1];
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>
1218 return messageObject <<
"|" << matrix(0, 0) <<
", " << matrix(0, 1) <<
"|\n|" << matrix(1, 0) <<
", " << matrix(1, 1) <<
"|";
static bool solveQuadratic(const T a, const T b, const T c, T &x1, T &x2)
Solves an quadratic equation with the form:
Definition Equation.h:168
Messenger object, one object for each message.
Definition Messenger.h:448
static constexpr T weakEps()
Returns a weak epsilon.
static T abs(const T value)
Returns the absolute value of a given value.
Definition Numeric.h:1220
static constexpr bool isWeakEqualEps(const T value)
Returns whether a value is smaller than or equal to a weak epsilon.
Definition Numeric.h:2171
static T sqrt(const T value)
Returns the square root of a given value.
Definition Numeric.h:1537
static constexpr T eps()
Returns a small epsilon.
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
static constexpr bool isNotEqualEps(const T value)
Returns whether a value is not smaller than or equal to a small epsilon.
Definition Numeric.h:2246
static bool isNotEqual(const T first, const T second)
Returns whether two values are not equal up to a small epsilon.
Definition Numeric.h:2622
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
bool invert(SquareMatrixT2< T > &invertedMatrix) const
Inverts the matrix and returns the result as parameter.
Definition SquareMatrix2.h:695
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
T * data()
Returns a pointer to the internal values.
Definition SquareMatrix2.h:858
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
T & operator[](const unsigned int index)
Element operator.
Definition SquareMatrix2.h:1013
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
Scalar 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
SquareMatrixT2(const U *arrayValues, const bool valuesRowAligned)
Creates a new SquareMatrixT2 object by four elements.
Definition SquareMatrix2.h:586
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
VectorT2< T > operator*(const VectorT2< T > &vector) const
Multiply operator for a 2D vector.
Definition SquareMatrix2.h:982
SquareMatrixT2< T > & operator*=(const T value)
Multiplies and assigns this matrix with a scalar value.
Definition SquareMatrix2.h:995
friend class SquareMatrixT2
Definition SquareMatrix2.h:74
SquareMatrixT2(const SquareMatrixT2< U > &matrix)
Copy constructor for a matrix with difference element data type than T.
Definition SquareMatrix2.h:529
SquareMatrixT2(const VectorT2< T > &diagonal)
Creates a new SquareMatrixT2 object by a given diagonal vector.
Definition SquareMatrix2.h:557
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
SquareMatrixT2(const T eigenValue0, const T eigenValue1, const VectorT2< T > &eigenVector0, const VectorT2< T > &eigenVector1)
Creates a covariance matrix by two eigen values and two corresponding eigen vectors.
Definition SquareMatrix2.h:636
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
T operator()(const unsigned int row, const unsigned int column) const
Element operator.
Definition SquareMatrix2.h:1020
void toIdentity()
Sets the matrix to the identity matrix.
Definition SquareMatrix2.h:777
SquareMatrixT2< T > operator-(const SquareMatrixT2< T > &matrix) const
Subtracts two matrices.
Definition SquareMatrix2.h:936
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
T & operator()(const unsigned int index)
Element operator.
Definition SquareMatrix2.h:1041
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
SquareMatrixT2(const U *arrayValues)
Creates a new SquareMatrixT2 object by four elements of float type U.
Definition SquareMatrix2.h:567
T determinant() const
Returns the determinant of the matrix.
Definition SquareMatrix2.h:749
SquareMatrixT2< T > operator*(const T value) const
Multiplies this matrix with a scalar value.
Definition SquareMatrix2.h:989
T operator()(const unsigned int index) const
Element operator.
Definition SquareMatrix2.h:1034
T * operator()()
Access operator.
Definition SquareMatrix2.h:1054
SquareMatrixT2()
Creates a new SquareMatrixT2 object with undefined elements.
Definition SquareMatrix2.h:522
size_t operator()(const SquareMatrixT2< T > &matrix) const
Hash function.
Definition SquareMatrix2.h:1060
SquareMatrixT2< T > operator-() const
Returns the negative matrix of this matrix (all matrix elements are multiplied by -1).
Definition SquareMatrix2.h:953
static std::vector< SquareMatrixT2< T > > matrices2matrices(const SquareMatrixT2< U > *matrices, const size_t size)
Converts matrices with specific data type to matrices with different data type.
Definition SquareMatrix2.h:1187
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
Scalar values_[4]
Definition SquareMatrix2.h:518
SquareMatrixT2(const bool setToIdentity)
Creates a new SquareMatrixT2 object.
Definition SquareMatrix2.h:538
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
void copyElements(T *arrayValues, const bool columnAligned=true) const
Copies the elements of this matrix to an array with floating point values of type T.
Definition SquareMatrix2.h:886
VectorT2< Scalar > diagonal() const
SquareMatrixT2(const T *arrayValues)
Creates a new SquareMatrixT2 object by four elements.
Definition SquareMatrix2.h:578
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
SquareMatrixT2(const T &m00, const T &m10, const T &m01, const T &m11)
Creates a 2x2 rotation matrix by 4 given matrix elements.
Definition SquareMatrix2.h:626
T & operator()(const unsigned int row, const unsigned int column)
Element operator.
Definition SquareMatrix2.h:1027
SquareMatrixT2(const T *arrayValues, const bool valuesRowAligned)
Creates a new SquareMatrixT2 object by four elements.
Definition SquareMatrix2.h:608
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:703
const T & y() const noexcept
Returns the y value.
Definition Vector2.h:715
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:745
bool normalize()
Normalizes this vector.
Definition Vector2.h:605
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
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
SquareMatrixT2< float > SquareMatrixF2
Instantiation of the SquareMatrixT2 template class using a single precision float data type.
Definition SquareMatrix2.h:43
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