Java Code Examples for org.apache.commons.math3.linear.RealVector#toArray()

The following examples show how to use org.apache.commons.math3.linear.RealVector#toArray() . 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: CommonsMathSolver.java    From elasticsearch-linear-regression with Apache License 2.0 5 votes vote down vote up
@Override
public SlopeCoefficients estimateCoefficients(final DerivationEquation eq)
    throws EstimationException {
  final double[][] sourceTriangleMatrix = eq.getCovarianceLowerTriangularMatrix();
  // Copy matrix and enhance it to a full matrix as expected by CholeskyDecomposition
  // FIXME: Avoid copy job to speed-up the solving process e.g. by extending the CholeskyDecomposition constructor
  final int length = sourceTriangleMatrix.length;
  final double[][] matrix = new double[length][];
  for (int i = 0; i < length; i++) {
    matrix[i] = new double[length];
    final double[] s = sourceTriangleMatrix[i];
    final double[] t = matrix[i];
    for (int j = 0; j <= i; j++) {
      t[j] = s[j];
    }
    for (int j = i + 1; j < length; j++) {
      t[j] = sourceTriangleMatrix[j][i];
    }
  }
  final RealMatrix coefficients =
      new Array2DRowRealMatrix(matrix, false);
  try {
    final DecompositionSolver solver = new CholeskyDecomposition(coefficients).getSolver();
    final RealVector constants = new ArrayRealVector(eq.getConstraints(), true);
    final RealVector solution = solver.solve(constants);
    return new DefaultSlopeCoefficients(solution.toArray());
  } catch (final NonPositiveDefiniteMatrixException e) {
    throw new EstimationException("Matrix inversion error due to data is linearly dependent", e);
  }
}
 
Example 2
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the -1 times the sum of all coefficients in the given array.
 * @param coefficients coefficients to sum
 * @return the -1 times the sum of all coefficients in the given array.
 */
protected static double getInvertedCoefficientSum(final RealVector coefficients) {
    double sum = 0;
    for (double coefficient : coefficients.toArray()) {
        sum -= coefficient;
    }
    return sum;
}
 
Example 3
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the -1 times the sum of all coefficients in the given array.
 * @param coefficients coefficients to sum
 * @return the -1 times the sum of all coefficients in the given array.
 */
protected static double getInvertedCoefficientSum(final RealVector coefficients) {
    double sum = 0;
    for (double coefficient : coefficients.toArray()) {
        sum -= coefficient;
    }
    return sum;
}
 
Example 4
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the -1 times the sum of all coefficients in the given array.
 * @param coefficients coefficients to sum
 * @return the -1 times the sum of all coefficients in the given array.
 */
protected static double getInvertedCoefficientSum(final RealVector coefficients) {
    double sum = 0;
    for (double coefficient : coefficients.toArray()) {
        sum -= coefficient;
    }
    return sum;
}
 
Example 5
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the -1 times the sum of all coefficients in the given array.
 * @param coefficients coefficients to sum
 * @return the -1 times the sum of all coefficients in the given array.
 */
protected static double getInvertedCoefficientSum(final RealVector coefficients) {
    double sum = 0;
    for (double coefficient : coefficients.toArray()) {
        sum -= coefficient;
    }
    return sum;
}
 
Example 6
Source File: Nopol2017_0066_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Get the -1 times the sum of all coefficients in the given array.
 * @param coefficients coefficients to sum
 * @return the -1 times the sum of all coefficients in the given array.
 */
protected static double getInvertedCoefficientSum(final RealVector coefficients) {
    double sum = 0;
    for (double coefficient : coefficients.toArray()) {
        sum -= coefficient;
    }
    return sum;
}
 
Example 7
Source File: MultivariateNormal.java    From macrobase with Apache License 2.0 5 votes vote down vote up
public MultivariateNormal(RealVector mean, RealMatrix sigma) {
    double[][] arrayOfMatrix = new double[sigma.getColumnDimension()][sigma.getRowDimension()];
    for (int i = 0; i < sigma.getColumnDimension(); i++) {
        arrayOfMatrix[i] = sigma.getRow(i);
    }
    distribution = new MultivariateNormalDistribution(mean.toArray(), arrayOfMatrix);
}
 
Example 8
Source File: LeastSquaresFactory.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public Pair<RealVector, RealMatrix> value(final RealVector point) {
    //TODO get array from RealVector without copying?
    final double[] p = point.toArray();

    // Evaluate.
    return new Pair<RealVector, RealMatrix>(computeValue(p),
                                            computeJacobian(p));
}
 
