All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tensorics.core.iterable.operations.IterableVar Maven / Gradle / Ivy

Go to download

Tensorics is a java framework which uses a tensor as a central object. A tensor represents a set of values placed in an N-dimensional space. Wherever you are tempted to use maps of maps, a tensor might be a good choice ;-) Tensorics provides methods to create, transform and performing calculations with those tensors.

There is a newer version: 0.0.81
Show newest version
/**
 * 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 java.util.stream.Collectors;
import java.util.stream.StreamSupport;

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 variance out of it.
 * 

* For the definition of the variance, have a look at wikipedia. * * @author caguiler * @param the type of the scalars (elements of the field on which the variance will be based) */ public class IterableVar extends ScalarSupport implements IterableOperation { private final IterableAverage iterableAverage; private final ExtendedField field; public IterableVar(ExtendedField field) { super(field); this.field = field; this.iterableAverage = new IterableAverage<>(field); } @Override public V apply(Iterable values) { if (isEmpty(values)) { throw new IllegalArgumentException("variance of empty value set is not possible."); } final V average = iterableAverage.apply(values); // @formatter:off Iterable squaredDifferences = StreamSupport.stream(values.spliterator(), false) .map(v -> calculate(v).minus(average)) .map(difference -> calculate(difference).toThePowerOf(field.two())) .collect(Collectors.toList()); // @formatter:on return iterableAverage.apply(squaredDifferences); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy