6#ifndef imath_TMatrix_included
7#define imath_TMatrix_included
31template <
int W
idth,
int Height,
typename Element =
double>
199 template <
int SecondW
idth>
202 for (
int resultY = 0; resultY < Height; ++resultY){
203 for (
int resultX = 0; resultX < SecondWidth; ++resultX){
205 for (
int i = 0; i < Width; ++i){
206 sum += m_elements[i][resultY] * matrix.m_elements[resultX][i];
209 result.m_elements[resultX][resultY] = sum;
217 template <
int SecondW
idth>
285 double minHhNorm = I_BIG_EPSILON)
const;
315 template <
int SecondW
idth>
338 typedef Element Elements[Width][Height];
346template <
int W
idth,
int Height,
typename Element>
351 GetTransposed(retVal);
357template <
int W
idth,
int Height,
typename Element>
362 GetTransposed(retVal);
368template <
int W
idth,
int Height,
typename Element>
373 GetMultiplied(vector, retVal);
379template <
int W
idth,
int Height,
typename Element>
384 GetAdded(matrix, retVal);
390template <
int W
idth,
int Height,
typename Element>
395 GetSubstracted(matrix, retVal);
401template <
int W
idth,
int Height,
typename Element>
412template <
int W
idth,
int Height,
typename Element>
417 GetScaled(value, retVal);
423template <
int W
idth,
int Height,
typename Element>
426 GetAdded(matrix, *
this);
432template <
int W
idth,
int Height,
typename Element>
435 GetSubstracted(matrix, *
this);
441template <
int W
idth,
int Height,
typename Element>
444 GetScaled(value, *
this);
450template <
int W
idth,
int Height,
typename Element>
453 return matrix * value;
459template <
int W
idth,
int Height,
typename Element>
465template <
int W
idth,
int Height,
typename Element>
470 for (
int x = 0; x < Width; ++x){
471 for (
int y = 0; y < Height; ++y){
472 m_elements[x][y] = 0;
478 for (
int x = 0; x < Width; ++x){
479 for (
int y = 0; y < Height; ++y){
480 m_elements[x][y] = 1;
486 for (
int x = 0; x < Width; ++x){
487 for (
int y = 0; y < Height; ++y){
489 m_elements[x][y] = 1;
492 m_elements[x][y] = 0;
498 case MIM_UPPER_TRIANGLE:
499 for (
int x = 0; x < Width; ++x){
500 for (
int y = 0; y < Height; ++y){
502 m_elements[x][y] = 1;
505 m_elements[x][y] = 0;
511 case MIM_DOWNER_TRIANGLE:
512 for (
int x = 0; x < Width; ++x){
513 for (
int y = 0; y < Height; ++y){
515 m_elements[x][y] = 1;
518 m_elements[x][y] = 0;
527template <
int W
idth,
int Height,
typename Element>
530 for (
int x = 0; x < Width; ++x){
531 for (
int y = 0; y < Height; ++y){
532 m_elements[x][y] = matrix.m_elements[x][y];
540template <
int W
idth,
int Height,
typename Element>
543 for (
int x = 0; x < Width; ++x){
544 for (
int y = 0; y < Height; ++y){
545 m_elements[x][y] = 0;
551template <
int W
idth,
int Height,
typename Element>
558template <
int W
idth,
int Height,
typename Element>
565template <
int W
idth,
int Height,
typename Element>
572template <
int W
idth,
int Height,
typename Element>
579template <
int W
idth,
int Height,
typename Element>
588template <
int W
idth,
int Height,
typename Element>
591 return (sizes[0] == Width) && (sizes[1] == Height);
595template <
int W
idth,
int Height,
typename Element>
611template <
int W
idth,
int Height,
typename Element>
616 return (size == Width);
619 return (size == Height);
627template <
int W
idth,
int Height,
typename Element>
630 return m_elements[index[0]][index[1]];
634template <
int W
idth,
int Height,
typename Element>
637 return m_elements[x][y];
641template <
int W
idth,
int Height,
typename Element>
644 return m_elements[index[0]][index[1]];
648template <
int W
idth,
int Height,
typename Element>
651 return m_elements[x][y];
655template <
int W
idth,
int Height,
typename Element>
658 m_elements[index[0]][index[1]] = value;
662template <
int W
idth,
int Height,
typename Element>
665 m_elements[x][y] = value;
669template <
int W
idth,
int Height,
typename Element>
672 for (
int x = 0; x < Width; ++x){
673 for (
int y = 0; y < Height; ++y){
675 m_elements[x][y] = 0;
678 m_elements[x][y] = 1;
685template <
int W
idth,
int Height,
typename Element>
688 double retVal = m_elements[0][0];
690 for (
int x = 0; x < Width; ++x){
691 for (
int y = 0; y < Height; ++y){
692 double value = m_elements[x][y];
704template <
int W
idth,
int Height,
typename Element>
707 double retVal = m_elements[0][0];
709 for (
int x = 0; x < Width; ++x){
710 for (
int y = 0; y < Height; ++y){
711 double value = m_elements[x][y];
723template <
int W
idth,
int Height,
typename Element>
726 for (
int x = 0; x < Width; ++x){
727 for (
int y = 0; y < Height; ++y){
728 result.m_elements[x][y] = -m_elements[x][y];
734template <
int W
idth,
int Height,
typename Element>
737 for (
int x = 0; x < Width; ++x){
738 for (
int y = 0; y < Height; ++y){
739 result.m_elements[x][y] = m_elements[x][y] + matrix.m_elements[x][y];
745template <
int W
idth,
int Height,
typename Element>
748 for (
int x = 0; x < Width; ++x){
749 for (
int y = 0; y < Height; ++y){
750 result.m_elements[x][y] = m_elements[x][y] - matrix.m_elements[x][y];
756template <
int W
idth,
int Height,
typename Element>
759 for (
int resultY = 0; resultY < Height; ++resultY){
761 for (
int i = 0; i < Width; ++i){
762 sum += m_elements[i][resultY] * vector[i];
765 result[resultY] = sum;
770template <
int W
idth,
int Height,
typename Element>
773 for (
int x = 0; x < Width; ++x){
774 for (
int y = 0; y < Height; ++y){
775 result.m_elements[x][y] = m_elements[x][y] * value;
781template <
int W
idth,
int Height,
typename Element>
784 for (
int x = 0; x < Width; ++x){
785 for (
int y = 0; y < Height; ++y){
786 result.m_elements[y][x] = m_elements[x][y];
792template <
int W
idth,
int Height,
typename Element>
795 int commonSize = qMin(Width, Height);
798 for (
int i = 0; i < commonSize; ++i){
799 retVal += m_elements[i][i];
806template <
int W
idth,
int Height,
typename Element>
811 for (
int x = 0; x < Width; ++x){
812 for (
int y = 0; y < Height; ++y){
813 double value = m_elements[x][y];
814 retVal += double(value * value);
821template <
int W
idth,
int Height,
typename Element>
824 return qSqrt(GetFrobeniusNorm2());
828template <
int W
idth,
int Height,
typename Element>
831 if (Width != Height){
838 for (
int i = 0; i < maxIterations; ++i){
850 for (index[0] = 0; index[0] < Width; ++index[0]){
851 for (index[1] = 0; index[1] < Width; ++index[1]){
852 if (index[0] != index[1]){
853 double element = matrixR.
GetAt(index);
855 residue += element * element;
860 if (residue <= tolerance){
861 for (
int i = 0; i < Height; ++i){
862 diagonalD[i] = matrixR.
GetAt(i, i);
873template <
int W
idth,
int Height,
typename Element>
876 Q_ASSERT(columnIndex < Width);
877 Q_ASSERT(columnIndex >= 0);
879 for (
int y = 0; y < Height; ++y){
880 result[y] = m_elements[columnIndex][y];
885template <
int W
idth,
int Height,
typename Element>
888 Q_ASSERT(columnIndex < Width);
889 Q_ASSERT(columnIndex >= 0);
891 for (
int y = 0; y < Height; ++y){
892 m_elements[columnIndex][y] = columnVector[y];
897template <
int W
idth,
int Height,
typename Element>
900 Q_ASSERT(rowIndex < Height);
902 for (
int x = 0; x < Width; ++x){
903 result[rowIndex] = m_elements[x][rowIndex];
908template <
int W
idth,
int Height,
typename Element>
913 double minHhNorm)
const
915 if (&result !=
this){
919 int columnsCount = qMin(Height - 1, Width);
920 if ((maxColumns >= 0) && (maxColumns < columnsCount)){
921 columnsCount = maxColumns;
924 for (
int hhIndex = 0; hhIndex < columnsCount; ++hhIndex){
926 for (
istd::CIndex2d normIndex(hhIndex, hhIndex); normIndex[1] < Height; ++normIndex[1]){
927 double element = result.
GetAt(normIndex);
929 hhNorm2 += element * element;
932 if (hhNorm2 < minHhNorm){
938 double hhLength = (element0 >= 0)? qSqrt(hhNorm2): -qSqrt(hhNorm2);
940 double hhVector0 = element0 + hhLength;
941 double hhVectorNorm2 = hhNorm2 + hhVector0 * hhVector0 - element0 * element0;
945 for (matrixIndex[0] = hhIndex + 1; matrixIndex[0] < Width; ++matrixIndex[0]){
946 matrixIndex[1] = hhIndex;
947 double dotProduct = result[matrixIndex] * hhVector0;
948 for (matrixIndex[1]++; matrixIndex[1] < Height; ++matrixIndex[1]){
952 double reflexionFactor = 2 * dotProduct / hhVectorNorm2;
953 matrixIndex[1] = hhIndex;
954 result[matrixIndex] -= hhVector0 * reflexionFactor;
955 for (matrixIndex[1]++; matrixIndex[1] < Height; ++matrixIndex[1]){
961 if (matrixQPtr !=
nullptr){
963 for (matrix2Index[0] = 0; matrix2Index[0] < Height; ++matrix2Index[0]){
964 matrix2Index[1] = hhIndex;
965 double dotProduct = matrixQPtr->
GetAt(matrix2Index) * hhVector0;
966 for (++matrix2Index[1]; matrix2Index[1] < Height; ++matrix2Index[1]){
967 dotProduct += matrixQPtr->
GetAt(matrix2Index) * result[
istd::CIndex2d(hhIndex, matrix2Index[1])];
970 double reflexionFactor = 2 * dotProduct / hhVectorNorm2;
971 matrix2Index[1] = hhIndex;
972 matrixQPtr->
GetAtRef(matrix2Index) -= hhVector0 * reflexionFactor;
973 for (matrix2Index[1]++; matrix2Index[1] < Height; ++matrix2Index[1]){
981 for (
int i = hhIndex + 1; i < Height; ++i){
990template <
int W
idth,
int Height,
typename Element>
1004 if ((size[0] != Width) || (size[1] != Height)) {
1010 [&](
auto& archive) {
1011 for (
int y = 0; y < Height; ++y) {
1012 for (
int x = 0; x < Width; ++x) {
1013 if (!archive.
ProcessLeaf(cellTag, m_elements[x][y])) {
1023template <
int W
idth,
int Height,
typename Element>
1026 for (
int x = 0; x < Width; ++x){
1027 for (
int y = 0; y < Height; ++y){
1028 if (m_elements[x][y] != matrix.m_elements[x][y]){
1038template <
int W
idth,
int Height,
typename Element>
1041 for (
int x = 0; x < Width; ++x){
1042 for (
int y = 0; y < Height; ++y){
1043 if (m_elements[x][y] != matrix.m_elements[x][y]){
1053template <
int W
idth,
int Height,
typename Element>
1056 return m_elements[index[0]][index[1]];
1060template <
int W
idth,
int Height,
typename Element>
1063 return m_elements[index[0]][index[1]];
Definition of mathematical matrix with fixed dimensions.
TVector< Height, Element > GetMultiplied(const TVector< Width, Element > &vector) const
Get result of multiplication of this matrix and some vector.
bool operator==(const TMatrix< Width, Height, Element > &matrix) const
TMatrix< Height, Width, Element > GetTransposed() const
Get transposed matrix.
void GetAdded(const TMatrix< Width, Height, Element > &matrix, TMatrix< Width, Height, Element > &result) const
Get sum of two matrices.
imath::TVector< Height, Element > ColumnVector
double GetTrace() const
Get trace of this matrix.
void GetMultiplied(const TMatrix< SecondWidth, Width, Element > &matrix, TMatrix< SecondWidth, Height, Element > &result) const
Get result of multiplication of two matrices.
TMatrix< Width, Height, Element > & operator-=(const TMatrix< Width, Height, Element > &matrix)
TMatrix(const TMatrix &matrix)
Copy constructor.
TMatrix(MatrixInitMode mode)
Create matrix with initialization to some specified one.
int GetDimensionsCount() const
Get number of dimensions of this array.
void GetNegated(TMatrix< Width, Height, Element > &result)
Get result matrix with negated all elements.
bool SetSizes(const SizesType &sizes)
Set list of all sizes.
const SizesType & GetSizes() const
Get list of all sizes.
const ElementType & GetAt(int x, int y) const
Get element stored at specified index.
TMatrix< SecondWidth, Height, Element > operator*(const TMatrix< SecondWidth, Width, Element > &matrix) const
TMatrix< Width, Height, Element > operator-(const TMatrix< Width, Height, Element > &matrix) const
imath::TVector< Width, Element > RowVector
double GetFrobeniusNorm2() const
void GetMultiplied(const TVector< Width, Element > &vector, TVector< Height, Element > &result) const
Get result of multiplication of this matrix and some vector.
bool SetSize(int dimension, int size)
Set size of array for specified dimension.
@ MIM_ZERO
All elements initialized to 0.
@ MIM_ONES
All elements initialized to 1.
@ MIM_IDENTITY
Identity matrix.
@ MIM_DOWNER_TRIANGLE
Downer triangle matrix.
@ MIM_UPPER_TRIANGLE
Upper triangle matrix.
double GetFrobeniusNorm() const
void Transpose()
Transpose matrix.
void GetScaled(double value, TMatrix< Width, Height, Element > &result) const
Get result of multiplication of this matrix with scalar value.
void SetAt(int x, int y, const ElementType &value)
Set element at specified index.
void GetRowVector(int rowIndex, TVector< Width, Element > &result)
Get single row as vector.
void InitToIdentity()
Create identity matrix.
ElementType & operator[](const IndexType &index)
TMatrix< Width, Height, Element > & operator*=(double value)
int GetSize(int dimension) const
Get size of array for specified dimension.
TMatrix()
Create matrix without initialization.
TMatrix< Width, Height, Element > & operator+=(const TMatrix< Width, Height, Element > &matrix)
ElementType & GetAtRef(int x, int y)
Get reference to element stored at specified index.
bool GetDecompositionQDQ(TMatrix< Height, Height, Element > &matrixQ, TVector< Height, Element > &diagonalD, double tolerance=I_BIG_EPSILON, int maxIterations=100) const
Calculate decomposition in form of QDQ where Q is orthogonal matrix and D is diagonal one.
TMatrix< Width, Height, Element > operator*(double value) const
double GetMinElement() const
void GetColumnVector(int columnIndex, TVector< Height, Element > &result) const
Get single column as vector.
TMatrix< SecondWidth, Height, Element > GetMultiplied(const TMatrix< SecondWidth, Width, Element > &matrix) const
Get result of multiplication of two matrices.
ElementType & GetAtRef(const IndexType &index)
Get reference to element stored at specified index.
void SetAt(const IndexType &index, const ElementType &value)
Set element at specified index.
void Reset()
Set all matrix cells to zero.
const ElementType & GetAt(const IndexType &index) const
Get element stored at specified index.
void SetColumnVector(int columnIndex, const TVector< Height, Element > &columnVector)
Set a single column vector to matrix.
bool SetDimensionsCount(int count)
Set number of dimensions of this array.
TMatrix< Width, Height, Element > operator-()
bool Serialize(iser::IArchive &archive)
void GetTransposed(TMatrix< Height, Width, Element > &result) const
Get transposed matrix.
void GetSubstracted(const TMatrix< Width, Height, Element > &matrix, TMatrix< Width, Height, Element > &result) const
Get result of substraction of two matrices.
double GetMaxElement() const
TMatrix< Width, Height, Element > operator+(const TMatrix< Width, Height, Element > &matrix) const
bool operator!=(const TMatrix< Width, Height, Element > &matrix) const
void Clear()
Set all matrix cells to zero.
bool IsDimensionsCountFixed() const
Check, if number dimensions is fixed.
bool GetTriangleDecomposed(TMatrix< Width, Height, Element > &result, TMatrix< Height, Height, Element > *matrixQPtr=nullptr, int maxColumns=-1, double minHhNorm=I_BIG_EPSILON) const
Transform matrix to upper triangle form using method of Householder reflexions.
const ElementType & operator[](const IndexType &index) const
Implementation of fixed-size mathematical vector with specified type of elements.
Process tag used to group data in archive stream.
@ Group
Normal tag used for grouping of tags or processed elements.
@ WeakLeaf
Weak leaf tag, if possible it will be not outputted.
static bool SerializeIndex2d(iser::IArchive &archive, const iser::CArchiveTag &tag, istd::CIndex2d &index)
Serialize index 2D.
Represent input/output persistence archive.
virtual bool ProcessLeaf(const CArchiveTag &tag, bool &value)=0
Process primitive type.
virtual bool ProcessGroup(const CArchiveTag &tag, std::function< bool(IArchive &)> groupArchiveFn)=0
Process some group of elements encapsulated in single tag.
virtual bool IsChanging() const =0
Check if this archive processing change the object state.
Index implementation for addressing elements in 2D-space.
Package with mathematical functions and algebraical primitives.
CVarMatrix operator*(double value, const imath::CVarMatrix &matrix)
Contains general persistence mechanism with basic archives implementations.