WeightedObservedPoints.java

  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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.fitting;

  22. import java.util.List;
  23. import java.util.ArrayList;
  24. import java.io.Serializable;

  25. /**
  26.  * Simple container for weighted observed points used
  27.  * in {@link AbstractCurveFitter curve fitting} algorithms.
  28.  *
  29.  */
  30. public class WeightedObservedPoints implements Serializable {

  31.     /** Serializable version id. */
  32.     private static final long serialVersionUID = 20130813L;

  33.     /** Observed points. */
  34.     private final List<WeightedObservedPoint> observations;

  35.     /** Simple constructor.
  36.      * @since 3.0
  37.      */
  38.     public WeightedObservedPoints() {
  39.         this.observations = new ArrayList<>();
  40.     }

  41.     /**
  42.      * Adds a point to the sample.
  43.      * Calling this method is equivalent to calling
  44.      * {@code add(1.0, x, y)}.
  45.      *
  46.      * @param x Abscissa of the point.
  47.      * @param y Observed value  at {@code x}. After fitting we should
  48.      * have {@code f(x)} as close as possible to this value.
  49.      *
  50.      * @see #add(double, double, double)
  51.      * @see #add(WeightedObservedPoint)
  52.      * @see #toList()
  53.      */
  54.     public void add(double x, double y) {
  55.         add(1d, x, y);
  56.     }

  57.     /**
  58.      * Adds a point to the sample.
  59.      *
  60.      * @param weight Weight of the observed point.
  61.      * @param x Abscissa of the point.
  62.      * @param y Observed value  at {@code x}. After fitting we should
  63.      * have {@code f(x)} as close as possible to this value.
  64.      *
  65.      * @see #add(double, double)
  66.      * @see #add(WeightedObservedPoint)
  67.      * @see #toList()
  68.      */
  69.     public void add(double weight, double x, double y) {
  70.         observations.add(new WeightedObservedPoint(weight, x, y));
  71.     }

  72.     /**
  73.      * Adds a point to the sample.
  74.      *
  75.      * @param observed Observed point to add.
  76.      *
  77.      * @see #add(double, double)
  78.      * @see #add(double, double, double)
  79.      * @see #toList()
  80.      */
  81.     public void add(WeightedObservedPoint observed) {
  82.         observations.add(observed);
  83.     }

  84.     /**
  85.      * Gets a <em>snapshot</em> of the observed points.
  86.      * The list of stored points is copied in order to ensure that
  87.      * modification of the returned instance does not affect this
  88.      * container.
  89.      * Conversely, further modification of this container (through
  90.      * the {@code add} or {@code clear} methods) will not affect the
  91.      * returned list.
  92.      *
  93.      * @return the observed points, in the order they were added to this
  94.      * container.
  95.      *
  96.      * @see #add(double, double)
  97.      * @see #add(double, double, double)
  98.      * @see #add(WeightedObservedPoint)
  99.      */
  100.     public List<WeightedObservedPoint> toList() {
  101.         // The copy is necessary to ensure thread-safety because of the
  102.         // "clear" method (which otherwise would be able to empty the
  103.         // list of points while it is being used by another thread).
  104.         return new ArrayList<>(observations);
  105.     }

  106.     /**
  107.      * Removes all observations from this container.
  108.      */
  109.     public void clear() {
  110.         observations.clear();
  111.     }
  112. }