Program: 153994
Project: 153994
Buggy source:
/** 
 * @param sourceCoords a double array containing the source coordinatesx_0, y_0, x_1, y_1, ...
 * @param destCoords a double array containing the source coordinatesx_0, y_0, x_1, y_1, ...
 * @return the best-fit coefficients for a bivariate polynomial of thegiven degree mapping the destination points into the source points.  The coefficients for the X polynomial are returned first, followed by those for the Y polynomial.
 */
public static float[] getCoeffs(float[] sourceCoords,int sourceOffset,float[] destCoords,int destOffset,int numCoords,float preScaleX,float preScaleY,float postScaleX,float postScaleY,int degree){
  int i, j, k;
  int equations=numCoords / 2;
  int unknowns=(degree + 1) * (degree + 2) / 2;
  float[] out=new float[2 * unknowns];
  if ((degree == 1) && (numCoords == 3)) {
    double x0, x1, x2, y0, y1, y2;
    double u0, u1, u2, v0, v1, v2;
    x0=sourceCoords[0] * preScaleX;
    y0=sourceCoords[1] * preScaleY;
    x1=sourceCoords[2] * preScaleX;
    y1=sourceCoords[3] * preScaleY;
    x2=sourceCoords[4] * preScaleY;
    y2=sourceCoords[5] * preScaleY;
    u0=destCoords[0] / postScaleX;
    v0=destCoords[1] / postScaleY;
    u1=destCoords[2] / postScaleX;
    v1=destCoords[3] / postScaleY;
    u2=destCoords[4] / postScaleX;
    v2=destCoords[5] / postScaleY;
    double v0mv1=v0 - v1;
    double v1mv2=v1 - v2;
    double v2mv0=v2 - v0;
    double u1mu0=u1 - u0;
    double u2mu1=u2 - u1;
    double u0mu2=u0 - u2;
    double u1v2mu2v1=u1 * v2 - u2 * v1;
    double u2v0mu0v2=u2 * v0 - u0 * v2;
    double u0v1mu1v0=u0 * v1 - u1 * v0;
    double invdet=1.0F / (u0 * (v1mv2) + v0 * (u2mu1) + (u1v2mu2v1));
    out[0]=(float)(((v1mv2) * x0 + (v2mv0) * x1 + (v0mv1) * x2) * invdet);
    out[1]=(float)(((u2mu1) * x0 + (u0mu2) * x1 + (u1mu0) * x2) * invdet);
    out[2]=(float)(((u1v2mu2v1) * x0 + (u2v0mu0v2) * x1 + (u0v1mu1v0) * x2) * invdet);
    out[3]=(float)(((v1mv2) * y0 + (v2mv0) * y1 + (v0mv1) * y2) * invdet);
    out[4]=(float)(((u2mu1) * y0 + (u0mu2) * y1 + (u1mu0) * y2) * invdet);
    out[5]=(float)(((u1v2mu2v1) * y0 + (u2v0mu0v2) * y1 + (u0v1mu1v0) * y2) * invdet);
    return out;
  }
  double[][] A=new double[equations][unknowns];
  double[] xpow=new double[degree + 1];
  double[] ypow=new double[degree + 1];
  for (i=0; i < equations; i++) {
    double[] Ai=A[i];
    double x=destCoords[2 * i + destOffset] / postScaleX;
    double y=destCoords[2 * i + 1 + destOffset] / postScaleY;
    double xtmp=1.0F;
    double ytmp=1.0F;
    for (int d=0; d <= degree; d++) {
      xpow[d]=xtmp;
      ypow[d]=ytmp;
      xtmp*=x;
      ytmp*=y;
    }
    int index=0;
    for (int deg=0; deg <= degree; deg++) {
      for (int ydeg=0; ydeg <= deg; ydeg++) {
        Ai[index++]=xpow[deg - ydeg] * ypow[ydeg];
      }
    }
  }
  double[][] V=new double[unknowns][unknowns];
  double[] W=new double[unknowns];
  double[][] U=new double[equations][unknowns];
  SVD(A,W,U,V);
  for (j=0; j < unknowns; j++) {
    double winv=W[j];
    if (winv != 0.0) {
      winv=1.0F / winv;
    }
    for (i=0; i < unknowns; i++) {
      V[i][j]*=winv;
    }
  }
  double[][] VWINVUT=matmul_t(V,U);
  for (i=0; i < unknowns; i++) {
    double tmp0=0;
    double tmp1=0;
    for (j=0; j < equations; j++) {
      double val=VWINVUT[i][j];
      tmp0+=val * sourceCoords[2 * j + sourceOffset] * preScaleX;
      tmp1+=val * sourceCoords[2 * j + 1 + sourceOffset] * preScaleY;
    }
    out[i]=(float)tmp0;
    out[i + unknowns]=(float)tmp1;
  }
  return out;
}

