Raymond | dee0849 | 2015-04-02 10:43:13 -0700 | [diff] [blame] | 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.analysis; |
| 19 | |
| 20 | import org.apache.commons.math.FunctionEvaluationException; |
| 21 | import org.apache.commons.math.util.FastMath; |
| 22 | |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * Base class for {@link BivariateRealFunction} that can be composed with other functions. |
| 27 | * |
| 28 | * @since 2.1 |
| 29 | * @version $Revision: 1073498 $ $Date: 2011-02-22 21:57:26 +0100 (mar. 22 févr. 2011) $ |
| 30 | * @deprecated in 2.2 |
| 31 | */ |
| 32 | @Deprecated |
| 33 | public abstract class BinaryFunction implements BivariateRealFunction { |
| 34 | |
| 35 | /** The + operator method wrapped as a {@link BinaryFunction}. */ |
| 36 | public static final BinaryFunction ADD = new BinaryFunction() { |
| 37 | /** {@inheritDoc} */ |
| 38 | @Override |
| 39 | public double value(double x, double y) { |
| 40 | return x + y; |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | /** The - operator method wrapped as a {@link BinaryFunction}. */ |
| 45 | public static final BinaryFunction SUBTRACT = new BinaryFunction() { |
| 46 | /** {@inheritDoc} */ |
| 47 | @Override |
| 48 | public double value(double x, double y) { |
| 49 | return x - y; |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | /** The * operator method wrapped as a {@link BinaryFunction}. */ |
| 54 | public static final BinaryFunction MULTIPLY = new BinaryFunction() { |
| 55 | /** {@inheritDoc} */ |
| 56 | @Override |
| 57 | public double value(double x, double y) { |
| 58 | return x * y; |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | /** The / operator method wrapped as a {@link BinaryFunction}. */ |
| 63 | public static final BinaryFunction DIVIDE = new BinaryFunction() { |
| 64 | /** {@inheritDoc} */ |
| 65 | @Override |
| 66 | public double value(double x, double y) { |
| 67 | return x / y; |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | /** The {@code FastMath.pow} method wrapped as a {@link BinaryFunction}. */ |
| 72 | public static final BinaryFunction POW = new BinaryFunction() { |
| 73 | /** {@inheritDoc} */ |
| 74 | @Override |
| 75 | public double value(double x, double y) { |
| 76 | return FastMath.pow(x, y); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | /** The {@code FastMath.atan2} method wrapped as a {@link BinaryFunction}. */ |
| 81 | public static final BinaryFunction ATAN2 = new BinaryFunction() { |
| 82 | /** {@inheritDoc} */ |
| 83 | @Override |
| 84 | public double value(double x, double y) { |
| 85 | return FastMath.atan2(x, y); |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | /** {@inheritDoc} */ |
| 90 | public abstract double value(double x, double y) throws FunctionEvaluationException; |
| 91 | |
| 92 | /** Get a composable function by fixing the first argument of the instance. |
| 93 | * @param fixedX fixed value of the first argument |
| 94 | * @return a function such that {@code f.value(y) == value(fixedX, y)} |
| 95 | */ |
| 96 | public ComposableFunction fix1stArgument(final double fixedX) { |
| 97 | return new ComposableFunction() { |
| 98 | @Override |
| 99 | /** {@inheritDoc} */ |
| 100 | public double value(double x) throws FunctionEvaluationException { |
| 101 | return BinaryFunction.this.value(fixedX, x); |
| 102 | } |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | /** Get a composable function by fixing the second argument of the instance. |
| 107 | * @param fixedY fixed value of the second argument |
| 108 | * @return a function such that {@code f.value(x) == value(x, fixedY)} |
| 109 | */ |
| 110 | public ComposableFunction fix2ndArgument(final double fixedY) { |
| 111 | return new ComposableFunction() { |
| 112 | @Override |
| 113 | /** {@inheritDoc} */ |
| 114 | public double value(double x) throws FunctionEvaluationException { |
| 115 | return BinaryFunction.this.value(x, fixedY); |
| 116 | } |
| 117 | }; |
| 118 | } |
| 119 | |
| 120 | } |