Program: 134721
Project: 134721
Buggy source:
/** 
 * Dtw Barycenter Averaging (DBA)
 * @param C average sequence to update
 * @param sequences set of sequences to average
 * @param w warping window size for DTW
 */
public static double[] DBA_update(double[] C,double[][] sequences,int w){
  final ArrayList<Double>[] tupleAssociation=new ArrayList[C.length];
  for (int i=0; i < tupleAssociation.length; i++) {
    tupleAssociation[i]=new ArrayList<Double>(sequences.length);
  }
  int nbTuplesAverageSeq, i, j, indiceRes;
  double res=0.0;
  int centerLength=C.length;
  int seqLength, jStart, jStop;
  ;
  for (  double[] T : sequences) {
    seqLength=T.length;
    costMatrix[0][0]=distanceTo(C[0],T[0]);
    pathMatrix[0][0]=NIL;
    optimalPathLength[0][0]=0;
    for (i=1; i < Math.min(centerLength,1 + w); i++) {
      costMatrix[i][0]=costMatrix[i - 1][0] + distanceTo(C[i],T[0]);
      pathMatrix[i][0]=UP;
      optimalPathLength[i][0]=i;
    }
    for (j=1; j < Math.min(seqLength,1 + w); j++) {
      costMatrix[0][j]=costMatrix[0][j - 1] + distanceTo(T[j],C[0]);
      pathMatrix[0][j]=LEFT;
      optimalPathLength[0][j]=j;
    }
    if (j < seqLength)     costMatrix[0][j]=Double.POSITIVE_INFINITY;
    for (i=1; i < centerLength; i++) {
      jStart=Math.max(1,i - w);
      jStop=Math.min(seqLength,i + w + 1);
      costMatrix[i][jStart - 1]=Double.POSITIVE_INFINITY;
      for (j=jStart; j < jStop; j++) {
        indiceRes=ArgMin3(costMatrix[i - 1][j - 1],costMatrix[i][j - 1],costMatrix[i - 1][j]);
        pathMatrix[i][j]=indiceRes;
switch (indiceRes) {
case DIAGONAL:
          res=costMatrix[i - 1][j - 1];
        optimalPathLength[i][j]=optimalPathLength[i - 1][j - 1] + 1;
      break;
case LEFT:
    res=costMatrix[i][j - 1];
  optimalPathLength[i][j]=optimalPathLength[i][j - 1] + 1;
break;
case UP:
res=costMatrix[i - 1][j];
optimalPathLength[i][j]=optimalPathLength[i - 1][j] + 1;
break;
}
costMatrix[i][j]=res + distanceTo(C[i],T[j]);
}
if (jStop < seqLength) costMatrix[i][jStop]=Double.POSITIVE_INFINITY;
}
nbTuplesAverageSeq=optimalPathLength[centerLength - 1][seqLength - 1] + 1;
i=centerLength - 1;
j=seqLength - 1;
for (int t=nbTuplesAverageSeq - 1; t >= 0; t--) {
tupleAssociation[i].add(T[j]);
switch (pathMatrix[i][j]) {
case DIAGONAL:
i=i - 1;
j=j - 1;
break;
case LEFT:
j=j - 1;
break;
case UP:
i=i - 1;
break;
}
}
}
for (int t=0; t < centerLength; t++) {
C[t]=barycenter((tupleAssociation[t].toArray()));
}
return C;
}

Fixed version (ground truth):
/** 
 * Dtw Barycenter Averaging (DBA)
 * @param C average sequence to update
 * @param sequences set of sequences to average
 * @param w warping window size for DTW
 */
public static double[] DBA_update(double[] C,double[][] sequences,int w){
  final ArrayList<Double>[] tupleAssociation=new ArrayList[C.length];
  for (int i=0; i < tupleAssociation.length; i++) {
    tupleAssociation[i]=new ArrayList<Double>(sequences.length);
  }
  int nbTuplesAverageSeq, i, j, indiceRes;
  double res=0.0;
  int centerLength=C.length;
  int seqLength, jStart, jStop;
  ;
  for (  double[] T : sequences) {
    seqLength=T.length;
    costMatrix[0][0]=distanceTo(C[0],T[0]);
    pathMatrix[0][0]=NIL;
    optimalPathLength[0][0]=0;
    for (i=1; i < Math.min(centerLength,1 + w); i++) {
      costMatrix[i][0]=costMatrix[i - 1][0] + distanceTo(C[i],T[0]);
      pathMatrix[i][0]=UP;
      optimalPathLength[i][0]=i;
    }
    for (j=1; j < Math.min(seqLength,1 + w); j++) {
      costMatrix[0][j]=costMatrix[0][j - 1] + distanceTo(T[j],C[0]);
      pathMatrix[0][j]=LEFT;
      optimalPathLength[0][j]=j;
    }
    if (j < seqLength)     costMatrix[0][j]=Double.POSITIVE_INFINITY;
    for (i=1; i < centerLength; i++) {
      jStart=Math.max(1,i - w);
      jStop=Math.min(seqLength,i + w + 1);
      if (jStart != 1)       costMatrix[i][jStart - 1]=Double.POSITIVE_INFINITY;
      for (j=jStart; j < jStop; j++) {
        indiceRes=ArgMin3(costMatrix[i - 1][j - 1],costMatrix[i][j - 1],costMatrix[i - 1][j]);
        pathMatrix[i][j]=indiceRes;
switch (indiceRes) {
case DIAGONAL:
          res=costMatrix[i - 1][j - 1];
        optimalPathLength[i][j]=optimalPathLength[i - 1][j - 1] + 1;
      break;
case LEFT:
    res=costMatrix[i][j - 1];
  optimalPathLength[i][j]=optimalPathLength[i][j - 1] + 1;
break;
case UP:
res=costMatrix[i - 1][j];
optimalPathLength[i][j]=optimalPathLength[i - 1][j] + 1;
break;
}
costMatrix[i][j]=res + distanceTo(C[i],T[j]);
}
if (jStop < seqLength) costMatrix[i][jStop]=Double.POSITIVE_INFINITY;
}
nbTuplesAverageSeq=optimalPathLength[centerLength - 1][seqLength - 1] + 1;
i=centerLength - 1;
j=seqLength - 1;
for (int t=nbTuplesAverageSeq - 1; t >= 0; t--) {
tupleAssociation[i].add(T[j]);
switch (pathMatrix[i][j]) {
case DIAGONAL:
i=i - 1;
j=j - 1;
break;
case LEFT:
j=j - 1;
break;
case UP:
i=i - 1;
break;
}
}
}
for (int t=0; t < centerLength; t++) {
C[t]=barycenter((tupleAssociation[t].toArray()));
}
return C;
}