// 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:   SumNode.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, TermNode, MulNode, 
//            MinusNode, PlusNode

public abstract class SumNode extends OperatorNode
{

    protected abstract void enumerateOperands(Vector vector);

    public Node simplify()
    {
        Vector vector = new Vector();
        enumerateOperands(vector);
        Object obj = new ConstantNode(0.0D);
        double d = 0.0D;
        for (int i = 0; i < vector.size(); i++)
        {
            double d1 = 1.0D;
            Node node = (Node)vector.elementAt(i);
            if (node instanceof TermNode)
            {
                TermNode termnode = (TermNode)node;
                d1 = termnode.multiplier();
                node = termnode.termNode();
            }
            if (node == null)
            {
                d += d1;
            } else
            {
                for (int j = i + 1; j < vector.size(); j++)
                {
                    double d2 = 1.0D;
                    Node node1 = (Node)vector.elementAt(j);
                    if (node1 instanceof TermNode)
                    {
                        TermNode termnode1 = (TermNode)node1;
                        d2 = termnode1.multiplier();
                        node1 = termnode1.termNode();
                    }
                    if (node.equals(node1))
                    {
                        d1 += d2;
                        vector.removeElementAt(j);
                        j--;
                    }
                }

                Object obj1 = new ConstantNode(Math.abs(d1));
                obj1 = (new MulNode(((Node) (obj1)), node)).simplifyOperation();
                if (d1 < 0.0D)
                    obj = new MinusNode(((Node) (obj)), ((Node) (obj1)));
                else
                    obj = new PlusNode(((Node) (obj)), ((Node) (obj1)));
                obj = ((Node) (obj)).simplifyOperation();
            }
        }

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

    public abstract double evaluate();

    public abstract Node cloneNode();

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