Graphviz 12.0.1~dev.20240716.0800
Loading...
Searching...
No Matches
solve.c
Go to the documentation of this file.
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#include <assert.h>
13#include <cgraph/alloc.h>
14#include <common/render.h>
15#include <math.h>
16#include <neatogen/neatoprocs.h>
17#include <stdio.h>
18#include <stdlib.h>
19
20static void swap(double *x, double *y) {
21 const double temp = *x;
22 *x = *y;
23 *y = temp;
24}
25
26void solve(double *a, double *b, double *c, size_t n) { // a[n][n],b[n],c[n]
27
28 assert(n >= 2);
29
30 const size_t nsq = n * n;
31 double *asave = gv_calloc(nsq, sizeof(double));
32 double *csave = gv_calloc(n, sizeof(double));
33
34 for (size_t i = 0; i < n; i++)
35 csave[i] = c[i];
36 for (size_t i = 0; i < nsq; i++)
37 asave[i] = a[i];
38 /* eliminate ith unknown */
39 const size_t nm = n - 1;
40 for (size_t i = 0; i < nm; i++) {
41 /* find largest pivot */
42 double amax = 0.;
43 size_t istar = 0;
44 for (size_t ii = i; ii < n; ii++) {
45 const double dum = fabs(a[ii * n + i]);
46 if (dum < amax)
47 continue;
48 istar = ii;
49 amax = dum;
50 }
51 /* return if pivot is too small */
52 if (amax < 1.e-10)
53 goto bad;
54 /* switch rows */
55 for (size_t j = i; j < n; j++) {
56 const size_t t = istar * n + j;
57 swap(&a[t], &a[i * n + j]);
58 }
59 swap(&c[istar], &c[i]);
60 /*pivot */
61 const size_t ip = i + 1;
62 for (size_t ii = ip; ii < n; ii++) {
63 const double pivot = a[ii * n + i] / a[i * n + i];
64 c[ii] -= pivot * c[i];
65 for (size_t j = 0; j < n; j++)
66 a[ii * n + j] = a[ii * n + j] - pivot * a[i * n + j];
67 }
68 }
69 /* return if last pivot is too small */
70 if (fabs(a[n * n - 1]) < 1.e-10)
71 goto bad;
72 b[n - 1] = c[n - 1] / a[n * n - 1];
73 /* back substitute */
74 for (size_t k = 0; k < nm; k++) {
75 const size_t m = n - k - 2;
76 b[m] = c[m];
77 const size_t mp = m + 1;
78 for (size_t j = mp; j < n; j++)
79 b[m] -= a[m * n + j] * b[j];
80 b[m] /= a[m * n + m];
81 }
82 /* restore original a,c */
83 for (size_t i = 0; i < n; i++)
84 c[i] = csave[i];
85 for (size_t i = 0; i < nsq; i++)
86 a[i] = asave[i];
87 free(asave);
88 free(csave);
89 return;
90bad:
91 printf("ill-conditioned\n");
92 free(asave);
93 free(csave);
94 return;
95}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
void free(void *)
static void swap(double *x, double *y)
Definition solve.c:20
void solve(double *a, double *b, double *c, size_t n)
Definition solve.c:26