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 * @since 3.3 028 */ 029public class WeightedObservedPoints implements Serializable { 030 /** Serializable version id. */ 031 private static final long serialVersionUID = 20130813L; 032 033 /** Observed points. */ 034 private final List<WeightedObservedPoint> observations 035 = new ArrayList<WeightedObservedPoint>(); 036 037 /** 038 * Adds a point to the sample. 039 * Calling this method is equivalent to calling 040 * {@code add(1.0, x, y)}. 041 * 042 * @param x Abscissa of the point. 043 * @param y Observed value at {@code x}. After fitting we should 044 * have {@code f(x)} as close as possible to this value. 045 * 046 * @see #add(double, double, double) 047 * @see #add(WeightedObservedPoint) 048 * @see #toList() 049 */ 050 public void add(double x, double y) { 051 add(1d, x, y); 052 } 053 054 /** 055 * Adds a point to the sample. 056 * 057 * @param weight Weight of the observed point. 058 * @param x Abscissa of the point. 059 * @param y Observed value at {@code x}. After fitting we should 060 * have {@code f(x)} as close as possible to this value. 061 * 062 * @see #add(double, double) 063 * @see #add(WeightedObservedPoint) 064 * @see #toList() 065 */ 066 public void add(double weight, double x, double y) { 067 observations.add(new WeightedObservedPoint(weight, x, y)); 068 } 069 070 /** 071 * Adds a point to the sample. 072 * 073 * @param observed Observed point to add. 074 * 075 * @see #add(double, double) 076 * @see #add(double, double, double) 077 * @see #toList() 078 */ 079 public void add(WeightedObservedPoint observed) { 080 observations.add(observed); 081 } 082 083 /** 084 * Gets a <em>snapshot</em> of the observed points. 085 * The list of stored points is copied in order to ensure that 086 * modification of the returned instance does not affect this 087 * container. 088 * Conversely, further modification of this container (through 089 * the {@code add} or {@code clear} methods) will not affect the 090 * returned list. 091 * 092 * @return the observed points, in the order they were added to this 093 * container. 094 * 095 * @see #add(double, double) 096 * @see #add(double, double, double) 097 * @see #add(WeightedObservedPoint) 098 */ 099 public List<WeightedObservedPoint> toList() { 100 // The copy is necessary to ensure thread-safety because of the 101 // "clear" method (which otherwise would be able to empty the 102 // list of points while it is being used by another thread). 103 return new ArrayList<WeightedObservedPoint>(observations); 104 } 105 106 /** 107 * Removes all observations from this container. 108 */ 109 public void clear() { 110 observations.clear(); 111 } 112}