#ifndef lint
static char *rcs="$Header: /mit/hst/src/sim/RCS/lineqs.c,v 2.2 90/08/28 14:45:44 tldavis Exp $";
#endif
/************************************************************************/
/*									*/
/*	lineqs.c	Version 1.0	Robert Sah	Aug 1983	*/
/*                                      Tim Davis       Aug 1990        */
/*	$Source: /mit/hst/src/sim/RCS/lineqs.c,v $  */
/*									*/
/*	routine which...						*/
/*									*/
/*	- finds solution vector to a set of  N	simultaneous 		*/
/*	  linear equations using the Gauss-Jordan reduction		*/
/*	  algorithm with the diagonal pivot strategy.  NxN		*/
/*	  matrix  A  contains the coefficients;  B  contains		*/
/*	  the right-hand side vector.					*/
/*									*/
/*	(Carnahan B., Luther H., Wilkes J.: Applied			*/
/*	 Numerical Methods, 1969, p. 276)				*/
/*									*/
/************************************************************************/

#include	<math.h>

#define	N	8
#define	EPS	0.00001

/*PUBLIC*/
int lineqs (A,B)
double	A[][N],B[];
{
  int	 i,j,k;

  for (k=0; k<N; k++) {
    if (fabs(A[k][k])<EPS) {
      printf ("Error in lineqs.c\n");
      return (-1);
    }
    /* normalize the pivot row */
    for (j=k+1; j<N; j++) A[j][k]=A[j][k]/A[k][k];
    B[k]=B[k]/A[k][k];
    A[k][k]=1.;

    /* eliminate k(th) column elements except for pivot */
    for (i=0; i<N; i++) {
      if (i!=k & A[k][i]!=0.) {
	for (j=k+1; j<N; j++)
	  A[j][i]-=A[k][i] * A[j][k];
	B[i]-=A[k][i] * B[k];
	A[k][i]=0.;
      }
    }
  }
  return (0);
}
