package org.nfunk.jep.function;

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

public class Dot extends PostfixMathCommand {
  static Add add = new Add();
  
  static Multiply mul = new Multiply();
  
  public void run(Stack paramStack) throws ParseException {
    checkStack(paramStack);
    Object object1 = paramStack.pop();
    Object object2 = paramStack.pop();
    paramStack.push(dot(object2, object1));
  }
  
  public Object dot(Object paramObject1, Object paramObject2) throws ParseException {
    if (paramObject1 instanceof Vector && paramObject2 instanceof Vector)
      return dot((Vector)paramObject1, (Vector)paramObject2); 
    throw new ParseException("Dot: Invalid parameter type, both arguments must be vectors");
  }
  
  public Object dot(Vector paramVector1, Vector paramVector2) throws ParseException {
    if (paramVector1.size() != paramVector2.size())
      throw new ParseException("Dot: both sides of dot must be same length"); 
    int i = paramVector1.size();
    if (i < 1)
      throw new ParseException("Dot: empty vectors parsed"); 
    Object object = mul.mul(paramVector1.elementAt(0), paramVector2.elementAt(0));
    for (byte b = 1; b < i; b++)
      object = add.add(object, mul.mul(paramVector1.elementAt(b), paramVector2.elementAt(b))); 
    return object;
  }
}
