Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
test_cg.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-2008 Monash University
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library in the file LICENSE; if not,
21 * write to the Free Software Foundation, Inc., 59 Temple Place,
22 * Suite 330, Boston, MA 02111-1307 USA
23 *
24*/
25
26#include <stdio.h>
27#include <cassert>
29#include <gsl/gsl_linalg.h>
30
31using std::valarray;
32
33static valarray<double>
34outer_prod(valarray<double> x, valarray<double> y) {
35 valarray<double> result(x.size()*y.size());
36 for(int j = 0; j < x.size(); j++) {
37 for(int i = 0; i < y.size(); i++) {
38 result[j*y.size() + i] = x[j]*y[i];
39 }
40 }
41 return result;
42}
43
44static double
46 return double(rand())/ RAND_MAX;
47}
48
49static void
50matrix_times_vector(valarray<double> const &matrix, /* m * n */
51 valarray<double> const &vec, /* n */
52 valarray<double> &result) /* m */
53{
54 unsigned n = vec.size();
55 unsigned m = result.size();
56 assert(m*n == matrix.size());
57 const double* mp = &matrix[0];
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
66static double
67Linfty(valarray<double> const &vec) {
68 return std::max(vec.max(), -vec.min());
69}
70
71int
72main (void)
73{
74 double tolerance = 1e-6;
75 for(int iters = 0; iters < 100; iters++) {
76 const unsigned N = unsigned(uniform()*40) + 1;
77 double A_data[N*N];
78 printf("%ux%u matrix\n", N,N);
79 for(int r = 0; r < N; r++) {
80 for(int c = 0; c <= r; c++) {
81 A_data[r*N + c] = A_data[c*N + r] = fabs(uniform());
82 }
83 }
84
85 double * A_c[N];
86 for(int i = 0; i < N; i++)
87 A_c[i] = &A_data[N*i];
88
89 double b_data[N];
90 for(int i = 0; i < N; i++)
91 b_data[i] = uniform()*3;
92 std::valarray<double> b(b_data, N), xx(0.0, N);
93 std::valarray<double> A(A_data, N*N);
94
95 conjugate_gradient(A, xx, b, N, tolerance, 2*N);
96
97 gsl_matrix_view m
98 = gsl_matrix_view_array (A_data, N, N);
99 gsl_vector_view bgsl
100 = gsl_vector_view_array (b_data, N);
101
102 gsl_vector *xgsl = gsl_vector_alloc (N);
103
104 int s;
105
106 gsl_permutation * p = gsl_permutation_alloc (N);
107
108 gsl_linalg_LU_decomp (&m.matrix, p, &s);
109
110 gsl_linalg_LU_solve (&m.matrix, p, &bgsl.vector, xgsl);
111
112 std::valarray<double> gsl_xx(0.0, N);
113 for(unsigned i = 0; i < xx.size(); i++) {
114 gsl_xx[i] = gsl_vector_get(xgsl, i);
115 }
116
117 double err = Linfty(xx - gsl_xx);
118 printf ("|xx-nxgsl|_infty = %g\n", err);
119 if(err > tolerance) {
120#if 0 //dubious value
121 for(int r = 0; r < N; r++) {
122 for(int c = 0; c < N; c++) {
123 printf("%g ", A_data[r*N + c]);
124 }
125 printf("\n");
126 }
127#endif
128 printf("\nx njh-cg = \n");
129 for(unsigned i = 0; i < xx.size(); i++)
130 printf("%g ", xx[i]);
131 printf("\nxgsl = ");
132 for(unsigned i = 0; i < xx.size(); i++)
133 printf("%g ", gsl_xx[i]);
134 printf("\n");
135 valarray<double> result(0.0,N);
137 result -= b;
138 printf("\nA xx -b = ");
139 for(unsigned i = 0; i < xx.size(); i++)
140 printf("%g ", result[i]);
141 printf("\n");
142 matrix_times_vector(A, gsl_xx, result);
143 result -= b;
144 printf("\nA gsl_xx -b = ");
145 for(unsigned i = 0; i < xx.size(); i++)
146 printf("%g ", result[i]);
147 printf("\n");
148
149 printf("FAILED!!!!!!!!!!!!!!!!!!!!!!!!\n");
150 exit(1);
151 }
152 }
153 return 0;
154}
155/*
156 Local Variables:
157 mode:c++
158 c-file-style:"stroustrup"
159 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
160 indent-tabs-mode:nil
161 fill-column:99
162 End:
163*/
164// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
void conjugate_gradient(valarray< double > const &A, valarray< double > &x, valarray< double > const &b, unsigned const n, double const tol, unsigned const max_iterations)
Css & result
double c[8][4]
size_t N
static double Linfty(valarray< double > const &vec)
Definition test_cg.cpp:67
int main(void)
Definition test_cg.cpp:72
static valarray< double > outer_prod(valarray< double > x, valarray< double > y)
Definition test_cg.cpp:34
static double uniform()
Definition test_cg.cpp:45
static void matrix_times_vector(valarray< double > const &matrix, valarray< double > const &vec, valarray< double > &result)
Definition test_cg.cpp:50