$search
00001 /* 00002 * OpenNL: Numerical Library 00003 * Copyright (C) 2004 Bruno Levy 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 * 00019 * If you modify this software, you should include a notice giving the 00020 * name of the person performing the modification, the date of modification, 00021 * and the reason for such modification. 00022 * 00023 * Contact: Bruno Levy 00024 * 00025 * levy@loria.fr 00026 * 00027 * ISA Project 00028 * LORIA, INRIA Lorraine, 00029 * Campus Scientifique, BP 239 00030 * 54506 VANDOEUVRE LES NANCY CEDEX 00031 * FRANCE 00032 * 00033 * Note that the GNU General Public License does not permit incorporating 00034 * the Software into proprietary programs. 00035 */ 00036 00037 #include <NL/nl_preconditioners.h> 00038 #include <NL/nl_blas.h> 00039 #include <NL/nl_matrix.h> 00040 #include <NL/nl_context.h> 00041 00042 /************************************************************************/ 00043 /* preconditioners */ 00044 00045 /* Utilities for preconditioners */ 00046 00047 void nlMultDiagonal(NLdouble* xy, NLdouble omega) { 00048 NLuint N = nlCurrentContext->n ; 00049 NLuint i ; 00050 NLdouble* diag = nlCurrentContext->M.diag ; 00051 for(i=0; i<N; i++) { 00052 xy[i] *= (diag[i] / omega) ; 00053 } 00054 } 00055 00056 void nlMultDiagonalInverse(NLdouble* xy, NLdouble omega) { 00057 NLuint N = nlCurrentContext->n ; 00058 NLuint i ; 00059 NLdouble* diag = nlCurrentContext->M.diag ; 00060 for(i=0; i<N; i++) { 00061 xy[i] *= (omega / diag[i]) ; 00062 } 00063 } 00064 00065 void nlMultLowerInverse(NLdouble* x, NLdouble* y, double omega) { 00066 NLSparseMatrix* A = &(nlCurrentContext->M) ; 00067 NLuint n = A->n ; 00068 NLdouble* diag = A->diag ; 00069 NLuint i ; 00070 NLuint ij ; 00071 NLRowColumn* Ri = NULL ; 00072 NLCoeff* c = NULL ; 00073 NLdouble S ; 00074 00075 nl_assert(A->storage & NL_MATRIX_STORE_SYMMETRIC) ; 00076 nl_assert(A->storage & NL_MATRIX_STORE_ROWS) ; 00077 00078 for(i=0; i<n; i++) { 00079 S = 0 ; 00080 Ri = &(A->row[i]) ; 00081 for(ij=0; ij < Ri->size; ij++) { 00082 c = &(Ri->coeff[ij]) ; 00083 nl_parano_assert(c->index <= i) ; 00084 if(c->index != i) { 00085 S += c->value * y[c->index] ; 00086 } 00087 } 00088 y[i] = (x[i] - S) * omega / diag[i] ; 00089 } 00090 } 00091 00092 void nlMultUpperInverse(NLdouble* x, NLdouble* y, NLdouble omega) { 00093 NLSparseMatrix* A = &(nlCurrentContext->M) ; 00094 NLuint n = A->n ; 00095 NLdouble* diag = A->diag ; 00096 NLint i ; 00097 NLuint ij ; 00098 NLRowColumn* Ci = NULL ; 00099 NLCoeff* c = NULL ; 00100 NLdouble S ; 00101 00102 nl_assert(A->storage & NL_MATRIX_STORE_SYMMETRIC) ; 00103 nl_assert(A->storage & NL_MATRIX_STORE_COLUMNS) ; 00104 00105 for(i=n-1; i>=0; i--) { 00106 S = 0 ; 00107 Ci = &(A->column[i]) ; 00108 for(ij=0; ij < Ci->size; ij++) { 00109 c = &(Ci->coeff[ij]) ; 00110 nl_parano_assert(c->index >= i) ; 00111 if(c->index != i) { 00112 S += c->value * y[c->index] ; 00113 } 00114 } 00115 y[i] = (x[i] - S) * omega / diag[i] ; 00116 } 00117 } 00118 00119 00120 void nlPreconditioner_Jacobi(NLdouble* x, NLdouble* y) { 00121 NLuint N = nlCurrentContext->n ; 00122 dcopy(N, x, 1, y, 1) ; 00123 nlMultDiagonalInverse(y, 1.0) ; 00124 } 00125 00126 void nlPreconditioner_SSOR(NLdouble* x, NLdouble* y) { 00127 NLdouble omega = nlCurrentContext->omega ; 00128 static double* work = NULL ; 00129 static int work_size = 0 ; 00130 NLuint n = nlCurrentContext->n ; 00131 if(n != work_size) { 00132 work = NL_RENEW_ARRAY(NLdouble, work, n) ; 00133 work_size = n ; 00134 } 00135 00136 nlMultLowerInverse(x, work, omega) ; 00137 nlMultDiagonal(work, omega) ; 00138 nlMultUpperInverse(work, y, omega) ; 00139 00140 dscal(n, 2.0 - omega, y, 1) ; 00141 } 00142