Exemplo n.º 1
0
        protected override Expression VisitPower(Power P)
        {
            Expression L = Visit(P.Left);
            Expression R = Visit(P.Right);

            if (R.IsInteger())
            {
                // Transform (x*y)^z => x^z*y^z if z is an integer.
                Product M = L as Product;
                if (!ReferenceEquals(M, null))
                {
                    return(Visit(Product.New(M.Terms.Select(i => Power.New(i, R)))));
                }
            }

            // Transform (x^y)^z => x^(y*z) if z is an integer.
            Power LP = L as Power;

            if (!ReferenceEquals(LP, null))
            {
                L = LP.Left;
                R = Visit(Product.New(P.Right, LP.Right));
            }

            // Handle identities.
            Real?LR = AsReal(L);

            if (EqualsZero(LR))
            {
                return(0);
            }
            if (EqualsOne(LR))
            {
                return(1);
            }

            Real?RR = AsReal(R);

            if (EqualsZero(RR))
            {
                return(1);
            }
            if (EqualsOne(RR))
            {
                return(L);
            }

            // Evaluate result.
            if (LR != null && RR != null)
            {
                return(Constant.New(LR.Value ^ RR.Value));
            }
            else
            {
                return(Power.New(L, R));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// If x is of the form a^n where n is an integer, return n and replace x with a.
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public static int IntegralExponentOf(Expression x)
        {
            Expression n = ExponentOf(x);

            if (n.IsInteger())
            {
                return((int)(Real)n);
            }
            return(1);
        }
Exemplo n.º 3
0
 public override bool Contains(Expression x)
 {
     return(x.IsInteger());
 }