820bc8f055870d18ca9469b24c8d4f98986030c2
[occt.git] / src / math / math_Gauss.hxx
1 // Created on: 1991-05-13
2 // Created by: Laurent PAINNOT
3 // Copyright (c) 1991-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #ifndef _math_Gauss_HeaderFile
18 #define _math_Gauss_HeaderFile
19
20 #include <Standard.hxx>
21 #include <Standard_DefineAlloc.hxx>
22 #include <Standard_Handle.hxx>
23
24 #include <Standard_Boolean.hxx>
25 #include <math_Matrix.hxx>
26 #include <math_IntegerVector.hxx>
27 #include <Standard_Real.hxx>
28 #include <math_Vector.hxx>
29 #include <Standard_OStream.hxx>
30 #include <Message_ProgressRange.hxx>
31
32 class math_NotSquare;
33 class Standard_DimensionError;
34 class StdFail_NotDone;
35 class math_Matrix;
36
37 //! This class implements the Gauss LU decomposition (Crout algorithm)
38 //! with partial pivoting (rows interchange) of a square matrix and
39 //! the different possible derived calculation :
40 //! - solution of a set of linear equations.
41 //! - inverse of a matrix.
42 //! - determinant of a matrix.
43 class math_Gauss 
44 {
45 public:
46
47   DEFINE_STANDARD_ALLOC
48
49   //! Given an input n X n matrix A this constructor performs its LU
50   //! decomposition with partial pivoting (interchange of rows).
51   //! This LU decomposition is stored internally and may be used to
52   //! do subsequent calculation.
53   //! If the largest pivot found is less than MinPivot the matrix A is
54   //! considered as singular.
55   //! Exception NotSquare is raised if A is not a square matrix.
56   Standard_EXPORT math_Gauss(const math_Matrix& A, 
57                              const Standard_Real MinPivot = 1.0e-20, 
58                              const Message_ProgressRange& theProgress = Message_ProgressRange());
59   
60   //! Returns true if the computations are successful, otherwise returns false
61   Standard_Boolean IsDone() const { return Done; }
62
63   //! Given the input Vector B this routine returns the solution X of the set
64   //! of linear equations A . X = B.
65   //! Exception NotDone is raised if the decomposition of A was not done
66   //! successfully.
67   //! Exception DimensionError is raised if the range of B is not
68   //! equal to the number of rows of A.
69   Standard_EXPORT void Solve (const math_Vector& B, math_Vector& X) const;
70
71   //! Given the input Vector B this routine solves the set of linear
72   //! equations A . X = B. B is replaced by the vector solution X.
73   //! Exception NotDone is raised if the decomposition of A was not done
74   //! successfully.
75   //! Exception DimensionError is raised if the range of B is not
76   //! equal to the number of rows of A.
77   Standard_EXPORT void Solve (math_Vector& B) const;
78
79   //! This routine returns the value of the determinant of the previously LU
80   //! decomposed matrix A.
81   //! Exception NotDone may be raised if the decomposition of A was not done
82   //! successfully, zero is returned if the matrix A was considered as singular.
83   Standard_EXPORT Standard_Real Determinant() const;
84
85   //! This routine outputs Inv the inverse of the previously LU decomposed
86   //! matrix A.
87   //! Exception DimensionError is raised if the ranges of B are not
88   //! equal to the ranges of A.
89   Standard_EXPORT void Invert (math_Matrix& Inv) const;
90   
91   //! Prints on the stream o information on the current state
92   //! of the object.
93   //! Is used to redefine the operator <<.
94   Standard_EXPORT void Dump (Standard_OStream& o) const;
95
96 protected:
97
98   math_Matrix LU;
99   math_IntegerVector Index;
100   Standard_Real D;
101   Standard_Boolean Done;
102
103 };
104
105 inline Standard_OStream& operator<<(Standard_OStream& o, const math_Gauss& mG)
106 {
107   mG.Dump(o);
108   return o;
109 }
110
111 #endif // _math_Gauss_HeaderFile