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 2011/02/22 16:34:21 UTC
svn commit: r1073363 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math/analysis/function/HarmonicOscillator.java
test/java/org/apache/commons/math/analysis/function/HarmonicOscillatorTest.java
Author: erans
Date: Tue Feb 22 15:34:20 2011
New Revision: 1073363
URL: http://svn.apache.org/viewvc?rev=1073363&view=rev
Log:
MATH-518
Created new function object to replace "HarmonicFunction" (in package
"optimization.fitting").
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/HarmonicOscillator.java (with props)
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/HarmonicOscillatorTest.java (with props)
Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/HarmonicOscillator.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/HarmonicOscillator.java?rev=1073363&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/HarmonicOscillator.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/HarmonicOscillator.java Tue Feb 22 15:34:20 2011
@@ -0,0 +1,155 @@
+/*
+ * 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.math.analysis.function;
+
+import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction;
+import org.apache.commons.math.analysis.ParametricUnivariateRealFunction;
+import org.apache.commons.math.exception.NullArgumentException;
+import org.apache.commons.math.exception.DimensionMismatchException;
+import org.apache.commons.math.util.FastMath;
+
+/**
+ * <a href="http://en.wikipedia.org/wiki/Harmonic_oscillator">
+ * simple harmonic oscillator</a> function.
+ *
+ * @version $Revision$ $Date$
+ * @since 3.0
+ */
+public class HarmonicOscillator implements DifferentiableUnivariateRealFunction {
+ /** Amplitude. */
+ private final double amplitude;
+ /** Angular requency. */
+ private final double omega;
+ /** Phase. */
+ private final double phase;
+
+ /**
+ * Harmonic oscillator function.
+ *
+ * @param amplitude Amplitude.
+ * @param omega Angular frequency.
+ * @param phase Phase.
+ */
+ public HarmonicOscillator(double amplitude,
+ double omega,
+ double phase) {
+ this.amplitude = amplitude;
+ this.omega = omega;
+ this.phase = phase;
+ }
+
+ /** {@inheritDoc} */
+ public double value(double x) {
+ return value(omega * x + phase, amplitude);
+ }
+
+ /** {@inheritDoc} */
+ public UnivariateRealFunction derivative() {
+ return new UnivariateRealFunction() {
+ /** {@inheritDoc} */
+ public double value(double x) {
+ return -amplitude * omega * FastMath.sin(omega * x + phase);
+ }
+ };
+ }
+
+ /**
+ * Parametric function where the input array contains the parameters of
+ * the harmonic oscillator function, ordered as follows:
+ * <ul>
+ * <li>Amplitude</li>
+ * <li>Angular frequency</li>
+ * <li>Phase</li>
+ * </ul>
+ */
+ public static class Parametric implements ParametricUnivariateRealFunction {
+ /**
+ * Computes the value of the harmonic oscillator at {@code x}.
+ *
+ * @param x Value for which the function must be computed.
+ * @param param Values of norm, mean and standard deviation.
+ * @return the value of the function.
+ * @throws NullArgumentException if {@code param} is {@code null}.
+ * @throws DimensionMismatchException if the size of {@code param} is
+ * not 3.
+ */
+ public double value(double x,
+ double[] param) {
+ validateParameters(param);
+ return HarmonicOscillator.value(x * param[1] + param[2], param[0]);
+ }
+
+ /**
+ * Computes the value of the gradient at {@code x}.
+ * The components of the gradient vector are the partial
+ * derivatives of the function with respect to each of the
+ * <em>parameters</em> (amplitude, angular frequency and phase).
+ *
+ * @param x Value at which the gradient must be computed.
+ * @param param Values of amplitude, angular frequency and phase.
+ * @return the gradient vector at {@code x}.
+ * @throws NullArgumentException if {@code param} is {@code null}.
+ * @throws DimensionMismatchException if the size of {@code param} is
+ * not 3.
+ */
+ public double[] gradient(double x, double[] param) {
+ validateParameters(param);
+
+ final double amplitude = param[0];
+ final double omega = param[1];
+ final double phase = param[2];
+
+ final double xTimesOmegaPlusPhase = omega * x + phase;
+ final double a = HarmonicOscillator.value(xTimesOmegaPlusPhase, 1);
+ final double p = -amplitude * FastMath.sin(xTimesOmegaPlusPhase);
+ final double w = p * x;
+
+ return new double[] { a, w, p };
+ }
+
+ /**
+ * Validates parameters to ensure they are appropriate for the evaluation of
+ * the {@link #value(double,double[])} and {@link #gradient(double,double[])}
+ * methods.
+ *
+ * @param param Values of norm, mean and standard deviation.
+ * @throws NullArgumentException if {@code param} is {@code null}.
+ * @throws DimensionMismatchException if the size of {@code param} is
+ * not 3.
+ */
+ private void validateParameters(double[] param) {
+ if (param == null) {
+ throw new NullArgumentException();
+ }
+ if (param.length != 3) {
+ throw new DimensionMismatchException(param.length, 3);
+ }
+ }
+ }
+
+ /**
+ * @param xTimesOmegaPlusPhase {@code omega * x + phase}.
+ * @param amplitude Amplitude.
+ * @return the value of the harmonic oscillator function at {@code x}.
+ */
+ private static double value(double xTimesOmegaPlusPhase,
+ double amplitude) {
+ return amplitude * FastMath.cos(xTimesOmegaPlusPhase);
+ }
+}
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/HarmonicOscillator.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/HarmonicOscillatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/HarmonicOscillatorTest.java?rev=1073363&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/HarmonicOscillatorTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/HarmonicOscillatorTest.java Tue Feb 22 15:34:20 2011
@@ -0,0 +1,118 @@
+/*
+ * 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.math.analysis.function;
+
+import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.exception.NotStrictlyPositiveException;
+import org.apache.commons.math.exception.NullArgumentException;
+import org.apache.commons.math.exception.DimensionMismatchException;
+import org.apache.commons.math.util.FastMath;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test for class {@link HarmonicOscillator}.
+ */
+public class HarmonicOscillatorTest {
+ private final double EPS = Math.ulp(1d);
+
+ @Test
+ public void testSomeValues() {
+ final double a = -1.2;
+ final double w = 0.34;
+ final double p = 5.6;
+ final UnivariateRealFunction f = new HarmonicOscillator(a, w, p);
+
+ final double d = 0.12345;
+ for (int i = 0; i < 10; i++) {
+ final double v = i * d;
+ Assert.assertEquals(a * FastMath.cos(w * v + p), f.value(v), 0);
+ }
+ }
+
+ @Test
+ public void testDerivative() {
+ final double a = -1.2;
+ final double w = 0.34;
+ final double p = 5.6;
+ final HarmonicOscillator f = new HarmonicOscillator(a, w, p);
+ final UnivariateRealFunction dfdx = f.derivative();
+
+ final double d = 0.12345;
+ for (int i = 0; i < 10; i++) {
+ final double v = i * d;
+ Assert.assertEquals(-a * w * FastMath.sin(w * v + p), dfdx.value(v), 0);
+ }
+ }
+
+ @Test(expected=NullArgumentException.class)
+ public void testParametricUsage1() {
+ final HarmonicOscillator.Parametric g = new HarmonicOscillator.Parametric();
+ g.value(0, null);
+ }
+
+ @Test(expected=DimensionMismatchException.class)
+ public void testParametricUsage2() {
+ final HarmonicOscillator.Parametric g = new HarmonicOscillator.Parametric();
+ g.value(0, new double[] {0});
+ }
+
+ @Test(expected=NullArgumentException.class)
+ public void testParametricUsage3() {
+ final HarmonicOscillator.Parametric g = new HarmonicOscillator.Parametric();
+ g.gradient(0, null);
+ }
+
+ @Test(expected=DimensionMismatchException.class)
+ public void testParametricUsage4() {
+ final HarmonicOscillator.Parametric g = new HarmonicOscillator.Parametric();
+ g.gradient(0, new double[] {0});
+ }
+
+ @Test
+ public void testParametricValue() {
+ final double amplitude = 2;
+ final double omega = 3;
+ final double phase = 4;
+ final HarmonicOscillator f = new HarmonicOscillator(amplitude, omega, phase);
+
+ final HarmonicOscillator.Parametric g = new HarmonicOscillator.Parametric();
+ Assert.assertEquals(f.value(-1), g.value(-1, new double[] {amplitude, omega, phase}), 0);
+ Assert.assertEquals(f.value(0), g.value(0, new double[] {amplitude, omega, phase}), 0);
+ Assert.assertEquals(f.value(2), g.value(2, new double[] {amplitude, omega, phase}), 0);
+ }
+
+ @Test
+ public void testParametricGradient() {
+ final double amplitude = 2;
+ final double omega = 3;
+ final double phase = 4;
+ final HarmonicOscillator.Parametric f = new HarmonicOscillator.Parametric();
+
+ final double x = 1;
+ final double[] grad = f.gradient(1, new double[] {amplitude, omega, phase});
+ final double xTimesOmegaPlusPhase = omega * x + phase;
+ final double a = FastMath.cos(xTimesOmegaPlusPhase);
+ Assert.assertEquals(a, grad[0], EPS);
+ final double w = -amplitude * x * FastMath.sin(xTimesOmegaPlusPhase);
+ Assert.assertEquals(w, grad[1], EPS);
+ final double p = -amplitude * FastMath.sin(xTimesOmegaPlusPhase);
+ Assert.assertEquals(p, grad[2], EPS);
+ }
+}
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/HarmonicOscillatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native