Beispiel #1
0
            public override bool VisitMultiply(TExpr left, TExpr right, TExpr original, Dummy data)
            {
                Polynomial <TVar, TExpr> l, r;
                var lBuilt = Build(left, out l);
                var rBuilt = Build(right, out r);

                if (lBuilt && rBuilt)
                {
                    long   kLeft, kRight;
                    object mult;
                    if (l.IsIntConstant(out kLeft) && r.IsIntConstant(out kRight) &&
                        EvaluateArithmeticWithOverflow.TryBinary(
                            Decoder.TypeOf(original), ExpressionOperator.Mult, kLeft, kRight, out mult))
                    {
                        var longValue = mult.ConvertToLong();
                        if (longValue.HasValue)
                        {
                            var monomial = Monomial <TVar> .From(Rational.For(longValue.Value));

                            return(true.With(new Polynomial <TVar, TExpr> (monomial), out poly));
                        }
                    }
                    return(TryToPolynomialHelperForMultiplication(l, r, out poly));
                }
                return(false);
            }
Beispiel #2
0
                                static bool TryEvaluateInt32 (BinaryOperator op, int l, int r, out long value)
                                {
                                        int result;
                                        if (EvaluateArithmeticWithOverflow.TryBinary (op.ToExpressionOperator (), l, r,
                                                                                      out result))
                                                return true.With (result, out value);

                                        return false.Without (out value);
                                }
Beispiel #3
0
            public override bool VisitSubtraction(TExpr left, TExpr right, TExpr original, Dummy data)
            {
                Polynomial <TVar, TExpr> polyLeft, polyRight;

                if (!Build(left, out polyLeft) || !Build(right, out polyRight))
                {
                    return(false);
                }

                long   kLeft, kRight;
                object subtraction;

                if (polyLeft.IsIntConstant(out kLeft) && polyRight.IsIntConstant(out kRight) &&
                    EvaluateArithmeticWithOverflow.TryBinary(Decoder.TypeOf(original), ExpressionOperator.Sub, kLeft, kRight, out subtraction))
                {
                    var longValue = subtraction.ConvertToLong();
                    if (longValue.HasValue)
                    {
                        return(true.With(new Polynomial <TVar, TExpr> (Monomial <TVar> .From(Rational.For(longValue.Value))), out poly));
                    }
                }
                return(TryToPolynomialHelperForSubtraction(polyLeft, polyRight, out poly));
            }