merged tag ooo/DEV300_m102
[LibreOffice.git] / hwpfilter / source / solver.cpp
blobe21d9b310debfcda5d1f0510d60958b000914877
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include <math.h>
29 #include "solver.h"
31 //---------------------------------------------------------------------------
32 double** mgcLinearSystemD::NewMatrix (int N)
34 double** A = new double*[N];
35 if ( !A )
36 return 0;
38 for (int row = 0; row < N; row++)
40 A[row] = new double[N];
41 if ( !A[row] )
43 for (int i = 0; i < row; i++)
44 delete[] A[i];
45 return 0;
47 for (int col = 0; col < N; col++)
48 A[row][col] = 0;
50 return A;
52 //---------------------------------------------------------------------------
53 void mgcLinearSystemD::DeleteMatrix (int N, double** A)
55 for (int row = 0; row < N; row++)
56 delete[] A[row];
57 delete[] A;
59 //---------------------------------------------------------------------------
60 double* mgcLinearSystemD::NewVector (int N)
62 double* B = new double[N];
63 if ( !B )
64 return 0;
66 for (int row = 0; row < N; row++)
67 B[row] = 0;
68 return B;
70 //---------------------------------------------------------------------------
71 int mgcLinearSystemD::Solve (int n, double** a, double* b)
73 int* indxc = new int[n];
74 if ( !indxc )
75 return 0;
76 int* indxr = new int[n];
77 if ( !indxr ) {
78 delete[] indxc;
79 return 0;
81 int* ipiv = new int[n];
82 if ( !ipiv ) {
83 delete[] indxc;
84 delete[] indxr;
85 return 0;
88 int i, j, k;
89 int irow = 0;
90 int icol = 0;
91 double big, pivinv, save;
93 for (j = 0; j < n; j++)
94 ipiv[j] = 0;
96 for (i = 0; i < n; i++)
98 big = 0;
99 for (j = 0; j < n; j++)
101 if ( ipiv[j] != 1 )
103 for (k = 0; k < n; k++)
105 if ( ipiv[k] == 0 )
107 if ( fabs(a[j][k]) >= big )
109 big = fabs(a[j][k]);
110 irow = j;
111 icol = k;
114 else if ( ipiv[k] > 1 )
116 delete[] ipiv;
117 delete[] indxr;
118 delete[] indxc;
119 return 0;
124 ipiv[icol]++;
126 if ( irow != icol )
128 double* rowptr = a[irow];
129 a[irow] = a[icol];
130 a[icol] = rowptr;
132 save = b[irow];
133 b[irow] = b[icol];
134 b[icol] = save;
137 indxr[i] = irow;
138 indxc[i] = icol;
139 if ( a[icol][icol] == 0 )
141 delete[] ipiv;
142 delete[] indxr;
143 delete[] indxc;
144 return 0;
147 pivinv = 1/a[icol][icol];
148 a[icol][icol] = 1;
149 for (k = 0; k < n; k++)
150 a[icol][k] *= pivinv;
151 b[icol] *= pivinv;
153 for (j = 0; j < n; j++)
155 if ( j != icol )
157 save = a[j][icol];
158 a[j][icol] = 0;
159 for (k = 0; k < n; k++)
160 a[j][k] -= a[icol][k]*save;
161 b[j] -= b[icol]*save;
166 for (j = n-1; j >= 0; j--)
168 if ( indxr[j] != indxc[j] )
170 for (k = 0; k < n; k++)
172 save = a[k][indxr[j]];
173 a[k][indxr[j]] = a[k][indxc[j]];
174 a[k][indxc[j]] = save;
179 delete[] ipiv;
180 delete[] indxr;
181 delete[] indxc;
182 return 1;