fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / hwpfilter / source / solver.cxx
blob5ee767c168d5779ac59116e8f5c3861b42e9cef1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <math.h>
21 #include "solver.h"
24 double** mgcLinearSystemD::NewMatrix (int N)
26 double** A = new double*[N];
27 if ( !A )
28 return 0;
30 for (int row = 0; row < N; row++)
32 A[row] = new double[N];
33 if ( !A[row] )
35 for (int i = 0; i < row; i++)
36 delete[] A[i];
37 delete[] A;
38 return 0;
40 for (int col = 0; col < N; col++)
41 A[row][col] = 0;
43 return A;
46 void mgcLinearSystemD::DeleteMatrix (int N, double** A)
48 for (int row = 0; row < N; row++)
49 delete[] A[row];
50 delete[] A;
53 double* mgcLinearSystemD::NewVector (int N)
55 double* B = new double[N];
56 if ( !B )
57 return 0;
59 for (int row = 0; row < N; row++)
60 B[row] = 0;
61 return B;
64 int mgcLinearSystemD::Solve (int n, double** a, double* b)
66 int* indxc = new int[n];
67 if ( !indxc )
68 return 0;
69 int* indxr = new int[n];
70 if ( !indxr ) {
71 delete[] indxc;
72 return 0;
74 int* ipiv = new int[n];
75 if ( !ipiv ) {
76 delete[] indxc;
77 delete[] indxr;
78 return 0;
81 int i, j, k;
82 int irow = 0;
83 int icol = 0;
84 double save;
86 for (j = 0; j < n; j++)
87 ipiv[j] = 0;
89 for (i = 0; i < n; i++)
91 double big = 0;
92 for (j = 0; j < n; j++)
94 if ( ipiv[j] != 1 )
96 for (k = 0; k < n; k++)
98 if ( ipiv[k] == 0 )
100 if ( fabs(a[j][k]) >= big )
102 big = fabs(a[j][k]);
103 irow = j;
104 icol = k;
107 else if ( ipiv[k] > 1 )
109 delete[] ipiv;
110 delete[] indxr;
111 delete[] indxc;
112 return 0;
117 ipiv[icol]++;
119 if ( irow != icol )
121 double* rowptr = a[irow];
122 a[irow] = a[icol];
123 a[icol] = rowptr;
125 save = b[irow];
126 b[irow] = b[icol];
127 b[icol] = save;
130 indxr[i] = irow;
131 indxc[i] = icol;
132 if ( a[icol][icol] == 0 )
134 delete[] ipiv;
135 delete[] indxr;
136 delete[] indxc;
137 return 0;
140 double pivinv = 1/a[icol][icol];
141 a[icol][icol] = 1;
142 for (k = 0; k < n; k++)
143 a[icol][k] *= pivinv;
144 b[icol] *= pivinv;
146 for (j = 0; j < n; j++)
148 if ( j != icol )
150 save = a[j][icol];
151 a[j][icol] = 0;
152 for (k = 0; k < n; k++)
153 a[j][k] -= a[icol][k]*save;
154 b[j] -= b[icol]*save;
159 for (j = n-1; j >= 0; j--)
161 if ( indxr[j] != indxc[j] )
163 for (k = 0; k < n; k++)
165 save = a[k][indxr[j]];
166 a[k][indxr[j]] = a[k][indxc[j]];
167 a[k][indxc[j]] = save;
172 delete[] ipiv;
173 delete[] indxr;
174 delete[] indxc;
175 return 1;
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */