// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) space 
// Source File Name:   TimesNode.java

package lt.monarch.function.node;

import java.util.Vector;
import lt.monarch.function.Node;

// Referenced classes of package lt.monarch.function.node:
//            OperatorNode, ConstantNode, NegativeNode, PowerNode, 
//            PlusNode, DivNode, MulNode

public abstract class TimesNode extends OperatorNode
{

    protected abstract void enumerateOperands(Vector vector);

    public Node simplify()
    {
        Vector vector = new Vector();
        enumerateOperands(vector);
        Object obj = new ConstantNode(1.0D);
        double d = 1.0D;
        for (int i = 0; i < vector.size(); i++)
        {
            Object obj1 = new ConstantNode(1.0D);
            Node node = (Node)vector.elementAt(i);
            if (node instanceof ConstantNode)
            {
                d *= node.evaluate();
            } else
            {
                if (node instanceof NegativeNode)
                {
                    node = ((NegativeNode)node).positive();
                    d = -d;
                }
                if (node instanceof PowerNode)
                {
                    PowerNode powernode = (PowerNode)node;
                    obj1 = powernode.powerNode();
                    node = powernode.termNode();
                }
                for (int j = i + 1; j < vector.size(); j++)
                {
                    Object obj2 = new ConstantNode(1.0D);
                    Node node1 = (Node)vector.elementAt(j);
                    if (node1 instanceof NegativeNode)
                    {
                        node1 = ((NegativeNode)node1).positive();
                        d = -d;
                    }
                    if (node1 instanceof PowerNode)
                    {
                        PowerNode powernode1 = (PowerNode)node1;
                        obj2 = powernode1.powerNode();
                        node1 = powernode1.termNode();
                    }
                    if (node1.equals(node))
                    {
                        obj1 = new PlusNode(((Node) (obj1)), ((Node) (obj2)));
                        vector.removeElementAt(j);
                        j--;
                    }
                }

                obj1 = ((Node) (obj1)).simplify();
                if ((obj1 instanceof ConstantNode) && ((Node) (obj1)).evaluate() < 0.0D)
                    obj = new DivNode(((Node) (obj)), (new PowerNode(node, new ConstantNode(-((Node) (obj1)).evaluate()))).simplifyOperation());
                else
                    obj = new MulNode(((Node) (obj)), (new PowerNode(node, ((Node) (obj1)))).simplifyOperation());
                obj = ((Node) (obj)).simplifyOperation();
            }
        }

        return (new MulNode(new ConstantNode(d), ((Node) (obj)))).simplifyOperation();
    }

    public abstract double evaluate();

    public abstract Node cloneNode();

    public TimesNode(Node node, Node node1)
    {
        super(node, node1);
    }
}
