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;
18  
19  import java.util.List;
20  import java.util.ArrayList;
21  
22  /**
23   * Simple container for weighted observed points used
24   * in {@link AbstractCurveFitter curve fitting} algorithms.
25   *
26   * @since 3.3
27   */
28  public class WeightedObservedPoints {
29      /** Observed points. */
30      private final List<WeightedObservedPoint> observations
31          = new ArrayList<>();
32  
33      /**
34       * Adds a point to the sample.
35       * Calling this method is equivalent to calling
36       * {@code add(1.0, x, y)}.
37       *
38       * @param x Abscissa of the point.
39       * @param y Observed value  at {@code x}. After fitting we should
40       * have {@code f(x)} as close as possible to this value.
41       *
42       * @see #add(double, double, double)
43       * @see #add(WeightedObservedPoint)
44       * @see #toList()
45       */
46      public void add(double x, double y) {
47          add(1d, x, y);
48      }
49  
50      /**
51       * Adds a point to the sample.
52       *
53       * @param weight Weight of the observed point.
54       * @param x Abscissa of the point.
55       * @param y Observed value  at {@code x}. After fitting we should
56       * have {@code f(x)} as close as possible to this value.
57       *
58       * @see #add(double, double)
59       * @see #add(WeightedObservedPoint)
60       * @see #toList()
61       */
62      public void add(double weight, double x, double y) {
63          observations.add(new WeightedObservedPoint(weight, x, y));
64      }
65  
66      /**
67       * Adds a point to the sample.
68       *
69       * @param observed Observed point to add.
70       *
71       * @see #add(double, double)
72       * @see #add(double, double, double)
73       * @see #toList()
74       */
75      public void add(WeightedObservedPoint observed) {
76          observations.add(observed);
77      }
78  
79      /**
80       * Gets a <em>snapshot</em> of the observed points.
81       * The list of stored points is copied in order to ensure that
82       * modification of the returned instance does not affect this
83       * container.
84       * Conversely, further modification of this container (through
85       * the {@code add} or {@code clear} methods) will not affect the
86       * returned list.
87       *
88       * @return the observed points, in the order they were added to this
89       * container.
90       *
91       * @see #add(double, double)
92       * @see #add(double, double, double)
93       * @see #add(WeightedObservedPoint)
94       */
95      public List<WeightedObservedPoint> toList() {
96          // The copy is necessary to ensure thread-safety because of the
97          // "clear" method (which otherwise would be able to empty the
98          // list of points while it is being used by another thread).
99          return new ArrayList<>(observations);
100     }
101 
102     /**
103      * Removes all observations from this container.
104      */
105     public void clear() {
106         observations.clear();
107     }
108 }