// Path: src/main/java/ro/hasna/ts/math/exception/ArrayLengthIsTooSmallException.java
// public class ArrayLengthIsTooSmallException extends NumberIsTooSmallException {
//     private static final long serialVersionUID = -2633584088507009304L;
// 
//     /**
//      * Construct the exception.
//      *
//      * @param wrong          Value that is smaller than the minimum.
//      * @param min            Minimum.
//      * @param boundIsAllowed Whether {@code min} is included in the allowed range.
//      */
//     public ArrayLengthIsTooSmallException(Number wrong, Number min, boolean boundIsAllowed) {
//         super(wrong, min, boundIsAllowed);
//     }
// 
//     /**
//      * Construct the exception with a specific context.
//      *
//      * @param specific       Specific context pattern.
//      * @param wrong          Value that is smaller than the minimum.
//      * @param min            Minimum.
//      * @param boundIsAllowed Whether {@code min} is included in the allowed range.
//      */
//     public ArrayLengthIsTooSmallException(Localizable specific, Number wrong, Number min, boolean boundIsAllowed) {
//         super(specific, wrong, min, boundIsAllowed);
//     }
// }
// 
// Path: src/main/java/ro/hasna/ts/math/exception/CancellationException.java
// public class CancellationException extends MathIllegalStateException {
//     private static final long serialVersionUID = 8488577710717564366L;
// 
//     public CancellationException() {
//         super(LocalizableMessages.OPERATION_WAS_CANCELLED);
//     }
// }
// 
// Path: src/main/java/ro/hasna/ts/math/representation/GenericTransformer.java
// public interface GenericTransformer<I, O> extends Serializable {
//     /**
//      * Transform the input vector from type I into type O.
//      *
//      * @param input the input vector
//      * @return the output vector
//      */
//     O transform(I input);
// }
// 
// Path: src/main/java/ro/hasna/ts/math/type/MatrixProfile.java
// @Data
// public class MatrixProfile implements Cloneable {
//     private final double[] profile;
//     private final int[] indexProfile;
// 
//     public MatrixProfile(double[] profile, int[] indexProfile) {
//         if (profile.length != indexProfile.length) {
//             throw new DimensionMismatchException(profile.length, indexProfile.length);
//         }
// 
//         this.profile = profile;
//         this.indexProfile = indexProfile;
//     }
// 
//     public MatrixProfile(int size) {
//         profile = new double[size];
//         indexProfile = new int[size];
//         for (int j = 0; j < size; j++) {
//             profile[j] = Double.POSITIVE_INFINITY;
//             indexProfile[j] = -1;
//         }
//     }
// 
//     @Override
//     public MatrixProfile clone() {
//         int size = profile.length;
//         MatrixProfile copy = new MatrixProfile(size);
//         for (int i = 0; i < size; i++) {
//             copy.profile[i] = profile[i];
//             copy.indexProfile[i] = indexProfile[i];
//         }
//         return copy;
//     }
// }


import org.apache.commons.math3.exception.NumberIsTooSmallException;
import org.apache.commons.math3.util.FastMath;
import ro.hasna.ts.math.exception.ArrayLengthIsTooSmallException;
import ro.hasna.ts.math.exception.CancellationException;
import ro.hasna.ts.math.representation.GenericTransformer;
import ro.hasna.ts.math.type.MatrixProfile;
import java.util.function.Predicate;
/*
 * 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.representation.mp;



/**
 * Useful methods for self-join matrix profile algorithms.
 *
 * @since 0.17
 */
public abstract class SelfJoinAbstractMatrixProfileTransformer extends AbstractMatrixProfileTransformer implements GenericTransformer<double[], MatrixProfile> {
    private static final long serialVersionUID = 4273395812927663256L;

    /**
     * Creates a new instance of this class with normalization enabled and an exclusion zone of 25%.
     *
     * @param window the length of the window
     * @throws NumberIsTooSmallException if window is lower than 4
     */
    public SelfJoinAbstractMatrixProfileTransformer(int window) {
        this(window, 0.25, true);
    }

    /**
     * @param window                  the length of the window
     * @param exclusionZonePercentage percentage of window that should be excluded at distance profile computing
     * @param useNormalization        flag to use Z-Normalization
     * @throws NumberIsTooSmallException if window or window * exclusionZonePercentage is lower than 1
     */
    public SelfJoinAbstractMatrixProfileTransformer(int window, double exclusionZonePercentage, boolean useNormalization) {
        super(window, exclusionZonePercentage, useNormalization);

        int skip = (int) (window * exclusionZonePercentage);
        if (skip < 1) {
            int minWindow = (int) Math.ceil(1 / exclusionZonePercentage);
            throw new NumberIsTooSmallException(window, minWindow, true);
        }
    }

