// Path: src/main/java/ro/hasna/ts/math/exception/util/LocalizableMessages.java
// public class LocalizableMessages {
//     public static final Localizable NUMBER_NOT_DIVISIBLE_WITH = new DummyLocalizable("{0} is not divisible with {1}");
//     public static final Localizable OUT_OF_RANGE_BOTH_EXCLUSIVE = new DummyLocalizable("{0} out of ({1}, {2}) range");
//     public static final Localizable OUT_OF_RANGE_BOTH_INCLUSIVE = new DummyLocalizable("{0} out of [{1}, {2}] range");
//     public static final Localizable OPERATION_WAS_CANCELLED = new DummyLocalizable("The operation was cancelled.");
// 
//     private LocalizableMessages() {
//     }
// }
// 
// Path: src/main/java/ro/hasna/ts/math/normalization/Normalizer.java
// public interface Normalizer extends Serializable {
//     /**
//      * Normalize the sequence of values.
//      * NOTE: The length of the output array should be equal to the input array.
//      *
//      * @param values the sequence of values
//      * @return the normalized sequence
//      */
//     double[] normalize(double[] values);
// }
// 
// Path: src/main/java/ro/hasna/ts/math/normalization/ZNormalizer.java
// public class ZNormalizer implements Normalizer {
//     private static final long serialVersionUID = 6446811014325682141L;
//     private final Mean mean;
//     private final StandardDeviation standardDeviation;
// 
//     public ZNormalizer() {
//         this(new Mean(), new StandardDeviation(false));
//     }
// 
//     /**
//      * Creates a new instance of this class with the given mean and standard deviation algorithms.
//      *
//      * @param mean              the mean
//      * @param standardDeviation the standard deviation
//      */
//     public ZNormalizer(final Mean mean, final StandardDeviation standardDeviation) {
//         this.mean = mean;
//         this.standardDeviation = standardDeviation;
//     }
// 
//     /**
//      * {@inheritDoc}
//      */
//     @Override
//     public double[] normalize(double[] values) {
//         double m = mean.evaluate(values);
//         double sd = standardDeviation.evaluate(values, m);
// 
//         int length = values.length;
//         double[] normalizedValues = new double[length];
//         for (int i = 0; i < length; i++) {
//             normalizedValues[i] = (values[i] - m) / sd;
//         }
//         return normalizedValues;
//     }
// }


import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.util.FastMath;
import ro.hasna.ts.math.exception.util.LocalizableMessages;
import ro.hasna.ts.math.normalization.Normalizer;
import ro.hasna.ts.math.normalization.ZNormalizer;
import java.util.Deque;
import java.util.LinkedList;
/*
 * Copyright 2015 Octavian Hasna
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ro.hasna.ts.math.ml.distance;



/**
 * Calculates the distance between two vectors using Dynamic Time Warping.
 * <p>
 * Reference:
 * Wikipedia https://en.wikipedia.org/wiki/Dynamic_time_warping
 * </p>
 *
 * @since 0.10
 */
public class DynamicTimeWarpingDistance implements GenericDistanceMeasure<double[]> {
    private static final long serialVersionUID = 1154818905340336905L;
    private final double radiusPercentage;
    private final Normalizer normalizer;

    public DynamicTimeWarpingDistance() {
        this(0.05, new ZNormalizer());
    }