Fixed version (ground truth):
/** 
 * @param sourceCoords a double array containing the source coordinatesx_0, y_0, x_1, y_1, ...
 * @param destCoords a double array containing the source coordinatesx_0, y_0, x_1, y_1, ...
 * @return the best-fit coefficients for a bivariate polynomial of thegiven degree mapping the destination points into the source points.  The coefficients for the X polynomial are returned first, followed by those for the Y polynomial.
 */
public static float[] getCoeffs(float[] sourceCoords,int sourceOffset,float[] destCoords,int destOffset,int numCoords,float preScaleX,float preScaleY,float postScaleX,float postScaleY,int degree){
  int i, j, k;
  int equations=numCoords / 2;
  int unknowns=(degree + 1) * (degree + 2) / 2;
  float[] out=new float[2 * unknowns];
  if ((degree == 1) && (numCoords == 3)) {
    double x0, x1, x2, y0, y1, y2;
    double u0, u1, u2, v0, v1, v2;
    x0=sourceCoords[0] / postScaleX;
    y0=sourceCoords[1] / postScaleY;
    x1=sourceCoords[2] / postScaleX;
    y1=sourceCoords[3] / postScaleY;
    x2=sourceCoords[4] / postScaleX;
    y2=sourceCoords[5] / postScaleY;
    u0=destCoords[0] * preScaleX;
    v0=destCoords[1] * preScaleY;
    u1=destCoords[2] * preScaleX;
    v1=destCoords[3] * preScaleY;
    u2=destCoords[4] * preScaleX;
    v2=destCoords[5] * preScaleY;
    double v0mv1=v0 - v1;
    double v1mv2=v1 - v2;
    double v2mv0=v2 - v0;
    double u1mu0=u1 - u0;
    double u2mu1=u2 - u1;
    double u0mu2=u0 - u2;
    double u1v2mu2v1=u1 * v2 - u2 * v1;
    double u2v0mu0v2=u2 * v0 - u0 * v2;
    double u0v1mu1v0=u0 * v1 - u1 * v0;
    double invdet=1.0F / (u0 * (v1mv2) + v0 * (u2mu1) + (u1v2mu2v1));
    out[0]=(float)(((v1mv2) * x0 + (v2mv0) * x1 + (v0mv1) * x2) * invdet);
    out[1]=(float)(((u2mu1) * x0 + (u0mu2) * x1 + (u1mu0) * x2) * invdet);
    out[2]=(float)(((u1v2mu2v1) * x0 + (u2v0mu0v2) * x1 + (u0v1mu1v0) * x2) * invdet);
    out[3]=(float)(((v1mv2) * y0 + (v2mv0) * y1 + (v0mv1) * y2) * invdet);
    out[4]=(float)(((u2mu1) * y0 + (u0mu2) * y1 + (u1mu0) * y2) * invdet);
    out[5]=(float)(((u1v2mu2v1) * y0 + (u2v0mu0v2) * y1 + (u0v1mu1v0) * y2) * invdet);
    return out;
  }
  double[][] A=new double[equations][unknowns];
  double[] xpow=new double[degree + 1];
  double[] ypow=new double[degree + 1];
  for (i=0; i < equations; i++) {
    double[] Ai=A[i];
    double x=destCoords[2 * i + destOffset] * preScaleX;
    double y=destCoords[2 * i + 1 + destOffset] * preScaleY;
    double xtmp=1.0F;
    double ytmp=1.0F;
    for (int d=0; d <= degree; d++) {
      xpow[d]=xtmp;
      ypow[d]=ytmp;
      xtmp*=x;
      ytmp*=y;
    }
    int index=0;
    for (int deg=0; deg <= degree; deg++) {
      for (int ydeg=0; ydeg <= deg; ydeg++) {
        Ai[index++]=xpow[deg - ydeg] * ypow[ydeg];
      }
    }
  }
  double[][] V=new double[unknowns][unknowns];
  double[] W=new double[unknowns];
  double[][] U=new double[equations][unknowns];
  SVD(A,W,U,V);
  for (j=0; j < unknowns; j++) {
    double winv=W[j];
    if (winv != 0.0) {
      winv=1.0F / winv;
    }
    for (i=0; i < unknowns; i++) {
      V[i][j]*=winv;
    }
  }
  double[][] VWINVUT=matmul_t(V,U);
  for (i=0; i < unknowns; i++) {
    double tmp0=0;
    double tmp1=0;
    for (j=0; j < equations; j++) {
      double val=VWINVUT[i][j];
      tmp0+=val * sourceCoords[2 * j + sourceOffset] / postScaleX;
      tmp1+=val * sourceCoords[2 * j + 1 + sourceOffset] / postScaleY;
    }
    out[i]=(float)tmp0;
    out[i + unknowns]=(float)tmp1;
  }
  return out;
}