/************************************************************************ NxN Matrix class Copyright (C) 1998 Michael Garland. See "COPYING.txt" for details. $Id: mixmops.cxx,v 1.7 1998/10/26 21:09:38 garland Exp $ ************************************************************************/ #include "stdmix.h" #include "MxMatrix.h" // This section originally from Paul's matrix library. #define SWAP(a, b, t) {t = a; a = b; b = t;} #define A(i,j) mxm_ref(_a, i, j, N) #define B(i,j) mxm_ref(_b, i, j, N) // Solve nxn system Ax=b. // Leaves solution x in b, and destroys original A and b vectors. // Return value is determinant of A. // If system is singular, returns 0 and leaves trash in b // // Uses Gaussian elimination with partial pivoting. // static double internal_solve(double *_a, double *b, const int N) { int i, j, k; double max, t, det, sum, pivot; /*---------- forward elimination ----------*/ det = 1.0; for (i=0; i max) { max = fabs(A(k, i)); j = k; } if (max<=0.) return 0.0; /* if no nonzero pivot, PUNT */ if (j!=i) { /* swap rows i and j */ for (k=i; k=0; i--) { /* solve for x[i] (put it in b[i]) */ sum = b[i]; for (k=i+1; k max) { max = fabs(A(k, i)); j = k; } if (max<=0.) return 0.; /* if no nonzero pivot, PUNT */ if (j!=i) { /* swap rows i and j */ for (k=i; k0; i--) { /* eliminate in column i, above diag */ for (j=0; j // which can be found at http://lib.stat.cmu.edu/general/ajay // // The factorization is valid as long as the returned nullity == 0 // U contains the upper triangular factor itself. // int mxm_cholesky(double *U, const double *A, const int N) { double sum; int nullity = 0; mxm_set(U, 0.0, N); for(int i=0; i 0 ) { mxm_ref(U, i, i, N) = sqrt(sum); /* Now find elements U[i][k], k > i. */ for(int k=(i+1); k