类org.apache.commons.math3.linear.DiagonalMatrix源码实例Demo

下面列出了怎么用org.apache.commons.math3.linear.DiagonalMatrix的API类实例代码及写法,或者点击链接到github查看源代码。

@Test
public void testInconsistentEquations() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 1,  1 },
            { 1, -1 },
            { 1,  3 }
    }, new double[] { 3, 1, 4 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 1, 1 });

    double[] optimum = optimizer.optimize().getPoint();

    Assert.assertTrue(optimizer.computeRMS(optimum) > 0.1);
}
 
private void assertSVDValues(final File outputFileV, final File outputFileS, final File outputFileU) {
    try {
        final ReadCountCollection rcc = ReadCountCollectionUtils.parse(CONTROL_PCOV_FULL_FILE);
        final SVD svd = SVDFactory.createSVD(rcc.counts());
        final RealMatrix sDiag = new DiagonalMatrix(svd.getSingularValues());
        assertOutputFileValues(outputFileU, svd.getU());
        assertOutputFileValues(outputFileS, sDiag);
        assertOutputFileValues(outputFileV, svd.getV());
        assertUnitaryMatrix(svd.getV());
        assertUnitaryMatrix(svd.getU());
        Assert.assertTrue(MatrixUtils.isSymmetric(sDiag, 1e-32));

    } catch (final IOException ioe) {
        Assert.fail("Could not open test file: " + CONTROL_PCOV_FULL_FILE, ioe);
    }
}
 
@Test
public void testInconsistentSizes1() {
    try {
        LinearProblem problem
                = new LinearProblem(new double[][]{{1, 0},
                {0, 1}},
                new double[]{-1, 1});

        //TODO why is this part here? hasn't it been tested already?
        Optimum optimum = optimizer.optimize(problem.getBuilder().build());

        Assert.assertEquals(0, optimum.getRMS(), TOl);
        assertEquals(TOl, optimum.getPoint(), -1, 1);

        //TODO move to builder test
        optimizer.optimize(
                problem.getBuilder().weight(new DiagonalMatrix(new double[]{1})).build());

        fail(optimizer);
    } catch (DimensionMismatchException e) {
        //expected
    }
}
 
@Test
public void testInconsistentSizes2() {
    try {
        LinearProblem problem
                = new LinearProblem(new double[][]{{1, 0}, {0, 1}},
                new double[]{-1, 1});

        Optimum optimum = optimizer.optimize(problem.getBuilder().build());

        Assert.assertEquals(0, optimum.getRMS(), TOl);
        assertEquals(TOl, optimum.getPoint(), -1, 1);

        //TODO move to builder test
        optimizer.optimize(
                problem.getBuilder()
                        .target(new double[]{1})
                        .weight(new DiagonalMatrix(new double[]{1}))
                        .build()
        );

        fail(optimizer);
    } catch (DimensionMismatchException e) {
        //expected
    }
}
 
@Test
public void testCircleFittingGoodInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] weights = new double[points.length];
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }
    final double[] start = {0, 0};

    Optimum optimum = optimizer.optimize(
            builder(circle).weight(new DiagonalMatrix(weights)).start(start).build());

    assertEquals(1e-6, optimum.getPoint(), -0.1517383071957963, 0.2074999736353867);
    Assert.assertEquals(0.04268731682389561, optimum.getRMS(), 1e-8);
}
 
@Test(expected=DimensionMismatchException.class)
public void testInconsistentSizes1() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, 0 },
                                             { 0, 1 } },
                            new double[] { -1, 1 });
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1 }))
        .withStartPoint(new double[] { 0, 0 });

    double[] optimum = optimizer.optimize().getPoint();

    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    Assert.assertEquals(-1, optimum[0], 1e-10);
    Assert.assertEquals(1, optimum[1], 1e-10);

    optimizer.withWeight(new DiagonalMatrix(new double[] { 1 })).optimize();
}
 
@Test
public void testCircleFittingGoodInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] target = new double[points.length];
    Arrays.fill(target, 0);
    double[] weights = new double[points.length];
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(circle.getModelFunction(),
                              circle.getModelFunctionJacobian())
        .withTarget(target)
        .withWeight(new DiagonalMatrix(weights))
        .withStartPoint(new double[] { 0, 0 });

    double[] optimum = optimizer.optimize().getPoint();

    Assert.assertEquals(-0.1517383071957963, optimum[0], 1e-6);
    Assert.assertEquals(0.2074999736353867,  optimum[1], 1e-6);
    Assert.assertEquals(0.04268731682389561, optimizer.computeRMS(optimum), 1e-8);
}
 
源代码8 项目: astor   文件: AbstractLeastSquaresOptimizerTest.java
@Test
public void testComputeCost() throws IOException {
    final StatisticalReferenceDataset dataset
        = StatisticalReferenceDatasetFactory.createKirby2();
    final double[] a = dataset.getParameters();
    final double[] y = dataset.getData()[1];
    final double[] w = new double[y.length];
    Arrays.fill(w, 1d);

    StatisticalReferenceDataset.LeastSquaresProblem problem
        = dataset.getLeastSquaresProblem();

    final LevenbergMarquardtOptimizer optim = LevenbergMarquardtOptimizer.create()
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(y)
        .withWeight(new DiagonalMatrix(w))
        .withStartPoint(a);

    final double expected = dataset.getResidualSumOfSquares();
    final double cost = optim.computeCost(optim.computeResiduals(optim.getModel().value(optim.getStart())));
    final double actual = cost * cost;
    Assert.assertEquals(dataset.getName(), expected, actual, 1e-11 * expected);
}
 
@Test
public void testTrivial() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 2 } },
                            new double[] { 3 });
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1 }))
        .withStartPoint(new double[] { 0 });

    PointVectorValuePair optimum = optimizer.optimize();

    Assert.assertEquals(0, optimizer.computeRMS(optimum.getPoint()), 1e-10);
    Assert.assertEquals(1.5, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(3.0, optimum.getValue()[0], 1e-10);
}
 
@Test
public void testQRColumnsPermutation() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, -1 }, { 0, 2 }, { 1, -2 } },
                            new double[] { 4, 6, 1 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0 });

    PointVectorValuePair optimum = optimizer.optimize();

    Assert.assertEquals(0, optimizer.computeRMS(optimum.getPoint()), 1e-10);
    Assert.assertEquals(7, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(3, optimum.getPoint()[1], 1e-10);
    Assert.assertEquals(4, optimum.getValue()[0], 1e-10);
    Assert.assertEquals(6, optimum.getValue()[1], 1e-10);
    Assert.assertEquals(1, optimum.getValue()[2], 1e-10);
}
 
@Test
public void testNoDependency() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 2, 0, 0, 0, 0, 0 },
            { 0, 2, 0, 0, 0, 0 },
            { 0, 0, 2, 0, 0, 0 },
            { 0, 0, 0, 2, 0, 0 },
            { 0, 0, 0, 0, 2, 0 },
            { 0, 0, 0, 0, 0, 2 }
    }, new double[] { 0, 1.1, 2.2, 3.3, 4.4, 5.5 });
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1, 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0, 0, 0, 0 });

    double[] optimum = optimizer.optimize().getPoint();
    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    for (int i = 0; i < problem.target.length; ++i) {
        Assert.assertEquals(0.55 * i, optimum[i], 1e-10);
    }
}
 
@Test
public void testOneSet() {
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1,  0, 0 },
            { -1,  1, 0 },
            {  0, -1, 1 }
    }, new double[] { 1, 1, 1});

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0 });

    double[] optimum = optimizer.optimize().getPoint();
    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    Assert.assertEquals(1, optimum[0], 1e-10);
    Assert.assertEquals(2, optimum[1], 1e-10);
    Assert.assertEquals(3, optimum[2], 1e-10);
}
 
@Test(expected=ConvergenceException.class)
public void testNonInvertible() throws Exception {
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0 });

    optimizer.optimize();
}
 
@Test
public void testMoreEstimatedParametersSimple() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 3, 2,  0, 0 },
            { 0, 1, -1, 1 },
            { 2, 0,  1, 0 }
    }, new double[] { 7, 3, 5 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 7, 6, 5, 4 });

    double[] optimum = optimizer.optimize().getPoint();
    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
}
 
@Test
public void testRedundantEquations() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 1,  1 },
            { 1, -1 },
            { 1,  3 }
    }, new double[] { 3, 1, 5 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 1, 1 });

    double[] optimum = optimizer.optimize().getPoint();

    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    Assert.assertEquals(2, optimum[0], 1e-10);
    Assert.assertEquals(1, optimum[1], 1e-10);
}
 
@Test
public void testInconsistentEquations() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 1,  1 },
            { 1, -1 },
            { 1,  3 }
    }, new double[] { 3, 1, 4 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 1, 1 });

    double[] optimum = optimizer.optimize().getPoint();

    Assert.assertTrue(optimizer.computeRMS(optimum) > 0.1);
}
 
@Test(expected=DimensionMismatchException.class)
public void testInconsistentSizes1() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, 0 },
                                             { 0, 1 } },
                            new double[] { -1, 1 });
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1 }))
        .withStartPoint(new double[] { 0, 0 });

    double[] optimum = optimizer.optimize().getPoint();

    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    Assert.assertEquals(-1, optimum[0], 1e-10);
    Assert.assertEquals(1, optimum[1], 1e-10);

    optimizer.withWeight(new DiagonalMatrix(new double[] { 1 })).optimize();
}
 
@Test
public void testMoreEstimatedParametersSimple() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 3, 2,  0, 0 },
            { 0, 1, -1, 1 },
            { 2, 0,  1, 0 }
    }, new double[] { 7, 3, 5 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 7, 6, 5, 4 });

    double[] optimum = optimizer.optimize().getPoint();
    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
}
 
@Test
public void testInconsistentSizes1() {
    try {
        LinearProblem problem
                = new LinearProblem(new double[][]{{1, 0},
                {0, 1}},
                new double[]{-1, 1});

        //TODO why is this part here? hasn't it been tested already?
        Optimum optimum = optimizer.optimize(problem.getBuilder().build());

        Assert.assertEquals(0, optimum.getRMS(), TOl);
        assertEquals(TOl, optimum.getPoint(), -1, 1);

        //TODO move to builder test
        optimizer.optimize(
                problem.getBuilder().weight(new DiagonalMatrix(new double[]{1})).build());

        fail(optimizer);
    } catch (DimensionMismatchException e) {
        //expected
    }
}
 
@Test
public void testCircleFittingBadInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] weights = new double[points.length];
    final double[] start = {-12, -12};
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }

    Optimum optimum = optimizer.optimize(builder(circle).weight(new DiagonalMatrix(weights)).start(start).build());

    Vector2D center = new Vector2D(optimum.getPoint().getEntry(0), optimum.getPoint().getEntry(1));
    Assert.assertTrue(optimum.getEvaluations() < 25);
    Assert.assertEquals(0.043, optimum.getRMS(), 1e-3);
    Assert.assertEquals(0.292235, circle.getRadius(center), 1e-6);
    Assert.assertEquals(-0.151738, center.getX(), 1e-6);
    Assert.assertEquals(0.2075001, center.getY(), 1e-6);
}
 
@Test
public void testInconsistentSizes2() {
    try {
        LinearProblem problem
                = new LinearProblem(new double[][]{{1, 0}, {0, 1}},
                new double[]{-1, 1});

        Optimum optimum = optimizer.optimize(problem.getBuilder().build());

        Assert.assertEquals(0, optimum.getRMS(), TOl);
        assertEquals(TOl, optimum.getPoint(), -1, 1);

        //TODO move to builder test
        optimizer.optimize(
                problem.getBuilder()
                        .target(new double[]{1})
                        .weight(new DiagonalMatrix(new double[]{1}))
                        .build()
        );

        fail(optimizer);
    } catch (DimensionMismatchException e) {
        //expected
    }
}
 
