Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package jason.functions;
 

Function: math.std_dev(L): returns the standard deviation of all values of L.

Examples:

  • math.std_dev([1,2,3]): returns 1.
  • math.std_dev([]): returns 0.

Author(s):
Francisco Grimaldo
See also:
Min
Max
Sum
Average
public class StdDev extends DefaultArithFunction  {
    public String getName() {
        return "math.std_dev";
    }
    
    @Override
    public double evaluate(TransitionSystem tsTerm[] argsthrows Exception {
        if (args[0].isList()) {
            double sum = 0, squareSum = 0, num;
            int    n = 0;
            for (Term t: (ListTerm)args[0])
                if (t.isNumeric()) {
                    if (t.isNumeric()) {
                        num = ((NumberTerm)t).solve();
                        sum += num;
                        n++;
                    }
                }
            double mean = sum  / n;
            for (Term t: (ListTerm)args[0])
                if (t.isNumeric()) {
                    num = ((NumberTerm)t).solve();
                    squareSum += (num - mean) * (num - mean);
                }
            return Math.sqrtsquareSum/(n - 1) );
        }
        throw new JasonException(getName()+" is not implemented for type '"+args[0]+"'.");
    }
    @Override
    public boolean checkArity(int a) {
        return a == 1;
    }
New to GrepCode? Check out our FAQ X