Пример #1
0
        /*
         * (a * b) ^ c => (a ^ c) * (b ^ c)
         * (a ^ b) ^ c => a ^ (b * c)
         */
        private Node BasicPowerConverter(Node node)
        {
            BiNode bin = node.TryToGetAsBiNode;

            if (bin != null)
            {
                if (bin.Signature == new Power(null, null).Signature)
                {
                    BiNode left = bin.Left.TryToGetAsBiNode;
                    if (left != null)
                    {
                        if (left.Signature == new Multiplication(null, null).Signature)
                        {
                            return(new Multiplication(BasicPowerConverter(new Power(left.Left, bin.Right)), BasicPowerConverter(new Power(left.Right, bin.Right))));
                        }
                        else if (left.Signature == new Power(null, null).Signature)
                        {
                            Value a = bin.Right.TryToGetAsValue;
                            Value b = left.Right.TryToGetAsValue;
                            if (a != null && b != null)
                            {
                                if (a.GetValue * b.GetValue != 1)
                                {
                                    return(new Power(left.Left, new Value(a.GetValue * b.GetValue)));
                                }
                                else
                                {
                                    return(left.Left);
                                }
                            }
                            else
                            {
                                return(new Power(left.Left, new Multiplication(bin.Right, left.Right)));
                            }
                        }
                    }
                }
            }
            return(node);
        }
Пример #2
0
        /*
         * (4 * 5) => 20 (for all values and operations)
         * ((a @ b) @ c) => (a @ (b @ c)) (for associative)
         * (a @ 4 @ b) => (4 @ a @ b) (for associative and commutative)
         */
        private Node BasicSimplificationConverter(Node node)
        {
            BiNode bin = node.TryToGetAsBiNode;

            if (bin != null)
            {
                Value valLeft  = bin.Left.TryToGetAsValue;
                Value valRight = bin.Right.TryToGetAsValue;
                if (valLeft != null && valRight != null)
                {
                    return(new Value(bin.Operation(valLeft.GetValue, valRight.GetValue)));
                }
                else if (valLeft != null)
                {
                    return(node);
                }
                BiNode binLeft = bin.Left.TryToGetAsBiNode;
                if (binLeft != null)
                {
                    if (bin.Signature == binLeft.Signature && bin.IsAssociative)
                    {
                        return(BasicSimplificationConverter(bin.Clone(binLeft.Left, bin.Clone(binLeft.Right, bin.Right))));
                    }
                }
                else
                {
                    Node   right    = BasicSimplificationConverter(bin.Right);
                    BiNode binRight = right.TryToGetAsBiNode;
                    if (binRight != null)
                    {
                        Value val = binRight.Left.TryToGetAsValue;
                        if (val != null && bin.Signature == binRight.Signature && bin.IsAssociative && bin.IsCommutative)
                        {
                            return(bin.Clone(val, bin.Clone(bin.Left, binRight.Right)));
                        }
                    }
                }
            }
            return(node);
        }