Java Code Examples for org.apache.commons.math3.util.MathArrays#copyOf()

The following examples show how to use org.apache.commons.math3.util.MathArrays#copyOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * This method calls {@link MathArrays#shuffle(int[],RandomGenerator)
 * MathArrays.shuffle} in order to create a random shuffle of the set
 * of natural numbers {@code { 0, 1, ..., n - 1 }}.
 *
 * @throws NumberIsTooLargeException if {@code k > n}.
 * @throws NotStrictlyPositiveException if {@code k <= 0}.
 */
public int[] nextPermutation(int n, int k)
    throws NumberIsTooLargeException, NotStrictlyPositiveException {
    if (k > n) {
        throw new NumberIsTooLargeException(LocalizedFormats.PERMUTATION_EXCEEDS_N,
                                            k, n, true);
    }
    if (k <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.PERMUTATION_SIZE,
                                               k);
    }

    int[] index = getNatural(n);
    MathArrays.shuffle(index, getRandomGenerator());

    // Return a new array containing the first "k" entries of "index".
    return MathArrays.copyOf(index, k);
}
 
Example 2
Source File: PercentileTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testAllEstimationTechniquesOnlyLimits() {
    final int N=testArray.length;

    final double[] input=MathArrays.copyOf(testArray);
    Arrays.sort(input);
    final double min = input[0];
    final double max=input[input.length-1];
    //limits may be ducked by 0.01 to induce the condition of p<pMin
    final Object[][] map =
            new Object[][] { { Percentile.EstimationType.LEGACY, 0d, 1d }, { Percentile.EstimationType.R_1, 0d, 1d },
                    { Percentile.EstimationType.R_2, 0d,1d }, { Percentile.EstimationType.R_3, 0.5/N,1d },
                    { Percentile.EstimationType.R_4, 1d/N-0.001,1d },
                    { Percentile.EstimationType.R_5, 0.5/N-0.001,(N-0.5)/N}, { Percentile.EstimationType.R_6, 0.99d/(N+1),
                        1.01d*N/(N+1)},
                    { Percentile.EstimationType.R_7, 0d,1d}, { Percentile.EstimationType.R_8, 1.99d/3/(N+1d/3),
                        (N-1d/3)/(N+1d/3)},
                    { Percentile.EstimationType.R_9, 4.99d/8/(N+0.25), (N-3d/8)/(N+0.25)} };

    for(final Object[] arr:map) {
        final Percentile.EstimationType t= (Percentile.EstimationType) arr[0];
        double pMin=(Double)arr[1];
        final double pMax=(Double)arr[2];
        Assert.assertEquals("Type:"+t,0d, t.index(pMin, N),0d);
        Assert.assertEquals("Type:"+t,N, t.index(pMax, N),0.5d);
        pMin=pMin==0d?pMin+0.01:pMin;
        testAssertMappedValues(testArray, new Object[][] { { t, min }}, pMin, 0.01);
        testAssertMappedValues(testArray, new Object[][] { { t, max }}, pMax * 100, tolerance);
    }
}
 
Example 3
Source File: FastFourierTransformer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the (forward, inverse) transform of the specified real data set.
 *
 * @param f the real data array to be transformed
 * @param type the type of transform (forward, inverse) to be performed
 * @return the complex transformed array
 * @throws MathIllegalArgumentException if the length of the data array is not a power of two
 */
public Complex[] transform(final double[] f, final TransformType type) {
    final double[][] dataRI = new double[][] {
        MathArrays.copyOf(f, f.length), new double[f.length]
    };

    transformInPlace(dataRI, normalization, type);

    return TransformUtils.createComplexArray(dataRI);
}
 
Example 4
Source File: FastFourierTransformer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the (forward, inverse) transform of the specified real data set.
 *
 * @param f the real data array to be transformed
 * @param type the type of transform (forward, inverse) to be performed
 * @return the complex transformed array
 * @throws MathIllegalArgumentException if the length of the data array is not a power of two
 */
