class DynamicTimeWarping implements IDistanceMetric {

	@Override
	public double distance(final double[] a, final double[] b) {
		// Care in the most algorithm descriptions, the time series are 1-indexed.

		int n = a.length;
		int m = b.length;
		double[][] matrix = new double[n + 1][m + 1]; // from 0 to n+1 incl. and 0 to m+1 incl.

		// 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;

		// Dynamic programming.
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				double cost = this.delta.distance(a[i - 1], b[j - 1]); // 1 indexed in algo.
				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);

}