    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    import org.apache.commons.math.util.FastMath;
    022
    023
    024
    025    /**
    026     * Base class for {@link BivariateRealFunction} that can be composed with other functions.
    027     *
    028     * @since 2.1
    029     * @version $Revision: 1073498 $ $Date: 2011-02-22 21:57:26 +0100 (mar. 22 f??vr. 2011) $
    030     * @deprecated in 2.2
    031     */
    032    @Deprecated
    033    public abstract class BinaryFunction implements BivariateRealFunction {
    034
    035        /** The + operator method wrapped as a {@link BinaryFunction}. */
    036        public static final BinaryFunction ADD = new BinaryFunction() {
    037            /** {@inheritDoc} */
    038            @Override
    039            public double value(double x, double y) {
    040                return x + y;
    041            }
    042        };
    043
    044        /** The - operator method wrapped as a {@link BinaryFunction}. */
    045        public static final BinaryFunction SUBTRACT = new BinaryFunction() {
    046            /** {@inheritDoc} */
    047            @Override
    048            public double value(double x, double y) {
    049                return x - y;
    050            }
    051        };
    052
    053        /** The * operator method wrapped as a {@link BinaryFunction}. */
    054        public static final BinaryFunction MULTIPLY = new BinaryFunction() {
    055            /** {@inheritDoc} */
    056            @Override
    057            public double value(double x, double y) {
    058                return x * y;
    059            }
    060        };
    061
    062        /** The / operator method wrapped as a {@link BinaryFunction}. */
    063        public static final BinaryFunction DIVIDE = new BinaryFunction() {
    064            /** {@inheritDoc} */
    065            @Override
    066            public double value(double x, double y) {
    067                return x / y;
    068            }
    069        };
    070
    071        /** The {@code FastMath.pow} method wrapped as a {@link BinaryFunction}. */
    072        public static final BinaryFunction POW = new BinaryFunction() {
    073            /** {@inheritDoc} */
    074            @Override
    075            public double value(double x, double y) {
    076                return FastMath.pow(x, y);
    077            }
    078        };
    079
    080        /** The {@code FastMath.atan2} method wrapped as a {@link BinaryFunction}. */
    081        public static final BinaryFunction ATAN2 = new BinaryFunction() {
    082            /** {@inheritDoc} */
    083            @Override
    084            public double value(double x, double y) {
    085                return FastMath.atan2(x, y);
    086            }
    087        };
    088
    089        /** {@inheritDoc} */
    090        public abstract double value(double x, double y) throws FunctionEvaluationException;
    091
    092        /** Get a composable function by fixing the first argument of the instance.
    093         * @param fixedX fixed value of the first argument
    094         * @return a function such that {@code f.value(y) == value(fixedX, y)}
    095         */
    096        public ComposableFunction fix1stArgument(final double fixedX) {
    097            return new ComposableFunction() {
    098                @Override
    099                /** {@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    }