Java Code Examples for org.apache.commons.math3.linear.RealMatrix#getEntry()

The following examples show how to use org.apache.commons.math3.linear.RealMatrix#getEntry() . 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: CMAESOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @return Row matrix representing the sums of the rows.
 */
private static RealMatrix sumRows(final RealMatrix m) {
    final double[][] d = new double[1][m.getColumnDimension()];
    for (int c = 0; c < m.getColumnDimension(); c++) {
        double sum = 0;
        for (int r = 0; r < m.getRowDimension(); r++) {
            sum += m.getEntry(r, c);
        }
        d[0][c] = sum;
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 2
Source File: JGenProg2017_00131_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix 1.
 * @param n Input matrix 2.
 * @return Matrix where the elements of m and n are element-wise divided.
 */
private static RealMatrix divide(final RealMatrix m, final RealMatrix n) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = m.getEntry(r, c) / n.getEntry(r, c);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 3
Source File: Arja_00166_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix 1.
 * @param n Input matrix 2.
 * @return the matrix where the elements of m and n are element-wise multiplied.
 */
private static RealMatrix times(final RealMatrix m, final RealMatrix n) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = m.getEntry(r, c) * n.getEntry(r, c);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 4
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param cols Columns to select.
 * @return Matrix representing the selected columns.
 */
private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) {
    double[][] d = new double[m.getRowDimension()][cols.length];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < cols.length; c++) {
            d[r][c] = m.getEntry(r, cols[c]);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 5
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param cols Columns to select.
 * @return Matrix representing the selected columns.
 */
private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) {
    final double[][] d = new double[m.getRowDimension()][cols.length];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < cols.length; c++) {
            d[r][c] = m.getEntry(r, cols[c]);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 6
Source File: JGenProg2017_0020_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param cols Columns to select.
 * @return Matrix representing the selected columns.
 */
private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) {
    double[][] d = new double[m.getRowDimension()][cols.length];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < cols.length; c++) {
            d[r][c] = m.getEntry(r, cols[c]);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 7
Source File: Math_20_CMAESOptimizer_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @return Row matrix representing the sums of the rows.
 */
private static RealMatrix sumRows(final RealMatrix m) {
    double[][] d = new double[1][m.getColumnDimension()];
    for (int c = 0; c < m.getColumnDimension(); c++) {
        double sum = 0;
        for (int r = 0; r < m.getRowDimension(); r++) {
            sum += m.getEntry(r, c);
        }
        d[0][c] = sum;
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 8
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @return Row matrix representing the sums of the rows.
 */
private static RealMatrix sumRows(final RealMatrix m) {
    final double[][] d = new double[1][m.getColumnDimension()];
    for (int c = 0; c < m.getColumnDimension(); c++) {
        double sum = 0;
        for (int r = 0; r < m.getRowDimension(); r++) {
            sum += m.getEntry(r, c);
        }
        d[0][c] = sum;
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 9
Source File: Arja_00154_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @return Row matrix representing the sums of the rows.
 */
private static RealMatrix sumRows(final RealMatrix m) {
    double[][] d = new double[1][m.getColumnDimension()];
    for (int c = 0; c < m.getColumnDimension(); c++) {
        double sum = 0;
        for (int r = 0; r < m.getRowDimension(); r++) {
            sum += m.getEntry(r, c);
        }
        d[0][c] = sum;
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 10
Source File: Math_19_CMAESOptimizer_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @return Row matrix representing the sums of the rows.
 */
private static RealMatrix sumRows(final RealMatrix m) {
    double[][] d = new double[1][m.getColumnDimension()];
    for (int c = 0; c < m.getColumnDimension(); c++) {
        double sum = 0;
        for (int r = 0; r < m.getRowDimension(); r++) {
            sum += m.getEntry(r, c);
        }
        d[0][c] = sum;
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 11
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m Input matrix
 * @return Matrix representing the element-wise square (^2) of m.
 */
private static RealMatrix square(final RealMatrix m) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            double e = m.getEntry(r, c);
            d[r][c] = e * e;
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 12
Source File: 1_CMAESOptimizer.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param cols Columns to select.
 * @return Matrix representing the selected columns.
 */
private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) {
    double[][] d = new double[m.getRowDimension()][cols.length];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < cols.length; c++) {
            d[r][c] = m.getEntry(r, cols[c]);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 13
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m Input matrix 1.
 * @param n Input matrix 2.
 * @return the matrix where the elements of m and n are element-wise multiplied.
 */
private static RealMatrix times(final RealMatrix m, final RealMatrix n) {
    final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = m.getEntry(r, c) * n.getEntry(r, c);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 14
Source File: Cardumen_00163_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix 1.
 * @param n Input matrix 2.
 * @return Matrix where the elements of m and n are element-wise divided.
 */
private static RealMatrix divide(final RealMatrix m, final RealMatrix n) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = m.getEntry(r, c) / n.getEntry(r, c);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 15
Source File: Arja_00181_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param cols Columns to select.
 * @return Matrix representing the selected columns.
 */
private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) {
    double[][] d = new double[m.getRowDimension()][cols.length];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < cols.length; c++) {
            d[r][c] = m.getEntry(r, cols[c]);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 16
Source File: 1_CMAESOptimizer.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param k Diagonal position.
 * @return Upper triangular part of matrix.
 */
private static RealMatrix triu(final RealMatrix m, int k) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = r <= c - k ? m.getEntry(r, c) : 0;
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 17
Source File: Arja_00181_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix 1.
 * @param n Input matrix 2.
 * @return Matrix where the elements of m and n are element-wise divided.
 */
private static RealMatrix divide(final RealMatrix m, final RealMatrix n) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = m.getEntry(r, c) / n.getEntry(r, c);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 18
Source File: Arja_0079_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix 1.
 * @param n Input matrix 2.
 * @return Matrix where the elements of m and n are element-wise divided.
 */
private static RealMatrix divide(final RealMatrix m, final RealMatrix n) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = m.getEntry(r, c) / n.getEntry(r, c);
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 19
Source File: Cardumen_00211_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param m Input matrix.
 * @param k Diagonal position.
 * @return Upper triangular part of matrix.
 */
private static RealMatrix triu(final RealMatrix m, int k) {
    double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
    for (int r = 0; r < m.getRowDimension(); r++) {
        for (int c = 0; c < m.getColumnDimension(); c++) {
            d[r][c] = r <= c - k ? m.getEntry(r, c) : 0;
        }
    }
    return new Array2DRowRealMatrix(d, false);
}
 
Example 20
Source File: GaussNewtonOptimizer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public PointVectorValuePair doOptimize() {
    final ConvergenceChecker<PointVectorValuePair> checker
        = getConvergenceChecker();

    // Computation will be useless without a checker (see "for-loop").
    if (checker == null) {
        throw new NullArgumentException();
    }

    final double[] targetValues = getTarget();
    final int nR = targetValues.length; // Number of observed data.

    final RealMatrix weightMatrix = getWeight();
    // Diagonal of the weight matrix.
    final double[] residualsWeights = new double[nR];
    for (int i = 0; i < nR; i++) {
        residualsWeights[i] = weightMatrix.getEntry(i, i);
    }

    final double[] currentPoint = getStartPoint();
    final int nC = currentPoint.length;

    // iterate until convergence is reached
    PointVectorValuePair current = null;
    int iter = 0;
    for (boolean converged = false; !converged;) {
        ++iter;

        // evaluate the objective function and its jacobian
        PointVectorValuePair previous = current;
        // Value of the objective function at "currentPoint".
        final double[] currentObjective = computeObjectiveValue(currentPoint);
        final double[] currentResiduals = computeResiduals(currentObjective);
        final RealMatrix weightedJacobian = computeWeightedJacobian(currentPoint);
        current = new PointVectorValuePair(currentPoint, currentObjective);

        // build the linear problem
        final double[]   b = new double[nC];
        final double[][] a = new double[nC][nC];
        for (int i = 0; i < nR; ++i) {

            final double[] grad   = weightedJacobian.getRow(i);
            final double weight   = residualsWeights[i];
            final double residual = currentResiduals[i];

            // compute the normal equation
            final double wr = weight * residual;
            for (int j = 0; j < nC; ++j) {
                b[j] += wr * grad[j];
            }

            // build the contribution matrix for measurement i
            for (int k = 0; k < nC; ++k) {
                double[] ak = a[k];
                double wgk = weight * grad[k];
                for (int l = 0; l < nC; ++l) {
                    ak[l] += wgk * grad[l];
                }
            }
        }

        try {
            // solve the linearized least squares problem
            RealMatrix mA = new BlockRealMatrix(a);
            DecompositionSolver solver = useLU ?
                    new LUDecomposition(mA).getSolver() :
                    new QRDecomposition(mA).getSolver();
            final double[] dX = solver.solve(new ArrayRealVector(b, false)).toArray();
            // update the estimated parameters
            for (int i = 0; i < nC; ++i) {
                currentPoint[i] += dX[i];
            }
        } catch (SingularMatrixException e) {
            throw new ConvergenceException(LocalizedFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM);
        }

        // Check convergence.
        if (previous != null) {
            converged = checker.converged(iter, previous, current);
            if (converged) {
                cost = computeCost(currentResiduals);
                // Update (deprecated) "point" field.
                point = current.getPoint();
                return current;
            }
        }
    }
    // Must never happen.
    throw new MathInternalError();
}