From 0a968002501102b211f1dcbb97c276743ba24844 Mon Sep 17 00:00:00 2001 From: Adam Szalkowski Date: Wed, 31 May 2006 18:39:11 +0000 Subject: [PATCH] added a routine to solve a system of linear equations using Gauss-Jordan elimination [r7843] --- ir/adt/Makefile.in | 5 +- ir/adt/gaussjordan.c | 130 +++++++++++++++++++++++++++++++++++++++++++ ir/adt/gaussjordan.h | 12 ++++ 3 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 ir/adt/gaussjordan.c create mode 100644 ir/adt/gaussjordan.h diff --git a/ir/adt/Makefile.in b/ir/adt/Makefile.in index b9f9178b2..e8efe058d 100644 --- a/ir/adt/Makefile.in +++ b/ir/adt/Makefile.in @@ -16,12 +16,13 @@ topdir = ../.. subdir := ir/adt disable_libiberty := @disable_libiberty@ -INSTALL_HEADERS_ADT = pset.h set.h pmap.h eset.h hashptr.h array.h pdeq.h iterator.h align.h fourcc.h util.h plist.h bipartite.h xmalloc.h +INSTALL_HEADERS_ADT = pset.h set.h pmap.h eset.h hashptr.h array.h pdeq.h iterator.h align.h fourcc.h util.h plist.h \ + bipartite.h xmalloc.h gaussjordan.h SOURCES = Makefile.in \ array.c obst.h pdeq.c \ set.c pmap.c eset.c iterator.c xmalloc.h \ - plist.c bipartite.c + plist.c bipartite.c gaussjordan.c ifeq ($(disable_libiberty),no) SOURCES += xmalloc.c diff --git a/ir/adt/gaussjordan.c b/ir/adt/gaussjordan.c new file mode 100644 index 000000000..52d804032 --- /dev/null +++ b/ir/adt/gaussjordan.c @@ -0,0 +1,130 @@ +/** + * taken from: http://coastal.er.usgs.gov/rvm/toolkit/rvmlibv2.c + * unknown license + */ + +/*------------------------------------------------------*/ +/* gauss.c */ +/* */ +/* 20 jan 89 */ +/* */ +/* Now does full pivoting--row and column swapping. */ +/* Requires keeping track of which variable corresponds */ +/* to each vector position. */ +/* */ +/* 18 jan 89 */ +/* paul o'neill */ +/* */ +/* from rob holman's pascal program--geo.pas */ +/* */ +/* Gauss-Jordan procedure to solve even-determined */ +/* square matrix equation Ax = vec,where A is N x N */ +/* and vec is N x 1. This is taken roughly from */ +/* Menke's book (Geophysical Data Analysis: Discrete */ +/* Inverse Theory--1984 p210 ), but performs actual */ +/* row switching to simplify the programming. */ +/* Partial pivoting is used. */ +/* */ +/* A[][] is a square matrix, N x N */ +/* vec[] is N x 1 of the matrix */ +/* nsize is the size of the equation system */ +/* */ +/* returns 0 if successful */ +/* returns -1 if ill-conditioned matrix */ +/*------------------------------------------------------*/ + +#include +#include + +#define SMALL 0.00001 + +int firm_gaussjordansolve(double *A, double *vec, int nsize) +{ + int i, j, row, col, col2, biggest_r, biggest_c; + double big, temp, sum; + double *x = malloc(nsize * sizeof(double)); + double *scramvec = malloc(nsize * sizeof(double)); + +#define _A(row,col) A[(row)*nsize + (col)] + /* init x[] */ + for(i=0;i big) { + biggest_r = row; + biggest_c = col2; + big = temp; /* largest element left */ + } + } + } + if (big < SMALL) { + return (-1); + } + /* swap rows */ + for(i=0;i=0;i--) { + sum = 0; + for(j=i+1;j