Program: 57497
Project: 57497
Buggy source:
/** 
 * Decompose the original square matrix. <p>The decomposition is based on a Choleski decomposition where additional transforms are performed: <ul> <li>the rows of the decomposed matrix are permuted</li> <li>columns with the too small diagonal element are discarded</li> <li>the matrix is permuted</li> </ul> This means that rather than computing M = U<sup>T</sup>.U where U is an upper triangular matrix, this method computed M=B.B<sup>T</sup> where B is a rectangular matrix.
 * @param covariance covariance matrix
 * @param small diagonal elements threshold under which  column areconsidered to be dependent on previous ones and are discarded
 * @exception NotPositiveDefiniteMatrixException if thecovariance matrix is not strictly positive definite
 */
private void decompose(RealMatrix covariance,double small) throws NotPositiveDefiniteMatrixException {
  int order=covariance.getRowDimension();
  double[][] c=covariance.getData();
  double[][] b=new double[order][order];
  int[] swap=new int[order];
  int[] index=new int[order];
  for (int i=0; i < order; ++i) {
    index[i]=i;
  }
  rank=0;
  for (boolean loop=true; loop; ) {
    swap[rank]=rank;
    for (int i=rank + 1; i < order; ++i) {
      int ii=index[i];
      int isi=index[swap[i]];
      if (c[ii][ii] > c[isi][isi]) {
        swap[rank]=i;
      }
    }
    if (swap[rank] != rank) {
      int tmp=index[rank];
      index[rank]=index[swap[rank]];
      index[swap[rank]]=tmp;
    }
    int ir=index[rank];
    if (c[ir][ir] < small) {
      if (rank == 0) {
        throw new NotPositiveDefiniteMatrixException();
      }
      for (int i=rank; i < order; ++i) {
        if (c[index[i]][index[i]] < -small) {
          throw new NotPositiveDefiniteMatrixException();
        }
      }
      ++rank;
      loop=false;
    }
 else {
      double sqrt=Math.sqrt(c[ir][ir]);
      b[rank][rank]=sqrt;
      double inverse=1 / sqrt;
      for (int i=rank + 1; i < order; ++i) {
        int ii=index[i];
        double e=inverse * c[ii][ir];
        b[i][rank]=e;
        c[ii][ii]-=e * e;
        for (int j=rank + 1; j < i; ++j) {
          int ij=index[j];
          double f=c[ii][ij] - e * b[j][rank];
          c[ii][ij]=f;
          c[ij][ii]=f;
        }
      }
      loop=++rank < order;
    }
  }
  root=new RealMatrixImpl(order,rank);
  for (int i=0; i < order; ++i) {
    System.arraycopy(b[i],0,root.getDataRef()[swap[i]],0,rank);
  }
}

Fixed version (ground truth):
/** 
 * Decompose the original square matrix. <p>The decomposition is based on a Choleski decomposition where additional transforms are performed: <ul> <li>the rows of the decomposed matrix are permuted</li> <li>columns with the too small diagonal element are discarded</li> <li>the matrix is permuted</li> </ul> This means that rather than computing M = U<sup>T</sup>.U where U is an upper triangular matrix, this method computed M=B.B<sup>T</sup> where B is a rectangular matrix.
 * @param covariance covariance matrix
 * @param small diagonal elements threshold under which  column areconsidered to be dependent on previous ones and are discarded
 * @exception NotPositiveDefiniteMatrixException if thecovariance matrix is not strictly positive definite
 */
private void decompose(RealMatrix covariance,double small) throws NotPositiveDefiniteMatrixException {
  int order=covariance.getRowDimension();
  double[][] c=covariance.getData();
  double[][] b=new double[order][order];
  int[] swap=new int[order];
  int[] index=new int[order];
  for (int i=0; i < order; ++i) {
    index[i]=i;
  }
  rank=0;
  for (boolean loop=true; loop; ) {
    swap[rank]=rank;
    for (int i=rank + 1; i < order; ++i) {
      int ii=index[i];
      int isi=index[swap[i]];
      if (c[ii][ii] > c[isi][isi]) {
        swap[rank]=i;
      }
    }
    if (swap[rank] != rank) {
      int tmp=index[rank];
      index[rank]=index[swap[rank]];
      index[swap[rank]]=tmp;
    }
    int ir=index[rank];
    if (c[ir][ir] < small) {
      if (rank == 0) {
        throw new NotPositiveDefiniteMatrixException();
      }
      for (int i=rank; i < order; ++i) {
        if (c[index[i]][index[i]] < -small) {
          throw new NotPositiveDefiniteMatrixException();
        }
      }
      ++rank;
      loop=false;
    }
 else {
      double sqrt=Math.sqrt(c[ir][ir]);
      b[rank][rank]=sqrt;
      double inverse=1 / sqrt;
      for (int i=rank + 1; i < order; ++i) {
        int ii=index[i];
        double e=inverse * c[ii][ir];
        b[i][rank]=e;
        c[ii][ii]-=e * e;
        for (int j=rank + 1; j < i; ++j) {
          int ij=index[j];
          double f=c[ii][ij] - e * b[j][rank];
          c[ii][ij]=f;
          c[ij][ii]=f;
        }
      }
      loop=++rank < order;
    }
  }
  root=new RealMatrixImpl(order,rank);
  for (int i=0; i < order; ++i) {
    System.arraycopy(b[i],0,root.getDataRef()[index[i]],0,rank);
  }
}