2#include <cgv/math/mat.h> 
   15bool gaussj(mat<T>& a, mat<T>& b)
 
   17    assert(a.nrows() == a.ncols());
 
   18    const unsigned N = a.nrows();
 
   19    const unsigned M = b.ncols();
 
   30    unsigned int pivotPos[2];
 
   35    mat<unsigned int> swapedRows(N,2);
 
   37    const int REDUCED_COLUMN = 0;
 
   38    const int PIVOT_ROW = 1;
 
   40    bool *isPivotCol = 
new bool[N];
 
   41    for (
unsigned int i = 0; i < N; i++) isPivotCol[i] = 
false;
 
   44    for (
unsigned int columnCnt = 0; columnCnt < N; columnCnt++)
 
   49        T largestAbsVal = T(0);
 
   50        for (
unsigned int j = 0; j < N; j++)
 
   53                for (
unsigned int k = 0; k < N; k++)
 
   56                        if (std::abs(a(j,k)) > largestAbsVal)
 
   58                            largestAbsVal = std::abs(a(j,k));
 
   64        isPivotCol[pivotPos[COL]] = 
true;
 
   67        if (pivotPos[ROW] != pivotPos[COL])
 
   69            for (
unsigned int l = 0; l < N; l++) std::swap(a(pivotPos[ROW],l),a(pivotPos[COL],l));
 
   70            for (
unsigned int l = 0; l < M; l++) std::swap(b(pivotPos[ROW],l),b(pivotPos[COL],l));
 
   72        swapedRows(columnCnt,PIVOT_ROW) = pivotPos[ROW];
 
   73        swapedRows(columnCnt,REDUCED_COLUMN) = pivotPos[COL];
 
   76        if (a(pivotPos[COL],pivotPos[COL]) == T(0))
 
   83        T pivotInv = T(1) / a(pivotPos[COL],pivotPos[COL]);
 
   86        a(pivotPos[COL],pivotPos[COL]) = T(1);
 
   87        for (
unsigned int l = 0; l < N; l++) a(pivotPos[COL],l) *= pivotInv;
 
   88        for (
unsigned int l = 0; l < M; l++) b(pivotPos[COL],l) *= pivotInv;
 
   91        for (
unsigned int ll = 0; ll < N; ll++)
 
   93            if (ll != pivotPos[COL])
 
   96                T factor = a(ll,pivotPos[COL]);
 
   99                a(ll,pivotPos[COL]) = T(0);
 
  101                for (
unsigned int l = 0; l < N; l++) a(ll,l) -= a(pivotPos[COL],l) * factor;
 
  107    for (
int undoSwapStep = N-1; undoSwapStep >= 0; undoSwapStep--)
 
  109        if (swapedRows(undoSwapStep,PIVOT_ROW) != swapedRows(undoSwapStep,REDUCED_COLUMN))
 
  111            for (
unsigned int k = 0; k < N; k++)
 
  112                std::swap( a(k,swapedRows(undoSwapStep,PIVOT_ROW)), a(k,swapedRows(undoSwapStep,REDUCED_COLUMN)) );
 
  116    delete [] isPivotCol;
 
  125bool gaussj(mat<T>& a)
 
  127    assert(a.nrows() == a.ncols());
 
  128    const unsigned N = a.nrows();
 
  137    unsigned int pivotPos[2];
 
  142    mat<unsigned int> swapedRows(N,2);
 
  144    const int REDUCED_COLUMN = 0;
 
  145    const int PIVOT_ROW = 1;
 
  147    bool *isPivotCol = 
new bool[N];
 
  148    for (
unsigned int i = 0; i < N; i++) isPivotCol[i] = 
false;
 
  151    for (
unsigned int columnCnt = 0; columnCnt < N; columnCnt++)
 
  156        T largestAbsVal = T(0);
 
  157        for (
unsigned int j = 0; j < N; j++)
 
  160                for (
unsigned int k = 0; k < N; k++)
 
  163                        if (std::abs(a(j,k)) > largestAbsVal)
 
  165                            largestAbsVal = std::abs(a(j,k));
 
  171        isPivotCol[pivotPos[COL]] = 
true;
 
  174        if (pivotPos[ROW] != pivotPos[COL])
 
  176            for (
unsigned int l = 0; l < N; l++) std::swap(a(pivotPos[ROW],l),a(pivotPos[COL],l));
 
  178        swapedRows(columnCnt,PIVOT_ROW) = pivotPos[ROW];
 
  179        swapedRows(columnCnt,REDUCED_COLUMN) = pivotPos[COL];
 
  182        if (a(pivotPos[COL],pivotPos[COL]) == T(0))
 
  184            delete [] isPivotCol;
 
  189        T pivotInv = T(1) / a(pivotPos[COL],pivotPos[COL]);
 
  192        a(pivotPos[COL],pivotPos[COL]) = T(1);
 
  193        for (
unsigned int l = 0; l < N; l++) a(pivotPos[COL],l) *= pivotInv;
 
  196        for (
unsigned int ll = 0; ll < N; ll++)
 
  198            if (ll != pivotPos[COL])
 
  201                T factor = a(ll,pivotPos[COL]);
 
  204                a(ll,pivotPos[COL]) = T(0);
 
  206                for (
unsigned int l = 0; l < N; l++) a(ll,l) -= a(pivotPos[COL],l) * factor;
 
  212    for (
int undoSwapStep = N-1; undoSwapStep >= 0; undoSwapStep--)
 
  214        if (swapedRows(undoSwapStep,PIVOT_ROW) != swapedRows(undoSwapStep,REDUCED_COLUMN))
 
  216            for (
unsigned int k = 0; k < N; k++)
 
  217                std::swap( a(k,swapedRows(undoSwapStep,PIVOT_ROW)), a(k,swapedRows(undoSwapStep,REDUCED_COLUMN)) );
 
  221    delete [] isPivotCol;