2017-09-12 122 views
1

我使用MathNet.Symbolics库simplyfy像这样的表达式:如何计算平方根在MathNet.Symbolics

string f = Infix.Print(Infix.ParseOrThrow("A+5*2")) 

可正常工作(f = A+10),但试图让根的数量为比我预期的要困难得多。例如:

string f = Infix.Print(Infix.ParseOrThrow("sqrt(9)")) 

f = "sqrt(9)“,而不是f = "3"你所期望的

string f = Infix.Print(Infix.ParseOrThrow("sqrt(x^2)")) 

f = "sqrt(x^2)" insted的的f = "x"

string f = Infix.Print(Infix.ParseOrThrow("9^(1/2)")) 

也不起作用insted的它被简化为f = "sqrt(9)"

我该如何强制它计算一个数字/变量的sqrt?

当使用MathNet.Symbolics的“自动简化”时,是否会遇到其他问题?

+0

参见:https://discuss.mathdotnet.com/t/how-do-i-calculate-the-sqrt-in-数学净符号处理/ 533 –

回答

1

您需要通过Evaluate方法来运行的表达式:

string f = Infix.Print(Infix.ParseOrThrow("sqrt(9)")); 
double result = Evaluate.Evaluate(null, f);