001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math4.legacy.fitting;
018
019import java.util.List;
020import java.util.ArrayList;
021
022/**
023 * Simple container for weighted observed points used
024 * in {@link AbstractCurveFitter curve fitting} algorithms.
025 *
026 * @since 3.3
027 */
028public class WeightedObservedPoints {
029    /** Observed points. */
030    private final List<WeightedObservedPoint> observations
031        = new ArrayList<>();
032
033    /**
034     * Adds a point to the sample.
035     * Calling this method is equivalent to calling
036     * {@code add(1.0, x, y)}.
037     *
038     * @param x Abscissa of the point.
039     * @param y Observed value  at {@code x}. After fitting we should
040     * have {@code f(x)} as close as possible to this value.
041     *
042     * @see #add(double, double, double)
043     * @see #add(WeightedObservedPoint)
044     * @see #toList()
045     */
046    public void add(double x, double y) {
047        add(1d, x, y);
048    }
049
050    /**
051     * Adds a point to the sample.
052     *
053     * @param weight Weight of the observed point.
054     * @param x Abscissa of the point.
055     * @param y Observed value  at {@code x}. After fitting we should
056     * have {@code f(x)} as close as possible to this value.
057     *
058     * @see #add(double, double)
059     * @see #add(WeightedObservedPoint)
060     * @see #toList()
061     */
062    public void add(double weight, double x, double y) {
063        observations.add(new WeightedObservedPoint(weight, x, y));
064    }
065
066    /**
067     * Adds a point to the sample.
068     *
069     * @param observed Observed point to add.
070     *
071     * @see #add(double, double)
072     * @see #add(double, double, double)
073     * @see #toList()
074     */
075    public void add(WeightedObservedPoint observed) {
076        observations.add(observed);
077    }
078
079    /**
080     * Gets a <em>snapshot</em> of the observed points.
081     * The list of stored points is copied in order to ensure that
082     * modification of the returned instance does not affect this
083     * container.
084     * Conversely, further modification of this container (through
085     * the {@code add} or {@code clear} methods) will not affect the
086     * returned list.
087     *
088     * @return the observed points, in the order they were added to this
089     * container.
090     *
091     * @see #add(double, double)
092     * @see #add(double, double, double)
093     * @see #add(WeightedObservedPoint)
094     */
095    public List<WeightedObservedPoint> toList() {
096        // The copy is necessary to ensure thread-safety because of the
097        // "clear" method (which otherwise would be able to empty the
098        // list of points while it is being used by another thread).
099        return new ArrayList<>(observations);
100    }
101
102    /**
103     * Removes all observations from this container.
104     */
105    public void clear() {
106        observations.clear();
107    }
108}