2 // class : Resolution d'un systeme de Cramer
4 // Copyright (C) 2009-2013 CEA/DEN, EDF R&D
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 #include "Hex_defines.hxx"
26 #include "hexa_base.hxx"
31 class HEXABLOCKENGINE_EXPORT Cramer
37 void defMatrix (double matuser[]);
38 int resoudre (double matr[], double second[], double sol[]);
39 void multiply (double vect[], double result[]);
42 int resoudre (double msecond[], double solution[]);
43 double determinant ();
44 void defCofacteur (Cramer* orig, int lig, int col);
45 bool cestNul (double x) { return x > -1e-10 && x < 1e-10 ; }
52 // ========================================================= Constructeur
53 inline Cramer::Cramer (int size)
56 matrix = new double* [mat_size];
57 col_saved = new double [mat_size];
60 co_facteur = new Cramer (mat_size-1);
62 for (int nl=0 ; nl<mat_size ; nl++)
64 matrix[nl] = new double [mat_size];
65 for (int nc=0 ; nc<mat_size ; nc++)
69 // ========================================================= Destructeur
70 inline Cramer::~Cramer ()
72 for (int nl=0 ; nl<mat_size ; nl++)
79 // ========================================================= defMatrix
80 inline void Cramer::defMatrix (double matuser[])
82 for (int nl=0 ; nl<mat_size ; nl++)
83 for (int nc=0 ; nc<mat_size ; nc++)
84 matrix [nl][nc] = matuser [nl*mat_size + nc];
86 // ========================================================= determinant
87 inline double Cramer::determinant ()
93 else if (mat_size == 2)
95 det =matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
99 for (int ni=0 ; ni<mat_size ; ni++)
101 if (NOT cestNul (matrix[ni][0]))
103 co_facteur->defCofacteur (this, ni, 0);
104 double signe = (ni MODULO 2) ? -1.0 : 1.0;
105 det += signe*matrix[ni][0] * co_facteur->determinant ();
110 // ========================================================= defCofacteur
111 inline void Cramer::defCofacteur (Cramer* orig, int lig, int col)
113 for (int nl=0 ; nl<mat_size ; nl++)
115 int ni = nl < lig ? nl : nl+1;
116 for (int nc=0 ; nc<mat_size ; nc++)
118 int nj = nc < col ? nc : nc+1;
119 matrix[nl][nc] = orig->matrix[ni][nj];
123 // ========================================================= resoudre
124 inline int Cramer::resoudre (double mat[], double second[], double sol[])
127 int ier = resoudre (second, sol);
130 // ========================================================= multiply
131 inline void Cramer::multiply (double facteur[], double produit[])
133 for (int ni=0 ; ni<mat_size ; ni++)
136 for (int nj=0 ; nj<mat_size ; nj++)
137 produit [ni] += matrix [ni][nj] * facteur [nj];
140 // ========================================================= resoudre
141 inline int Cramer::resoudre (double msecond[], double solution[])
143 double det = determinant();
147 for (int nj=0 ; nj<mat_size ; nj++)
149 for (int ni=0 ; ni<mat_size ; ni++)
151 col_saved [ni] = matrix [ni][nj];
152 matrix [ni][nj] = msecond [ni];
154 solution [nj] = determinant() / det;
156 for (int ni=0 ; ni<mat_size ; ni++)
157 matrix [ni][nj] = col_saved [ni];