Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-31 09:36:33

0001 // Created on: 1991-05-07
0002 // Created by: Laurent PAINNOT
0003 // Copyright (c) 1991-1999 Matra Datavision
0004 // Copyright (c) 1999-2014 OPEN CASCADE SAS
0005 //
0006 // This file is part of Open CASCADE Technology software library.
0007 //
0008 // This library is free software; you can redistribute it and/or modify it under
0009 // the terms of the GNU Lesser General Public License version 2.1 as published
0010 // by the Free Software Foundation, with special exception defined in the file
0011 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0012 // distribution for complete text of the license and disclaimer of any warranty.
0013 //
0014 // Alternatively, this file may be used under the terms of Open CASCADE
0015 // commercial license or contractual agreement.
0016 
0017 #ifndef _math_Matrix_HeaderFile
0018 #define _math_Matrix_HeaderFile
0019 
0020 #include <Standard.hxx>
0021 #include <Standard_DefineAlloc.hxx>
0022 #include <NCollection_Allocator.hxx>
0023 
0024 #include <math_DoubleTab.hxx>
0025 #include <Standard_OStream.hxx>
0026 
0027 // resolve name collisions with X11 headers
0028 #ifdef Opposite
0029   #undef Opposite
0030 #endif
0031 
0032 template <typename T = double>
0033 class math_VectorBase;
0034 
0035 //! This class implements the real matrix abstract data type.
0036 //! Matrixes can have an arbitrary range which must be defined
0037 //! at the declaration and cannot be changed after this declaration
0038 //! math_Matrix(-3,5,2,4); //a vector with range [-3..5, 2..4]
0039 //! Matrix values may be initialized and
0040 //! retrieved using indexes which must lie within the range
0041 //! of definition of the matrix.
0042 //! Matrix objects follow "value semantics", that is, they
0043 //! cannot be shared and are copied through assignment
0044 //! Matrices are copied through assignment:
0045 //! @code
0046 //! math_Matrix M2(1, 9, 1, 3);
0047 //! ...
0048 //! M2 = M1;
0049 //! M1(1) = 2.0;//the matrix M2 will not be modified.
0050 //! @endcode
0051 //! The exception RangeError is raised when trying to access
0052 //! outside the range of a matrix :
0053 //! @code
0054 //! M1(11, 1)=0.0// --> will raise RangeError.
0055 //! @endcode
0056 //!
0057 //! The exception DimensionError is raised when the dimensions of
0058 //! two matrices or vectors are not compatible.
0059 //! @code
0060 //! math_Matrix M3(1, 2, 1, 2);
0061 //! M3 = M1;   // will raise DimensionError
0062 //! M1.Add(M3) // --> will raise DimensionError.
0063 //! @endcode
0064 //! A Matrix can be constructed with a pointer to "c array".
0065 //! It allows to carry the bounds inside the matrix.
0066 //! Example :
0067 //! @code
0068 //! double tab1[10][20];
0069 //! double tab2[200];
0070 //!
0071 //! math_Matrix A (tab1[0][0], 1, 10, 1, 20);
0072 //! math_Matrix B (tab2[0],    1, 10, 1, 20);
0073 //! @endcode
0074 class math_Matrix
0075 {
0076 public:
0077   DEFINE_STANDARD_ALLOC
0078 
0079   //! Constructs a non-initialized matrix of range [LowerRow..UpperRow,
0080   //! LowerCol..UpperCol]
0081   //! For the constructed matrix:
0082   //! -   LowerRow and UpperRow are the indexes of the
0083   //! lower and upper bounds of a row, and
0084   //! -   LowerCol and UpperCol are the indexes of the
0085   //! lower and upper bounds of a column.
0086   inline math_Matrix(const int LowerRow,
0087                      const int UpperRow,
0088                      const int LowerCol,
0089                      const int UpperCol);
0090 
0091   //! constructs a non-initialized matrix of range [LowerRow..UpperRow,
0092   //! LowerCol..UpperCol]
0093   //! whose values are all initialized with the value InitialValue.
0094   inline math_Matrix(const int    LowerRow,
0095                      const int    UpperRow,
0096                      const int    LowerCol,
0097                      const int    UpperCol,
0098                      const double InitialValue);
0099 
0100   //! constructs a matrix of range [LowerRow..UpperRow,
0101   //! LowerCol..UpperCol]
0102   //! Sharing data with a "C array" pointed by Tab.
0103   inline math_Matrix(void* const Tab,
0104                      const int   LowerRow,
0105                      const int   UpperRow,
0106                      const int   LowerCol,
0107                      const int   UpperCol);
0108 
0109   //! constructs a matrix for copy in initialization.
0110   //! An exception is raised if the matrixes have not the same dimensions.
0111   inline math_Matrix(const math_Matrix& Other);
0112 
0113   //! Move constructor
0114   inline math_Matrix(math_Matrix&& Other) noexcept;
0115 
0116   //! Initialize all the elements of a matrix to InitialValue.
0117   inline void Init(const double InitialValue) noexcept;
0118 
0119   //! Returns the number of rows of this matrix.
0120   //! Note that for a matrix A you always have the following relations:
0121   //! - A.RowNumber() = A.UpperRow() -   A.LowerRow() + 1
0122   //! - A.ColNumber() = A.UpperCol() -   A.LowerCol() + 1
0123   //! - the length of a row of A is equal to the number of columns of A,
0124   //! - the length of a column of A is equal to the number of
0125   //! rows of A.returns the row range of a matrix.
0126   int RowNumber() const noexcept;
0127 
0128   //! Returns the number of rows of this matrix.
0129   //! Note that for a matrix A you always have the following relations:
0130   //! - A.RowNumber() = A.UpperRow() -   A.LowerRow() + 1
0131   //! - A.ColNumber() = A.UpperCol() -   A.LowerCol() + 1
0132   //! - the length of a row of A is equal to the number of columns of A,
0133   //! - the length of a column of A is equal to the number of
0134   //! rows of A.returns the row range of a matrix.
0135   int ColNumber() const noexcept;
0136 
0137   //! Returns the value of the Lower index of the row
0138   //! range of a matrix.
0139   int LowerRow() const noexcept;
0140 
0141   //! Returns the Upper index of the row range
0142   //! of a matrix.
0143   int UpperRow() const noexcept;
0144 
0145   //! Returns the value of the Lower index of the
0146   //! column range of a matrix.
0147   int LowerCol() const noexcept;
0148 
0149   //! Returns the value of the upper index of the
0150   //! column range of a matrix.
0151   int UpperCol() const noexcept;
0152 
0153   //! Computes the determinant of a matrix.
0154   //! An exception is raised if the matrix is not a square matrix.
0155   Standard_EXPORT double Determinant() const;
0156 
0157   //! Transposes a given matrix.
0158   //! An exception is raised if the matrix is not a square matrix.
0159   inline void Transpose();
0160 
0161   //! Inverts a matrix using Gauss algorithm.
0162   //! Exception NotSquare is raised if the matrix is not square.
0163   //! Exception SingularMatrix is raised if the matrix is singular.
0164   Standard_EXPORT void Invert();
0165 
0166   //! Sets this matrix to the product of the matrix Left, and the matrix Right.
0167   //! Example
0168   //! math_Matrix A (1, 3, 1, 3);
0169   //! math_Matrix B (1, 3, 1, 3);
0170   //! // A = ... , B = ...
0171   //! math_Matrix C (1, 3, 1, 3);
0172   //! C.Multiply(A, B);
0173   //! Exceptions
0174   //! Standard_DimensionError if matrices are of incompatible dimensions, i.e. if:
0175   //! -   the number of columns of matrix Left, or the number of
0176   //! rows of matrix TLeft is not equal to the number of rows
0177   //! of matrix Right, or
0178   //! -   the number of rows of matrix Left, or the number of
0179   //! columns of matrix TLeft is not equal to the number of
0180   //! rows of this matrix, or
0181   //! -   the number of columns of matrix Right is not equal to
0182   //! the number of columns of this matrix.
0183   inline void Multiply(const double Right) noexcept;
0184 
0185   void operator*=(const double Right) noexcept { Multiply(Right); }
0186 
0187   //! multiplies all the elements of a matrix by the
0188   //! value <Right>.
0189   [[nodiscard]] inline math_Matrix Multiplied(const double Right) const noexcept;
0190 
0191   [[nodiscard]] math_Matrix operator*(const double Right) const noexcept
0192   {
0193     return Multiplied(Right);
0194   }
0195 
0196   //! Sets this matrix to the product of the
0197   //! transposed matrix TLeft, and the matrix Right.
0198   //! Example
0199   //! math_Matrix A (1, 3, 1, 3);
0200   //! math_Matrix B (1, 3, 1, 3);
0201   //! // A = ... , B = ...
0202   //! math_Matrix C (1, 3, 1, 3);
0203   //! C.Multiply(A, B);
0204   //! Exceptions
0205   //! Standard_DimensionError if matrices are of incompatible dimensions, i.e. if:
0206   //! -   the number of columns of matrix Left, or the number of
0207   //! rows of matrix TLeft is not equal to the number of rows
0208   //! of matrix Right, or
0209   //! -   the number of rows of matrix Left, or the number of
0210   //! columns of matrix TLeft is not equal to the number of
0211   //! rows of this matrix, or
0212   //! -   the number of columns of matrix Right is not equal to
0213   //! the number of columns of this matrix.
0214   [[nodiscard]] inline math_Matrix TMultiplied(const double Right) const noexcept;
0215   friend math_Matrix               operator*(const double Left, const math_Matrix& Right);
0216 
0217   //! divides all the elements of a matrix by the value <Right>.
0218   //! An exception is raised if <Right> = 0.
0219   inline void Divide(const double Right);
0220 
0221   void operator/=(const double Right) { Divide(Right); }
0222 
0223   //! divides all the elements of a matrix by the value <Right>.
0224   //! An exception is raised if <Right> = 0.
0225   [[nodiscard]] inline math_Matrix Divided(const double Right) const;
0226 
0227   [[nodiscard]] math_Matrix operator/(const double Right) const { return Divided(Right); }
0228 
0229   //! adds the matrix <Right> to a matrix.
0230   //! An exception is raised if the dimensions are different.
0231   //! Warning
0232   //! In order to save time when copying matrices, it is
0233   //! preferable to use operator += or the function Add
0234   //! whenever possible.
0235   inline void Add(const math_Matrix& Right);
0236 
0237   void operator+=(const math_Matrix& Right) { Add(Right); }
0238 
0239   //! adds the matrix <Right> to a matrix.
0240   //! An exception is raised if the dimensions are different.
0241   [[nodiscard]] inline math_Matrix Added(const math_Matrix& Right) const;
0242 
0243   [[nodiscard]] math_Matrix operator+(const math_Matrix& Right) const { return Added(Right); }
0244 
0245   //! sets a matrix to the addition of <Left> and <Right>.
0246   //! An exception is raised if the dimensions are different.
0247   inline void Add(const math_Matrix& Left, const math_Matrix& Right);
0248 
0249   //! Subtracts the matrix <Right> from <me>.
0250   //! An exception is raised if the dimensions are different.
0251   //! Warning
0252   //! In order to avoid time-consuming copying of matrices, it
0253   //! is preferable to use operator -= or the function
0254   //! Subtract whenever possible.
0255   inline void Subtract(const math_Matrix& Right);
0256 
0257   void operator-=(const math_Matrix& Right) { Subtract(Right); }
0258 
0259   //! Returns the result of the subtraction of <Right> from <me>.
0260   //! An exception is raised if the dimensions are different.
0261   [[nodiscard]] inline math_Matrix Subtracted(const math_Matrix& Right) const;
0262 
0263   [[nodiscard]] math_Matrix operator-(const math_Matrix& Right) const { return Subtracted(Right); }
0264 
0265   //! Sets the values of this matrix,
0266   //! -   from index I1 to index I2 on the row dimension, and
0267   //! -   from index J1 to index J2 on the column dimension,
0268   //! to those of matrix M.
0269   //! Exceptions
0270   //! Standard_DimensionError if:
0271   //! -   I1 is less than the index of the lower row bound of this matrix, or
0272   //! -   I2 is greater than the index of the upper row bound of this matrix, or
0273   //! -   J1 is less than the index of the lower column bound of this matrix, or
0274   //! -   J2 is greater than the index of the upper column bound of this matrix, or
0275   //! -   I2 - I1 + 1 is not equal to the number of rows of matrix M, or
0276   //! -   J2 - J1 + 1 is not equal to the number of columns of matrix M.
0277   inline void Set(const int I1, const int I2, const int J1, const int J2, const math_Matrix& M);
0278 
0279   //! Sets the row of index Row of a matrix to the vector <V>.
0280   //! An exception is raised if the dimensions are different.
0281   //! An exception is raises if <Row> is inferior to the lower
0282   //! row of the matrix or <Row> is superior to the upper row.
0283   Standard_EXPORT void SetRow(const int Row, const math_VectorBase<>& V);
0284 
0285   //! Sets the column of index Col of a matrix to the vector <V>.
0286   //! An exception is raised if the dimensions are different.
0287   //! An exception is raises if <Col> is inferior to the lower
0288   //! column of the matrix or <Col> is superior to the upper
0289   //! column.
0290   Standard_EXPORT void SetCol(const int Col, const math_VectorBase<>& V);
0291 
0292   //! Sets the diagonal of a matrix to the value <Value>.
0293   //! An exception is raised if the matrix is not square.
0294   inline void SetDiag(const double Value);
0295 
0296   //! Returns the row of index Row of a matrix.
0297   Standard_EXPORT math_VectorBase<> Row(const int Row) const;
0298 
0299   //! Returns the column of index <Col> of a matrix.
0300   Standard_EXPORT math_VectorBase<> Col(const int Col) const;
0301 
0302   //! Swaps the rows of index Row1 and Row2.
0303   //! An exception is raised if <Row1> or <Row2> is out of range.
0304   Standard_EXPORT void SwapRow(const int Row1, const int Row2);
0305 
0306   //! Swaps the columns of index <Col1> and <Col2>.
0307   //! An exception is raised if <Col1> or <Col2> is out of range.
0308   Standard_EXPORT void SwapCol(const int Col1, const int Col2);
0309 
0310   //! Teturns the transposed of a matrix.
0311   //! An exception is raised if the matrix is not a square matrix.
0312   [[nodiscard]] inline math_Matrix Transposed() const;
0313 
0314   //! Returns the inverse of a matrix.
0315   //! Exception NotSquare is raised if the matrix is not square.
0316   //! Exception SingularMatrix is raised if the matrix is singular.
0317   Standard_EXPORT math_Matrix Inverse() const;
0318 
0319   //! Returns the product of the transpose of a matrix with
0320   //! the matrix <Right>.
0321   //! An exception is raised if the dimensions are different.
0322   inline math_Matrix TMultiply(const math_Matrix& Right) const;
0323 
0324   //! Computes a matrix as the product of 2 vectors.
0325   //! An exception is raised if the dimensions are different.
0326   //! <me> = <Left> * <Right>.
0327   Standard_EXPORT void Multiply(const math_VectorBase<>& Left, const math_VectorBase<>& Right);
0328 
0329   //! Computes a matrix as the product of 2 matrixes.
0330   //! An exception is raised if the dimensions are different.
0331   inline void Multiply(const math_Matrix& Left, const math_Matrix& Right);
0332 
0333   //! Computes a matrix to the product of the transpose of
0334   //! the matrix <TLeft> with the matrix <Right>.
0335   //! An exception is raised if the dimensions are different.
0336   inline void TMultiply(const math_Matrix& TLeft, const math_Matrix& Right);
0337 
0338   //! Sets a matrix to the Subtraction of the matrix <Right>
0339   //! from the matrix <Left>.
0340   //! An exception is raised if the dimensions are different.
0341   inline void Subtract(const math_Matrix& Left, const math_Matrix& Right);
0342 
0343   //! Accesses the value of index <Row>
0344   //! and <Col> of a matrix.
0345   //! An exception is raised if <Row> and <Col> are not
0346   //! in the correct range.
0347   const double& Value(const int Row, const int Col) const;
0348 
0349   //! Accesses (in read or write mode) the value of index <Row>
0350   //! and <Col> of a matrix.
0351   //! An exception is raised if <Row> and <Col> are not
0352   //! in the correct range.
0353   double& Value(const int Row, const int Col);
0354 
0355   const double& operator()(const int Row, const int Col) const { return Value(Row, Col); }
0356 
0357   double& operator()(const int Row, const int Col) { return Value(Row, Col); }
0358 
0359   //! Matrixes are copied through assignment.
0360   //! An exception is raised if the dimensions are different.
0361   inline math_Matrix& Initialized(const math_Matrix& Other);
0362 
0363   math_Matrix& operator=(const math_Matrix& Other) { return Initialized(Other); }
0364 
0365   //! Move assignment operator
0366   inline math_Matrix& operator=(math_Matrix&& Other);
0367 
0368   //! Returns the product of 2 matrices.
0369   //! An exception is raised if the dimensions are different.
0370   inline void Multiply(const math_Matrix& Right);
0371 
0372   void operator*=(const math_Matrix& Right) { Multiply(Right); }
0373 
0374   //! Returns the product of 2 matrices.
0375   //! An exception is raised if the dimensions are different.
0376   [[nodiscard]] inline math_Matrix Multiplied(const math_Matrix& Right) const;
0377 
0378   [[nodiscard]] math_Matrix operator*(const math_Matrix& Right) const { return Multiplied(Right); }
0379 
0380   //! Returns the product of a matrix by a vector.
0381   //! An exception is raised if the dimensions are different.
0382   [[nodiscard]] Standard_EXPORT math_VectorBase<> Multiplied(const math_VectorBase<>& Right) const;
0383   [[nodiscard]] Standard_EXPORT math_VectorBase<> operator*(const math_VectorBase<>& Right) const;
0384 
0385   //! Returns the opposite of a matrix.
0386   //! An exception is raised if the dimensions are different.
0387   inline math_Matrix Opposite() const;
0388 
0389   math_Matrix operator-() const { return Opposite(); }
0390 
0391   //! Prints information on the current state of the object.
0392   //! Is used to redefine the operator <<.
0393   inline void Dump(Standard_OStream& o) const;
0394 
0395 protected:
0396   //! The new lower row of the matrix is set to <LowerRow>
0397   inline void SetLowerRow(const int LowerRow) noexcept;
0398 
0399   //! The new lower column of the matrix is set to the column
0400   //! of range <LowerCol>.
0401   inline void SetLowerCol(const int LowerCol) noexcept;
0402 
0403   //! The new lower row of the matrix is set to <LowerRow>
0404   //! and the new lower column of the matrix is set to the column
0405   //! of range <LowerCol>.
0406   void SetLower(const int LowerRow, const int LowerCol) noexcept;
0407 
0408 private:
0409   math_DoubleTab Array;
0410 };
0411 
0412 #include <math_Matrix.lxx>
0413 
0414 #endif // _math_Matrix_HeaderFile