001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.math.analysis; 019 020 import org.apache.commons.math.FunctionEvaluationException; 021 022 023 024 /** 025 * Base class for {@link BivariateRealFunction} that can be composed with other functions. 026 * 027 * @since 2.1 028 * @version $Revision: 924453 $ $Date: 2010-03-17 16:05:20 -0400 (Wed, 17 Mar 2010) $ 029 */ 030 public abstract class BinaryFunction implements BivariateRealFunction { 031 032 /** The + operator method wrapped as a {@link BinaryFunction}. */ 033 public static final BinaryFunction ADD = new BinaryFunction() { 034 /** {@inheritDoc} */ 035 @Override 036 public double value(double x, double y) { 037 return x + y; 038 } 039 }; 040 041 /** The - operator method wrapped as a {@link BinaryFunction}. */ 042 public static final BinaryFunction SUBTRACT = new BinaryFunction() { 043 /** {@inheritDoc} */ 044 @Override 045 public double value(double x, double y) { 046 return x - y; 047 } 048 }; 049 050 /** The * operator method wrapped as a {@link BinaryFunction}. */ 051 public static final BinaryFunction MULTIPLY = new BinaryFunction() { 052 /** {@inheritDoc} */ 053 @Override 054 public double value(double x, double y) { 055 return x * y; 056 } 057 }; 058 059 /** The / operator method wrapped as a {@link BinaryFunction}. */ 060 public static final BinaryFunction DIVIDE = new BinaryFunction() { 061 /** {@inheritDoc} */ 062 @Override 063 public double value(double x, double y) { 064 return x / y; 065 } 066 }; 067 068 /** The {@code Math.pow} method wrapped as a {@link BinaryFunction}. */ 069 public static final BinaryFunction POW = new BinaryFunction() { 070 /** {@inheritDoc} */ 071 @Override 072 public double value(double x, double y) { 073 return Math.pow(x, y); 074 } 075 }; 076 077 /** The {@code Math.atan2} method wrapped as a {@link BinaryFunction}. */ 078 public static final BinaryFunction ATAN2 = new BinaryFunction() { 079 /** {@inheritDoc} */ 080 @Override 081 public double value(double x, double y) { 082 return Math.atan2(x, y); 083 } 084 }; 085 086 /** {@inheritDoc} */ 087 public abstract double value(double x, double y) throws FunctionEvaluationException; 088 089 /** Get a composable function by fixing the first argument of the instance. 090 * @param fixedX fixed value of the first argument 091 * @return a function such that {@code f.value(y) == value(fixedX, y)} 092 */ 093 public ComposableFunction fix1stArgument(final double fixedX) { 094 return new ComposableFunction() { 095 @Override 096 /** {@inheritDoc} */ 097 public double value(double x) throws FunctionEvaluationException { 098 return BinaryFunction.this.value(fixedX, x); 099 } 100 }; 101 } 102 103 /** Get a composable function by fixing the second argument of the instance. 104 * @param fixedY fixed value of the second argument 105 * @return a function such that {@code f.value(x) == value(x, fixedY)} 106 */ 107 public ComposableFunction fix2ndArgument(final double fixedY) { 108 return new ComposableFunction() { 109 @Override 110 /** {@inheritDoc} */ 111 public double value(double x) throws FunctionEvaluationException { 112 return BinaryFunction.this.value(x, fixedY); 113 } 114 }; 115 } 116 117 }