Example usage for org.apache.commons.math3.analysis UnivariateFunction value

List of usage examples for org.apache.commons.math3.analysis UnivariateFunction value

Introduction

In this page you can find the example usage for org.apache.commons.math3.analysis UnivariateFunction value.

Prototype

double value(double x);

Source Link

Document

Compute the value of the function.

Usage

From source file:org.asoem.greyfish.utils.math.PeriodicFunctionsTest.java

@Test
public void testSawtoothWave() throws Exception {
    final UnivariateFunction wave = PeriodicFunctions.sawtoothWave(1.0, 0.0);

    final List<Double> generated = Lists.newArrayList();
    BigDecimal x = BigDecimal.valueOf(-1.0);
    while (x.doubleValue() <= 1.0) {
        final double y = wave.value(x.doubleValue());
        generated.add(y);/*w  ww  . j  av  a  2 s  . c o m*/
        x = x.add(BigDecimal.valueOf(0.1));
    }

    System.out.print(Joiner.on("\n").join(generated));

    final ImmutableList<Double> expected = ImmutableList.of(0.0, 0.2, 0.4, 0.6, 0.8, -1.0, -0.8, -0.6, -0.4,
            -0.2, 0.0, 0.2, 0.4, 0.6, 0.8, -1.0, -0.8, -0.6, -0.4, -0.2, 0.0);
    assertThat(generated, hasSize(expected.size()));
    for (int i = 0; i < generated.size(); i++) {
        assertThat(generated.get(i), is(closeTo(expected.get(i), 0.000000000000001)));
    }
}

From source file:org.asoem.greyfish.utils.math.PeriodicFunctionsTest.java

@Test
public void testSquareWave() throws Exception {
    final UnivariateFunction wave = PeriodicFunctions.squareWave(1.0, 0.0);

    final List<Double> generated = Lists.newArrayList();
    BigDecimal x = BigDecimal.valueOf(-1.0);
    while (x.doubleValue() <= 1.0) {
        final double y = wave.value(x.doubleValue());
        generated.add(y);//w  w  w. j a  va  2 s  . c  o  m
        x = x.add(BigDecimal.valueOf(0.1));
    }

    System.out.print(Joiner.on("\n").join(generated));

    final ImmutableList<Double> expected = ImmutableList.of(1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0,
            -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0);
    assertThat(generated, hasSize(expected.size()));
    for (int i = 0; i < generated.size(); i++) {
        assertThat(generated.get(i), is(closeTo(expected.get(i), 0.000000000000001)));
    }
}

From source file:org.grouplens.samantha.modeler.featurizer.LogarithmicExtractor.java

public Map<String, List<Feature>> extract(JsonNode entity, boolean update, IndexSpace indexSpace) {
    Map<String, List<Feature>> feaMap = new HashMap<>();
    if (entity.has(attrName)) {
        List<Feature> feaList = new ArrayList<>();
        double val = entity.get(attrName).asDouble();
        UnivariateFunction log10 = new Log10();
        FeatureExtractorUtilities.getOrSetIndexSpaceToFeaturize(feaList, update, indexSpace, indexName,
                attrName, log10.value(val + 1.0));
        feaMap.put(feaName, feaList);//from w ww  .j a v a2s.  co m
    }
    return feaMap;
}

From source file:org.grouplens.samantha.modeler.featurizer.MultiplicativeInteractionExtractor.java

public Map<String, List<Feature>> extract(JsonNode entity, boolean update, IndexSpace indexSpace) {
    Map<String, List<Feature>> feaMap = new HashMap<>();
    double product = 1.0;
    boolean complete = true;
    UnivariateFunction sig = new SelfPlusOneRatioFunction();
    for (String attrName : attrNames) {
        if (entity.has(attrName)) {
            double val = entity.get(attrName).asDouble();
            if (sigmoid) {
                val = sig.value(val);
            }// w  w w.java 2 s.  co  m
            product *= val;
        } else {
            complete = false;
        }
    }
    if (complete) {
        List<Feature> feaList = new ArrayList<>();
        String key = FeatureExtractorUtilities.composeKey(attrNames);
        FeatureExtractorUtilities.getOrSetIndexSpaceToFeaturize(feaList, update, indexSpace, indexName, key,
                product);
        feaMap.put(feaName, feaList);
    }
    return feaMap;
}

