1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2006 Torus Knot Software Ltd |
---|
8 | Also see acknowledgements in Readme.html |
---|
9 | |
---|
10 | This program is free software; you can redistribute it and/or modify it under |
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
13 | version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
23 | ----------------------------------------------------------------------------- |
---|
24 | */ |
---|
25 | |
---|
26 | #include "OgreStableHeaders.h" |
---|
27 | #include "OgreCommon.h" |
---|
28 | #include "OgreNumerics.h" |
---|
29 | |
---|
30 | |
---|
31 | namespace Ogre |
---|
32 | { |
---|
33 | bool NumericSolver::solveNxNLinearSysDestr(int n, PreciseReal **coeff, PreciseReal *col) |
---|
34 | { |
---|
35 | // we'll use standard row reduction; since we only care about systems with unique |
---|
36 | // solutions, our job is slightly easier. more can probably be done later to improve |
---|
37 | // precision versus this naive method |
---|
38 | |
---|
39 | int i, j; |
---|
40 | |
---|
41 | for(j=0; j<n; j++) |
---|
42 | { |
---|
43 | // look for a row with a leading coefficient we can use to cancel the rest |
---|
44 | int nonzeroIndex = -1; |
---|
45 | for(i=j; i<n; i++) { |
---|
46 | if (coeff[i][j] != 0.0) { |
---|
47 | nonzeroIndex = i; |
---|
48 | break; |
---|
49 | } |
---|
50 | } |
---|
51 | if (nonzeroIndex < 0) |
---|
52 | return false; |
---|
53 | PreciseReal *tptr = coeff[j]; |
---|
54 | coeff[j] = coeff[nonzeroIndex]; |
---|
55 | coeff[nonzeroIndex] = tptr; |
---|
56 | PreciseReal tval = col[j]; |
---|
57 | col[j] = col[nonzeroIndex]; |
---|
58 | col[nonzeroIndex] = tval; |
---|
59 | nonzeroIndex = j; |
---|
60 | |
---|
61 | // normalize row to have leading coeff of 1 and kill other rows' entries |
---|
62 | PreciseReal invelt = 1.0 / coeff[nonzeroIndex][j]; |
---|
63 | int k; |
---|
64 | for (k=j; k<n; k++) |
---|
65 | coeff[nonzeroIndex][k] *= invelt; |
---|
66 | col[nonzeroIndex] *= invelt; |
---|
67 | for (i=0; i<n; i++) { |
---|
68 | if (i==nonzeroIndex || coeff[i][j] == 0.0) |
---|
69 | continue; |
---|
70 | PreciseReal temp = coeff[i][j]; |
---|
71 | for (k=j; k<n; k++) |
---|
72 | coeff[i][k] -= temp * coeff[nonzeroIndex][k]; |
---|
73 | col[i] -= temp * col[nonzeroIndex]; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | return true; |
---|
78 | } |
---|
79 | |
---|
80 | } |
---|