本文整理汇总了C#中Parser.Calculate方法的典型用法代码示例。如果您正苦于以下问题:C# Parser.Calculate方法的具体用法?C# Parser.Calculate怎么用?C# Parser.Calculate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Parser
的用法示例。
在下文中一共展示了Parser.Calculate方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Iterative
public static string Iterative(string sfx, string sfdx, double a, double b, out int count)
{
Parser fx = new Parser(sfx);
fx.AddFunction<Algorithms>("log");
fx.Compile();
Parser fdx = new Parser(sfdx);
fdx.AddFunction<Algorithms>("log");
fdx.Compile();
double x0 = (a + b)/2;
if (x0 == 0)
{
x0 = (b + x0)/2;
}
fdx.Variables.Add("x", x0);
double lambda = -1 / fdx.Calculate();
count = 0;
fx.Variables.Add("x", x0);
double xn = x0 + lambda*fx.Calculate();
while ((xn - x0) > eps)
{
x0 = xn;
fx.Variables["x"] = xn;
xn = xn + lambda * fx.Calculate();
++count;
}
return xn.ToString();
}
示例2: Parse
public static double Parse(string input)
{
Parser OMG = new Parser(input);
OMG.AddFunction<Agent>("F");
OMG.Compile();
OMG.Variables.Add("x",2);
OMG.Variables.Add("y", 2);
OMG.Variables.Add("z", 2);
return OMG.Calculate();
}
示例3: Chord
public static string Chord(string sfx, string sfdx, double a, double b, out int count)
{
Parser fx = new Parser(sfx);
fx.AddFunction<Algorithms>("log");
fx.Compile();
Parser fdx = new Parser(sfdx);
fdx.AddFunction<Algorithms>("log");
fdx.Compile();
fx.Variables["x"] = a;
fdx.Variables["x"] = a;
double a_fpt = fx.Calculate()*fdx.Calculate();
fx.Variables["x"] = b;
fdx.Variables["x"] = b;
double b_fpt = fx.Calculate()*fdx.Calculate();
double fixedPoint = a_fpt > 0 ? a : b;
double xnm1 = (a + b) / 2;
fx.Variables["x"] = xnm1;
double fx_r = fx.Calculate();
fx.Variables["x"] = fixedPoint;
double fx0_r = fx.Calculate();
double xn = xnm1 - (xnm1 - fixedPoint)/(fx_r - fx0_r)*fx_r;
count = 0;
while ((xn - xnm1) > eps)
{
xnm1 = xn;
fx.Variables["x"] = xn;
fx_r = fx.Calculate();
xn = xn - (xn - fixedPoint) / (fx_r - fx0_r) * fx_r;
++count;
}
return xn.ToString();
}
示例4: Newton
public static string Newton(string sfx, string sfdx, string sfddx, double a, double b, out int count)
{
Parser fx = new Parser(sfx);
fx.AddFunction<Algorithms>("log");
fx.Compile();
Parser fdx = new Parser(sfdx);
fdx.AddFunction<Algorithms>("log");
fdx.Compile();
Parser fddx = new Parser(sfddx);
fddx.AddFunction<Algorithms>("log");
fdx.Compile();
fx.Variables["x"] = a;
fddx.Variables["x"] = a;
double a_fpt = fx.Calculate() * fddx.Calculate();
fx.Variables["x"] = b;
fddx.Variables["x"] = b;
fx.Compile();
double b_fpt = fx.Calculate() * fddx.Calculate();
double fixedPoint = a_fpt > 0 ? a : b;
fx.Variables["x"] = fixedPoint;
fdx.Variables["x"] = fixedPoint;
double xn = fixedPoint - fx.Calculate()/fdx.Calculate();
count = 0;
while ((xn - fixedPoint) > eps)
{
fixedPoint = xn;
fx.Variables["x"] = xn;
fdx.Variables["x"] = xn;
xn = fixedPoint - fx.Calculate() / fdx.Calculate();
++count;
}
return xn.ToString();
}