How to disable support of complex arithmetics?

16 views
Skip to first unread message

gattin...@googlemail.com

unread,
Nov 14, 2017, 11:01:45 AM11/14/17
to Jep Java Users
Hi,

is it possible to disable support of complex arithmetics so that evaluation of sqrt(-1) returns NaN instead of an instance of type com.singularsys.jep.standard.Complex with a value of (0.0, 1.0)?

Kind regards,
Marcus

gattin...@googlemail.com

unread,
Nov 14, 2017, 11:45:53 AM11/14/17
to Jep Java Users
BTW: I do not want to replace the function sqrt(). I'm searching for a solution to intercept the result of an evaluation, test it if it is a complex number and throw an EvaluationException in this case.

Richard Morris

unread,
Nov 14, 2017, 11:59:55 AM11/14/17
to Jep Java Users
Yes there is a facility for this. Many of the PFMC functions have a strict version. 

To remove all trace of complex numbers use

 Jep jep = new Jep();
 jep
.getVariableTable().remove("i");
 
FunctionTable ft = jep.getFunctionTable();
 ft
.addFunction("sqrt", new SquareRoot(true));
 ft
.addFunction("log",   new Logarithm(true));
 ft
.addFunction("ln",    new StrictNaturalLogarithm());
 ft
.addFunction("lg",    new LogBase2(true));
 ft
.addFunction("pow",   new Power(true));

 ft
.addFunction("asin",  new ArcSine(true));
 ft
.addFunction("acos",  new ArcCosine(true));
 ft
.addFunction("acosh", new ArcCosineH(true));
 ft
.addFunction("atanh", new ArcTanH(true));
       
 ft
.remove("re");
 ft
.remove("im");
 ft
.remove("arg");
 ft
.remove("cmod");
 ft
.remove("complex");
 ft
.remove("polar");
 ft
.remove("conj");
       
 
jep.getOperatorTable().getPower().setPFMC(new Power(true));


1

Richard Morris

unread,
Nov 14, 2017, 12:01:12 PM11/14/17
to Jep Java Users
Just read your followup. 

Yes there is a way. I'll get back to you in a bit but I have to dash.

Richard

gattin...@googlemail.com

unread,
Nov 14, 2017, 12:51:37 PM11/14/17
to Jep Java Users
Hi Richard, good to know. Thanks.

I solved it now by means of a custom evaluator I pass to the Jep constructor (as an additional component):

package de.onesense.services.formula;

import com.singularsys.jep.EvaluationException;
import com.singularsys.jep.parser.Node;
import com.singularsys.jep.standard.Complex;
import com.singularsys.jep.standard.FastEvaluator;

/**
* A custom evaluator that is used to
* <ul>
* <li>disable support of infinity numbers</li>
* <li>disable support of NaN numbers</li>
* <li>disable support of complex numbers</li>
* </ul>
*/
public class RestrictedEvaluator extends FastEvaluator {
/**
* Creates a new instance of the restricted evaluator.
*/
public RestrictedEvaluator() {
super();
setTrapInfinity(true);
setTrapNaN(true);
}

@Override
public Object eval(final Node node) throws EvaluationException {
return assertNonComplexNumber(super.eval(node));
}

@Override
public Object evaluate(final Node node) throws EvaluationException {
return assertNonComplexNumber(super.evaluate(node));
}

/**
* Ensures the given result is not a complex number.
*
* @param result The result to check.
*
* @return The checked result.
*
* @throws EvaluationException The result is a complex number.
*/
private Object assertNonComplexNumber(final Object result) throws EvaluationException {
if (result instanceof Complex) {
throw new EvaluationException("EvaluationException: Complex number detected");
}
return result;
}
}

Do you recomment this solution, too, or are there any drawbacks?

Regards,
Marcus

Richard Morris

unread,
Nov 14, 2017, 2:14:12 PM11/14/17
to Jep Java Users
Yes that's the way I would do it.

You could just set the functions to return NaN and use the trapNaN option on the evaluator. But you method might have a better degree of error catching.

Rich


On Tuesday, 14 November 2017 16:01:45 UTC, Marcus Gattinger wrote:
Reply all
Reply to author
Forward
0 new messages