// Path: src/main/java/ro/hasna/ts/math/exception/ArrayLengthIsNotDivisibleException.java
// public class ArrayLengthIsNotDivisibleException extends NumberIsNotDivisibleException {
//     private static final long serialVersionUID = 1652407890465175618L;
// 
//     /**
//      * Construct the exception.
//      *
//      * @param wrong  Value that is not divisible with the factor.
//      * @param factor The factor.
//      */
//     public ArrayLengthIsNotDivisibleException(Number wrong, Integer factor) {
//         super(wrong, factor);
//     }
// 
//     /**
//      * Construct the exception with a specific context.
//      *
//      * @param specific Specific context pattern.
//      * @param wrong    Value that is not divisible with the factor.
//      * @param factor   The factor.
//      */
//     public ArrayLengthIsNotDivisibleException(Localizable specific, Number wrong, Integer factor) {
//         super(specific, wrong, factor);
//     }
// }
// 
// 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/type/MeanSlopePair.java
// @Data
// public class MeanSlopePair {
//     /**
//      * The mean value for the segment.
//      */
//     private final double mean;
//     /**
//      * The slope of the segment.
//      */
//     private final double slope;
// }


import org.apache.commons.math3.exception.NumberIsTooSmallException;
import org.apache.commons.math3.stat.correlation.Covariance;
import org.apache.commons.math3.stat.descriptive.moment.Mean;
import org.apache.commons.math3.stat.descriptive.moment.Variance;
import ro.hasna.ts.math.exception.ArrayLengthIsNotDivisibleException;
import ro.hasna.ts.math.exception.ArrayLengthIsTooSmallException;
import ro.hasna.ts.math.type.MeanSlopePair;
/*
 * 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;


/**
 * Implements the Piecewise Linear Aggregate Approximation (PLAA) algorithm.
 * <p>
 * Reference:
 * Hung N. Q. V., Anh D. T. (2008)
 * <i>An Improvement of PAA for Dimensionality Reduction in Large Time Series Databases</i>
 * </p>
 *
 * @since 0.6
 */
public class PiecewiseLinearAggregateApproximation implements GenericTransformer<double[], MeanSlopePair[]> {
    private static final long serialVersionUID = -4073250977010141095L;
    private final int segments;

    /**
     * Creates a new instance of this class.
     *
     * @param segments the number of segments
     * @throws NumberIsTooSmallException if segments lower than 1
     */
    public PiecewiseLinearAggregateApproximation(int segments) {
        if (segments < 1) {
            throw new NumberIsTooSmallException(segments, 1, true);
        }

        this.segments = segments;
    }

    /**
     * Transform a given sequence of values using the algorithm PLAA.
     *
     * @param values the sequence of values
     * @return the result of the transformation
     */
    public MeanSlopePair[] transform(double[] values) {
        int len = values.length;
        if (len < segments) {
            throw new ArrayLengthIsTooSmallException(len, segments, true);
        }

        int modulo = len % segments;
        if (modulo != 0) {
            throw new ArrayLengthIsNotDivisibleException(len, segments);
        }

// Path: src/main/java/ro/hasna/ts/math/exception/ArrayLengthIsNotDivisibleException.java
// public class ArrayLengthIsNotDivisibleException extends NumberIsNotDivisibleException {
//     private static final long serialVersionUID = 1652407890465175618L;
// 
//     /**
//      * Construct the exception.
//      *
//      * @param wrong  Value that is not divisible with the factor.
//      * @param factor The factor.
//      */
//     public ArrayLengthIsNotDivisibleException(Number wrong, Integer factor) {
//         super(wrong, factor);
//     }
// 
//     /**
//      * Construct the exception with a specific context.
//      *
//      * @param specific Specific context pattern.
//      * @param wrong    Value that is not divisible with the factor.
//      * @param factor   The factor.
//      */
//     public ArrayLengthIsNotDivisibleException(Localizable specific, Number wrong, Integer factor) {
//         super(specific, wrong, factor);
//     }
// }
// 
// 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/type/MeanSlopePair.java
// @Data
// public class MeanSlopePair {
//     /**
//      * The mean value for the segment.
//      */
//     private final double mean;
//     /**
//      * The slope of the segment.
//      */
//     private final double slope;
// }


// Path: src/main/java/ro/hasna/ts/math/representation/PiecewiseLinearAggregateApproximation.java
import org.apache.commons.math3.exception.NumberIsTooSmallException;
import org.apache.commons.math3.stat.correlation.Covariance;
import org.apache.commons.math3.stat.descriptive.moment.Mean;
import org.apache.commons.math3.stat.descriptive.moment.Variance;
import ro.hasna.ts.math.exception.ArrayLengthIsNotDivisibleException;
import ro.hasna.ts.math.exception.ArrayLengthIsTooSmallException;
import ro.hasna.ts.math.type.MeanSlopePair;

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


/**
 * Implements the Piecewise Linear Aggregate Approximation (PLAA) algorithm.
 * <p>
 * Reference:
 * Hung N. Q. V., Anh D. T. (2008)
 * <i>An Improvement of PAA for Dimensionality Reduction in Large Time Series Databases</i>
 * </p>
 *
 * @since 0.6
 */
public class PiecewiseLinearAggregateApproximation implements GenericTransformer<double[], MeanSlopePair[]> {
    private static final long serialVersionUID = -4073250977010141095L;
    private final int segments;

    /**
     * Creates a new instance of this class.
     *
     * @param segments the number of segments
     * @throws NumberIsTooSmallException if segments lower than 1
     */
    public PiecewiseLinearAggregateApproximation(int segments) {
        if (segments < 1) {
            throw new NumberIsTooSmallException(segments, 1, true);
        }

        this.segments = segments;
    }

    /**
     * Transform a given sequence of values using the algorithm PLAA.
     *
     * @param values the sequence of values
     * @return the result of the transformation
     */
    public MeanSlopePair[] transform(double[] values) {
        int len = values.length;
        if (len < segments) {
            throw new ArrayLengthIsTooSmallException(len, segments, true);
        }

        int modulo = len % segments;
        if (modulo != 0) {
            throw new ArrayLengthIsNotDivisibleException(len, segments);
        }

        MeanSlopePair[] reducedValues = new MeanSlopePair[segments];