Example 9
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the -1 times the sum of all coefficients in the given array.
 * @param coefficients coefficients to sum
 * @return the -1 times the sum of all coefficients in the given array.
 */
protected static double getInvertedCoefficientSum(final RealVector coefficients) {
    double sum = 0;
    for (double coefficient : coefficients.toArray()) {
        sum -= coefficient;
    }
    return sum;
}
 
Example 10
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the -1 times the sum of all coefficients in the given array.
 * @param coefficients coefficients to sum
 * @return the -1 times the sum of all coefficients in the given array.
 */
protected static double getInvertedCoefficientSum(final RealVector coefficients) {
    double sum = 0;
    for (double coefficient : coefficients.toArray()) {
        sum -= coefficient;
    }
    return sum;
}
 
Example 11
Source File: 1_SimplexTableau.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the -1 times the sum of all coefficients in the given array.
 * @param coefficients coefficients to sum
 * @return the -1 times the sum of all coefficients in the given array.
 */
protected static double getInvertedCoefficientSum(final RealVector coefficients) {
    double sum = 0;
    for (double coefficient : coefficients.toArray()) {
        sum -= coefficient;
    }
    return sum;
}
 
Example 12
Source File: OmsCurvaturesBivariate.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Calculates the parameters of a bivariate quadratic equation.
 * 
 * @param elevationValues the window of points to use.
 * @return the parameters of the bivariate quadratic equation as [a, b, c, d, e, f]
 */
private static double[] calculateParameters( final double[][] elevationValues ) {
    int rows = elevationValues.length;
    int cols = elevationValues[0].length;
    int pointsNum = rows * cols;

    final double[][] xyMatrix = new double[pointsNum][6];
    final double[] valueArray = new double[pointsNum];

    // TODO check on resolution
    int index = 0;
    for( int y = 0; y < rows; y++ ) {
        for( int x = 0; x < cols; x++ ) {
            xyMatrix[index][0] = x * x; // x^2
            xyMatrix[index][1] = y * y; // y^2
            xyMatrix[index][2] = x * y; // xy
            xyMatrix[index][3] = x; // x
            xyMatrix[index][4] = y; // y
            xyMatrix[index][5] = 1;
            valueArray[index] = elevationValues[y][x];
            index++;
        }
    }

    RealMatrix A = MatrixUtils.createRealMatrix(xyMatrix);
    RealVector z = MatrixUtils.createRealVector(valueArray);

    DecompositionSolver solver = new RRQRDecomposition(A).getSolver();
    RealVector solution = solver.solve(z);

    // start values for a, b, c, d, e, f, all set to 0.0
    final double[] parameters = solution.toArray();
    return parameters;
}
 
Example 13
Source File: 1_SimplexTableau.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the -1 times the sum of all coefficients in the given array.
 * @param coefficients coefficients to sum
 * @return the -1 times the sum of all coefficients in the given array.
 */
protected static double getInvertedCoefficientSum(final RealVector coefficients) {
    double sum = 0;
    for (double coefficient : coefficients.toArray()) {
        sum -= coefficient;
    }
    return sum;
}
 
Example 14
Source File: SimplexTableau.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the -1 times the sum of all coefficients in the given array.
 * @param coefficients coefficients to sum
 * @return the -1 times the sum of all coefficients in the given array.
 */
protected static double getInvertedCoefficientSum(final RealVector coefficients) {
    double sum = 0;
    for (double coefficient : coefficients.toArray()) {
        sum -= coefficient;
    }
    return sum;
}
 
Example 15
Source File: AbstractMultipleLinearRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public double[] estimateRegressionParameters() {
    RealVector b = calculateBeta();
    return b.toArray();
}
 
Example 16
Source File: AbstractMultipleLinearRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public double[] estimateResiduals() {
    RealVector b = calculateBeta();
    RealVector e = yVector.subtract(xMatrix.operate(b));
    return e.toArray();
}
 
Example 17
Source File: AbstractMultipleLinearRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public double[] estimateResiduals() {
    RealVector b = calculateBeta();
    RealVector e = yVector.subtract(xMatrix.operate(b));
    return e.toArray();
}
 
Example 18
Source File: AbstractMultipleLinearRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public double[] estimateRegressionParameters() {
    RealVector b = calculateBeta();
    return b.toArray();
}
 
Example 19
Source File: AbstractMultipleLinearRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public double[] estimateRegressionParameters() {
    RealVector b = calculateBeta();
    return b.toArray();
}
 
Example 20
Source File: AbstractMultipleLinearRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public double[] estimateResiduals() {
    RealVector b = calculateBeta();
    RealVector e = yVector.subtract(xMatrix.operate(b));
    return e.toArray();
}