org.tensorics.core.iterable.operations.IterableStd Maven / Gradle / Ivy
Show all versions of tensorics-core Show documentation
/**
* Copyright (c) 2016 European Organisation for Nuclear Research (CERN), All Rights Reserved.
*/
package org.tensorics.core.iterable.operations;
import static com.google.common.collect.Iterables.isEmpty;
import org.tensorics.core.math.ExtendedField;
import org.tensorics.core.scalar.lang.ScalarSupport;
/**
* An operation that takes and iterable of a certain type of values (for which a field has to be provided) and
* calculates the standard deviation out of it.
*
* For the definition of the standard deviation, have a look at
* wikipedia.
*
* @author caguiler
* @param the type of the scalars (elements of the field on which the standard deviation will be based)
*/
public class IterableStd extends ScalarSupport implements IterableOperation {
private final IterableVar iterableVar;
private final ExtendedField field;
public IterableStd(ExtendedField field) {
super(field);
this.field = field;
this.iterableVar = new IterableVar<>(field);
}
@Override
public V apply(Iterable values) {
if (isEmpty(values)) {
throw new IllegalArgumentException("standard deviation of empty value set is not possible.");
}
return calculate(iterableVar.apply(values)).root(field.two());
}
}