Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-04 08:45:49

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 //! Standard_Real tab1[10][20];
0069 //! Standard_Real 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   friend class math_VectorBase<>;
0080 
0081   //! Constructs a non-initialized  matrix of range [LowerRow..UpperRow,
0082   //! LowerCol..UpperCol]
0083   //! For the constructed matrix:
0084   //! -   LowerRow and UpperRow are the indexes of the
0085   //! lower and upper bounds of a row, and
0086   //! -   LowerCol and UpperCol are the indexes of the
0087   //! lower and upper bounds of a column.
0088   Standard_EXPORT math_Matrix(const Standard_Integer LowerRow,
0089                               const Standard_Integer UpperRow,
0090                               const Standard_Integer LowerCol,
0091                               const Standard_Integer UpperCol);
0092 
0093   //! constructs a non-initialized matrix of range [LowerRow..UpperRow,
0094   //! LowerCol..UpperCol]
0095   //! whose values are all initialized with the value InitialValue.
0096   Standard_EXPORT math_Matrix(const Standard_Integer LowerRow,
0097                               const Standard_Integer UpperRow,
0098                               const Standard_Integer LowerCol,
0099                               const Standard_Integer UpperCol,
0100                               const Standard_Real    InitialValue);
0101 
0102   //! constructs a matrix of range [LowerRow..UpperRow,
0103   //! LowerCol..UpperCol]
0104   //! Sharing data with a "C array" pointed by Tab.
0105   Standard_EXPORT math_Matrix(const Standard_Address Tab,
0106                               const Standard_Integer LowerRow,
0107                               const Standard_Integer UpperRow,
0108                               const Standard_Integer LowerCol,
0109                               const Standard_Integer UpperCol);
0110 
0111   //! constructs a matrix for copy in initialization.
0112   //! An exception is raised if the matrixes have not the same dimensions.
0113   Standard_EXPORT math_Matrix(const math_Matrix& Other);
0114 
0115   //! Initialize all the elements of a matrix to InitialValue.
0116   Standard_EXPORT void Init(const Standard_Real InitialValue);
0117 
0118   //! Returns the number of rows  of this matrix.
0119   //! Note that for a matrix A you always have the following relations:
0120   //! - A.RowNumber() = A.UpperRow() -   A.LowerRow() + 1
0121   //! - A.ColNumber() = A.UpperCol() -   A.LowerCol() + 1
0122   //! - the length of a row of A is equal to the number of columns of A,
0123   //! - the length of a column of A is equal to the number of
0124   //! rows of A.returns the row range of a matrix.
0125   Standard_Integer RowNumber() const;
0126 
0127   //! Returns the number of rows  of this matrix.
0128   //! Note that for a matrix A you always have the following relations:
0129   //! - A.RowNumber() = A.UpperRow() -   A.LowerRow() + 1
0130   //! - A.ColNumber() = A.UpperCol() -   A.LowerCol() + 1
0131   //! - the length of a row of A is equal to the number of columns of A,
0132   //! - the length of a column of A is equal to the number of
0133   //! rows of A.returns the row range of a matrix.
0134   Standard_Integer ColNumber() const;
0135 
0136   //! Returns the value of the Lower index of the row
0137   //! range of a matrix.
0138   Standard_Integer LowerRow() const;
0139 
0140   //! Returns the Upper index of the row range
0141   //! of a matrix.
0142   Standard_Integer UpperRow() const;
0143 
0144   //! Returns the value of the Lower index of the
0145   //! column range of a matrix.
0146   Standard_Integer LowerCol() const;
0147 
0148   //! Returns the value of the upper index of the
0149   //! column range of a matrix.
0150   Standard_Integer UpperCol() const;
0151 
0152   //! Computes the determinant of a matrix.
0153   //! An exception is raised if the matrix is not a square matrix.
0154   Standard_EXPORT Standard_Real Determinant() const;
0155 
0156   //! Transposes a given matrix.
0157   //! An exception is raised if the matrix is not a square matrix.
0158   Standard_EXPORT void Transpose();
0159 
0160   //! Inverts a matrix using Gauss algorithm.
0161   //! Exception NotSquare is raised if the matrix is not square.
0162   //! Exception SingularMatrix is raised if the matrix is singular.
0163   Standard_EXPORT void Invert();
0164 
0165   //! Sets this matrix to the product of the matrix Left, and the matrix Right.
0166   //! Example
0167   //! math_Matrix A (1, 3, 1, 3);
0168   //! math_Matrix B (1, 3, 1, 3);
0169   //! // A = ... , B = ...
0170   //! math_Matrix C (1, 3, 1, 3);
0171   //! C.Multiply(A, B);
0172   //! Exceptions
0173   //! Standard_DimensionError if matrices are of incompatible dimensions, i.e. if:
0174   //! -   the number of columns of matrix Left, or the number of
0175   //! rows of matrix TLeft is not equal to the number of rows
0176   //! of matrix Right, or
0177   //! -   the number of rows of matrix Left, or the number of
0178   //! columns of matrix TLeft is not equal to the number of
0179   //! rows of this matrix, or
0180   //! -   the number of columns of matrix Right is not equal to
0181   //! the number of columns of this matrix.
0182   Standard_EXPORT void Multiply(const Standard_Real Right);
0183 
0184   void operator*=(const Standard_Real Right) { Multiply(Right); }
0185 
0186   //! multiplies all the elements of a matrix by the
0187   //! value <Right>.
0188   Standard_NODISCARD Standard_EXPORT math_Matrix Multiplied(const Standard_Real Right) const;
0189 
0190   Standard_NODISCARD math_Matrix operator*(const Standard_Real Right) const
0191   {
0192     return Multiplied(Right);
0193   }
0194 
0195   //! Sets this matrix to the product of the
0196   //! transposed matrix TLeft, and the matrix Right.
0197   //! Example
0198   //! math_Matrix A (1, 3, 1, 3);
0199   //! math_Matrix B (1, 3, 1, 3);
0200   //! // A = ... , B = ...
0201   //! math_Matrix C (1, 3, 1, 3);
0202   //! C.Multiply(A, B);
0203   //! Exceptions
0204   //! Standard_DimensionError if matrices are of incompatible dimensions, i.e. if:
0205   //! -   the number of columns of matrix Left, or the number of
0206   //! rows of matrix TLeft is not equal to the number of rows
0207   //! of matrix Right, or
0208   //! -   the number of rows of matrix Left, or the number of
0209   //! columns of matrix TLeft is not equal to the number of
0210   //! rows of this matrix, or
0211   //! -   the number of columns of matrix Right is not equal to
0212   //! the number of columns of this matrix.
0213   Standard_NODISCARD Standard_EXPORT math_Matrix TMultiplied(const Standard_Real Right) const;
0214   friend math_Matrix operator*(const Standard_Real Left, const math_Matrix& Right);
0215 
0216   //! divides all the elements of a matrix by the value <Right>.
0217   //! An exception is raised if <Right> = 0.
0218   Standard_EXPORT void Divide(const Standard_Real Right);
0219 
0220   void operator/=(const Standard_Real Right) { Divide(Right); }
0221 
0222   //! divides all the elements of a matrix by the value <Right>.
0223   //! An exception is raised if <Right> = 0.
0224   Standard_NODISCARD Standard_EXPORT math_Matrix Divided(const Standard_Real Right) const;
0225 
0226   Standard_NODISCARD math_Matrix operator/(const Standard_Real Right) const
0227   {
0228     return Divided(Right);
0229   }
0230 
0231   //! adds the matrix <Right> to a matrix.
0232   //! An exception is raised if the dimensions are different.
0233   //! Warning
0234   //! In order to save time when copying matrices, it is
0235   //! preferable to use operator += or the function Add
0236   //! whenever possible.
0237   Standard_EXPORT void Add(const math_Matrix& Right);
0238 
0239   void operator+=(const math_Matrix& Right) { Add(Right); }
0240 
0241   //! adds the matrix <Right> to a matrix.
0242   //! An exception is raised if the dimensions are different.
0243   Standard_NODISCARD Standard_EXPORT math_Matrix Added(const math_Matrix& Right) const;
0244 
0245   Standard_NODISCARD math_Matrix operator+(const math_Matrix& Right) const { return Added(Right); }
0246 
0247   //! sets a  matrix to the addition of <Left> and <Right>.
0248   //! An exception is raised if the dimensions are different.
0249   Standard_EXPORT void Add(const math_Matrix& Left, const math_Matrix& Right);
0250 
0251   //! Subtracts the matrix <Right> from <me>.
0252   //! An exception is raised if the dimensions are different.
0253   //! Warning
0254   //! In order to avoid time-consuming copying of matrices, it
0255   //! is preferable to use operator -= or the function
0256   //! Subtract whenever possible.
0257   Standard_EXPORT void Subtract(const math_Matrix& Right);
0258 
0259   void operator-=(const math_Matrix& Right) { Subtract(Right); }
0260 
0261   //! Returns the result of the subtraction of <Right> from <me>.
0262   //! An exception is raised if the dimensions are different.
0263   Standard_NODISCARD Standard_EXPORT math_Matrix Subtracted(const math_Matrix& Right) const;
0264 
0265   Standard_NODISCARD math_Matrix operator-(const math_Matrix& Right) const
0266   {
0267     return Subtracted(Right);
0268   }
0269 
0270   //! Sets the values of this matrix,
0271   //! -   from index I1 to index I2 on the row dimension, and
0272   //! -   from index J1 to index J2 on the column dimension,
0273   //! to those of matrix M.
0274   //! Exceptions
0275   //! Standard_DimensionError if:
0276   //! -   I1 is less than the index of the lower row bound of this matrix, or
0277   //! -   I2 is greater than the index of the upper row bound of this matrix, or
0278   //! -   J1 is less than the index of the lower column bound of this matrix, or
0279   //! -   J2 is greater than the index of the upper column bound of this matrix, or
0280   //! -   I2 - I1 + 1 is not equal to the number of rows of matrix M, or
0281   //! -   J2 - J1 + 1 is not equal to the number of columns of matrix M.
0282   Standard_EXPORT void Set(const Standard_Integer I1,
0283                            const Standard_Integer I2,
0284                            const Standard_Integer J1,
0285                            const Standard_Integer J2,
0286                            const math_Matrix&     M);
0287 
0288   //! Sets the row of index Row of a matrix to the vector <V>.
0289   //! An exception is raised if the dimensions are different.
0290   //! An exception is raises if <Row> is inferior to the lower
0291   //! row of the matrix or <Row> is superior to the upper row.
0292   Standard_EXPORT void SetRow(const Standard_Integer Row, const math_VectorBase<>& V);
0293 
0294   //! Sets the column of index Col of a matrix to the vector <V>.
0295   //! An exception is raised if the dimensions are different.
0296   //! An exception is raises if <Col> is inferior to the lower
0297   //! column of the matrix or <Col> is superior to the upper
0298   //! column.
0299   Standard_EXPORT void SetCol(const Standard_Integer Col, const math_VectorBase<>& V);
0300 
0301   //! Sets the diagonal of a matrix to the value <Value>.
0302   //! An exception is raised if the matrix is not square.
0303   Standard_EXPORT void SetDiag(const Standard_Real Value);
0304 
0305   //! Returns the row of index Row of a matrix.
0306   Standard_EXPORT math_VectorBase<> Row(const Standard_Integer Row) const;
0307 
0308   //! Returns the column of index <Col> of a matrix.
0309   Standard_EXPORT math_VectorBase<> Col(const Standard_Integer Col) const;
0310 
0311   //! Swaps the rows of index Row1 and Row2.
0312   //! An exception is raised if <Row1> or <Row2> is out of range.
0313   Standard_EXPORT void SwapRow(const Standard_Integer Row1, const Standard_Integer Row2);
0314 
0315   //! Swaps the columns of index <Col1> and <Col2>.
0316   //! An exception is raised if <Col1> or <Col2> is out of range.
0317   Standard_EXPORT void SwapCol(const Standard_Integer Col1, const Standard_Integer Col2);
0318 
0319   //! Teturns the transposed of a matrix.
0320   //! An exception is raised if the matrix is not a square matrix.
0321   Standard_NODISCARD Standard_EXPORT math_Matrix Transposed() const;
0322 
0323   //! Returns the inverse of a matrix.
0324   //! Exception NotSquare is raised if the matrix is not square.
0325   //! Exception SingularMatrix is raised if the matrix is singular.
0326   Standard_EXPORT math_Matrix Inverse() const;
0327 
0328   //! Returns the product of the transpose of a matrix with
0329   //! the matrix <Right>.
0330   //! An exception is raised if the dimensions are different.
0331   Standard_EXPORT math_Matrix TMultiply(const math_Matrix& Right) const;
0332 
0333   //! Computes a matrix as the product of 2 vectors.
0334   //! An exception is raised if the dimensions are different.
0335   //! <me> = <Left> * <Right>.
0336   Standard_EXPORT void Multiply(const math_VectorBase<>& Left, const math_VectorBase<>& Right);
0337 
0338   //! Computes a matrix as the product of 2 matrixes.
0339   //! An exception is raised if the dimensions are different.
0340   Standard_EXPORT void Multiply(const math_Matrix& Left, const math_Matrix& Right);
0341 
0342   //! Computes a matrix to the product of the transpose of
0343   //! the matrix <TLeft> with the matrix <Right>.
0344   //! An exception is raised if the dimensions are different.
0345   Standard_EXPORT void TMultiply(const math_Matrix& TLeft, const math_Matrix& Right);
0346 
0347   //! Sets a matrix to the Subtraction of the matrix <Right>
0348   //! from the matrix <Left>.
0349   //! An exception is raised if the dimensions are different.
0350   Standard_EXPORT void Subtract(const math_Matrix& Left, const math_Matrix& Right);
0351 
0352   //! Accesses (in read or write mode) the value of index <Row>
0353   //! and <Col> of a matrix.
0354   //! An exception is raised if <Row> and <Col> are not
0355   //! in the correct range.
0356   Standard_Real& Value(const Standard_Integer Row, const Standard_Integer Col) const;
0357 
0358   Standard_Real& operator()(const Standard_Integer Row, const Standard_Integer Col) const
0359   {
0360     return Value(Row, Col);
0361   }
0362 
0363   //! Matrixes are copied through assignment.
0364   //! An exception is raised if the dimensions are different.
0365   Standard_EXPORT math_Matrix& Initialized(const math_Matrix& Other);
0366 
0367   math_Matrix& operator=(const math_Matrix& Other) { return Initialized(Other); }
0368 
0369   //! Returns the product of 2 matrices.
0370   //! An exception is raised if the dimensions are different.
0371   Standard_EXPORT void Multiply(const math_Matrix& Right);
0372 
0373   void operator*=(const math_Matrix& Right) { Multiply(Right); }
0374 
0375   //! Returns the product of 2 matrices.
0376   //! An exception is raised if the dimensions are different.
0377   Standard_NODISCARD Standard_EXPORT math_Matrix Multiplied(const math_Matrix& Right) const;
0378 
0379   Standard_NODISCARD math_Matrix operator*(const math_Matrix& Right) const
0380   {
0381     return Multiplied(Right);
0382   }
0383 
0384   //! Returns the product of a matrix by a vector.
0385   //! An exception is raised if the dimensions are different.
0386   Standard_NODISCARD Standard_EXPORT math_VectorBase<> Multiplied(
0387     const math_VectorBase<>& Right) const;
0388   Standard_NODISCARD Standard_EXPORT math_VectorBase<> operator*(
0389     const math_VectorBase<>& Right) const;
0390 
0391   //! Returns the opposite of a matrix.
0392   //! An exception is raised if the dimensions are different.
0393   Standard_EXPORT math_Matrix Opposite();
0394 
0395   math_Matrix operator-() { return Opposite(); }
0396 
0397   //! Prints information on the current state of the object.
0398   //! Is used to redefine the operator <<.
0399   Standard_EXPORT void Dump(Standard_OStream& o) const;
0400 
0401 protected:
0402   //! The new lower row of the matrix is set to <LowerRow>
0403   Standard_EXPORT void SetLowerRow(const Standard_Integer LowerRow);
0404 
0405   //! The new lower column of the matrix is set to the column
0406   //! of range <LowerCol>.
0407   Standard_EXPORT void SetLowerCol(const Standard_Integer LowerCol);
0408 
0409   //! The new lower row of the matrix is set to <LowerRow>
0410   //! and the new lower column of the matrix is set to the column
0411   //! of range <LowerCol>.
0412   void SetLower(const Standard_Integer LowerRow, const Standard_Integer LowerCol);
0413 
0414 private:
0415   Standard_Integer LowerRowIndex;
0416   Standard_Integer UpperRowIndex;
0417   Standard_Integer LowerColIndex;
0418   Standard_Integer UpperColIndex;
0419   math_DoubleTab   Array;
0420 };
0421 
0422 #include <math_Matrix.lxx>
0423 
0424 #endif // _math_Matrix_HeaderFile