1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
/*************************************************************************
* Copyright (c) 2011 AT&T Intellectual Property
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Details at https://graphviz.org
*************************************************************************/
#include <neatogen/matrix_ops.h>
#include <neatogen/conjgrad.h>
#include <stdbool.h>
#include <stdlib.h>
#include <util/alloc.h>
/*************************
** C.G. method - SPARSE *
*************************/
int conjugate_gradient
(vtx_data * A, double *x, double *b, int n, double tol,
int max_iterations) {
/* Solves Ax=b using Conjugate-Gradients method */
/* 'x' and 'b' are orthogonalized against 1 */
int i, rv = 0;
double alpha, beta, r_r, r_r_new, p_Ap;
double *r = gv_calloc(n, sizeof(double));
double *p = gv_calloc(n, sizeof(double));
double *Ap = gv_calloc(n, sizeof(double));
double *Ax = gv_calloc(n, sizeof(double));
double *alphap = gv_calloc(n, sizeof(double));
double *orth_b = gv_calloc(n, sizeof(double));
copy_vector(n, b, orth_b);
orthog1(n, orth_b);
orthog1(n, x);
right_mult_with_vector(A, n, x, Ax);
vectors_subtraction(n, orth_b, Ax, r);
copy_vector(n, r, p);
r_r = vectors_inner_product(n, r, r);
for (i = 0; i < max_iterations && max_abs(n, r) > tol; i++) {
right_mult_with_vector(A, n, p, Ap);
p_Ap = vectors_inner_product(n, p, Ap);
if (p_Ap == 0)
break; /*exit(1); */
alpha = r_r / p_Ap;
/* derive new x: */
vectors_scalar_mult(n, p, alpha, alphap);
vectors_addition(n, x, alphap, x);
/* compute values for next iteration: */
if (i < max_iterations - 1) { /* not last iteration */
vectors_scalar_mult(n, Ap, alpha, Ap);
vectors_subtraction(n, r, Ap, r); /* fast computation of r, the residual */
r_r_new = vectors_inner_product(n, r, r);
if (r_r == 0) {
agerrorf("conjugate_gradient: unexpected length 0 vector\n");
rv = 1;
goto cleanup0;
}
beta = r_r_new / r_r;
r_r = r_r_new;
vectors_scalar_mult(n, p, beta, p);
vectors_addition(n, r, p, p);
}
}
cleanup0 :
free(r);
free(p);
free(Ap);
free(Ax);
free(alphap);
free(orth_b);
return rv;
}
/****************************
** C.G. method - DENSE *
****************************/
int conjugate_gradient_f
(float **A, double *x, double *b, int n, double tol,
int max_iterations, bool ortho1) {
/* Solves Ax=b using Conjugate-Gradients method */
/* 'x' and 'b' are orthogonalized against 1 if 'ortho1=true' */
int i, rv = 0;
double alpha, beta, r_r, r_r_new, p_Ap;
double *r = gv_calloc(n, sizeof(double));
double *p = gv_calloc(n, sizeof(double));
double *Ap = gv_calloc(n, sizeof(double));
double *Ax = gv_calloc(n, sizeof(double));
double *alphap = gv_calloc(n, sizeof(double));
double *orth_b = gv_calloc(n, sizeof(double));
copy_vector(n, b, orth_b);
if (ortho1) {
orthog1(n, orth_b);
orthog1(n, x);
}
right_mult_with_vector_f(A, n, x, Ax);
vectors_subtraction(n, orth_b, Ax, r);
copy_vector(n, r, p);
r_r = vectors_inner_product(n, r, r);
for (i = 0; i < max_iterations && max_abs(n, r) > tol; i++) {
right_mult_with_vector_f(A, n, p, Ap);
p_Ap = vectors_inner_product(n, p, Ap);
if (p_Ap == 0)
break; /*exit(1); */
alpha = r_r / p_Ap;
/* derive new x: */
vectors_scalar_mult(n, p, alpha, alphap);
vectors_addition(n, x, alphap, x);
/* compute values for next iteration: */
if (i < max_iterations - 1) { /* not last iteration */
vectors_scalar_mult(n, Ap, alpha, Ap);
vectors_subtraction(n, r, Ap, r); /* fast computation of r, the residual */
// Alternative accurate, but slow, computation of the residual - r
/* right_mult_with_vector(A, n, x, Ax); */
/* vectors_subtraction(n,b,Ax,r); */
r_r_new = vectors_inner_product(n, r, r);
if (r_r == 0) {
rv = 1;
agerrorf("conjugate_gradient: unexpected length 0 vector\n");
goto cleanup1;
}
beta = r_r_new / r_r;
r_r = r_r_new;
vectors_scalar_mult(n, p, beta, p);
vectors_addition(n, r, p, p);
}
}
cleanup1:
free(r);
free(p);
free(Ap);
free(Ax);
free(alphap);
free(orth_b);
return rv;
}
int
conjugate_gradient_mkernel(float *A, float *x, float *b, int n,
double tol, int max_iterations)
{
/* Solves Ax=b using Conjugate-Gradients method */
/* A is a packed symmetric matrix */
// matrix A is “packed” (only upper triangular portion exists, row-major)
int i, rv = 0;
double alpha, beta, r_r, r_r_new, p_Ap;
float *r = gv_calloc(n, sizeof(float));
float *p = gv_calloc(n, sizeof(float));
float *Ap = gv_calloc(n, sizeof(float));
float *Ax = gv_calloc(n, sizeof(float));
/* centering x and b */
orthog1f(n, x);
orthog1f(n, b);
right_mult_with_vector_ff(A, n, x, Ax);
/* centering Ax */
orthog1f(n, Ax);
vectors_subtractionf(n, b, Ax, r);
copy_vectorf(n, r, p);
r_r = vectors_inner_productf(n, r, r);
for (i = 0; i < max_iterations && max_absf(n, r) > tol; i++) {
orthog1f(n, p);
orthog1f(n, x);
orthog1f(n, r);
right_mult_with_vector_ff(A, n, p, Ap);
/* centering Ap */
orthog1f(n, Ap);
p_Ap = vectors_inner_productf(n, p, Ap);
if (p_Ap == 0)
break;
alpha = r_r / p_Ap;
/* derive new x: */
vectors_mult_additionf(n, x, (float) alpha, p);
/* compute values for next iteration: */
if (i < max_iterations - 1) { /* not last iteration */
vectors_mult_additionf(n, r, (float) -alpha, Ap);
r_r_new = vectors_inner_productf(n, r, r);
if (r_r == 0) {
rv = 1;
agerrorf("conjugate_gradient: unexpected length 0 vector\n");
goto cleanup2;
}
beta = r_r_new / r_r;
r_r = r_r_new;
for (size_t j = 0; j < (size_t)n; ++j) {
p[j] = (float)beta * p[j] + r[j];
}
}
}
cleanup2 :
free(r);
free(p);
free(Ap);
free(Ax);
return rv;
}
|