Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
matinv.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/* Matinv() inverts the matrix A using LU decomposition. Arguments:
29 * A - the (n x n) matrix to be inverted
30 * Ainv - the (n x n) inverted matrix
31 * n - the order of the matrices A and Ainv
32 */
33
34#include <stdlib.h>
35#include <common/render.h>
36#include <neatogen/neato.h>
37#include <util/alloc.h>
38
39int matinv(double **A, double **Ainv, int n)
40{
41 int i, j;
42 double temp;
43
44 /* Decompose matrix into L and U triangular matrices */
45 if (lu_decompose(A, n) == 0)
46 return (0); /* Singular */
47
48 /* Invert matrix by solving n simultaneous equations n times */
49 double *b = gv_calloc(n, sizeof(double));
50 for (i = 0; i < n; i++) {
51 for (j = 0; j < n; j++)
52 b[j] = 0.0;
53 b[i] = 1.0;
54 lu_solve(Ainv[i], b, n); /* Into a row of Ainv: fix later */
55 }
56 free(b);
57
58 /* Transpose matrix */
59 for (i = 0; i < n; i++) {
60 for (j = 0; j < i; j++) {
61 temp = Ainv[i][j];
62 Ainv[i][j] = Ainv[j][i];
63 Ainv[j][i] = temp;
64 }
65 }
66
67 return (1);
68}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
#define A(n, t)
Definition expr.h:76
void free(void *)
void lu_solve(double *x, double *b, int n)
Definition lu.c:135
int lu_decompose(double **a, int n)
Definition lu.c:65
int matinv(double **A, double **Ainv, int n)
Definition matinv.c:39