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

org.tensorics.incubate.function.Functions 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
// @formatter:off
 /*******************************************************************************
 *
 * This file is part of tensorics.
 * 
 * Copyright (c) 2008-2011, CERN. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 ******************************************************************************/
// @formatter:on

package org.tensorics.incubate.function;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Provides utility methods which deal with functions.
 * 

* * @author agorzaws */ public final class Functions { private static final double ZERO = 0.0; private Functions() { /* only static methods */ } public static DiscreteFunction convertToDiscreteFunctionFrom(double[] times, double[] values) { double[] errors = new double[values.length]; for (int i = 0; i < values.length; i++) { errors[i] = ZERO; } return convertToDiscreteFunctionFrom(times, values, errors); } public static List getXsIntersection(DiscreteFunction first, DiscreteFunction second) { List firstTs = first.getXs(); List secondTs = second.getXs(); List tsToReturn = new ArrayList(); if (firstTs.size() == 0 || secondTs.size() == 0) { return tsToReturn; } double upperLimit = Math.min(firstTs.get(firstTs.size() - 1), secondTs.get(secondTs.size() - 1)); double lowerLimit = Math.max(firstTs.get(0), secondTs.get(0)); for (Double one : firstTs) { if (one > lowerLimit && one < upperLimit) tsToReturn.add(one); } return tsToReturn; } @SuppressWarnings("boxing") public static DiscreteFunction convertToDiscreteFunctionFrom(double[] times, double[] values, double[] errors) { if (times.length != values.length && values.length != errors.length) { throw new IllegalArgumentException("Cannot create discrete function from two diffrent size arrays"); } DiscreteFunctionBuilder builder = SortedMapBackedDiscreteFunction.builder(); for (int i = 0; i < times.length; i++) { builder.put(times[i], values[i], errors[i]); } return builder.withName("NoName").build(); } public static DiscreteFunction convertToDiscreteFunctionFrom(Map map, String name) { DiscreteFunctionBuilder builder = SortedMapBackedDiscreteFunction.builder(); for (Entry one : map.entrySet()) { builder.put(one.getKey(), one.getValue(), ZERO); } return builder.withInterpolationStrategy(new LinearInterpolationStrategy()).withName(name).build(); } public static DiscreteFunction emptyDiscreteFunction() { DiscreteFunctionBuilder builder = SortedMapBackedDiscreteFunction.builder(); return builder.withName("EMPTY").build(); } public static DiscreteFunction createInterpolatedFunctionFromTwoPoints(double startPoint, double endPoint, double length) { DiscreteFunctionBuilder builder = SortedMapBackedDiscreteFunction.builder(); builder.put(0.0, startPoint, ZERO); builder.put(length, endPoint, ZERO); return builder.withName("INTERPOLATED_TWO_POINTS").withInterpolationStrategy(new LinearInterpolationStrategy()) .build(); } /** * Multiplies the Y values of the given function with the value {@code scale}. * * @param function the function to scale * @param scale the factor, by which to multiply the function Y values * @return the scaled function */ public static DiscreteFunction // scaleFunction(DiscreteFunction function, double scale) { DiscreteFunctionBuilder builder = SortedMapBackedDiscreteFunction.builder(); builder.withName(function.getName()); for (Double oneTime : function.getXs()) { builder.put(oneTime, function.getY(oneTime) * scale); } return builder.build(); } /** * Performs numerical integral of given function within full range of given function. Simplest method of sum of * dt*Y. * * @param function the function to treat * @return the value of the numerical integral */ public static double integrate(DiscreteFunction function) { List xValues = function.getXs(); double firstTime = xValues.get(0); double endTime = xValues.get(xValues.size() - 1); return integrate(function, firstTime, endTime); } /** * performs numerical integral of given function within start and end values. Simplest method of sum of dt*Y. * * @param function the function to integrate * @param start the lower border of the integration * @param end the upper border of the integration * @return the value calculated from the integration */ public static double integrate(DiscreteFunction function, double start, double end) { double sum = 0.0; List xValues = function.getXs(); double lastT = start; for (Double t : xValues) { if (t >= start && t <= end) { sum += function.getY(t) * (t - lastT); lastT = t; } } return sum; } /** * Allows to get cut first part of ig exp decay if above limit. * * @param function function to treat * @param yLimit UPPER limit, above which original values are omitted * @return the cut function */ public static DiscreteFunction cutBeginingOfExp(DiscreteFunction function, double yLimit) { DiscreteFunctionBuilder builder = SortedMapBackedDiscreteFunction.builder(); for (Double x : function.getXs()) { if (function.getY(x) <= yLimit) { builder.put(x, function.getY(x)); } } return builder.build(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy