| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
/* solves the system ab=c using gauss reduction */ |
| 12 |
|
| 13 |
#include "config.h" |
| 14 |
|
| 15 |
#include <assert.h> |
| 16 |
#include <common/render.h> |
| 17 |
#include <math.h> |
| 18 |
#include <neatogen/neatoprocs.h> |
| 19 |
#include <stdio.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <util/alloc.h> |
| 22 |
#include <util/gv_math.h> |
| 23 |
|
| 24 |
void solve(double *a, double *b, double *c, size_t n) { // a[n][n],b[n],c[n] |
| 25 |
|
| 26 |
assert(n >= 2); |
| 27 |
|
| 28 |
const size_t nsq = n * n; |
| 29 |
double *asave = gv_calloc(nsq, sizeof(double)); |
| 30 |
double *csave = gv_calloc(n, sizeof(double)); |
| 31 |
|
| 32 |
for (size_t i = 0; i < n; i++) |
| 33 |
csave[i] = c[i]; |
| 34 |
for (size_t i = 0; i < nsq; i++) |
| 35 |
asave[i] = a[i]; |
| 36 |
/* eliminate ith unknown */ |
| 37 |
const size_t nm = n - 1; |
| 38 |
for (size_t i = 0; i < nm; i++) { |
| 39 |
/* find largest pivot */ |
| 40 |
double amax = 0.; |
| 41 |
size_t istar = 0; |
| 42 |
for (size_t ii = i; ii < n; ii++) { |
| 43 |
const double dum = fabs(a[ii * n + i]); |
| 44 |
if (dum < amax) |
| 45 |
continue; |
| 46 |
istar = ii; |
| 47 |
amax = dum; |
| 48 |
} |
| 49 |
/* return if pivot is too small */ |
| 50 |
if (amax < 1.e-10) |
| 51 |
goto bad; |
| 52 |
/* switch rows */ |
| 53 |
for (size_t j = i; j < n; j++) { |
| 54 |
const size_t t = istar * n + j; |
| 55 |
SWAP(&a[t], &a[i * n + j]); |
| 56 |
} |
| 57 |
SWAP(&c[istar], &c[i]); |
| 58 |
/*pivot */ |
| 59 |
const size_t ip = i + 1; |
| 60 |
for (size_t ii = ip; ii < n; ii++) { |
| 61 |
const double pivot = a[ii * n + i] / a[i * n + i]; |
| 62 |
c[ii] -= pivot * c[i]; |
| 63 |
for (size_t j = 0; j < n; j++) |
| 64 |
a[ii * n + j] = a[ii * n + j] - pivot * a[i * n + j]; |
| 65 |
} |
| 66 |
} |
| 67 |
/* return if last pivot is too small */ |
| 68 |
if (fabs(a[n * n - 1]) < 1.e-10) |
| 69 |
goto bad; |
| 70 |
b[n - 1] = c[n - 1] / a[n * n - 1]; |
| 71 |
/* back substitute */ |
| 72 |
for (size_t k = 0; k < nm; k++) { |
| 73 |
const size_t m = n - k - 2; |
| 74 |
b[m] = c[m]; |
| 75 |
const size_t mp = m + 1; |
| 76 |
for (size_t j = mp; j < n; j++) |
| 77 |
b[m] -= a[m * n + j] * b[j]; |
| 78 |
b[m] /= a[m * n + m]; |
| 79 |
} |
| 80 |
/* restore original a,c */ |
| 81 |
for (size_t i = 0; i < n; i++) |
| 82 |
c[i] = csave[i]; |
| 83 |
for (size_t i = 0; i < nsq; i++) |
| 84 |
a[i] = asave[i]; |
| 85 |
free(asave); |
| 86 |
free(csave); |
| 87 |
return; |
| 88 |
bad: |
| 89 |
printf("ill-conditioned\n"); |
| 90 |
free(asave); |
| 91 |
free(csave); |
| 92 |
} |