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 <2geom/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 /**
59 // only used in commented code below
60 static double Linfty(valarray<double> const &vec) {
61 return std::max(vec.max(), -vec.min());
62 }
63 **/
65 double
66 inner(valarray<double> const &x,
67 valarray<double> const &y) {
68 double total = 0;
69 for(unsigned i = 0; i < x.size(); i++)
70 total += x[i]*y[i];
71 return total;// (x*y).sum(); <- this is more concise, but ineff
72 }
74 void
75 conjugate_gradient(double **A,
76 double *x,
77 double *b,
78 unsigned n,
79 double tol,
80 int max_iterations,
81 bool ortho1) {
82 valarray<double> vA(n*n);
83 valarray<double> vx(n);
84 valarray<double> vb(n);
85 for(unsigned i=0;i<n;i++) {
86 vx[i]=x[i];
87 vb[i]=b[i];
88 for(unsigned j=0;j<n;j++) {
89 vA[i*n+j]=A[i][j];
90 }
91 }
92 conjugate_gradient(vA,vx,vb,n,tol,max_iterations,ortho1);
93 for(unsigned i=0;i<n;i++) {
94 x[i]=vx[i];
95 }
96 }
97 void
98 conjugate_gradient(valarray<double> const &A,
99 valarray<double> &x,
100 valarray<double> const &b,
101 unsigned n, double tol,
102 unsigned max_iterations, bool /*ortho1*/) {
103 valarray<double> Ap(n), p(n), r(n);
104 matrix_times_vector(A,x,Ap);
105 r=b-Ap;
106 double r_r = inner(r,r);
107 unsigned k = 0;
108 tol *= tol;
109 while(k < max_iterations && r_r > tol) {
110 k++;
111 double r_r_new = r_r;
112 if(k == 1)
113 p = r;
114 else {
115 r_r_new = inner(r,r);
116 p = r + (r_r_new/r_r)*p;
117 }
118 matrix_times_vector(A, p, Ap);
119 double alpha_k = r_r_new / inner(p, Ap);
120 x += alpha_k*p;
121 r -= alpha_k*Ap;
122 r_r = r_r_new;
123 }
124 //printf("njh: %d iters, Linfty = %g L2 = %g\n", k,
125 //std::max(-r.min(), r.max()), sqrt(r_r));
126 // x is solution
127 }
129 /*
130 Local Variables:
131 mode:c++
132 c-file-style:"stroustrup"
133 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
134 indent-tabs-mode:nil
135 fill-column:99
136 End:
137 */
138 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :