当前位置: 首页>>代码示例>>C#>>正文


C# Expression.Simplify方法代码示例

本文整理汇总了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.");
        }
开发者ID:AlexSabaka,项目名称:symbolicmath,代码行数:90,代码来源:Program.cs

示例2: Simplify

 public static Expression Simplify(Expression x)
 {
     return x.Simplify();
 }
开发者ID:JackWangCUMT,项目名称:ComputerAlgebra,代码行数:4,代码来源:GlobalNamespace.cs


注:本文中的Expression.Simplify方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。