Beispiel #1
0
        private static bool SatisfyCalcCondition(
            Func <Expression, Expression, BinaryExpression> func,
            object x, object y, out object output)
        {
            output = null;
            if (func.Method.Name.Equals("Divide"))
            {
                return(false);
            }

            if (LogicSharp.IsNumeric(x) && LogicSharp.IsNumeric(y))
            {
                double xDoubleVal;
                double yDoubleVal;
                bool   isXDouble = LogicSharp.IsDouble(x, out xDoubleVal);
                bool   isYDouble = LogicSharp.IsDouble(y, out yDoubleVal);
                if (isXDouble || isYDouble)
                {
                    var xExpr = Expression.Constant(xDoubleVal);
                    var yExpr = Expression.Constant(yDoubleVal);
                    var rExpr = func(xExpr, yExpr);
                    output = Expression.Lambda <Func <double> >(rExpr).Compile().Invoke();
                    int iResult;
                    if (LogicSharp.IsInt(output, out iResult))
                    {
                        output = iResult;
                        return(true);
                    }
                    return(true);
                }
            }
            return(false);
        }
        public static bool IsQuadraticTerm(this Term term)
        {
            if (term == null)
            {
                return(false);
            }
            if (!term.ContainsVar())
            {
                return(false);
            }
            var cond1 = term.Op == Expression.Power;
            var lst   = term.Args as List <object>;

            if (lst == null || lst.Count != 2)
            {
                return(false);
            }
            var isNum = LogicSharp.IsNumeric(lst[1]);

            if (!isNum)
            {
                return(false);
            }
            double number;

            LogicSharp.IsDouble(lst[1], out number);
            var cond2 = number.Equals(2.0);

            return(cond1 && cond2);
        }
Beispiel #3
0
        public static object Calculate(Func <Expression, Expression, BinaryExpression> func,
                                       object x, object y)
        {
            double xDoubleVal;
            double yDoubleVal;
            bool   isXDouble = LogicSharp.IsDouble(x, out xDoubleVal);
            bool   isYDouble = LogicSharp.IsDouble(y, out yDoubleVal);

            if (isXDouble || isYDouble)
            {
                var xExpr  = Expression.Constant(xDoubleVal);
                var yExpr  = Expression.Constant(yDoubleVal);
                var rExpr  = func(xExpr, yExpr);
                var result = Expression.Lambda <Func <double> >(rExpr).Compile().Invoke();

                int iResult;
                if (LogicSharp.IsInt(result, out iResult))
                {
                    return(iResult);
                }
                else
                {
                    return(result);
                }
            }
            else
            {
                return(null);
            }
        }
Beispiel #4
0
        public override string ToString()
        {
            double dNum;
            bool   isNum = LogicSharp.IsDouble(Rhs, out dNum);

            if (isNum)
            {
                int  iNum;
                bool isInteger = LogicSharp.IsInt(Rhs, out iNum);
                if (isInteger)
                {
                    return(base.ToString());
                }

                double roundD = Math.Round(dNum, 4);
                return(string.Format("{0}={1}", Lhs, roundD));
            }
            return(base.ToString());
        }
Beispiel #5
0
        private bool InvertOp()
        {
            var lst = Args as List <object>;

            if (lst == null)
            {
                return(false);
            }
            double dnum;
            bool   result = LogicSharp.IsDouble(lst[0], out dnum);

            if (!result)
            {
                return(false);
            }
            if (dnum < 0.0)
            {
                return(true);
            }
            return(false);
        }
