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.math3.fitting;
018
019import java.util.List;
020import java.util.ArrayList;
021import java.io.Serializable;
022
023/**
024 * Simple container for weighted observed points used
025 * in {@link AbstractCurveFitter curve fitting} algorithms.
026 *
027 * @version $Id: WeightedObservedPoints.html 885258 2013-11-03 02:46:49Z tn $
028 * @since 3.3
029 */
030public class WeightedObservedPoints implements Serializable {
031    /** Serializable version id. */
032    private static final long serialVersionUID = 20130813L;
033
034    /** Observed points. */
035    private final List<WeightedObservedPoint> observations
036        = new ArrayList<WeightedObservedPoint>();
037
038    /**
039     * Adds a point to the sample.
040     * Calling this method is equivalent to calling
041     * {@code add(1.0, x, y)}.
042     *
043     * @param x Abscissa of the point.
044     * @param y Observed value  at {@code x}. After fitting we should
045     * have {@code f(x)} as close as possible to this value.
046     *
047     * @see #add(double, double, double)
048     * @see #add(WeightedObservedPoint)
049     * @see #toList()
050     */
051    public void add(double x, double y) {
052        add(1d, x, y);
053    }
054
055    /**
056     * Adds a point to the sample.
057     *
058     * @param weight Weight of the observed point.
059     * @param x Abscissa of the point.
060     * @param y Observed value  at {@code x}. After fitting we should
061     * have {@code f(x)} as close as possible to this value.
062     *
063     * @see #add(double, double)
064     * @see #add(WeightedObservedPoint)
065     * @see #toList()
066     */
067    public void add(double weight, double x, double y) {
068        observations.add(new WeightedObservedPoint(weight, x, y));
069    }
070
071    /**
072     * Adds a point to the sample.
073     *
074     * @param observed Observed point to add.
075     *
076     * @see #add(double, double)
077     * @see #add(double, double, double)
078     * @see #toList()
079     */
080    public void add(WeightedObservedPoint observed) {
081        observations.add(observed);
082    }
083
084    /**
085     * Gets a <em>snapshot</em> of the observed points.
086     * The list of stored points is copied in order to ensure that
087     * modification of the returned instance does not affect this
088     * container.
089     * Conversely, further modification of this container (through
090     * the {@code add} or {@code clear} methods) will not affect the
091     * returned list.
092     *
093     * @return the observed points, in the order they were added to this
094     * container.
095     *
096     * @see #add(double, double)
097     * @see #add(double, double, double)
098     * @see #add(WeightedObservedPoint)
099     */
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<WeightedObservedPoint>(observations);
105    }
106
107    /**
108     * Removes all observations from this container.
109     */
110    public void clear() {
111        observations.clear();
112    }
113}