package com.opticron.mathexpr;

import java.util.HashMap;
/** Class that represents a negation operation (-x) */
public class MONegation implements IMathObject {
	IMathObject rhs;
	/** Constructor that takes the equation input string but does not actually modify it
	 * @param equationString The input string that holds the equation */
    MONegation (RefString input) {
            parse(input);
    }
    /** Generate a string representing this node. */
    @Override
	public String toString() {
            return "-"+rhs.toString();
    }
    /** Evaluate this node and any subnodes given a set of variables and values */
    public Double evaluate(HashMap<String,Double>vars) {
            return -rhs.evaluate(vars);
    }
    /** This function parses a set of nodes out of an equation string. */
    public void parse(RefString source) {
            // this does nothing on purpose, it will not be used by default
    }
    /** Return the simplification of this node (where possible). */
    public IMathObject simplify(HashMap<String,Double>vars) {
    		try {
            	return new MONumber(evaluate(vars));
    		} catch (MathParseError e) {}
            // it only makes sense to pass the simplification up the tree here since negation is so simple
            rhs = rhs.simplify(vars);
            return this;
    }
}
