gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:21:59 25 4
gpt4 key购买 nike

我正在将 C# 库移植到 Java(用于非线性回归,原始代码 here)。该库使用 Java 中不存在的 Func<> 类。 IE。 (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 值)。我需要一个新类、一个接口(interface),还是最好的方法?任何帮助,将不胜感激!提前致谢。

最佳答案

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

// 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);
}
}
};

为了使实现工作,ABCDtime 变量必须是实例/类变量,或者是 final 局部变量。

关于c# - 将 C# Func<> 移植到 Java- Math Equation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11124668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com