class WeightedDynamicTimeWarping implements IDistanceMetric {

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

		double[] weights = this.calculateWeights(Math.max(n, m));

		// Dynamic Programming initialization.
		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;
		}
		matrix[0][0] = 0d;

		// Dynamic programming.
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				// Paper: | w[i-j] (a_i - b_j) |^p
				double cost = weights[Math.abs(i - j)] * this.d.distance(a[i - 1], b[j - 1]);
				double minimum = Math.min(matrix[i - 1][j], Math.min(matrix[i][j - 1], matrix[i - 1][j - 1]));
				matrix[i][j] = cost + minimum;
			}
		}
		return matrix[n][m];
	}

	public  WeightedDynamicTimeWarping(final double g, final double maximumWeight, final IScalarDistance d);

}