Graphviz 14.1.3~dev.20260207.0611
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 "config.h"
35
36#include <common/render.h>
37#include <neatogen/neato.h>
38#include <util/gv_math.h>
39
40int matinv(double **A, double **Ainv, int n)
41{
42 /* Decompose matrix into L and U triangular matrices */
43 lu_t lu = {0};
44 if (lu_decompose(&lu, A, n) == 0)
45 return 0; // singular
46
47 /* Invert matrix by solving n simultaneous equations n times */
48 for (int i = 0; i < n; i++) {
49 lu_solve(&lu, Ainv[i], i, n); // into a row of Ainv: fix later
50 }
51 lu_free(&lu);
52
53 /* Transpose matrix */
54 for (int i = 0; i < n; i++) {
55 for (int j = 0; j < i; j++) {
56 SWAP(&Ainv[i][j], &Ainv[j][i]);
57 }
58 }
59
60 return 1;
61}
#define A(n, t)
Definition expr.h:76
Arithmetic helper functions.
#define SWAP(a, b)
Definition gv_math.h:134
void lu_solve(const lu_t *lu, double *x, int bi, int n)
Definition lu.c:137
void lu_free(lu_t *lu)
release resources relating to LU decomposition
Definition lu.c:155
int lu_decompose(lu_t *lu, double **a, int n)
Definition lu.c:67
int matinv(double **A, double **Ainv, int n)
Definition matinv.c:40
state for working on LU decomposition
Definition neato.h:38