Nary Function return Vector

35 views
Skip to first unread message

Simone Di Nobile

unread,
Jun 15, 2018, 4:07:05 AM6/15/18
to Jep Java Users
I need to create a function that have input array and return the array modified. I need that all data will be processed by this function in a single time, functions like an FFT, ButterwothFIlter, executed by Compiled Matlab code. 

import com.singularsys.jep.EvaluationException;
import com.singularsys.jep.Jep;
import com.singularsys.jep.JepException;
import com.singularsys.jep.configurableparser.StandardConfigurableParser;
import com.singularsys.jep.functions.NaryFunction;
import java.util.Arrays;
import java.util.Vector;

public class FormulaArray extends NaryFunction {

    public static void main(String[] args) {
        Jep jep = new Jep();

        jep.addStandardConstants();
        jep.setImplicitMul(true);
        jep.setAllowUndeclared(false);
        jep.addFunction("func", new FormulaArray());

        Double[] vals = new Double[]{1., 2., 3., 4.};
        try {
            jep.addVariable("P0001", vals);
            jep.parse("func(P0001)+5");
            Object res = jep.evaluate();
        } catch (JepException ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }

    @Override
    public Object eval(Object[] args) throws EvaluationException {
        Double[] val = (Double[]) args[0];
        for (int i = 0; i < val.length; i++) {
            val[i] = val[i] * 2;
            System.out.println(val[i]);
        }

        Vector res = new Vector<Object>(Arrays.asList(val));

        return res;
    }
}


I get this error.

com.singularsys.jep.functions.IllegalParameterException: +: lllegal parameter [2.0, 4.0, 6.0, 8.0](Vector)
at com.singularsys.jep.functions.Add.add(Unknown Source)
at com.singularsys.jep.functions.A

Richard Morris

unread,
Jun 15, 2018, 4:36:25 AM6/15/18
to Jep Java Users
The current Add function can only do the following options:
       Number + Number
       String + String
       Vector<?> + Vector<?>

It can't add Vector<?> and a Double which looks like the calculation of "func(P0001)+5" would be. 
And it cant work with Double arrays.

The workaround would be to subclass the Add function with a method like

public class MyAdd extends Add {

    @Override
    public Object add(Object l, Object r) throws Exception {
        if(l instanceof Vector<?> && r instnceof Double) {
            .....
        } else if (l instanceof Double[] && r instanceof Double[]) {
            .....
        } else {
           return super.add(l,r);
        }
    }
}

and then change the PFMC for the add operator
 
   jep.getOperatorTable().getAdd().setPFMC(new myAdd());



work with vectors of the form Vector<?>. It does not have the code to work with a double array 
Message has been deleted

Simone Di Nobile

unread,
Jun 15, 2018, 5:41:02 AM6/15/18
to Jep Java Users
Thank you!
Reply all
Reply to author
Forward
0 new messages