Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
conjugate_gradient.cpp
Go to the documentation of this file.
1/*
2 * vim: ts=4 sw=4 et tw=0 wm=0
3 *
4 * libcola - A library providing force-directed network layout using the
5 * stress-majorization method subject to separation constraints.
6 *
7 * Copyright (C) 2006 Nathan Hurst <njh@njhurst.com>
8 * Copyright (C) 2006-2008 Monash University
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 * See the file LICENSE.LGPL distributed with the library.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * Author(s): Nathan Hurst
21 * Tim Dwyer
22 *
23*/
24
25
26#include <cmath>
27#include <cstdlib>
28#include <cassert>
29#include <valarray>
30
31#include "libvpsc/assertions.h"
32#include "libcola/commondefs.h"
34
35/* lifted wholely from wikipedia. Well, apart from the bug in the wikipedia version. */
36
37using std::valarray;
38
39static void
40matrix_times_vector(valarray<double> const &matrix, /* m * n */
41 valarray<double> const &vec, /* n */
42 valarray<double> &result) /* m */
43{
44 unsigned n = vec.size();
45 unsigned m = result.size();
46 COLA_ASSERT(m*n == matrix.size());
47# if defined(_MSC_VER)
48 // magmy: The following lines show how operator[] is defined for valarray under MSVC
49 // _Ty valarray<_Ty>::operator[](size_t _Off) const;
50 // _Ty &valarray<_Ty>::operator[](size_t _Off);
51 // As a consequence, it is not possible to take the address of a constant valarray[n].
52 // This looks like a bug in the Microsoft's <valarray> file.
53 // Below is a workaround
54 double const *mp = &const_cast<valarray<double> &>(matrix)[0];
55# else
56 const double* mp = &matrix[0];
57# endif
58 for (unsigned i = 0; i < m; i++) {
59 double res = 0;
60 for (unsigned j = 0; j < n; j++)
61 res += *mp++ * vec[j];
62 result[i] = res;
63 }
64}
65
66/*
67static double Linfty(valarray<double> const &vec) {
68 return std::max(vec.max(), -vec.min());
69}
70*/
71
72double
73inner(valarray<double> const &x,
74 valarray<double> const &y) {
75 double total = 0;
76 for(unsigned i = 0; i < x.size(); i++)
77 total += x[i]*y[i];
78 return total;// (x*y).sum(); <- this is more concise, but ineff
79}
80
81double compute_cost(valarray<double> const &A,
82 valarray<double> const &b,
83 valarray<double> const &x,
84 const unsigned n) {
85 // computes cost = 2 b x - x A x
86 double cost = 2. * inner(b,x);
87 valarray<double> Ax(n);
88 for (unsigned i=0; i<n; i++) {
89 Ax[i] = 0;
90 for (unsigned j=0; j<n; j++) {
91 Ax[i] += A[i*n+j]*x[j];
92 }
93 }
94 return cost - inner(x,Ax);
95}
96void
97conjugate_gradient(valarray<double> const &A,
98 valarray<double> &x,
99 valarray<double> const &b,
100 unsigned const n, double const tol,
101 unsigned const max_iterations) {
102 //printf("Conjugate Gradient...\n");
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 double tol_squared = tol*tol;
109#ifdef EXAMINE_COST
110 double previousCost=compute_cost(A,b,x,n),cost;
111#endif
112 while(k < max_iterations && r_r > tol_squared) {
113 k++;
114 double r_r_new = r_r;
115 if(k == 1)
116 p = r;
117 else {
118 r_r_new = inner(r,r);
119 if(r_r_new<tol_squared) break;
120 p = r + (r_r_new/r_r)*p;
121 }
122 matrix_times_vector(A, p, Ap);
123 double alpha_k = r_r_new / inner(p, Ap);
124 x += alpha_k*p;
125#ifdef EXAMINE_COST
126 cost=compute_cost(A,b,x,n);
127 printf(" CG[%d] %.15f %.15f\n",k,previousCost,cost);
128 previousCost=cost;
129#endif
130 r -= alpha_k*Ap;
131 r_r = r_r_new;
132 }
133 //printf(" CG finished after %d iterations\n",k);
134 //printf("njh: %d iters, Linfty = %g L2 = %g\n", k,
135 //std::max(-r.min(), r.max()), sqrt(r_r));
136 // x is solution
137}
void conjugate_gradient(valarray< double > const &A, valarray< double > &x, valarray< double > const &b, unsigned const n, double const tol, unsigned const max_iterations)
double compute_cost(valarray< double > const &A, valarray< double > const &b, valarray< double > const &x, const unsigned n)
double inner(valarray< double > const &x, valarray< double > const &y)
static void matrix_times_vector(valarray< double > const &matrix, valarray< double > const &vec, valarray< double > &result)
Css & result