1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: solver.cpp,v $
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 ************************************************************************/
34 //---------------------------------------------------------------------------
35 double** mgcLinearSystemD::NewMatrix (int N
)
37 double** A
= new double*[N
];
41 for (int row
= 0; row
< N
; row
++)
43 A
[row
] = new double[N
];
46 for (int i
= 0; i
< row
; i
++)
50 for (int col
= 0; col
< N
; col
++)
55 //---------------------------------------------------------------------------
56 void mgcLinearSystemD::DeleteMatrix (int N
, double** A
)
58 for (int row
= 0; row
< N
; row
++)
62 //---------------------------------------------------------------------------
63 double* mgcLinearSystemD::NewVector (int N
)
65 double* B
= new double[N
];
69 for (int row
= 0; row
< N
; row
++)
73 //---------------------------------------------------------------------------
74 int mgcLinearSystemD::Solve (int n
, double** a
, double* b
)
76 int* indxc
= new int[n
];
79 int* indxr
= new int[n
];
84 int* ipiv
= new int[n
];
94 double big
, pivinv
, save
;
96 for (j
= 0; j
< n
; j
++)
99 for (i
= 0; i
< n
; i
++)
102 for (j
= 0; j
< n
; j
++)
106 for (k
= 0; k
< n
; k
++)
110 if ( fabs(a
[j
][k
]) >= big
)
117 else if ( ipiv
[k
] > 1 )
131 double* rowptr
= a
[irow
];
142 if ( a
[icol
][icol
] == 0 )
150 pivinv
= 1/a
[icol
][icol
];
152 for (k
= 0; k
< n
; k
++)
153 a
[icol
][k
] *= pivinv
;
156 for (j
= 0; j
< n
; j
++)
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
;