gpt4 book ai didi

java - 将函数作为静态类传递以实现 Java 中的快速数字

转载 作者:行者123 更新时间:2023-12-01 07:34:10 24 4
gpt4 key购买 nike

我想在java中进行一些数值计算,并且为了使操作真正模块化,我想将函数作为其他函数的参数传递。我正在搜索,通常它是在 java 中使用扭曲函数的类完成的。我真的不需要实例化这些类(里面没有数据),并且我想让它尽可能快(某处写到最终静态方法由 JIT 编译器内联)。所以我做了这样的事情

public static class Function2 {
public static float eval(float a, float b){ return Float.NaN; }
}

public static class FAdd extends Function2 {
public static float eval(float a, float b){ return a+b; }
}

public static class Fmult extends Function2 {
public static float eval(float a, float b){ return a*b; }
}

void arrayOp( float [] a, float [] b, float [] out, Function2 func ){
for (int i=0; i<a.length; i++){ out[i] = func.eval( a[i], b[i] ); }
}

float [] a,b, out;

void setup(){
println( FAdd.eval(10,20) );
arrayOp( a,b, out, FAdd );
}

但是,当我尝试将其传递给 arrayOp 时,它会打印错误:“找不到类似 FAdd 的内容”,即使 println( FAdd.eval(10,20) ) 工作正常。因此,由于某种原因,似乎不可能将静态类作为参数传递。

您建议如何解决此类任务?我实际上希望 FAdd 是类似宏的东西,而 arrayOp 是多态的(行为取决于我传入的宏)。但理想的情况是在编译时(而不是运行时)解决它以提高数值速度。编译的结果应该和我写的一样

void arrayAdd( float [] a, float [] b, float [] out ){
for (int i=0; i<a.length; i++){ out[i] = a[i] + b[i]; }
}
void arrayMult( float [] a, float [] b, float [] out ){
for (int i=0; i<a.length; i++){ out[i] = a[i] * b[i]; }
}

最佳答案

您考虑过使用枚举吗?

private void test() {
test(3.0f, 4.0f, F.Add);
test(3.0f, 4.0f, F.Sub);
test(3.0f, 4.0f, F.Mul);
test(3.0f, 4.0f, F.Div);
float[] a = {1f, 2f, 3f, 4f, 5f};
float[] b = {4f, 9f, 16f, 25f, 36f};
test(a, b, F.Add);
test(a, b, F.Sub);
test(a, b, F.Mul);
test(a, b, F.Div);
}

private void test(float[] a, float[] b, F f) {
System.out.println(Arrays.toString(a) + " " + f + " " + Arrays.toString(b) + " = " + Arrays.toString(f.f(a, b, f)));
}

private void test(float a, float b, F f) {
System.out.println(a + " " + f + " " + b + " = " + f.f(a, b));
}

public enum F {
Add {
@Override
public float f(float x, float y) {
return x + y;
}

@Override
public String toString() {
return "+";
}
},
Sub {
@Override
public float f(float x, float y) {
return x - y;
}

@Override
public String toString() {
return "-";
}
},
Mul {
@Override
public float f(float x, float y) {
return x * y;
}

@Override
public String toString() {
return "*";
}
},
Div {
@Override
public float f(float x, float y) {
return x / y;
}

@Override
public String toString() {
return "/";
}
};

// Evaluate to a new array.
static float[] f(float[] x, float[] y, F f) {
float[] c = new float[x.length];
for (int i = 0; i < x.length; i++) {
c[i] = f.f(x[i], y[i]);
}
return c;
}

// All must have an f(x,y) method.
public abstract float f(float x, float y);

// Also offer a toString - defaults to the enum name.
@Override
public String toString() {
return this.name();
}
}

打印:

3.0 + 4.0 = 7.0
3.0 - 4.0 = -1.0
3.0 * 4.0 = 12.0
3.0 / 4.0 = 0.75
[1.0, 2.0, 3.0, 4.0, 5.0] + [4.0, 9.0, 16.0, 25.0, 36.0] = [5.0, 11.0, 19.0, 29.0, 41.0]
[1.0, 2.0, 3.0, 4.0, 5.0] - [4.0, 9.0, 16.0, 25.0, 36.0] = [-3.0, -7.0, -13.0, -21.0, -31.0]
[1.0, 2.0, 3.0, 4.0, 5.0] * [4.0, 9.0, 16.0, 25.0, 36.0] = [4.0, 18.0, 48.0, 100.0, 180.0]
[1.0, 2.0, 3.0, 4.0, 5.0] / [4.0, 9.0, 16.0, 25.0, 36.0] = [0.25, 0.22222222, 0.1875, 0.16, 0.1388889]

关于java - 将函数作为静态类传递以实现 Java 中的快速数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14338953/

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