Code

update 2geom
[inkscape.git] / src / 2geom / conjugate_gradient.cpp
1 /*
2  * conjugate_gradient.cpp
3  *
4  * Copyright 2006 Nathan Hurst <njh@mail.csse.monash.edu.au>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it either under the terms of the GNU Lesser General Public
8  * License version 2.1 as published by the Free Software Foundation
9  * (the "LGPL") or, at your option, under the terms of the Mozilla
10  * Public License Version 1.1 (the "MPL"). If you do not alter this
11  * notice, a recipient may use your version of this file under either
12  * the MPL or the LGPL.
13  *
14  * You should have received a copy of the LGPL along with this library
15  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  * You should have received a copy of the MPL along with this library
18  * in the file COPYING-MPL-1.1
19  *
20  * The contents of this file are subject to the Mozilla Public License
21  * Version 1.1 (the "License"); you may not use this file except in
22  * compliance with the License. You may obtain a copy of the License at
23  * http://www.mozilla.org/MPL/
24  *
25  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27  * the specific language governing rights and limitations.
28  *
29  */
31 #include <math.h>
32 #include <stdlib.h>
33 #include <valarray>
34 #include <cassert>
35 #include "conjugate_gradient.h"
37 /* lifted wholely from wikipedia. */
39 using std::valarray;
41 static void 
42 matrix_times_vector(valarray<double> const &matrix, /* m * n */
43                     valarray<double> const &vec,  /* n */
44                     valarray<double> &result) /* m */
45 {
46     unsigned n = vec.size();
47     unsigned m = result.size();
48     assert(m*n == matrix.size());
49     const double* mp = &matrix[0];
50     for (unsigned i = 0; i < m; i++) {
51         double res = 0;
52         for (unsigned j = 0; j < n; j++)
53             res += *mp++ * vec[j];
54         result[i] = res;
55     }
56 }
58 static double Linfty(valarray<double> const &vec) {
59     return std::max(vec.max(), -vec.min());
60 }
62 double
63 inner(valarray<double> const &x, 
64       valarray<double> const &y) {
65     double total = 0;
66     for(unsigned i = 0; i < x.size(); i++)
67         total += x[i]*y[i];
68     return total;// (x*y).sum(); <- this is more concise, but ineff
69 }
71 void 
72 conjugate_gradient(double **A, 
73                    double *x, 
74                    double *b, 
75                    unsigned n, 
76                    double tol,
77                    int max_iterations, 
78                    bool ortho1) {
79     valarray<double> vA(n*n);
80     valarray<double> vx(n);
81     valarray<double> vb(n);
82     for(unsigned i=0;i<n;i++) {
83         vx[i]=x[i];
84         vb[i]=b[i];
85         for(unsigned j=0;j<n;j++) {
86             vA[i*n+j]=A[i][j];
87         }
88     }
89     conjugate_gradient(vA,vx,vb,n,tol,max_iterations,ortho1);
90     for(unsigned i=0;i<n;i++) {
91         x[i]=vx[i];
92     }
93 }
94 void 
95 conjugate_gradient(valarray<double> const &A, 
96                    valarray<double> &x, 
97                    valarray<double> const &b, 
98                    unsigned n, double tol,
99                    unsigned max_iterations, bool ortho1) {
100     valarray<double> Ap(n), p(n), r(n);
101     matrix_times_vector(A,x,Ap);
102     r=b-Ap; 
103     double r_r = inner(r,r);
104     unsigned k = 0;
105     tol *= tol;
106     while(k < max_iterations && r_r > tol) {
107         k++;
108         double r_r_new = r_r;
109         if(k == 1)
110             p = r;
111         else {
112             r_r_new = inner(r,r);
113             p = r + (r_r_new/r_r)*p;
114         }
115         matrix_times_vector(A, p, Ap);
116         double alpha_k = r_r_new / inner(p, Ap);
117         x += alpha_k*p;
118         r -= alpha_k*Ap;
119         r_r = r_r_new;
120     }
121     //printf("njh: %d iters, Linfty = %g L2 = %g\n", k, 
122     //std::max(-r.min(), r.max()), sqrt(r_r));
123     // x is solution
126 /*
127   Local Variables:
128   mode:c++
129   c-file-style:"stroustrup"
130   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
131   indent-tabs-mode:nil
132   fill-column:99
133   End:
134 */
135 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :