将C#Func <>移植到Java- Math Equation

提问

我正在将C#库移植到Java(用于非线性回归,原始代码here).该库使用Func<>.类,在Java中不存在.即(A,B,C和D,以及时间是用于回归的参数,不需要固定.)

Func<double>[] regressionFunctions = new Func<double>[]
{() => A * Math.Exp(time) * Math.Sin(B * time),
() => C * Math.Exp(time) * Math.Cos(D * time)}; 

我想做的就是将其转换为Java代码.我看到了有关创建匿名内部类的内容,但是我不确定这种特殊情况的正确用法.我希望等式可以在以后的时间(对于t的特定值)求值.我需要一个新的类,一个接口,还是最好的方法是什么?
任何帮助,将不胜感激!提前致谢.

最佳答案

您可以定义自己的接口,并使用匿名实现来移植代码:

// Declaring the interface
interface FuncDouble {
    double calculate();
}

public LevenbergMarquardt(FuncDouble[] regressionFunctions, ...) {
    // Using the functor:
    double functionValue = _regressionFunctions[i].calculate()
}

// Declaring an array of functors:
FuncDouble[] regressionFunctions = new FuncDouble[] {
    new FuncDouble() {
        public double calculate() {
            return A * Math.Exp(time) * Math.Sin(B * time);
        }
    }
,   new FuncDouble() {
        public double calculate() {
            return C * Math.Exp(time) * Math.Cos(D * time);
        }
    }
};

为了使实现起作用,A,B,C,D和时间变量必须是实例/类变量,或者是最终的局部变量.