class DynamicTimeWarping implements IDistanceMetric {

	public double distanceWithWindow(final double[] a, final double[] b, int w) {
		int n = a.length;
		int m = b.length;
		double[][] matrix = new double[n + 1][m + 1];

		w = Math.max(w, Math.abs(n - m));

		// Initialize first row and column to infinity (except [0][0]).
		for (int i = 1; i <= n; i++) {
			matrix[i][0] = Double.MAX_VALUE;
		}
		for (int j = 1; j <= m; j++) {
			matrix[0][j] = Double.MAX_VALUE;
		}
		// Initialize [0][0] with 0.
		matrix[0][0] = 0d;

		for (int i = 1; i <= n; i++) {
			for (int j = Math.max(1, i - w); j <= Math.min(m, i + w); j++) {
				double cost = this.delta.distance(a[i - 1], b[j - 1]);
				double mini = Math.min(matrix[i - 1][j], Math.min(matrix[i][j - 1], matrix[i - 1][j - 1]));
				matrix[i][j] = cost + mini;
			}
		}

		return matrix[n][m];
	}

	public  DynamicTimeWarping();
	public  DynamicTimeWarping(final IScalarDistance delta);

}