public Complex[] transform(final double[] f, final TransformType type) {
    final double[][] dataRI = new double[][] {
        MathArrays.copyOf(f, f.length), new double[f.length]
    };

    transformInPlace(dataRI, normalization, type);

    return TransformUtils.createComplexArray(dataRI);
}
 
Example 5
Source File: StepFunction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Builds a step function from a list of arguments and the corresponding
 * values. Specifically, returns the function h(x) defined by <pre><code>
 * h(x) = y[0] for all x < x[1]
 *        y[1] for x[1] <= x < x[2]
 *        ...
 *        y[y.length - 1] for x >= x[x.length - 1]
 * </code></pre>
 * The value of {@code x[0]} is ignored, but it must be strictly less than
 * {@code x[1]}.
 *
 * @param x Domain values where the function changes value.
 * @param y Values of the function.
 * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException
 * if the {@code x} array is not sorted in strictly increasing order.
 * @throws NullArgumentException if {@code x} or {@code y} are {@code null}.
 * @throws NoDataException if {@code x} or {@code y} are zero-length.
 * @throws DimensionMismatchException if {@code x} and {@code y} do not
 * have the same length.
 */
public StepFunction(double[] x,
                    double[] y) {
    if (x == null ||
        y == null) {
        throw new NullArgumentException();
    }
    if (x.length == 0 ||
        y.length == 0) {
        throw new NoDataException();
    }
    if (y.length != x.length) {
        throw new DimensionMismatchException(y.length, x.length);
    }
    MathArrays.checkOrder(x);

    abscissa = MathArrays.copyOf(x);
    ordinate = MathArrays.copyOf(y);
}
 
Example 6
Source File: RegressionResults.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructor for Regression Results.
 *
 * @param parameters a double array with the regression slope estimates
 * @param varcov the variance covariance matrix, stored either in a square matrix
 * or as a compressed
 * @param isSymmetricCompressed a flag which denotes that the variance covariance
 * matrix is in symmetric compressed format
 * @param nobs the number of observations of the regression estimation
 * @param rank the number of independent variables in the regression
 * @param sumy the sum of the independent variable
 * @param sumysq the sum of the squared independent variable
 * @param sse sum of squared errors
 * @param containsConstant true model has constant,  false model does not have constant
 * @param copyData if true a deep copy of all input data is made, if false only references
 * are copied and the RegressionResults become mutable
 */
public RegressionResults(
        final double[] parameters, final double[][] varcov,
        final boolean isSymmetricCompressed,
        final long nobs, final int rank,
        final double sumy, final double sumysq, final double sse,
        final boolean containsConstant,
        final boolean copyData) {
    if (copyData) {
        this.parameters = MathArrays.copyOf(parameters);
        this.varCovData = new double[varcov.length][];
        for (int i = 0; i < varcov.length; i++) {
            this.varCovData[i] = MathArrays.copyOf(varcov[i]);
        }
    } else {
        this.parameters = parameters;
        this.varCovData = varcov;
    }
    this.isSymmetricVCD = isSymmetricCompressed;
    this.nobs = nobs;
    this.rank = rank;
    this.containsConstant = containsConstant;
    this.globalFitInfo = new double[5];
    Arrays.fill(this.globalFitInfo, Double.NaN);

    if (rank > 0) {
        this.globalFitInfo[SST_IDX] = containsConstant ?
                (sumysq - sumy * sumy / nobs) : sumysq;
    }

    this.globalFitInfo[SSE_IDX] = sse;
    this.globalFitInfo[MSE_IDX] = this.globalFitInfo[SSE_IDX] /
            (nobs - rank);
    this.globalFitInfo[RSQ_IDX] = 1.0 -
            this.globalFitInfo[SSE_IDX] /
            this.globalFitInfo[SST_IDX];

    if (!containsConstant) {
        this.globalFitInfo[ADJRSQ_IDX] = 1.0-
                (1.0 - this.globalFitInfo[RSQ_IDX]) *
                ( (double) nobs / ( (double) (nobs - rank)));
    } else {
        this.globalFitInfo[ADJRSQ_IDX] = 1.0 - (sse * (nobs - 1.0)) /
                (globalFitInfo[SST_IDX] * (nobs - rank));
    }
}
 
