本文整理汇总了C#中Expression.Simplify方法的典型用法代码示例。如果您正苦于以下问题:C# Expression.Simplify方法的具体用法?C# Expression.Simplify怎么用?C# Expression.Simplify使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Expression
的用法示例。
在下文中一共展示了Expression.Simplify方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Solve
static Expression Solve(Expression left, Expression right, Variable v)
{
left = left.Simplify();
right = right.Simplify();
Console.WriteLine("\t{0} = {1} // {2} == {3}", left, right, left.GetType().Name, right.GetType().Name);
if (!left.IsDependOf(v)) throw new Exception("Can't solve equation. No dependencies of variable '" + v.Name + "'.");
if (left is Symbolic.Operators.Scalar.Add)
{
Expression ll = Left(left);
Expression lr = Right(left);
if (lr.IsDependOf(v)) return Solve(lr, right - ll, v);
else return Solve(ll, right - lr, v);
}
else if (left is Symbolic.Operators.Scalar.Sub)
{
Expression ll = Left(left);
Expression lr = Right(left);
if (lr.IsDependOf(v)) return Solve(-lr, right - ll, v);
else return Solve(ll, right + lr, v);
}
else if (left is Symbolic.Operators.Scalar.Div)
{
Expression ll = Left(left);
Expression lr = Right(left);
if (lr.IsDependOf(v)) return Solve(lr, ll / right, v);
else return Solve(ll, right * lr, v);
}
else if (left is Symbolic.Operators.Scalar.Mul)
{
Expression ll = Left(left);
Expression lr = Right(left);
if (lr.IsDependOf(v)) return Solve(lr, right / ll, v);
else return Solve(ll, right / lr, v);
}
else if (left is Symbolic.Operators.Scalar.Pow)
{
Expression ll = Left(left);
Expression lr = Right(left);
if (lr.IsDependOf(v)) return Solve(lr, SMath.Log(right, ll), v);
else return Solve(ll, right ^ (1 / lr), v);
}
else if (left is Symbolic.Functions.Sqrt)
{
Expression ll = (left as Symbolic.Functions.Sqrt).Args[0];
return Solve(ll, right ^ 2, v);
}
else if (left is Symbolic.Functions.Sin)
{
Expression ll = (left as Symbolic.Functions.Sin).Args[0];
return Solve(ll, SMath.ArcSin(right), v);
}
else if (left is Symbolic.Functions.Cos)
{
Expression ll = (left as Symbolic.Functions.Cos).Args[0];
return Solve(ll, SMath.ArcCos(right), v);
}
else if (left is Symbolic.Functions.Tan)
{
Expression ll = (left as Symbolic.Functions.Tan).Args[0];
return Solve(ll, SMath.ArcTan(right), v);
}
else if (left is Symbolic.Functions.ArcSin)
{
Expression ll = (left as Symbolic.Functions.ArcSin).Args[0];
return Solve(ll, SMath.Sin(right), v);
}
else if (left is Symbolic.Functions.ArcCos)
{
Expression ll = (left as Symbolic.Functions.ArcCos).Args[0];
return Solve(ll, SMath.Cos(right), v);
}
else if (left is Symbolic.Functions.ArcTan)
{
Expression ll = (left as Symbolic.Functions.ArcTan).Args[0];
return Solve(ll, SMath.Tan(right), v);
}
else if (left is Symbolic.Functions.Exp)
{
Expression ll = (left as Symbolic.Functions.Exp).Args[0];
return Solve(ll, SMath.Ln(right), v);
}
else if (left is Symbolic.Functions.Ln)
{
Expression ll = (left as Symbolic.Functions.Ln).Args[0];
return Solve(ll, SMath.Exp(right), v);
}
else if (left is Variable && left.IsDependOf(v)) return right;
else throw new Exception("Can't solve equation. Unknown equation type.");
}
示例2: Simplify
public static Expression Simplify(Expression x)
{
return x.Simplify();
}