    /**
     * Creates a new instance of this class with
     *
     * @param radiusPercentage Sakoe-Chiba Band width used to constraint the warping window
     * @param normalizer       the normalizer (it can be null if the values were normalized)
     * @throws OutOfRangeException if radiusPercentage is outside the interval [0, 1]
     */
    public DynamicTimeWarpingDistance(double radiusPercentage, Normalizer normalizer) {
        if (radiusPercentage < 0 || radiusPercentage > 1) {
// Path: src/main/java/ro/hasna/ts/math/exception/util/LocalizableMessages.java
// public class LocalizableMessages {
//     public static final Localizable NUMBER_NOT_DIVISIBLE_WITH = new DummyLocalizable("{0} is not divisible with {1}");
//     public static final Localizable OUT_OF_RANGE_BOTH_EXCLUSIVE = new DummyLocalizable("{0} out of ({1}, {2}) range");
//     public static final Localizable OUT_OF_RANGE_BOTH_INCLUSIVE = new DummyLocalizable("{0} out of [{1}, {2}] range");
//     public static final Localizable OPERATION_WAS_CANCELLED = new DummyLocalizable("The operation was cancelled.");
// 
//     private LocalizableMessages() {
//     }
// }
// 
// Path: src/main/java/ro/hasna/ts/math/normalization/Normalizer.java
// public interface Normalizer extends Serializable {
//     /**
//      * Normalize the sequence of values.
//      * NOTE: The length of the output array should be equal to the input array.
//      *
//      * @param values the sequence of values
//      * @return the normalized sequence
//      */
//     double[] normalize(double[] values);
// }
// 
// Path: src/main/java/ro/hasna/ts/math/normalization/ZNormalizer.java
// public class ZNormalizer implements Normalizer {
//     private static final long serialVersionUID = 6446811014325682141L;
//     private final Mean mean;
//     private final StandardDeviation standardDeviation;
// 
//     public ZNormalizer() {
//         this(new Mean(), new StandardDeviation(false));
//     }
// 
//     /**
//      * Creates a new instance of this class with the given mean and standard deviation algorithms.
//      *
//      * @param mean              the mean
//      * @param standardDeviation the standard deviation
//      */
//     public ZNormalizer(final Mean mean, final StandardDeviation standardDeviation) {
//         this.mean = mean;
//         this.standardDeviation = standardDeviation;
//     }
// 
//     /**
//      * {@inheritDoc}
//      */
//     @Override
//     public double[] normalize(double[] values) {
//         double m = mean.evaluate(values);
//         double sd = standardDeviation.evaluate(values, m);
// 
//         int length = values.length;
//         double[] normalizedValues = new double[length];
//         for (int i = 0; i < length; i++) {
//             normalizedValues[i] = (values[i] - m) / sd;
//         }
//         return normalizedValues;
//     }
// }


// Path: src/main/java/ro/hasna/ts/math/ml/distance/DynamicTimeWarpingDistance.java
import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.util.FastMath;
import ro.hasna.ts.math.exception.util.LocalizableMessages;
import ro.hasna.ts.math.normalization.Normalizer;
import ro.hasna.ts.math.normalization.ZNormalizer;
import java.util.Deque;
import java.util.LinkedList;

/*
 * Copyright 2015 Octavian Hasna
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ro.hasna.ts.math.ml.distance;



/**
 * Calculates the distance between two vectors using Dynamic Time Warping.
 * <p>
 * Reference:
 * Wikipedia https://en.wikipedia.org/wiki/Dynamic_time_warping
 * </p>
 *
 * @since 0.10
 */
public class DynamicTimeWarpingDistance implements GenericDistanceMeasure<double[]> {
    private static final long serialVersionUID = 1154818905340336905L;
    private final double radiusPercentage;
    private final Normalizer normalizer;

    public DynamicTimeWarpingDistance() {
        this(0.05, new ZNormalizer());
    }

    /**
     * Creates a new instance of this class with
     *
     * @param radiusPercentage Sakoe-Chiba Band width used to constraint the warping window
     * @param normalizer       the normalizer (it can be null if the values were normalized)
     * @throws OutOfRangeException if radiusPercentage is outside the interval [0, 1]
     */
    public DynamicTimeWarpingDistance(double radiusPercentage, Normalizer normalizer) {
        if (radiusPercentage < 0 || radiusPercentage > 1) {
            throw new OutOfRangeException(LocalizableMessages.OUT_OF_RANGE_BOTH_INCLUSIVE, radiusPercentage, 0, 1);