Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
lu.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/*
12 * This code was (mostly) written by Ken Turkowski, who said:
13 *
14 * Oh, that. I wrote it in college the first time. It's open source - I think I
15 * posted it after seeing so many people solve equations by inverting matrices
16 * by computing minors naïvely.
17 * -Ken
18 *
19 * The views represented here are mine and are not necessarily shared by
20 * my employer.
21 Ken Turkowski turk@apple.com
22 Immersive Media Technologist http://www.worldserver.com/turk/
23 Apple Computer, Inc.
24 1 Infinite Loop, MS 302-3VR
25 Cupertino, CA 95014
26 */
27
28
29
30/* This module solves linear equations in several variables (Ax = b) using
31 * LU decomposition with partial pivoting and row equilibration. Although
32 * slightly more work than Gaussian elimination, it is faster for solving
33 * several equations using the same coefficient matrix. It is
34 * particularly useful for matrix inversion, by sequentially solving the
35 * equations with the columns of the unit matrix.
36 *
37 * lu_decompose() decomposes the coefficient matrix into the LU matrix,
38 * and lu_solve() solves the series of matrix equations using the
39 * previous LU decomposition.
40 *
41 * Ken Turkowski (apple!turk)
42 * written 3/2/79, revised and enhanced 8/9/83.
43 */
44
45#include <math.h>
46#include <neatogen/neato.h>
47#include <util/alloc.h>
48
49static double *scales;
50static double **lu;
51static int *ps;
52
53/* lu_decompose() decomposes the coefficient matrix A into upper and lower
54 * triangular matrices, the composite being the LU matrix.
55 *
56 * The arguments are:
57 *
58 * a - the (n x n) coefficient matrix
59 * n - the order of the matrix
60 *
61 * 1 is returned if the decomposition was successful,
62 * and 0 is returned if the coefficient matrix is singular.
63 */
64
65int lu_decompose(double **a, int n)
66{
67 int i, j, k;
68 int pivotindex = 0;
69 double pivot, biggest, mult, tempf;
70
71 if (lu)
73 lu = new_array(n, n, 0.0);
74 free(ps);
75 ps = gv_calloc(n, sizeof(int));
76 free(scales);
77 scales = gv_calloc(n, sizeof(double));
78
79 for (i = 0; i < n; i++) { /* For each row */
80 /* Find the largest element in each row for row equilibration */
81 biggest = 0.0;
82 for (j = 0; j < n; j++)
83 biggest = fmax(biggest, fabs(lu[i][j] = a[i][j]));
84 if (biggest > 0.0)
85 scales[i] = 1.0 / biggest;
86 else {
87 scales[i] = 0.0;
88 return 0; /* Zero row: singular matrix */
89 }
90 ps[i] = i; /* Initialize pivot sequence */
91 }
92
93 for (k = 0; k < n - 1; k++) { /* For each column */
94 /* Find the largest element in each column to pivot around */
95 biggest = 0.0;
96 for (i = k; i < n; i++) {
97 if (biggest < (tempf = fabs(lu[ps[i]][k]) * scales[ps[i]])) {
98 biggest = tempf;
99 pivotindex = i;
100 }
101 }
102 if (biggest <= 0.0)
103 return 0; /* Zero column: singular matrix */
104 if (pivotindex != k) { /* Update pivot sequence */
105 j = ps[k];
106 ps[k] = ps[pivotindex];
107 ps[pivotindex] = j;
108 }
109
110 /* Pivot, eliminating an extra variable each time */
111 pivot = lu[ps[k]][k];
112 for (i = k + 1; i < n; i++) {
113 lu[ps[i]][k] = mult = lu[ps[i]][k] / pivot;
114 for (j = k + 1; j < n; j++)
115 lu[ps[i]][j] -= mult * lu[ps[k]][j];
116 }
117 }
118
119 if (lu[ps[n - 1]][n - 1] == 0.0)
120 return 0; /* Singular matrix */
121 return 1;
122}
123
124/* lu_solve() solves the linear equation (Ax = b) after the matrix A has
125 * been decomposed with lu_decompose() into the lower and upper triangular
126 * matrices L and U.
127 *
128 * The arguments are:
129 *
130 * x - the solution vector
131 * b - the constant vector
132 * n - the order of the equation
133*/
134
135void lu_solve(double *x, double *b, int n)
136{
137 int i, j;
138 double dot;
139
140 /* Vector reduction using U triangular matrix */
141 for (i = 0; i < n; i++) {
142 dot = 0.0;
143 for (j = 0; j < i; j++)
144 dot += lu[ps[i]][j] * x[j];
145 x[i] = b[ps[i]] - dot;
146 }
147
148 /* Back substitution, in L triangular matrix */
149 for (i = n - 1; i >= 0; i--) {
150 dot = 0.0;
151 for (j = i + 1; j < n; j++)
152 dot += lu[ps[i]][j] * x[j];
153 x[i] = (x[i] - dot) / lu[ps[i]][i];
154 }
155}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
#define dot(v, w)
Definition geom.c:228
void free(void *)
void lu_solve(double *x, double *b, int n)
Definition lu.c:135
static double ** lu
Definition lu.c:50
int lu_decompose(double **a, int n)
Definition lu.c:65
static double * scales
Definition lu.c:49
static int * ps
Definition lu.c:51
NEATOPROCS_API void free_array(double **rv)
Definition stuff.c:62
NEATOPROCS_API double ** new_array(int i, int j, double val)
Definition stuff.c:47