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

org.tensorics.core.reduction.AbstractInterpolationStrategy 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) 2015 European Organisation for Nuclear Research (CERN), All Rights Reserved.
 */

package org.tensorics.core.reduction;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.tensorics.core.tensor.Tensor;

/**
 * An abstract interpolation implementation that provides the basic functions like sorting the {@link Comparable}
 * coordinate and finding the PREVIOUS/NEXT instances in the coordinates.
 * 
 * @author agorzaws
 * @param  type of the coordinate, must be {@link Comparable}
 * @param  type of the value in the {@link Tensor}
 */
public abstract class AbstractInterpolationStrategy, V> implements InterpolationStrategy {

    /**
     * Extracts the ordered list of the comparable coordinate along which the interpolation will be done
     * 
     * @param tensorWithTheOnlyOneCoordinateOfC tensor with only ONE coordinate
     * @param coordineteToInterpolate the coordinate to extract
     * @return an ordered (ascending) list of the comparable coordinates
     */
    protected List getOrderedListOfComparableCoodrinate(Tensor tensorWithTheOnlyOneCoordinateOfC,
            C coordineteToInterpolate) {

        checkIfOnlyCoordinatesOfTypeCAreIn(tensorWithTheOnlyOneCoordinateOfC, coordineteToInterpolate);

        /* we know that slice is the class of C */
        @SuppressWarnings("unchecked")
        Set coordinatesOfType = (Set) tensorWithTheOnlyOneCoordinateOfC.shape().coordinatesOfType(
                coordineteToInterpolate.getClass());

        List orderedList = new ArrayList(coordinatesOfType);
        Collections.sort(orderedList);
        return orderedList;
    }

    private void checkIfOnlyCoordinatesOfTypeCAreIn(Tensor tensor, C coordineteToInterpolate) {

        boolean contains = tensor.shape().dimensionSet().contains(coordineteToInterpolate.getClass());
        int dimensionality = tensor.shape().dimensionality();
        int entries = tensor.shape().size();
        if (dimensionality != 1 && entries > 0 && contains) {
            Set coordinates = tensor.shape().positionSet().iterator().next().coordinates();
            // TODO how to check if the only coordinate in the position is the class of C ?
            throw new IllegalStateException("Cannot perform interpolation in the tensor of more that 1 dimension "
                    + "or the given dimension is not of the correct class.");
        }
    }

    /**
     * Finds the comparable coordinate before of after the slice position.
     * 
     * @param orderedList an ordered list of coordinates
     * @param referencePosition
     * @param indexMove if 0 then the PREVIOUS will be returned, if +1 the NEXT will be returned.
     * @return the comparable coordinate BEFORE or AFTER the reference one.
     */
    protected C findIndex(List orderedList, C referencePosition, int indexMove) {
        if (indexMove != 0 && indexMove != 1) {
            throw new IllegalArgumentException("Only 0 and +1 are allowed as indexMove parameters");
        }
        int indexToReturn = -1;
        for (C one : orderedList) {
            if (one.compareTo(referencePosition) < 0) {
                indexToReturn++;
            } else {
                break;
            }
        }

        if (indexToReturn < 0) {
            indexToReturn = 0;
        } else if (indexToReturn > orderedList.size() - 2) {
            indexToReturn = orderedList.size() - 2;
        }
        return orderedList.get(indexToReturn + indexMove);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy