BinaryFunction.java revision dee0849a9704d532af0b550146cbafbaa6ee1d19
1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/* 2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more 3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements. See the NOTICE file distributed with 4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership. 5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0 6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with 7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License. You may obtain a copy of the License at 8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * http://www.apache.org/licenses/LICENSE-2.0 10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software 12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS, 13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and 15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License. 16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.analysis; 19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.FunctionEvaluationException; 21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.FastMath; 22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/** 26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Base class for {@link BivariateRealFunction} that can be composed with other functions. 27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 2.1 29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1073498 $ $Date: 2011-02-22 21:57:26 +0100 (mar. 22 févr. 2011) $ 30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated in 2.2 31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond@Deprecated 33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic abstract class BinaryFunction implements BivariateRealFunction { 34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** The + operator method wrapped as a {@link BinaryFunction}. */ 36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static final BinaryFunction ADD = new BinaryFunction() { 37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double value(double x, double y) { 40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return x + y; 41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond }; 43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** The - operator method wrapped as a {@link BinaryFunction}. */ 45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static final BinaryFunction SUBTRACT = new BinaryFunction() { 46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double value(double x, double y) { 49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return x - y; 50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond }; 52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** The * operator method wrapped as a {@link BinaryFunction}. */ 54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static final BinaryFunction MULTIPLY = new BinaryFunction() { 55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double value(double x, double y) { 58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return x * y; 59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond }; 61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** The / operator method wrapped as a {@link BinaryFunction}. */ 63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static final BinaryFunction DIVIDE = new BinaryFunction() { 64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double value(double x, double y) { 67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return x / y; 68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond }; 70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** The {@code FastMath.pow} method wrapped as a {@link BinaryFunction}. */ 72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static final BinaryFunction POW = new BinaryFunction() { 73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double value(double x, double y) { 76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return FastMath.pow(x, y); 77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond }; 79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** The {@code FastMath.atan2} method wrapped as a {@link BinaryFunction}. */ 81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public static final BinaryFunction ATAN2 = new BinaryFunction() { 82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double value(double x, double y) { 85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return FastMath.atan2(x, y); 86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond }; 88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public abstract double value(double x, double y) throws FunctionEvaluationException; 91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Get a composable function by fixing the first argument of the instance. 93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param fixedX fixed value of the first argument 94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return a function such that {@code f.value(y) == value(fixedX, y)} 95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public ComposableFunction fix1stArgument(final double fixedX) { 97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new ComposableFunction() { 98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double value(double x) throws FunctionEvaluationException { 101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return BinaryFunction.this.value(fixedX, x); 102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond }; 104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Get a composable function by fixing the second argument of the instance. 107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param fixedY fixed value of the second argument 108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return a function such that {@code f.value(x) == value(x, fixedY)} 109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public ComposableFunction fix2ndArgument(final double fixedY) { 111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new ComposableFunction() { 112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond @Override 113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** {@inheritDoc} */ 114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public double value(double x) throws FunctionEvaluationException { 115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return BinaryFunction.this.value(x, fixedY); 116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond }; 118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond} 121