本文整理汇总了C#中IExpr.IsSymbol方法的典型用法代码示例。如果您正苦于以下问题:C# IExpr.IsSymbol方法的具体用法?C# IExpr.IsSymbol怎么用?C# IExpr.IsSymbol使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IExpr
的用法示例。
在下文中一共展示了IExpr.IsSymbol方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Subtitute
public static IExpr Subtitute(IExpr expr, string variable, IExpr subt)
{
if (expr.IsSymbol())
{
if (expr.SymbolValue() == variable)
return subt;
else
return expr;
}
else if (expr.IsConstant())
{
return expr;
}
else
{
var func = expr as Function;
if (!func.IsBinary)
{
return new Function(func.Name, Subtitute(func.FirstArgument, variable, subt));
}
else
{
return new Function(func.Name, Subtitute(func.FirstArgument, variable, subt), Subtitute(func.SecondArgument, variable, subt));
}
}
}
示例2: Variables
public static IEnumerable<string> Variables(IExpr expr)
{
if (expr.IsSymbol())
yield return expr.SymbolValue();
if (expr.IsFunc())
{
var func = expr as Function;
if (!func.IsBinary)
{
foreach(var variable in Variables(func.FirstArgument))
yield return variable;
}
else
{
foreach (var variable in Variables(func.FirstArgument).Concat(Variables(func.SecondArgument)))
yield return variable;
}
}
}
示例3: Differentiate
public static IExpr Differentiate(IExpr expr, string variable)
{
if (expr.IsConstant())
{
return Constant(0.0);
}
else if (expr.IsSymbol())
{
if (expr.SymbolValue() == variable)
return Constant(1.0);
else
return Constant(0.0);
}
else
{
var func = expr as Function;
if (func.IsBinary)
{
switch (func.Name)
{
case "plus":
{
var left = Differentiate(func.FirstArgument, variable);
var right = Differentiate(func.SecondArgument, variable);
return Plus(left, right);
}
case "minus":
{
var left = Differentiate(func.FirstArgument, variable);
var right = Differentiate(func.SecondArgument, variable);
return Plus(left, Times(Constant(-1.0), right));
}
case "times":
{
var f = func.FirstArgument;
var fPrime = Differentiate(f, variable);
var g = func.SecondArgument;
var gPrime = Differentiate(g, variable);
return Plus(Times(f, gPrime), Times(g, fPrime));
}
case "pow":
{
var g = func.FirstArgument;
var h = func.SecondArgument;
if (h.IsConstant())
{
var pow = h.ConstantValue();
return Times(Times(Constant(pow), Pow(g, Constant(pow - 1.0))), Differentiate(g, variable));
}
else if (g.IsConstant() && h.IsSymbol())
{
var constant = g.ConstantValue();
return Times(func, Log(g));
}
else
{
return func;
}
}
case "div":
{
var rewritten = Div(func.FirstArgument, func.SecondArgument);
return Differentiate(rewritten, variable);
}
default:
return func;
}
}
else
{
switch(func.Name)
{
case "sin":
return Times(Cos(func.FirstArgument), Differentiate(func.FirstArgument, variable));
case "cos":
return Times(Times(Constant(-1.0), Sin(func.FirstArgument)), Differentiate(func.FirstArgument, variable));
case "tan":
return Pow(Sec(func.FirstArgument), Constant(2.0));
case "log":
return Div(Differentiate(func.FirstArgument, variable), func.FirstArgument);
default:
return func;
}
}
}
}