You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2013/08/21 04:03:30 UTC
svn commit: r1516059 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/fitting/WeightedObservedPoints.java
test/java/org/apache/commons/math3/fitting/WeightedObservedPointsTest.java
Author: erans
Date: Wed Aug 21 02:03:30 2013
New Revision: 1516059
URL: http://svn.apache.org/r1516059
Log:
MATH-1014
Container for storing observations, to allow separating the curve fitting
algorithms from the data to be fitted.
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/WeightedObservedPoints.java (with props)
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/WeightedObservedPointsTest.java (with props)
Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/WeightedObservedPoints.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/WeightedObservedPoints.java?rev=1516059&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/WeightedObservedPoints.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/WeightedObservedPoints.java Wed Aug 21 02:03:30 2013
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.fitting;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.io.Serializable;
+
+/**
+ * Simple container for weighted observed points used
+ * in {@link AbstractCurveFitter curve fitting} algorithms.
+ *
+ * @version $Id$
+ * @since 3.3
+ */
+public class WeightedObservedPoints implements Serializable {
+ /** Serializable version id. */
+ private static final long serialVersionUID = 20130813L;
+
+ /** Observed points. */
+ private final List<WeightedObservedPoint> observations
+ = new ArrayList<WeightedObservedPoint>();
+
+ /**
+ * Adds a point to the sample.
+ * Calling this method is equivalent to calling
+ * {@code add(1.0, x, y)}.
+ *
+ * @param x Abscissa of the point.
+ * @param y Observed value at {@code x}. After fitting we should
+ * have {@code f(x)} as close as possible to this value.
+ *
+ * @see #add(double, double, double)
+ * @see #add(WeightedObservedPoint)
+ * @see #getObservations()
+ */
+ public void add(double x, double y) {
+ add(1d, x, y);
+ }
+
+ /**
+ * Adds a point to the sample.
+ *
+ * @param weight Weight of the observed point.
+ * @param x Abscissa of the point.
+ * @param y Observed value at {@code x}. After fitting we should
+ * have {@code f(x)} as close as possible to this value.
+ *
+ * @see #add(double, double)
+ * @see #add(WeightedObservedPoint)
+ * @see #getObservations()
+ */
+ public void add(double weight, double x, double y) {
+ observations.add(new WeightedObservedPoint(weight, x, y));
+ }
+
+ /**
+ * Adds a point to the sample.
+ *
+ * @param observed Observed point to add.
+ *
+ * @see #add(double, double)
+ * @see #add(double, double, double)
+ * @see #getObservations()
+ */
+ public void add(WeightedObservedPoint observed) {
+ observations.add(observed);
+ }
+
+ /**
+ * Gets a <em>snapshot</em> of the observed points.
+ * The list of stored points is copied in order to ensure that
+ * modification of the returned instance does not affect this
+ * container.
+ * Conversely, further modification of this container (through
+ * the {@code add} or {@code clear} methods) will not affect the
+ * returned list.
+ *
+ * @return the observed points, in the order they were added to this
+ * container.
+ *
+ * @see #add(double, double)
+ * @see #add(double, double, double)
+ * @see #add(WeightedObservedPoint)
+ */
+ public List<WeightedObservedPoint> toList() {
+ // The copy is necessary to ensure thread-safety because of the
+ // "clear" method (which otherwise would be able to empty the
+ // list of points while it is being used by another thread).
+ return new ArrayList<WeightedObservedPoint>(observations);
+ }
+
+ /**
+ * Removes all observations from this container.
+ */
+ public void clear() {
+ observations.clear();
+ }
+}
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/WeightedObservedPoints.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/WeightedObservedPoints.java
------------------------------------------------------------------------------
svn:keywords = Id Revision
Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/WeightedObservedPointsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/WeightedObservedPointsTest.java?rev=1516059&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/WeightedObservedPointsTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/WeightedObservedPointsTest.java Wed Aug 21 02:03:30 2013
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.fitting;
+
+import java.util.List;
+import org.junit.Assert;
+import org.junit.Test;
+import org.apache.commons.math3.util.Precision;
+
+/**
+ * Tests {@link WeightedObservedPoints}.
+ *
+ * @version $Id$
+ */
+public class WeightedObservedPointsTest {
+ @Test
+ public void testAdd1() {
+ final WeightedObservedPoints store = new WeightedObservedPoints();
+
+ final double x = 1.2;
+ final double y = 34.56;
+ final double w = 0.789;
+
+ store.add(w, x, y);
+
+ Assert.assertTrue(lastElementIsSame(store, new WeightedObservedPoint(w, x, y)));
+ }
+
+ @Test
+ public void testAdd2() {
+ final WeightedObservedPoints store = new WeightedObservedPoints();
+
+ final double x = 1.2;
+ final double y = 34.56;
+ final double w = 0.789;
+
+ store.add(new WeightedObservedPoint(w, x, y));
+
+ Assert.assertTrue(lastElementIsSame(store, new WeightedObservedPoint(w, x, y)));
+ }
+
+ @Test
+ public void testAdd3() {
+ final WeightedObservedPoints store = new WeightedObservedPoints();
+
+ final double x = 1.2;
+ final double y = 34.56;
+
+ store.add(x, y);
+
+ Assert.assertTrue(lastElementIsSame(store, new WeightedObservedPoint(1, x, y)));
+ }
+
+ @Test
+ public void testClear() {
+ final WeightedObservedPoints store = new WeightedObservedPoints();
+
+ store.add(new WeightedObservedPoint(1, 2, 3));
+ store.add(new WeightedObservedPoint(2, -1, -2));
+ Assert.assertTrue(store.toList().size() == 2);
+
+ store.clear();
+ Assert.assertTrue(store.toList().size() == 0);
+ }
+
+ // Ensure that an instance returned by "toList()" is independent from
+ // the original container.
+ @Test
+ public void testToListCopy() {
+ final WeightedObservedPoints store = new WeightedObservedPoints();
+
+ store.add(new WeightedObservedPoint(1, 2, 3));
+ store.add(new WeightedObservedPoint(2, -3, -4));
+
+ final List<WeightedObservedPoint> list = store.toList();
+ Assert.assertTrue(list.size() == 2);
+
+ // Adding an element to "list" has no impact on "store".
+ list.add(new WeightedObservedPoint(1.2, 3.4, 5.6));
+ Assert.assertFalse(list.size() == store.toList().size());
+
+ // Clearing "store" has no impact on "list".
+ store.clear();
+ Assert.assertFalse(list.size() == 0);
+ }
+
+ /**
+ * Checks that the contents of the last element is equal to the
+ * contents of {@code p}.
+ *
+ * @param store Container.
+ * @param point Observation.
+ * @return {@code true} if both elements have the same contents.
+ */
+ private boolean lastElementIsSame(WeightedObservedPoints store,
+ WeightedObservedPoint point) {
+ final List<WeightedObservedPoint> list = store.toList();
+ final WeightedObservedPoint lastPoint = list.get(list.size() - 1);
+
+ if (!Precision.equals(lastPoint.getX(), point.getX())) {
+ return false;
+ }
+ if (!Precision.equals(lastPoint.getY(), point.getY())) {
+ return false;
+ }
+ if (!Precision.equals(lastPoint.getWeight(), point.getWeight())) {
+ return false;
+ }
+
+ return true;
+ }
+}
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/WeightedObservedPointsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/WeightedObservedPointsTest.java
------------------------------------------------------------------------------
svn:keywords = Id Revision