/** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { weightMatrix = new DiagonalMatrix(weight); }
/** {@inheritDoc} */ @Override public RealMatrix copy() { return new DiagonalMatrix(data); }
/** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { weightMatrix = new DiagonalMatrix(weight); }
/** * {@inheritDoc} * * @throws DimensionMismatchException if the requested dimensions are not equal. */ @Override public RealMatrix createMatrix(final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException, DimensionMismatchException { if (rowDimension != columnDimension) { throw new DimensionMismatchException(rowDimension, columnDimension); } return new DiagonalMatrix(rowDimension); }
/** {@inheritDoc} */ @Override public double[] operate(final double[] v) throws DimensionMismatchException { return multiply(new DiagonalMatrix(v, false)).getDataRef(); }
/** * Apply a diagonal weight matrix to the {@link LeastSquaresProblem}. * * @param problem the unweighted problem * @param weights the diagonal of the weight matrix * @return a new {@link LeastSquaresProblem} with the weights applied. The original * {@code problem} is not modified. */ public static LeastSquaresProblem weightDiagonal(final LeastSquaresProblem problem, final RealVector weights) { // TODO more efficient implementation return weightMatrix(problem, new DiagonalMatrix(weights.toArray())); }
/** * Returns {@code this} minus {@code m}. * * @param m Matrix to be subtracted. * @return {@code this - m} * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ public DiagonalMatrix subtract(final DiagonalMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkSubtractionCompatible(this, m); final int dim = getRowDimension(); final double[] outData = new double[dim]; for (int i = 0; i < dim; i++) { outData[i] = data[i] - m.data[i]; } return new DiagonalMatrix(outData, false); }
/** * Computes the inverse of this diagonal matrix. * * @param threshold Singularity threshold. * @return the inverse of {@code m} * @throws SingularMatrixException if the matrix is singular * @since 3.3 */ public DiagonalMatrix inverse(double threshold) throws SingularMatrixException { if (isSingular(threshold)) { throw new SingularMatrixException(); } final double[] result = new double[data.length]; for (int i = 0; i < data.length; i++) { result[i] = 1.0 / data[i]; } return new DiagonalMatrix(result, false); }
/** * Returns the result of postmultiplying {@code this} by {@code m}. * * @param m matrix to postmultiply by * @return {@code this * m} * @throws DimensionMismatchException if * {@code columnDimension(this) != rowDimension(m)} */ public DiagonalMatrix multiply(final DiagonalMatrix m) throws DimensionMismatchException { MatrixUtils.checkMultiplicationCompatible(this, m); final int dim = getRowDimension(); final double[] outData = new double[dim]; for (int i = 0; i < dim; i++) { outData[i] = data[i] * m.data[i]; } return new DiagonalMatrix(outData, false); }
/** * Compute the sum of {@code this} and {@code m}. * * @param m Matrix to be added. * @return {@code this + m}. * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ public DiagonalMatrix add(final DiagonalMatrix m) throws MatrixDimensionMismatchException { // Safety check. MatrixUtils.checkAdditionCompatible(this, m); final int dim = getRowDimension(); final double[] outData = new double[dim]; for (int i = 0; i < dim; i++) { outData[i] = data[i] + m.data[i]; } return new DiagonalMatrix(outData, false); }
/** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { if (m instanceof DiagonalMatrix) { final int dim = m.getRowDimension(); final RealMatrix sqrtM = new DiagonalMatrix(dim); for (int i = 0; i < dim; i++) { sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i))); } return sqrtM; } else { final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); } } }
/** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private static RealMatrix squareRoot(final RealMatrix m) { if (m instanceof DiagonalMatrix) { final int dim = m.getRowDimension(); final RealMatrix sqrtM = new DiagonalMatrix(dim); for (int i = 0; i < dim; i++) { sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i))); } return sqrtM; } else { final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); } }
/** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { if (m instanceof DiagonalMatrix) { final int dim = m.getRowDimension(); final RealMatrix sqrtM = new DiagonalMatrix(dim); for (int i = 0; i < dim; i++) { sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i))); } return sqrtM; } else { final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); } } }
/** {@inheritDoc} */ @Override protected LeastSquaresProblem getProblem(Collection<WeightedObservedPoint> observations) { // Prepare least-squares problem. final int len = observations.size(); final double[] target = new double[len]; final double[] weights = new double[len]; int count = 0; for (WeightedObservedPoint obs : observations) { target[count] = obs.getY(); weights[count] = obs.getWeight(); ++count; } final AbstractCurveFitter.TheoreticalValuesFunction model = new AbstractCurveFitter.TheoreticalValuesFunction(function, observations); // Create an optimizer for fitting the curve to the observed points. return new LeastSquaresBuilder(). maxEvaluations(Integer.MAX_VALUE). maxIterations(maxIter). start(initialGuess). target(target). weight(new DiagonalMatrix(weights)). model(model.getModelFunction(), model.getModelFunctionJacobian()). build(); } }
/** {@inheritDoc} */ @Override protected LeastSquaresProblem getProblem(Collection<WeightedObservedPoint> observations) { // Prepare least-squares problem. final int len = observations.size(); final double[] target = new double[len]; final double[] weights = new double[len]; int i = 0; for (WeightedObservedPoint obs : observations) { target[i] = obs.getY(); weights[i] = obs.getWeight(); ++i; } final AbstractCurveFitter.TheoreticalValuesFunction model = new AbstractCurveFitter.TheoreticalValuesFunction(FUNCTION, observations); if (initialGuess == null) { throw new MathInternalError(); } // Return a new least squares problem set up to fit a polynomial curve to the // observed points. return new LeastSquaresBuilder(). maxEvaluations(Integer.MAX_VALUE). maxIterations(maxIter). start(initialGuess). target(target). weight(new DiagonalMatrix(weights)). model(model.getModelFunction(), model.getModelFunctionJacobian()). build(); }
/** {@inheritDoc} */ @Override protected LeastSquaresProblem getProblem(Collection<WeightedObservedPoint> observations) { // Prepare least-squares problem. final int len = observations.size(); final double[] target = new double[len]; final double[] weights = new double[len]; int i = 0; for (WeightedObservedPoint obs : observations) { target[i] = obs.getY(); weights[i] = obs.getWeight(); ++i; } final AbstractCurveFitter.TheoreticalValuesFunction model = new AbstractCurveFitter.TheoreticalValuesFunction(FUNCTION, observations); final double[] startPoint = initialGuess != null ? initialGuess : // Compute estimation. new ParameterGuesser(observations).guess(); // Return a new least squares problem set up to fit a Gaussian curve to the // observed points. return new LeastSquaresBuilder(). maxEvaluations(Integer.MAX_VALUE). maxIterations(maxIter). start(startPoint). target(target). weight(new DiagonalMatrix(weights)). model(model.getModelFunction(), model.getModelFunctionJacobian()). build(); }
start(startPoint). target(target). weight(new DiagonalMatrix(weights)). model(model.getModelFunction(), model.getModelFunctionJacobian()). build();
RealMatrix sqrtEigenValues = new DiagonalMatrix(d); for(int i=0;i<d;i++) { sqrtEigenValues.setEntry(i, i, FastMath.sqrt(eigenValues.getEntry(i)));
/** {@inheritDoc} */ @Override public RealMatrix copy() { return new DiagonalMatrix(data); }
/** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { weightMatrix = new DiagonalMatrix(weight); }