Beispiel #6
0
        public override string ToString()
        {
            var builder = new StringBuilder();

            var tuple = Args as Tuple <object, object>;

            if (tuple != null)
            {
                #region Tuple Format
                builder.Append('(');

                var lTerm = tuple.Item1;
                var rTerm = tuple.Item2;

                builder.Append(lTerm);

                if (Op.Method.Name.Equals("Add"))
                {
                    builder.Append('+');
                }
                else if (Op.Method.Name.Equals("Substract"))
                {
                    builder.Append('-');
                }
                else if (Op.Method.Name.Equals("Multiply"))
                {
                    builder.Append('*');
                }
                else if (Op.Method.Name.Equals("Divide"))
                {
                    builder.Append('/');
                }

                builder.Append(rTerm.ToString());
                builder.Append(')');
                #endregion
            }

            var lst = Args as List <object>;
            if (lst != null)
            {
                for (int i = 0; i < lst.Count; i++)
                {
                    var  variable  = lst[i] as Var;
                    bool number    = LogicSharp.IsNumeric(lst[i]);
                    var  localTerm = lst[i] as Term;

                    #region Var
                    if (variable != null)
                    {
                        if (Op.Method.Name.Equals("Add"))
                        {
                            if (i != 0)
                            {
                                builder.Append("+");
                            }
                        }

                        if (Op.Method.Name.Equals("Power"))
                        {
                            if (i != 0)
                            {
                                builder.Append("^");
                            }
                        }
                        builder.Append(variable);
                    }
                    #endregion

                    #region Numerics
                    if (number)
                    {
                        if (Op.Method.Name.Equals("Add"))
                        {
                            if (i != 0)
                            {
                                double dnum;
                                bool   result = LogicSharp.IsDouble(lst[i], out dnum);
                                if (dnum < 0.0)
                                {
                                    builder.Append("-");
                                    double absNum = Math.Abs(dnum);
                                    builder.Append(absNum);
                                }
                                else
                                {
                                    builder.Append("+");
                                    builder.Append(lst[i].ToString());
                                }
                            }
                            else
                            {
                                builder.Append(lst[i].ToString());
                            }
                        }
                        else if (Op.Method.Name.Equals("Multiply"))
                        {
                            double dnum;
                            LogicSharp.IsDouble(lst[i], out dnum);
                            double absNum = Math.Abs(dnum) - 1.0;
                            if (absNum > 0.0001)
                            {
                                builder.Append(lst[i]);
                            }
                            else
                            {
                                if (dnum < 0.0d)
                                {
                                    builder.Append("-");
                                }
                            }
                        }
                        else if (Op.Method.Name.Equals("Divide"))
                        {
                            if (i != 0)
                            {
                                builder.Append("/");
                                builder.Append(lst[i].ToString());
                            }
                            else
                            {
                                builder.Append(lst[i].ToString());
                            }
                        }
                        else if (Op.Method.Name.Equals("Power"))
                        {
                            if (i != 0)
                            {
                                builder.Append("^");
                            }
                            builder.Append(lst[i].ToString());
                        }
                    }
                    #endregion

                    #region Term
                    if (localTerm != null)
                    {
                        //precatch
                        if (Op.Method.Name.Equals("Add"))
                        {
                            if (i != 0)
                            {
                                bool result = localTerm.InvertOp();
                                if (!result)
                                {
                                    builder.Append("+");
                                }

                                /*else
                                 * {
                                 *  builder.Append("-");
                                 * }*/
                            }
                            builder.Append(localTerm.ToString());
                        }

                        if (Op.Method.Name.Equals("Multiply") ||
                            Op.Method.Name.Equals("Power")
                            )
                        {
                            bool needBrace = false;
                            needBrace = localTerm.NeedBracket();
                            if (needBrace)
                            {
                                builder.Append("(");
                                builder.Append(localTerm.ToString());
                                builder.Append(")");
                            }
                            else
                            {
                                builder.Append(localTerm.ToString());
                            }
                        }

                        if (Op.Method.Name.Equals("Divide"))
                        {
                            if (i != 0)
                            {
                                builder.Append("/");
                            }
                            bool needBrace = false;
                            needBrace = localTerm.NeedBracket();
                            if (needBrace)
                            {
                                builder.Append("(");
                                builder.Append(localTerm.ToString());
                                builder.Append(")");
                            }
                            else
                            {
                                builder.Append(localTerm.ToString());
                            }
                        }
                    }
                    #endregion
                }
            }

            return(builder.ToString());
        }