package org.nfunk.jep.function;

import java.util.Stack;
import java.util.Vector;
import org.nfunk.jep.ParseException;
import org.nfunk.jep.type.Complex;

public class Divide extends PostfixMathCommand {
  public void run(Stack paramStack) throws ParseException {
    checkStack(paramStack);
    Object object1 = paramStack.pop();
    Object object2 = paramStack.pop();
    paramStack.push(div(object2, object1));
  }
  
  public Object div(Object paramObject1, Object paramObject2) throws ParseException {
    if (paramObject1 instanceof Complex) {
      if (paramObject2 instanceof Complex)
        return div((Complex)paramObject1, (Complex)paramObject2); 
      if (paramObject2 instanceof Number)
        return div((Complex)paramObject1, (Number)paramObject2); 
      if (paramObject2 instanceof Vector)
        return div((Complex)paramObject1, (Vector)paramObject2); 
    } else if (paramObject1 instanceof Number) {
      if (paramObject2 instanceof Complex)
        return div((Number)paramObject1, (Complex)paramObject2); 
      if (paramObject2 instanceof Number)
        return div((Number)paramObject1, (Number)paramObject2); 
      if (paramObject2 instanceof Vector)
        return div((Number)paramObject1, (Vector)paramObject2); 
    } else if (paramObject1 instanceof Vector) {
      if (paramObject2 instanceof Complex)
        return div((Vector)paramObject1, (Complex)paramObject2); 
      if (paramObject2 instanceof Number)
        return div((Vector)paramObject1, (Number)paramObject2); 
    } 
    throw new ParseException("Invalid parameter type");
  }
  
  public Double div(Number paramNumber1, Number paramNumber2) {
    return new Double(paramNumber1.doubleValue() / paramNumber2.doubleValue());
  }
  
  public Complex div(Complex paramComplex1, Complex paramComplex2) {
    return paramComplex1.div(paramComplex2);
  }
  
  public Complex div(Number paramNumber, Complex paramComplex) {
    Complex complex = new Complex(paramNumber.doubleValue(), 0.0D);
    return complex.div(paramComplex);
  }
  
  public Complex div(Complex paramComplex, Number paramNumber) {
    return new Complex(paramComplex.re() / paramNumber.doubleValue(), paramComplex.im() / paramNumber.doubleValue());
  }
  
  public Vector div(Vector paramVector, Number paramNumber) {
    Vector vector = new Vector();
    for (byte b = 0; b < paramVector.size(); b++)
      vector.addElement(div(paramVector.elementAt(b), paramNumber)); 
    return vector;
  }
  
  public Vector div(Number paramNumber, Vector paramVector) {
    Vector vector = new Vector();
    for (byte b = 0; b < paramVector.size(); b++)
      vector.addElement(div(paramNumber, paramVector.elementAt(b))); 
    return vector;
  }
  
  public Vector div(Vector paramVector, Complex paramComplex) {
    Vector vector = new Vector();
    for (byte b = 0; b < paramVector.size(); b++)
      vector.addElement(div(paramVector.elementAt(b), paramComplex)); 
    return vector;
  }
  
  public Vector div(Complex paramComplex, Vector paramVector) {
    Vector vector = new Vector();
    for (byte b = 0; b < paramVector.size(); b++)
      vector.addElement(div(paramComplex, paramVector.elementAt(b))); 
    return vector;
  }
}
