将java函数作为参数传递

时间:2022-12-05 17:03:07

I'm using Wolfram Mathematica 9.0 in my java application. The problem is that I need to generate a very long expression (something like 1.2 * (x-1) + 1.25 * (x-1.5) + ...etc.) as a function and it takes a long time to build it with StringBuilder. Are there any ways to pass a java function without converting it into a wolfram expression?

我在我的java应用程序中使用Wolfram Mathematica 9.0。问题是我需要生成一个非常长的表达式(类似于1.2 *(x-1)+ 1.25 *(x-1.5)+ ...等)作为一个函数,并且需要很长时间来构建它StringBuilder的。有没有办法传递java函数而不将其转换为wolfram表达式?

UPD

This function is what I am talking about. It builds the wolfram function exxpression. Usually, arr length is more than 100000.

我正在谈论这个功能。它构建了wolfram函数exxpression。通常,arr长度超过100000。

public static String buildCdfExpressionForWolfram(double [] arr){

    StringBuilder strBuilder = new StringBuilder();

    int n = arr.length;
    strBuilder.append("(");
    for(int i = 0; i < n; i++){
        strBuilder.append("HeavisideTheta[x");
        if(arr[i] > 0) {
            strBuilder.append("-");
            strBuilder.append(arr[i]);
        }
        else if(arr[i] < 0){
            strBuilder.append("+");
            strBuilder.append(-arr[i]);
        }
        strBuilder.append("]");
        if(i != n - 1){
            strBuilder.append("+");
        }
    }

    strBuilder.append(") / " + n);


    return strBuilder.toString();
}

1 个解决方案

#1


What happens if you reduce the amount of append call ?

如果减少追加电话的数量会怎样?

public static String buildCdfExpressionForWolfram(double [] arr){

    StringBuilder strBuilder = new StringBuilder("(");

    int n = arr.length;
    for(int i = 0; i < n; i++){
        if(arr[i] > 0) {
            strBuilder.append("HeavisideTheta[x-"+arr[i]+"]");
        }
        else if(arr[i] < 0){
            strBuilder.append("HeavisideTheta[x+-"+arr[i]+"]");
        }
        if(i != n - 1){
            strBuilder.append("+");
        }
    }

    strBuilder.append(") / " + n);


    return strBuilder.toString();
}

#1


What happens if you reduce the amount of append call ?

如果减少追加电话的数量会怎样?

public static String buildCdfExpressionForWolfram(double [] arr){

    StringBuilder strBuilder = new StringBuilder("(");

    int n = arr.length;
    for(int i = 0; i < n; i++){
        if(arr[i] > 0) {
            strBuilder.append("HeavisideTheta[x-"+arr[i]+"]");
        }
        else if(arr[i] < 0){
            strBuilder.append("HeavisideTheta[x+-"+arr[i]+"]");
        }
        if(i != n - 1){
            strBuilder.append("+");
        }
    }

    strBuilder.append(") / " + n);


    return strBuilder.toString();
}