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