我对javacalculus库有疑问:
Javacalculus工作正常,但
如果变量数为奇数(3、5、7、9,..),则不计算导数,也没有输出
如果我输入3个相乘的变量(p1 * p2 * p3),它将无法正常工作。
如果我输入4个相乘的变量(p1 * p2 * p3 * p4),则可以正常工作。
如果我输入5个相乘的变量(p1 * p2 * p3 * p4 * p5),它将无法正常工作
如果我输入6个乘以(p1 * p2 * p3 * p4 * p5 * p6)的变量,它将起作用。
有人知道哪里有问题吗?
这是我的代码:
import java.util.Scanner;
import javacalculus.core.CALC;
import javacalculus.core.CalcParser;
import javacalculus.evaluator.CalcSUB;
import javacalculus.struct.CalcDouble;
import javacalculus.struct.CalcObject;
import javacalculus.struct.CalcSymbol;
public class Main {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
System.out.println("Enter expression:");
String expression = in.nextLine();
// javacalculus uses uppercase function names
System.out.println("Differentiate with respect to:");
String variable = in.nextLine();
// differentiate
String command = "DIFF(" + expression + ", " + variable + ")";
CalcParser parser = new CalcParser();
CalcObject parsed = parser.parse(command);
CalcObject result = parsed.evaluate();
// compute numerical value
result = subst(result, "p1", 1-0.069727798);
result = subst(result, "p2", 1-0.205574309);
result = subst(result, "p3", 1-0.022450503);
result = subst(result, "p4", 1-0.010557907);
result = CALC.SYM_EVAL(result);
System.out.println("Result:");
System.out.println(result);
}
static CalcObject subst(CalcObject input, String var, double number)
{
CalcSymbol symbol = new CalcSymbol(var);
CalcDouble value = new CalcDouble(number);
return CalcSUB.numericSubstitute(input, symbol, value);
}
}