All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ojalgo.function.BigFunction Maven / Gradle / Ivy

Go to download

oj! Algorithms - ojAlgo - is Open Source Java code that has to do with mathematics, linear algebra and optimisation.

There is a newer version: 55.0.1
Show newest version
/*
 * Copyright 1997-2024 Optimatika
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.ojalgo.function;

import java.math.BigDecimal;

import org.ojalgo.function.aggregator.AggregatorSet;
import org.ojalgo.function.aggregator.BigAggregator;
import org.ojalgo.function.constant.BigMath;
import org.ojalgo.type.context.NumberContext;

/**
 * Only the reference type parameter (BigDecimal) methods are actually implemented. The methods with the
 * primitive parameters (double) should create a BigDecimal and then delegate to the primitive methods (and do
 * nothing else). If possible the implementations should be pure BigDecimal arithmatic without rounding. If
 * rounding is necessary MathContext.DECIMAL128 should be used. If BigDecimal arithmatic is not possible at
 * all the implementation should delegate to PrimitiveFunction.
 *
 * @author apete
 */
public final class BigFunction extends FunctionSet {

    @FunctionalInterface
    public interface Binary extends BinaryFunction {

        default BigDecimal invoke(final BigDecimal arg1, final double arg2) {
            return this.invoke(arg1, BigDecimal.valueOf(arg2));
        }

        default double invoke(final double arg1, final double arg2) {
            return this.invoke(BigDecimal.valueOf(arg1), BigDecimal.valueOf(arg2)).doubleValue();
        }

        default float invoke(final float arg1, final float arg2) {
            return this.invoke(BigDecimal.valueOf(arg1), BigDecimal.valueOf(arg2)).floatValue();
        }

    }

    @FunctionalInterface
    public interface Consumer extends VoidFunction {

        default void invoke(final double arg) {
            this.invoke(BigDecimal.valueOf(arg));
        }

        default void invoke(final float arg) {
            this.invoke(BigDecimal.valueOf(arg));
        }

    }

    @FunctionalInterface
    public interface Nullary extends NullaryFunction {

        default double doubleValue() {
            return this.invoke().doubleValue();
        }

    }

    @FunctionalInterface
    public interface Parameter extends ParameterFunction {

        default double invoke(final double arg, final int param) {
            return this.invoke(BigDecimal.valueOf(arg), param).doubleValue();
        }

        default float invoke(final float arg, final int param) {
            return this.invoke(BigDecimal.valueOf(arg), param).floatValue();
        }

    }

    @FunctionalInterface
    public interface Predicate extends PredicateFunction {

        default boolean invoke(final double arg) {
            return this.invoke(BigDecimal.valueOf(arg));
        }

        default boolean invoke(final float arg) {
            return this.invoke(BigDecimal.valueOf(arg));
        }

    }

    @FunctionalInterface
    public interface Unary extends UnaryFunction {

        default double invoke(final double arg) {
            return this.invoke(BigDecimal.valueOf(arg)).doubleValue();
        }

        default float invoke(final float arg) {
            return this.invoke(BigDecimal.valueOf(arg)).floatValue();
        }

    }

    private static final BigFunction SET = new BigFunction();

    public static BigFunction getSet() {
        return SET;
    }

    public static Nullary nullary(final BigDecimal value) {
        return () -> value;
    }

    private BigFunction() {
        super();
    }

    @Override
    public UnaryFunction abs() {
        return BigMath.ABS;
    }

    @Override
    public UnaryFunction acos() {
        return BigMath.ACOS;
    }

    @Override
    public UnaryFunction acosh() {
        return BigMath.ACOSH;
    }

    @Override
    public BinaryFunction add() {
        return BigMath.ADD;
    }

    @Override
    public AggregatorSet aggregator() {
        return BigAggregator.getSet();
    }

    @Override
    public UnaryFunction asin() {
        return BigMath.ASIN;
    }

    @Override
    public UnaryFunction asinh() {
        return BigMath.ASINH;
    }

    @Override
    public UnaryFunction atan() {
        return BigMath.ATAN;
    }

    @Override
    public BinaryFunction atan2() {
        return BigMath.ATAN2;
    }

    @Override
    public UnaryFunction atanh() {
        return BigMath.ATANH;
    }

    @Override
    public UnaryFunction cardinality() {
        return BigMath.CARDINALITY;
    }

    @Override
    public UnaryFunction cbrt() {
        return BigMath.CBRT;
    }

    @Override
    public UnaryFunction ceil() {
        return BigMath.CEIL;
    }

    @Override
    public UnaryFunction conjugate() {
        return BigMath.CONJUGATE;
    }

    @Override
    public UnaryFunction cos() {
        return BigMath.COS;
    }

    @Override
    public UnaryFunction cosh() {
        return BigMath.COSH;
    }

    @Override
    public BinaryFunction divide() {
        return BigMath.DIVIDE;
    }

    @Override
    public Unary enforce(final NumberContext context) {
        return arg -> context.enforce(arg);
    }

    @Override
    public UnaryFunction exp() {
        return BigMath.EXP;
    }

    @Override
    public UnaryFunction expm1() {
        return BigMath.EXPM1;
    }

    @Override
    public UnaryFunction floor() {
        return BigMath.FLOOR;
    }

    @Override
    public BinaryFunction hypot() {
        return BigMath.HYPOT;
    }

    @Override
    public UnaryFunction invert() {
        return BigMath.INVERT;
    }

    @Override
    public UnaryFunction log() {
        return BigMath.LOG;
    }

    @Override
    public UnaryFunction log10() {
        return BigMath.LOG10;
    }

    @Override
    public UnaryFunction log1p() {
        return BigMath.LOG1P;
    }

    @Override
    public UnaryFunction logistic() {
        return BigMath.LOGISTIC;
    }

    @Override
    public UnaryFunction logit() {
        return BigMath.LOGIT;
    }

    @Override
    public BinaryFunction max() {
        return BigMath.MAX;
    }

    @Override
    public BinaryFunction min() {
        return BigMath.MIN;
    }

    @Override
    public BinaryFunction multiply() {
        return BigMath.MULTIPLY;
    }

    @Override
    public UnaryFunction negate() {
        return BigMath.NEGATE;
    }

    @Override
    public BinaryFunction pow() {
        return BigMath.POW;
    }

    @Override
    public ParameterFunction power() {
        return BigMath.POWER;
    }

    @Override
    public UnaryFunction rint() {
        return BigMath.RINT;
    }

    @Override
    public ParameterFunction root() {
        return BigMath.ROOT;
    }

    @Override
    public ParameterFunction scale() {
        return BigMath.SCALE;
    }

    @Override
    public UnaryFunction signum() {
        return BigMath.SIGNUM;
    }

    @Override
    public UnaryFunction sin() {
        return BigMath.SIN;
    }

    @Override
    public UnaryFunction sinh() {
        return BigMath.SINH;
    }

    @Override
    public UnaryFunction sqrt() {
        return BigMath.SQRT;
    }

    @Override
    public UnaryFunction sqrt1px2() {
        return BigMath.SQRT1PX2;
    }

    @Override
    public BinaryFunction subtract() {
        return BigMath.SUBTRACT;
    }

    @Override
    public UnaryFunction tan() {
        return BigMath.TAN;
    }

    @Override
    public UnaryFunction tanh() {
        return BigMath.TANH;
    }

    @Override
    public UnaryFunction value() {
        return BigMath.VALUE;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy