update dev300-m58
[ooovba.git] / hwpfilter / source / solver.cpp
blobdad2535648852afcecfad25f671ec6ef319fb966
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: solver.cpp,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <math.h>
32 #include "solver.h"
34 //---------------------------------------------------------------------------
35 double** mgcLinearSystemD::NewMatrix (int N)
37 double** A = new double*[N];
38 if ( !A )
39 return 0;
41 for (int row = 0; row < N; row++)
43 A[row] = new double[N];
44 if ( !A[row] )
46 for (int i = 0; i < row; i++)
47 delete[] A[i];
48 return 0;
50 for (int col = 0; col < N; col++)
51 A[row][col] = 0;
53 return A;
55 //---------------------------------------------------------------------------
56 void mgcLinearSystemD::DeleteMatrix (int N, double** A)
58 for (int row = 0; row < N; row++)
59 delete[] A[row];
60 delete[] A;
62 //---------------------------------------------------------------------------
63 double* mgcLinearSystemD::NewVector (int N)
65 double* B = new double[N];
66 if ( !B )
67 return 0;
69 for (int row = 0; row < N; row++)
70 B[row] = 0;
71 return B;
73 //---------------------------------------------------------------------------
74 int mgcLinearSystemD::Solve (int n, double** a, double* b)
76 int* indxc = new int[n];
77 if ( !indxc )
78 return 0;
79 int* indxr = new int[n];
80 if ( !indxr ) {
81 delete[] indxc;
82 return 0;
84 int* ipiv = new int[n];
85 if ( !ipiv ) {
86 delete[] indxc;
87 delete[] indxr;
88 return 0;
91 int i, j, k;
92 int irow = 0;
93 int icol = 0;
94 double big, pivinv, save;
96 for (j = 0; j < n; j++)
97 ipiv[j] = 0;
99 for (i = 0; i < n; i++)
101 big = 0;
102 for (j = 0; j < n; j++)
104 if ( ipiv[j] != 1 )
106 for (k = 0; k < n; k++)
108 if ( ipiv[k] == 0 )
110 if ( fabs(a[j][k]) >= big )
112 big = fabs(a[j][k]);
113 irow = j;
114 icol = k;
117 else if ( ipiv[k] > 1 )
119 delete[] ipiv;
120 delete[] indxr;
121 delete[] indxc;
122 return 0;
127 ipiv[icol]++;
129 if ( irow != icol )
131 double* rowptr = a[irow];
132 a[irow] = a[icol];
133 a[icol] = rowptr;
135 save = b[irow];
136 b[irow] = b[icol];
137 b[icol] = save;
140 indxr[i] = irow;
141 indxc[i] = icol;
142 if ( a[icol][icol] == 0 )
144 delete[] ipiv;
145 delete[] indxr;
146 delete[] indxc;
147 return 0;
150 pivinv = 1/a[icol][icol];
151 a[icol][icol] = 1;
152 for (k = 0; k < n; k++)
153 a[icol][k] *= pivinv;
154 b[icol] *= pivinv;
156 for (j = 0; j < n; j++)
158 if ( j != icol )
160 save = a[j][icol];
161 a[j][icol] = 0;
162 for (k = 0; k < n; k++)
163 a[j][k] -= a[icol][k]*save;
164 b[j] -= b[icol]*save;
169 for (j = n-1; j >= 0; j--)
171 if ( indxr[j] != indxc[j] )
173 for (k = 0; k < n; k++)
175 save = a[k][indxr[j]];
176 a[k][indxr[j]] = a[k][indxc[j]];
177 a[k][indxc[j]] = save;
182 delete[] ipiv;
183 delete[] indxr;
184 delete[] indxc;
185 return 1;