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