/// Evolver Copyright 2000, Morgan McGuire
/// matrix@alum.mit.edu   http://graphics3d.com/matrix
/**
 * A scalar expression.
 *
 * ELScalar := -1 <= x e Reals <= 1
 */
public class ELScalar extends AbstractELExpression implements ELExpression
{
  float value;
  
  public ELScalar()
  {
    value = 0.0f;
  }

  public ELScalar(double value)
  {
    this.value = (float)Math.max(-1.0, Math.min(1.0, value));
  }
  
  public ELExpression geneticClone(ELCall parent, GeneticParameters gp)
  {

    if (gp.mutationOccurs(parent)) {
      while (true) {

        int mutation = gp.randomMutationType();
        switch (mutation) {
        case GeneticParameters.ENCLOSE_IN_NEW_CALL_MUTATION:
          return encloseInNewCallMutation(gp);
        
        case GeneticParameters.NEW_EXPRESSION_MUTATION:
          return gp.randomExpression();
        
        case GeneticParameters.CLONE_SIBLING_MUTATION:
          if (parent != null) {
            return cloneSiblingMutation(parent, gp);
          } else {
            // Not appropriate; no siblings.
            break;
          }
        
        case GeneticParameters.SCALAR_CONSTANT_MUTATION:
          double r = Math.random() * 2.0 - 1.0;
          return new ELScalar(value + r * gp.scalarDisturbance);
          
        default:
          // not an appropriate mutation; keep cycling.
        }
      }      
    } else {
      return cloneWithChildMutations(gp);
    }
  }

  public Object clone()
  {
    return new ELScalar(value);
  }
  
  public String unparse()
  {
    return "" + value;
  }

  public String toString()
  {
    return "ELScalar(" + value + ")";
  }

  public void evaluate(ELMatrix out, ELEnvironment environment, ParameterList pl)
  {
    out.setAll(value, value, value);
  }
}