Example 7
Source File: StatisticalReferenceDataset.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns a copy of the data arrays. The data is laid out as follows <li>
 * {@code data[0][i] = x[i]},</li> <li>{@code data[1][i] = y[i]},</li>
 *
 * @return the array of data points.
 */
public double[][] getData() {
    return new double[][] {
        MathArrays.copyOf(x), MathArrays.copyOf(y)
    };
}
 
Example 8
Source File: MillerUpdatingRegression.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Gets the order of the regressors, useful if some type of reordering
 * has been called. Calling regress with int[]{} args will trigger
 * a reordering.
 *
 * @return int[] with the current order of the regressors
 */
public int[] getOrderOfRegressors(){
    return MathArrays.copyOf(vorder);
}
 
Example 9
Source File: StatisticalReferenceDataset.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Reurns the certified values of the standard deviations of the parameters.
 *
 * @return the standard deviations of the parameters
 */
public double[] getParametersStandardDeviations() {
    return MathArrays.copyOf(sigA);
}
 
Example 10
Source File: StatisticalReferenceDataset.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the certified values of the paramters.
 *
 * @return the values of the parameters
 */
public double[] getParameters() {
    return MathArrays.copyOf(a);
}
 
Example 11
Source File: StatisticalReferenceDataset.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the certified values of the paramters.
 *
 * @return the values of the parameters
 */
public double[] getParameters() {
    return MathArrays.copyOf(a);
}
 
Example 12
Source File: StatisticalReferenceDataset.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns a copy of the data arrays. The data is laid out as follows <li>
 * {@code data[0][i] = x[i]},</li> <li>{@code data[1][i] = y[i]},</li>
 *
 * @return the array of data points.
 */
public double[][] getData() {
    return new double[][] {
        MathArrays.copyOf(x), MathArrays.copyOf(y)
    };
}
 
Example 13
Source File: StatisticalReferenceDataset.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Reurns the certified values of the standard deviations of the parameters.
 *
 * @return the standard deviations of the parameters
 */
public double[] getParametersStandardDeviations() {
    return MathArrays.copyOf(sigA);
}
 
Example 14
Source File: BesselJ.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a new BesselJResult with the given values and valid value count.
 *
 * @param b values
 * @param n count of valid values
 */
public BesselJResult(double[] b, int n) {
    vals = MathArrays.copyOf(b, b.length);
    nVals = n;
}
 
Example 15
Source File: StatisticalReferenceDataset.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Reurns the certified values of the standard deviations of the parameters.
 *
 * @return the standard deviations of the parameters
 */
public double[] getParametersStandardDeviations() {
    return MathArrays.copyOf(sigA);
}
 
Example 16
Source File: StatisticalReferenceDataset.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the certified values of the paramters.
 *
 * @return the values of the parameters
 */
public double[] getParameters() {
    return MathArrays.copyOf(a);
}
 
Example 17
Source File: MultivariateNormalDistribution.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Gets the mean vector.
 *
 * @return the mean vector.
 */
public double[] getMeans() {
    return MathArrays.copyOf(means);
}
 
Example 18
Source File: StatisticalReferenceDataset.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the {@code i}-th set of initial values of the parameters.
 *
 * @param i the index of the starting point
 * @return the starting point
 */
public double[] getStartingPoint(final int i) {
    return MathArrays.copyOf(startingValues[i]);
}
 
Example 19
Source File: StatisticalReferenceDataset.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Reurns the certified values of the standard deviations of the parameters.
 *
 * @return the standard deviations of the parameters
 */
public double[] getParametersStandardDeviations() {
    return MathArrays.copyOf(sigA);
}
 
Example 20
Source File: StatisticalReferenceDataset.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns a copy of the data arrays. The data is laid out as follows <li>
 * {@code data[0][i] = x[i]},</li> <li>{@code data[1][i] = y[i]},</li>
 *
 * @return the array of data points.
 */
public double[][] getData() {
    return new double[][] {
        MathArrays.copyOf(x), MathArrays.copyOf(y)
    };
}