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.  *      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. import java.util.List;
  19. import java.util.ArrayList;

  20. /**
  21.  * Simple container for weighted observed points used
  22.  * in {@link AbstractCurveFitter curve fitting} algorithms.
  23.  *
  24.  * @since 3.3
  25.  */
  26. public class WeightedObservedPoints {
  27.     /** Observed points. */
  28.     private final List<WeightedObservedPoint> observations
  29.         = new ArrayList<>();

  30.     /**
  31.      * Adds a point to the sample.
  32.      * Calling this method is equivalent to calling
  33.      * {@code add(1.0, x, y)}.
  34.      *
  35.      * @param x Abscissa of the point.
  36.      * @param y Observed value  at {@code x}. After fitting we should
  37.      * have {@code f(x)} as close as possible to this value.
  38.      *
  39.      * @see #add(double, double, double)
  40.      * @see #add(WeightedObservedPoint)
  41.      * @see #toList()
  42.      */
  43.     public void add(double x, double y) {
  44.         add(1d, x, y);
  45.     }

  46.     /**
  47.      * Adds a point to the sample.
  48.      *
  49.      * @param weight Weight of the observed point.
  50.      * @param x Abscissa of the point.
  51.      * @param y Observed value  at {@code x}. After fitting we should
  52.      * have {@code f(x)} as close as possible to this value.
  53.      *
  54.      * @see #add(double, double)
  55.      * @see #add(WeightedObservedPoint)
  56.      * @see #toList()
  57.      */
  58.     public void add(double weight, double x, double y) {
  59.         observations.add(new WeightedObservedPoint(weight, x, y));
  60.     }

  61.     /**
  62.      * Adds a point to the sample.
  63.      *
  64.      * @param observed Observed point to add.
  65.      *
  66.      * @see #add(double, double)
  67.      * @see #add(double, double, double)
  68.      * @see #toList()
  69.      */
  70.     public void add(WeightedObservedPoint observed) {
  71.         observations.add(observed);
  72.     }

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

  95.     /**
  96.      * Removes all observations from this container.
  97.      */
  98.     public void clear() {
  99.         observations.clear();
  100.     }
  101. }