[ 
https://issues.apache.org/jira/browse/NUMBERS-167?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17426761#comment-17426761
 ] 

Alex Herbert commented on NUMBERS-167:
--------------------------------------

{quote}call either P or Q
{quote}
I think we need a functional interface that is not in java.util.function. The 
alternative is to use ToDoubleBiFunction<RG, Double> which would require boxing 
the argument of interest when calling this.
{code:java}
public final class RegularizedGamma {
    // ...

    @FunctionalInterface
    public interface ObjDoubleBiFunction<T> {
        double applyAsDouble(T t, double value);
    }

    public enum RG {
        P,
        Q
    }

    public static ObjDoubleBiFunction<RG> withArgumentA(double a) {
        final ArgumentA argA = ArgumentA.of(a);
        return (rg, x) -> {
            switch (rg) {
            case P:
                return P.value(argA, x, DEFAULT_EPSILON, DEFAULT_ITERATIONS);
            case Q:
                return Q.value(argA, x, DEFAULT_EPSILON, DEFAULT_ITERATIONS);
            default:
                throw new IllegalStateException(); // Should never happen.      
                                                                                
            }
        };
    }

    public static void call() {
        double a = 0.5;
        double x = 33;
        ObjDoubleBiFunction<RG> f = RegularizedGamma.withArgumentA(a);
        double v = f.applyAsDouble(RG.P, x);
    }
 
    // ...
{code}
If we are to define a custom interface then I would suggest this:
{code:java}
    public interface ComplementFunction {
        double value(double x);
        double complement(double x);
    }

    public static ComplementFunction withArgumentA(double a) {
        return new ComplementFunction() {
            final ArgumentA argA = ArgumentA.of(a);

            @Override
            public double value(double x) {
                return P.value(argA, x, DEFAULT_EPSILON, DEFAULT_ITERATIONS);
            }

            @Override
            public double complement(double x) {
                return Q.value(argA, x, DEFAULT_EPSILON, DEFAULT_ITERATIONS);
            }
        };
    }

    public static void call() {
        double a = 0.5;
        double x = 33;
        ComplementFunction f = RegularizedGamma.withArgumentA(a);
        double v = f.value(x);
        double c = f.complement(x);
    }
{code}

> RegularizedGamma.P with precomputed LogGamma value
> --------------------------------------------------
>
>                 Key: NUMBERS-167
>                 URL: https://issues.apache.org/jira/browse/NUMBERS-167
>             Project: Commons Numbers
>          Issue Type: Wish
>          Components: gamma
>            Reporter: Gilles Sadowski
>            Priority: Minor
>             Fix For: 1.1
>
>          Time Spent: 0.5h
>  Remaining Estimate: 0h
>
> We have
> {code:java}
> double v = RegularizedGamma.P.value(a, x);
> {code}
> where method {{value}} internally calls {{LogGamma.value(a)}}.
> There is a use-case for
> {code:java}
> double logGammaA = LogGamma.value(a);
> double v = RegularizedGamma.P.value(a, x, logGammaA);
> {code}
> for when the user varies {{x}} but not {{a}}.
> Method name TBD:  Another overload of {{value}} may be confusing (?).



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to