1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.math.optimization.fitting; 19 20 import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; 21 import org.apache.commons.math.util.FastMath; 22 23 /** Harmonic function of the form <code>f (t) = a cos (ω t + φ)</code>. 24 * @version $Revision: 990655 $ $Date: 2010-08-29 23:49:40 +0200 (dim. 29 aot 2010) $ 25 * @since 2.0 26 */ 27 public class HarmonicFunction implements DifferentiableUnivariateRealFunction { 28 29 /** Amplitude a. */ 30 private final double a; 31 32 /** Pulsation ω. */ 33 private final double omega; 34 35 /** Phase φ. */ 36 private final double phi; 37 38 /** Simple constructor. 39 * @param a amplitude 40 * @param omega pulsation 41 * @param phi phase 42 */ 43 public HarmonicFunction(double a, double omega, double phi) { 44 this.a = a; 45 this.omega = omega; 46 this.phi = phi; 47 } 48 49 /** {@inheritDoc} */ 50 public double value(double x) { 51 return a * FastMath.cos(omega * x + phi); 52 } 53 54 /** {@inheritDoc} */ 55 public HarmonicFunction derivative() { 56 return new HarmonicFunction(a * omega, omega, phi + FastMath.PI / 2); 57 } 58 59 /** Get the amplitude a. 60 * @return amplitude a; 61 */ 62 public double getAmplitude() { 63 return a; 64 } 65 66 /** Get the pulsation ω. 67 * @return pulsation ω 68 */ 69 public double getPulsation() { 70 return omega; 71 } 72 73 /** Get the phase φ. 74 * @return phase φ 75 */ 76 public double getPhase() { 77 return phi; 78 } 79 80 } 81