From source file:org.grouplens.samantha.modeler.featurizer.OuterProductExtractor.java

public Map<String, List<Feature>> extract(JsonNode entity, boolean update, IndexSpace indexSpace) {
    Map<String, List<Feature>> feaMap = new HashMap<>();
    List<Feature> feaList = new ArrayList<>();
    for (String leftName : attrNames) {
        for (String rightName : attrNames) {
            List<String> keyNames = Lists.newArrayList(leftName, rightName);
            String key = FeatureExtractorUtilities.composeKey(keyNames);
            double product;
            if (sigmoid) {
                UnivariateFunction func = new SelfPlusOneRatioFunction();
                product = func.value(entity.get(leftName).asDouble())
                        * func.value(entity.get(rightName).asDouble());
            } else {
                product = entity.get(leftName).asDouble() * entity.get(rightName).asDouble();
            }/*w ww .jav  a2  s  .c  o m*/
            FeatureExtractorUtilities.getOrSetIndexSpaceToFeaturize(feaList, update, indexSpace, indexName, key,
                    product);
            feaMap.put(feaName, feaList);
        }
    }
    return feaMap;
}

From source file:org.grouplens.samantha.modeler.featurizer.PairwiseInteractionExtractor.java

public Map<String, List<Feature>> extract(JsonNode entity, boolean update, IndexSpace indexSpace) {
    Map<String, List<Feature>> feaMap = new HashMap<>();
    UnivariateFunction sig = new SelfPlusOneRatioFunction();
    for (int i = 0; i < attrNames.size(); i++) {
        String attrNameLeft = attrNames.get(i);
        for (int j = i + 1; j < attrNames.size(); j++) {
            String attrNameRight = attrNames.get(j);
            if (entity.has(attrNameLeft) && entity.has(attrNameRight)) {
                double valLeft = entity.get(attrNameLeft).asDouble();
                double valRight = entity.get(attrNameRight).asDouble();
                if (sigmoid) {
                    valLeft = sig.value(valLeft);
                    valRight = sig.value(valRight);
                }//from  w ww.jav a2  s .c  o m
                double value = valLeft * valRight;
                List<Feature> feaList = new ArrayList<>();
                String key = FeatureExtractorUtilities.composeKey(attrNameLeft, attrNameRight);
                FeatureExtractorUtilities.getOrSetIndexSpaceToFeaturize(feaList, update, indexSpace, indexName,
                        key, value);
                feaMap.put(attrNameLeft + ":" + attrNameRight, feaList);
            }
        }
    }
    return feaMap;
}

From source file:org.libreplan.business.planner.entities.StretchesFunctionTypeEnum.java

private static int evaluate(UnivariateFunction accumulatedFunction, int x) {
    return (int) accumulatedFunction.value(x);
}

From source file:org.lightjason.agentspeak.action.buildin.math.interpolate.CInterpolate.java

@Override
public final IFuzzyValue<Boolean> execute(final IContext p_context, final boolean p_parallel,
        final List<ITerm> p_argument, final List<ITerm> p_return, final List<ITerm> p_annotation) {
    final UnivariateFunction l_function = p_argument.get(0).raw();

    CCommon.flatcollection(p_argument.subList(1, p_argument.size())).stream()
            .mapToDouble(i -> i.<Number>raw().doubleValue()).mapToObj(i -> CRawTerm.from(l_function.value(i)))
            .forEach(p_return::add);

    return CFuzzyValue.from(true);
}

From source file:org.meteoinfo.math.interpolate.InterpUtil.java

/**
 * Compute the value of the function//from   w  ww  .  j av  a  2s. c o  m
 * @param func The function
 * @param x Input data
 * @return Function value
 */
public static Array evaluate(UnivariateFunction func, Array x) {
    Array r = Array.factory(DataType.DOUBLE, x.getShape());
    for (int i = 0; i < r.getSize(); i++)
        r.setDouble(i, func.value(x.getDouble(i)));

    return r;
}

From source file:org.meteoinfo.math.interpolate.InterpUtil.java

/**
 * Compute the value of the function/*from w  ww .j  a  v a2s  . c  om*/
 * @param func The function
 * @param x Input data
 * @return Function value
 */
public static double evaluate(UnivariateFunction func, Number x) {
    return func.value(x.doubleValue());
}