View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math4.legacy.fitting.leastsquares;
18  
19  import org.apache.commons.math4.legacy.fitting.leastsquares.LeastSquaresProblem.Evaluation;
20  import org.apache.commons.math4.legacy.linear.RealMatrix;
21  import org.apache.commons.math4.legacy.linear.RealVector;
22  import org.apache.commons.math4.legacy.optim.ConvergenceChecker;
23  import org.junit.Assert;
24  import org.junit.Test;
25  
26  /** Unit tests for {@link EvaluationRmsChecker}. */
27  public class EvaluationRmsCheckerTest {
28  
29      /** check {@link ConvergenceChecker#converged(int, Object, Object)}. */
30      @Test
31      public void testConverged() {
32          //setup
33          ConvergenceChecker<Evaluation> checker = new EvaluationRmsChecker(0.1, 1);
34          Evaluation e200 = mockEvaluation(200);
35          Evaluation e1 = mockEvaluation(1);
36  
37          //action + verify
38          //just matches rel tol
39          Assert.assertTrue(checker.converged(0, e200, mockEvaluation(210)));
40          //just matches abs tol
41          Assert.assertTrue(checker.converged(0, e1, mockEvaluation(1.9)));
42          //matches both
43          Assert.assertTrue(checker.converged(0, e1, mockEvaluation(1.01)));
44          //matches neither
45          Assert.assertFalse(checker.converged(0, e200, mockEvaluation(300)));
46      }
47  
48      /**
49       * Create a mock {@link Evaluation}.
50       *
51       * @param rms the evaluation's rms.
52       * @return a new mock evaluation.
53       */
54      private static Evaluation mockEvaluation(final double rms) {
55          return new Evaluation() {
56              @Override
57              public RealMatrix getCovariances(double threshold) {
58                  return null;
59              }
60  
61              @Override
62              public RealVector getSigma(double covarianceSingularityThreshold) {
63                  return null;
64              }
65  
66              @Override
67              public double getRMS() {
68                  return rms;
69              }
70  
71              @Override
72              public RealMatrix getJacobian() {
73                  return null;
74              }
75  
76              @Override
77              public double getCost() {
78                  return 0;
79              }
80  
81              @Override
82              public double getChiSquare() {
83                  return 0;
84              }
85  
86              @Override
87              public double getReducedChiSquare(int n) {
88                  return 0;
89              }
90  
91              @Override
92              public RealVector getResiduals() {
93                  return null;
94              }
95  
96              @Override
97              public RealVector getPoint() {
98                  return null;
99              }
100         };
101     }
102 }