    @Override
    public MatrixProfile transform(double[] input) {
        int len = input.length;
        int skip = (int) (window * exclusionZonePercentage);
        if (len < window + skip) {
// Path: src/main/java/ro/hasna/ts/math/exception/ArrayLengthIsTooSmallException.java
// public class ArrayLengthIsTooSmallException extends NumberIsTooSmallException {
//     private static final long serialVersionUID = -2633584088507009304L;
// 
//     /**
//      * Construct the exception.
//      *
//      * @param wrong          Value that is smaller than the minimum.
//      * @param min            Minimum.
//      * @param boundIsAllowed Whether {@code min} is included in the allowed range.
//      */
//     public ArrayLengthIsTooSmallException(Number wrong, Number min, boolean boundIsAllowed) {
//         super(wrong, min, boundIsAllowed);
//     }
// 
//     /**
//      * Construct the exception with a specific context.
//      *
//      * @param specific       Specific context pattern.
//      * @param wrong          Value that is smaller than the minimum.
//      * @param min            Minimum.
//      * @param boundIsAllowed Whether {@code min} is included in the allowed range.
//      */
//     public ArrayLengthIsTooSmallException(Localizable specific, Number wrong, Number min, boolean boundIsAllowed) {
//         super(specific, wrong, min, boundIsAllowed);
//     }
// }
// 
// Path: src/main/java/ro/hasna/ts/math/exception/CancellationException.java
// public class CancellationException extends MathIllegalStateException {
//     private static final long serialVersionUID = 8488577710717564366L;
// 
//     public CancellationException() {
//         super(LocalizableMessages.OPERATION_WAS_CANCELLED);
//     }
// }
// 
// Path: src/main/java/ro/hasna/ts/math/representation/GenericTransformer.java
// public interface GenericTransformer<I, O> extends Serializable {
//     /**
//      * Transform the input vector from type I into type O.
//      *
//      * @param input the input vector
//      * @return the output vector
//      */
//     O transform(I input);
// }
// 
// Path: src/main/java/ro/hasna/ts/math/type/MatrixProfile.java
// @Data
// public class MatrixProfile implements Cloneable {
//     private final double[] profile;
//     private final int[] indexProfile;
// 
//     public MatrixProfile(double[] profile, int[] indexProfile) {
//         if (profile.length != indexProfile.length) {
//             throw new DimensionMismatchException(profile.length, indexProfile.length);
//         }
// 
//         this.profile = profile;
//         this.indexProfile = indexProfile;
//     }
// 
//     public MatrixProfile(int size) {
//         profile = new double[size];
//         indexProfile = new int[size];
//         for (int j = 0; j < size; j++) {
//             profile[j] = Double.POSITIVE_INFINITY;
//             indexProfile[j] = -1;
//         }
//     }
// 
//     @Override
//     public MatrixProfile clone() {
//         int size = profile.length;
//         MatrixProfile copy = new MatrixProfile(size);
//         for (int i = 0; i < size; i++) {
//             copy.profile[i] = profile[i];
//             copy.indexProfile[i] = indexProfile[i];
//         }
//         return copy;
//     }
// }


// Path: src/main/java/ro/hasna/ts/math/representation/mp/SelfJoinAbstractMatrixProfileTransformer.java
import org.apache.commons.math3.exception.NumberIsTooSmallException;
import org.apache.commons.math3.util.FastMath;
import ro.hasna.ts.math.exception.ArrayLengthIsTooSmallException;
import ro.hasna.ts.math.exception.CancellationException;
import ro.hasna.ts.math.representation.GenericTransformer;
import ro.hasna.ts.math.type.MatrixProfile;
import java.util.function.Predicate;

/*
 * 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.representation.mp;



/**
 * Useful methods for self-join matrix profile algorithms.
 *
 * @since 0.17
 */
public abstract class SelfJoinAbstractMatrixProfileTransformer extends AbstractMatrixProfileTransformer implements GenericTransformer<double[], MatrixProfile> {
    private static final long serialVersionUID = 4273395812927663256L;

    /**
     * Creates a new instance of this class with normalization enabled and an exclusion zone of 25%.
     *
     * @param window the length of the window
     * @throws NumberIsTooSmallException if window is lower than 4
     */
    public SelfJoinAbstractMatrixProfileTransformer(int window) {
        this(window, 0.25, true);
    }

    /**
     * @param window                  the length of the window
     * @param exclusionZonePercentage percentage of window that should be excluded at distance profile computing
     * @param useNormalization        flag to use Z-Normalization
     * @throws NumberIsTooSmallException if window or window * exclusionZonePercentage is lower than 1
     */
    public SelfJoinAbstractMatrixProfileTransformer(int window, double exclusionZonePercentage, boolean useNormalization) {
        super(window, exclusionZonePercentage, useNormalization);

        int skip = (int) (window * exclusionZonePercentage);
        if (skip < 1) {
            int minWindow = (int) Math.ceil(1 / exclusionZonePercentage);
            throw new NumberIsTooSmallException(window, minWindow, true);
        }
    }

    @Override
    public MatrixProfile transform(double[] input) {
        int len = input.length;
        int skip = (int) (window * exclusionZonePercentage);
        if (len < window + skip) {
            throw new ArrayLengthIsTooSmallException(len, window + skip, true);