// 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:   PowerNode.java

package lt.monarch.function.node;

import lt.monarch.function.*;

// Referenced classes of package lt.monarch.function.node:
//            OperatorNode, ConstantNode, MulNode, SumNode, 
//            TimesNode, PlusNode, DivNode, IdentifierNode

public class PowerNode extends OperatorNode
{

    public Node cloneNode()
    {
        return (Node)new PowerNode(super.l.cloneNode(), super.r.cloneNode());
    }

    public Node simplifyOperation()
    {
        if ((super.r instanceof ConstantNode) && super.r.evaluate() == 1.0D)
            return super.l;
        if ((super.r instanceof ConstantNode) && super.r.evaluate() == 0.0D)
            return (Node)new ConstantNode(1.0D);
        if ((super.l instanceof ConstantNode) && super.l.evaluate() == 1.0D)
            return super.l;
        if (super.l instanceof PowerNode)
        {
            PowerNode powernode = (PowerNode)super.l;
            return (Node)new PowerNode(powernode.termNode(), (new MulNode(powernode.powerNode(), super.r)).simplifyOperation());
        } else
        {
            return super.simplifyOperation();
        }
    }

    public String toString()
    {
        return (!(super.l instanceof SumNode) && !(super.l instanceof TimesNode) ? super.l.toString() : "(" + super.l + ")") + "^" + (!(super.r instanceof SumNode) && !(super.r instanceof TimesNode) ? super.r.toString() : "(" + super.r + ")");
    }

    public double evaluate()
    {
        return Math.pow(super.l.evaluate(), super.r.evaluate());
    }

    public Node powerNode()
    {
        return super.r;
    }

    public Node termNode()
    {
        return super.l;
    }

    public Node derivative(Variable variable)
    {
        return (Node)new MulNode(this, new PlusNode(new DivNode(new MulNode(super.l.derivative(variable), super.r), super.l), new MulNode(new IdentifierNode(new StdFunction(4), super.l), super.r.derivative(variable))));
    }

    public void compile(NodeCompiler nodecompiler)
    {
        if (super.r instanceof ConstantNode)
        {
            double d = super.r.evaluate();
            int i = (int)d;
            if (d == (double)i && i <= 5 && i > 0)
            {
                super.l.compile(nodecompiler);
                i--;
                nodecompiler.reserveStack(i * 2);
                for (int j = 0; j < i; j++)
                    nodecompiler.append(92);

                for (int k = 0; k < i; k++)
                    nodecompiler.append(107);

                nodecompiler.freeStack(i * 2);
                return;
            }
        }
        super.l.compile(nodecompiler);
        super.r.compile(nodecompiler);
        nodecompiler.appendStaticCall("java.lang.Math", "pow", "(DD)D");
        nodecompiler.freeStack(2);
    }

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