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

io.engineblock.extensions.optimizers.MultivariateArrayScript Maven / Gradle / Ivy

There is a newer version: 2.12.65
Show newest version
package io.engineblock.extensions.optimizers;

import jdk.nashorn.api.scripting.ScriptObjectMirror;
import org.apache.commons.math3.analysis.MultivariateFunction;
import org.slf4j.Logger;

import java.security.InvalidParameterException;
import java.util.HashMap;
import java.util.Map;

public class MultivariateArrayScript implements MultivariateFunction {
    private final ScriptObjectMirror script;
    private final MVParams params;
    private Logger logger;

    public MultivariateArrayScript(Logger logger, MVParams params, ScriptObjectMirror script) {
        this.logger = logger;
        this.script = script;
        this.params = params;
    }

    @Override
    public double value(double[] doubles) {
        if (doubles.length != params.size()) {
            throw new InvalidParameterException("Expected " + params.size() + " doubles, not " + doubles.length);
        }

        Map map = new HashMap<>();

        for (int i = 0; i < doubles.length; i++) {
            map.put(i,doubles[i]);
        }

        Object result = null;
        result = this.script.call(script,doubles);
        return Double.valueOf(result.toString());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy