/// Evolver Copyright 2000, Morgan McGuire
/// matrix@alum.mit.edu   http://graphics3d.com/matrix
public final class Average extends MultiArgumentOperator implements ELOperator
{
  public void apply(ELMatrix argument[], ELMatrix out)
  {
    checkArguments(argument);

    int numArguments = argument.length;
    
    out.set(argument[0]);
    for(int a = 1; a < numArguments; a++) {
      combine(out, argument[a]);
    }
    
    // Scale
    float f = 1.0f / numArguments;
    for(int c = 0; c < 3; c++) {
      float outChannel[] = out.channel[c];
      for(int i = out.numElements - 1; i >= 0; i--) {
        outChannel[i] *= f;
      }
    }
  }

  protected float combine(float a, float b)
  {
    // Note that this will be normalized in apply by dividing by the
    // number of arguments.
    return a + b;
  }

  public String name()
  {
    return "Average";
  }

  public String description()
  {
    return "Averages all inputs.";
  }  

  protected String iconString()
  {
    return "Avg";
  }
}