源代码22 项目: astor   文件: AbstractLeastSquaresOptimizerTest.java
@Test
public void testComputeRMS() throws IOException {
    final StatisticalReferenceDataset dataset
        = StatisticalReferenceDatasetFactory.createKirby2();
    final double[] a = dataset.getParameters();
    final double[] y = dataset.getData()[1];
    final double[] w = new double[y.length];
    Arrays.fill(w, 1d);

    StatisticalReferenceDataset.LeastSquaresProblem problem
        = dataset.getLeastSquaresProblem();

    final LevenbergMarquardtOptimizer optim = LevenbergMarquardtOptimizer.create()
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(y)
        .withWeight(new DiagonalMatrix(w))
        .withStartPoint(a);

    final double expected = FastMath.sqrt(dataset.getResidualSumOfSquares() /
                                          dataset.getNumObservations());
    final double actual = optim.computeRMS(optim.getStart());
    Assert.assertEquals(dataset.getName(), expected, actual, 1e-11 * expected);
}
 
@Test(expected=ConvergenceException.class)
public void testNonInvertible() throws Exception {
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0 });

    optimizer.optimize();
}
 
源代码24 项目: astor   文件: GaussNewtonOptimizerTest.java
@Test(expected=TooManyEvaluationsException.class)
public void testMaxEvaluations() throws Exception {
    CircleVectorial circle = new CircleVectorial();
    circle.addPoint( 30.0,  68.0);
    circle.addPoint( 50.0,  -6.0);
    circle.addPoint(110.0, -20.0);
    circle.addPoint( 35.0,  15.0);
    circle.addPoint( 45.0,  97.0);

    GaussNewtonOptimizer optimizer = createOptimizer()
        .withConvergenceChecker(new SimpleVectorValueChecker(1e-30, 1e-30))
        .withMaxIterations(Integer.MAX_VALUE)
        .withMaxEvaluations(100)
        .withModelAndJacobian(circle.getModelFunction(),
                              circle.getModelFunctionJacobian())
        .withTarget(new double[] { 0, 0, 0, 0, 0 })
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1, 1, 1 }))
        .withStartPoint(new double[] { 98.680, 47.345 });

    optimizer.optimize();
}
 
@Test
public void testTrivial() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 2 } },
                            new double[] { 3 });
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1 }))
        .withStartPoint(new double[] { 0 });

    PointVectorValuePair optimum = optimizer.optimize();

    Assert.assertEquals(0, optimizer.computeRMS(optimum.getPoint()), 1e-10);
    Assert.assertEquals(1.5, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(3.0, optimum.getValue()[0], 1e-10);
}
 
@Test
public void testQRColumnsPermutation() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, -1 }, { 0, 2 }, { 1, -2 } },
                            new double[] { 4, 6, 1 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0 });

    PointVectorValuePair optimum = optimizer.optimize();

    Assert.assertEquals(0, optimizer.computeRMS(optimum.getPoint()), 1e-10);
    Assert.assertEquals(7, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(3, optimum.getPoint()[1], 1e-10);
    Assert.assertEquals(4, optimum.getValue()[0], 1e-10);
    Assert.assertEquals(6, optimum.getValue()[1], 1e-10);
    Assert.assertEquals(1, optimum.getValue()[2], 1e-10);
}
 
/**
 * 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 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();
    }
}
 
@Test
public void testGetIterations() {
    LeastSquaresProblem lsp = base()
            .target(new double[]{1})
            .weight(new DiagonalMatrix(new double[]{1}))
            .start(new double[]{3})
            .model(new MultivariateJacobianFunction() {
                public Pair<RealVector, RealMatrix> value(final RealVector point) {
                    return new Pair<RealVector, RealMatrix>(
                            new ArrayRealVector(
                                    new double[]{
                                            FastMath.pow(point.getEntry(0), 4)
                                    },
                                    false),
                            new Array2DRowRealMatrix(
                                    new double[][]{
                                            {0.25 * FastMath.pow(point.getEntry(0), 3)}
                                    },
                                    false)
                    );
                }
            })
            .build();

    Optimum optimum = optimizer.optimize(lsp);

    //TODO more specific test? could pass with 'return 1;'
    Assert.assertTrue(optimum.getIterations() > 0);
}
 
源代码30 项目: astor   文件: GaussianCurveFitter.java
/** {@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();

}