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 2015/08/21 14:44:47 UTC
[math] MATH-1243
Repository: commons-math
Updated Branches:
refs/heads/master 3aa34dbc9 -> 0b5bd38e8
MATH-1243
Removed deprecated code.
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/0b5bd38e
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/0b5bd38e
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/0b5bd38e
Branch: refs/heads/master
Commit: 0b5bd38e8658b4836ab41564ddb59febb423ecf6
Parents: 3aa34db
Author: Gilles <er...@apache.org>
Authored: Fri Aug 21 14:42:13 2015 +0200
Committer: Gilles <er...@apache.org>
Committed: Fri Aug 21 14:42:13 2015 +0200
----------------------------------------------------------------------
.../MicrosphereInterpolatingFunction.java | 251 -------------------
.../interpolation/MicrosphereInterpolator.java | 103 --------
.../MicrosphereInterpolatorTest.java | 133 ----------
3 files changed, 487 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/0b5bd38e/src/main/java/org/apache/commons/math4/analysis/interpolation/MicrosphereInterpolatingFunction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/analysis/interpolation/MicrosphereInterpolatingFunction.java b/src/main/java/org/apache/commons/math4/analysis/interpolation/MicrosphereInterpolatingFunction.java
deleted file mode 100644
index ff468a2..0000000
--- a/src/main/java/org/apache/commons/math4/analysis/interpolation/MicrosphereInterpolatingFunction.java
+++ /dev/null
@@ -1,251 +0,0 @@
-/*
- * 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.math4.analysis.interpolation;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.commons.math4.analysis.MultivariateFunction;
-import org.apache.commons.math4.exception.DimensionMismatchException;
-import org.apache.commons.math4.exception.NoDataException;
-import org.apache.commons.math4.exception.NullArgumentException;
-import org.apache.commons.math4.linear.ArrayRealVector;
-import org.apache.commons.math4.linear.RealVector;
-import org.apache.commons.math4.random.UnitSphereRandomVectorGenerator;
-import org.apache.commons.math4.util.FastMath;
-
-/**
- * Interpolating function that implements the
- * <a href="http://www.dudziak.com/microsphere.php">Microsphere Projection</a>.
- *
- */
-public class MicrosphereInterpolatingFunction
- implements MultivariateFunction {
- /**
- * Space dimension.
- */
- private final int dimension;
- /**
- * Internal accounting data for the interpolation algorithm.
- * Each element of the list corresponds to one surface element of
- * the microsphere.
- */
- private final List<MicrosphereSurfaceElement> microsphere;
- /**
- * Exponent used in the power law that computes the weights of the
- * sample data.
- */
- private final double brightnessExponent;
- /**
- * Sample data.
- */
- private final Map<RealVector, Double> samples;
-
- /**
- * Class for storing the accounting data needed to perform the
- * microsphere projection.
- */
- private static class MicrosphereSurfaceElement {
- /** Normal vector characterizing a surface element. */
- private final RealVector normal;
- /** Illumination received from the brightest sample. */
- private double brightestIllumination;
- /** Brightest sample. */
- private Map.Entry<RealVector, Double> brightestSample;
-
- /**
- * @param n Normal vector characterizing a surface element
- * of the microsphere.
- */
- MicrosphereSurfaceElement(double[] n) {
- normal = new ArrayRealVector(n);
- }
-
- /**
- * Return the normal vector.
- * @return the normal vector
- */
- RealVector normal() {
- return normal;
- }
-
- /**
- * Reset "illumination" and "sampleIndex".
- */
- void reset() {
- brightestIllumination = 0;
- brightestSample = null;
- }
-
- /**
- * Store the illumination and index of the brightest sample.
- * @param illuminationFromSample illumination received from sample
- * @param sample current sample illuminating the element
- */
- void store(final double illuminationFromSample,
- final Map.Entry<RealVector, Double> sample) {
- if (illuminationFromSample > this.brightestIllumination) {
- this.brightestIllumination = illuminationFromSample;
- this.brightestSample = sample;
- }
- }
-
- /**
- * Get the illumination of the element.
- * @return the illumination.
- */
- double illumination() {
- return brightestIllumination;
- }
-
- /**
- * Get the sample illuminating the element the most.
- * @return the sample.
- */
- Map.Entry<RealVector, Double> sample() {
- return brightestSample;
- }
- }
-
- /**
- * @param xval Arguments for the interpolation points.
- * {@code xval[i][0]} is the first component of interpolation point
- * {@code i}, {@code xval[i][1]} is the second component, and so on
- * until {@code xval[i][d-1]}, the last component of that interpolation
- * point (where {@code dimension} is thus the dimension of the sampled
- * space).
- * @param yval Values for the interpolation points.
- * @param brightnessExponent Brightness dimming factor.
- * @param microsphereElements Number of surface elements of the
- * microsphere.
- * @param rand Unit vector generator for creating the microsphere.
- * @throws DimensionMismatchException if the lengths of {@code yval} and
- * {@code xval} (equal to {@code n}, the number of interpolation points)
- * do not match, or the the arrays {@code xval[0]} ... {@code xval[n]},
- * have lengths different from {@code dimension}.
- * @throws NoDataException if there an array has zero-length.
- * @throws NullArgumentException if an argument is {@code null}.
- */
- public MicrosphereInterpolatingFunction(double[][] xval,
- double[] yval,
- double brightnessExponent,
- int microsphereElements,
- UnitSphereRandomVectorGenerator rand)
- throws DimensionMismatchException,
- NoDataException,
- NullArgumentException {
- if (xval == null ||
- yval == null) {
- throw new NullArgumentException();
- }
- if (xval.length == 0) {
- throw new NoDataException();
- }
- if (xval.length != yval.length) {
- throw new DimensionMismatchException(xval.length, yval.length);
- }
- if (xval[0] == null) {
- throw new NullArgumentException();
- }
-
- dimension = xval[0].length;
- this.brightnessExponent = brightnessExponent;
-
- // Copy data samples.
- samples = new HashMap<RealVector, Double>(yval.length);
- for (int i = 0; i < xval.length; ++i) {
- final double[] xvalI = xval[i];
- if (xvalI == null) {
- throw new NullArgumentException();
- }
- if (xvalI.length != dimension) {
- throw new DimensionMismatchException(xvalI.length, dimension);
- }
-
- samples.put(new ArrayRealVector(xvalI), yval[i]);
- }
-
- microsphere = new ArrayList<MicrosphereSurfaceElement>(microsphereElements);
- // Generate the microsphere, assuming that a fairly large number of
- // randomly generated normals will represent a sphere.
- for (int i = 0; i < microsphereElements; i++) {
- microsphere.add(new MicrosphereSurfaceElement(rand.nextVector()));
- }
- }
-
- /**
- * @param point Interpolation point.
- * @return the interpolated value.
- * @throws DimensionMismatchException if point dimension does not math sample
- */
- @Override
- public double value(double[] point) throws DimensionMismatchException {
- final RealVector p = new ArrayRealVector(point);
-
- // Reset.
- for (MicrosphereSurfaceElement md : microsphere) {
- md.reset();
- }
-
- // Compute contribution of each sample points to the microsphere elements illumination
- for (Map.Entry<RealVector, Double> sd : samples.entrySet()) {
-
- // Vector between interpolation point and current sample point.
- final RealVector diff = sd.getKey().subtract(p);
- final double diffNorm = diff.getNorm();
-
- if (FastMath.abs(diffNorm) < FastMath.ulp(1d)) {
- // No need to interpolate, as the interpolation point is
- // actually (very close to) one of the sampled points.
- return sd.getValue();
- }
-
- for (MicrosphereSurfaceElement md : microsphere) {
- final double w = FastMath.pow(diffNorm, -brightnessExponent);
- md.store(cosAngle(diff, md.normal()) * w, sd);
- }
-
- }
-
- // Interpolation calculation.
- double value = 0;
- double totalWeight = 0;
- for (MicrosphereSurfaceElement md : microsphere) {
- final double iV = md.illumination();
- final Map.Entry<RealVector, Double> sd = md.sample();
- if (sd != null) {
- value += iV * sd.getValue();
- totalWeight += iV;
- }
- }
-
- return value / totalWeight;
- }
-
- /**
- * Compute the cosine of the angle between 2 vectors.
- *
- * @param v Vector.
- * @param w Vector.
- * @return the cosine of the angle between {@code v} and {@code w}.
- */
- private double cosAngle(final RealVector v, final RealVector w) {
- return v.dotProduct(w) / (v.getNorm() * w.getNorm());
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/0b5bd38e/src/main/java/org/apache/commons/math4/analysis/interpolation/MicrosphereInterpolator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/analysis/interpolation/MicrosphereInterpolator.java b/src/main/java/org/apache/commons/math4/analysis/interpolation/MicrosphereInterpolator.java
deleted file mode 100644
index 9ff4e84..0000000
--- a/src/main/java/org/apache/commons/math4/analysis/interpolation/MicrosphereInterpolator.java
+++ /dev/null
@@ -1,103 +0,0 @@
-/*
- * 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.math4.analysis.interpolation;
-
-import org.apache.commons.math4.analysis.MultivariateFunction;
-import org.apache.commons.math4.exception.DimensionMismatchException;
-import org.apache.commons.math4.exception.NoDataException;
-import org.apache.commons.math4.exception.NotPositiveException;
-import org.apache.commons.math4.exception.NotStrictlyPositiveException;
-import org.apache.commons.math4.exception.NullArgumentException;
-import org.apache.commons.math4.random.UnitSphereRandomVectorGenerator;
-
-/**
- * Interpolator that implements the algorithm described in
- * <em>William Dudziak</em>'s
- * <a href="http://www.dudziak.com/microsphere.pdf">MS thesis</a>.
- *
- * @since 2.1
- */
-public class MicrosphereInterpolator
- implements MultivariateInterpolator {
- /**
- * Default number of surface elements that composes the microsphere.
- */
- public static final int DEFAULT_MICROSPHERE_ELEMENTS = 2000;
- /**
- * Default exponent used the weights calculation.
- */
- public static final double DEFAULT_BRIGHTNESS_EXPONENT = 2d;
- /**
- * Number of surface elements of the microsphere.
- */
- private final int microsphereElements;
- /**
- * Exponent used in the power law that computes the weights of the
- * sample data.
- */
- private final double brightnessExponent;
-
- /**
- * Create a microsphere interpolator with default settings.
- * Calling this constructor is equivalent to call {@link
- * #MicrosphereInterpolator(int, int)
- * MicrosphereInterpolator(MicrosphereInterpolator.DEFAULT_MICROSPHERE_ELEMENTS,
- * MicrosphereInterpolator.DEFAULT_BRIGHTNESS_EXPONENT)}.
- */
- public MicrosphereInterpolator() {
- this(DEFAULT_MICROSPHERE_ELEMENTS, DEFAULT_BRIGHTNESS_EXPONENT);
- }
-
- /** Create a microsphere interpolator.
- * @param elements Number of surface elements of the microsphere.
- * @param exponent Exponent used in the power law that computes the
- * weights (distance dimming factor) of the sample data.
- * @throws NotPositiveException if {@code exponent < 0}.
- * @throws NotStrictlyPositiveException if {@code elements <= 0}.
- */
- public MicrosphereInterpolator(final int elements,
- final double exponent)
- throws NotPositiveException,
- NotStrictlyPositiveException {
- if (exponent < 0) {
- throw new NotPositiveException(exponent);
- }
- if (elements <= 0) {
- throw new NotStrictlyPositiveException(elements);
- }
-
- microsphereElements = elements;
- brightnessExponent = exponent;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public MultivariateFunction interpolate(final double[][] xval,
- final double[] yval)
- throws DimensionMismatchException,
- NoDataException,
- NullArgumentException {
- final UnitSphereRandomVectorGenerator rand
- = new UnitSphereRandomVectorGenerator(xval[0].length);
- return new MicrosphereInterpolatingFunction(xval, yval,
- brightnessExponent,
- microsphereElements,
- rand);
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/0b5bd38e/src/test/java/org/apache/commons/math4/analysis/interpolation/MicrosphereInterpolatorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/analysis/interpolation/MicrosphereInterpolatorTest.java b/src/test/java/org/apache/commons/math4/analysis/interpolation/MicrosphereInterpolatorTest.java
deleted file mode 100644
index 1fd798d..0000000
--- a/src/test/java/org/apache/commons/math4/analysis/interpolation/MicrosphereInterpolatorTest.java
+++ /dev/null
@@ -1,133 +0,0 @@
-/*
- * 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.math4.analysis.interpolation;
-
-import org.apache.commons.math4.analysis.MultivariateFunction;
-import org.apache.commons.math4.util.FastMath;
-import org.junit.Assert;
-import org.junit.Test;
-
-/**
- * Test case for the "microsphere projection" interpolator.
- *
- */
-public final class MicrosphereInterpolatorTest {
- /**
- * Test of interpolator for a plane.
- * <p>
- * y = 2 x<sub>1</sub> - 3 x<sub>2</sub> + 5
- */
- @Test
- public void testLinearFunction2D() {
- MultivariateFunction f = new MultivariateFunction() {
- @Override
- public double value(double[] x) {
- if (x.length != 2) {
- throw new IllegalArgumentException();
- }
- return 2 * x[0] - 3 * x[1] + 5;
- }
- };
-
- MultivariateInterpolator interpolator = new MicrosphereInterpolator();
-
- // Interpolating points in [-1, 1][-1, 1] by steps of 1.
- final int n = 9;
- final int dim = 2;
- double[][] x = new double[n][dim];
- double[] y = new double[n];
- int index = 0;
- for (int i = -1; i <= 1; i++) {
- for (int j = -1; j <= 1; j++) {
- x[index][0] = i;
- x[index][1] = j;
- y[index] = f.value(x[index]);
- ++index;
- }
- }
-
- MultivariateFunction p = interpolator.interpolate(x, y);
-
- double[] c = new double[dim];
- double expected, result;
-
- c[0] = 0;
- c[1] = 0;
- expected = f.value(c);
- result = p.value(c);
- Assert.assertEquals("On sample point", expected, result, FastMath.ulp(1d));
-
- c[0] = 0 + 1e-5;
- c[1] = 1 - 1e-5;
- expected = f.value(c);
- result = p.value(c);
- Assert.assertEquals("1e-5 away from sample point", expected, result, 1e-4);
- }
-
- /**
- * Test of interpolator for a quadratic function.
- * <p>
- * y = 2 x<sub>1</sub><sup>2</sup> - 3 x<sub>2</sub><sup>2</sup>
- * + 4 x<sub>1</sub> x<sub>2</sub> - 5
- */
- @Test
- public void testParaboloid2D() {
- MultivariateFunction f = new MultivariateFunction() {
- @Override
- public double value(double[] x) {
- if (x.length != 2) {
- throw new IllegalArgumentException();
- }
- return 2 * x[0] * x[0] - 3 * x[1] * x[1] + 4 * x[0] * x[1] - 5;
- }
- };
-
- MultivariateInterpolator interpolator = new MicrosphereInterpolator(567, 0.89);
-
- // Interpolating points in [-10, 10][-10, 10] by steps of 2.
- final int n = 121;
- final int dim = 2;
- double[][] x = new double[n][dim];
- double[] y = new double[n];
- int index = 0;
- for (int i = -10; i <= 10; i += 2) {
- for (int j = -10; j <= 10; j += 2) {
- x[index][0] = i;
- x[index][1] = j;
- y[index] = f.value(x[index]);
- ++index;
- }
- }
-
- MultivariateFunction p = interpolator.interpolate(x, y);
-
- double[] c = new double[dim];
- double expected, result;
-
- c[0] = 0;
- c[1] = 0;
- expected = f.value(c);
- result = p.value(c);
- Assert.assertEquals("On sample point", expected, result, FastMath.ulp(1d));
-
- c[0] = 2 + 1e-5;
- c[1] = 2 - 1e-5;
- expected = f.value(c);
- result = p.value(c);
- Assert.assertEquals("1e-5 away from sample point", expected, result, 1e-3);
- }
-}