lphy.base.function.Sum Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lphy-base Show documentation
Show all versions of lphy-base Show documentation
The standard library of LPhy, which contains the required generative distributions and basic functions.
The newest version!
package lphy.base.function;
import lphy.base.ParameterNames;
import lphy.core.model.DeterministicFunction;
import lphy.core.model.Value;
import lphy.core.model.annotation.GeneratorInfo;
import lphy.core.model.annotation.ParameterInfo;
import lphy.core.model.datatype.NumberValue;
public class Sum extends DeterministicFunction {
public Sum(@ParameterInfo(name = ParameterNames.ArrayParamName, description = "the array to sum the elements of.")
Value x) {
setParam(ParameterNames.ArrayParamName, x);
}
@GeneratorInfo(name = "sum", description = "The sum of the elements of the given array")
public Value apply() {
Number[] x = (Number[])getParams().get(ParameterNames.ArrayParamName).value();
double sum = 0.0;
for (int i = 0; i < x.length; i++ ) {
sum += x[i].doubleValue();
}
if(x.length > 0 && x[0] instanceof Integer) {
return new NumberValue<>(null, (int) sum, this);
}
return new NumberValue<>(null, sum, this);
}
}