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

lphy.base.function.ARange Maven / Gradle / Ivy

Go to download

The standard library of LPhy, which contains the required generative distributions and basic functions.

The newest version!
package lphy.base.function;

import lphy.core.model.DeterministicFunction;
import lphy.core.model.Value;
import lphy.core.model.annotation.GeneratorInfo;
import lphy.core.model.annotation.ParameterInfo;

public class ARange extends DeterministicFunction {

    String startParamName;
    String stopParamName;
    String stepParamName;

    public ARange(@ParameterInfo(name="start", description ="value of the range to start at (inclusive)") Value start,
                  @ParameterInfo(name="stop", description ="value of the range to stop at (inclusive)") Value stop,
                  @ParameterInfo(name="step", description ="the step size") Value step) {

        startParamName = getParamName(0);
        stopParamName = getParamName(1);
        stepParamName = getParamName(2);
        setParam(startParamName, start);
        setParam(stopParamName, stop);
        setParam(startParamName, step);
    }

    @Override
    @GeneratorInfo(name = "arange", description = "A function to produce an equally space array of doubles. Takes a start value, and stop value and a step value and returns a double array value.")
    public Value apply() {

        double s = start().value();
        double e = stop().value();
        double t = step().value();

        int len = (int)Math.floor((e-s)/t)+1;

        Double[] sequence = new Double[len];

        int c = 0;
        for (double i = s; i <= e; i += t) {
            sequence[c] = i;
            c += 1;
        }
        return new Value<>(null, sequence, this);
    }

    public Value start() {
        return (Value)paramMap.get(startParamName);
    }
    public Value stop() {
        return (Value) paramMap.get(stopParamName);
    }
    public Value step() {
        return (Value) paramMap.get(stepParamName);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy