First of all, credits for Jep, it is almost what I’m looking for.
Jep.Net offers support for arrays, but I can’t find something similar in the Jep Java documentation.
I want to work with custom functions on an array and the result should be also an array. An example:
Thank you very much for the detailed answer, this helped me tremendously with my use case and I will buy JEP.
Is it also possible to customize the other operators to work with BigDecimal Lists? To be precise comparative operators (=, != <, <=, >, >=), boolean operators (&&, ||) and if.
a)
The goal is to have for each value in the result list a Boolean (or 0, 1 would also be possible). I tried to
customize BigDecRelational (http://www.singularsys.com/jep/doc/javadoc/com/singularsys/jep/bigdecimal/functions/BigDecRelational.html)
like the post above.
public class MyBigDecRelational extends BigDecRelational{
private static final long serialVersionUID = 1L;
public JepCustomBigRelationalOfficial(int id) {
super(id);
}
@Override
public Object compare(Object l, Object r) throws EvaluationException {
if(l instanceof List<?> && r instanceof List<?>) {
return compareList((List<?>) l, (List<?>) r);
}
return super.compare(l, r);
}
public Object compareList(List<?> v1, List<?> v2) throws EvaluationException {
if(v1.size() != v2.size())
throw new EvaluationException(MessageFormat.format(JepMessages.getString("functions.DimensionsOfVectorsDoNotMatch"),getName(),v1.size(),v2.size())); //$NON-NLS-1$
List<Boolean> result = new ArrayList<>(v1.size());
for(int i = 0; i < v1.size(); ++i) {
result.add(i, (Boolean) this.compare(v1.get(i), v2.get(i)));
}
return result;
}
}
The error is “The return type is incompatible with BigDecRelational.compare(Object, Object)”. This should be easy fixable by buying the source code and change the return type in BigDecRelational to Object.
b)
What is with if?
The simplest solution would be, to create an custom if-function, which evaluates the expression for each value in the list once.
But then nesting of if’s won’t work:
Is there any chance to customize jep to this extend? (If possible, still with LazyEvaluation)
Thanks for
the comparative operator.
What worries me is the if-function. Apparently, my example was poor.
jep.addVariable("A",
Arrays.asList(new BigDecimal("1"), new BigDecimal("1"), new
BigDecimal("2")));
jep.addVariable("B",
Arrays.asList(new BigDecimal("2"), new BigDecimal("1"), new
BigDecimal("1")));
jep.parse("
if(A <
B, 10, if(A == B, 20, 30))
");
Object
result = jep.evaluate();
> This
function performs lazy evaluation so that only posExpr or negExpr will be
evaluated.
http://www.singularsys.com/jep/doc/javadoc/com/singularsys/jep/functions/If.html
My thought
process how the custom if can work.
Simple
example: if(A < B, 10, 20)
Step one:
It evaluates A < B
Result in
my case: [true, false, false]
Step two:
0: Evaluate
posExpr and add 10 result to array
1: Evaluate
negExpr and add 20 result to array
2: Evaluate
negExpr and add 20 result to array
Seems fine, but the trouble starts on nesting:
if(A <
B, 10, if(A == B, 20, 30))
Step one:
It evaluates A < B
Result in
my example: [true, false, false]
Step two:
0: Evaluate
posExpr and add 10 to array
1: Evaluate
negExpr and evaluate second if. Evaluates A == B, goes through the whole array
of A and B, even though I only need the [1] result. Returns [30, 20, 30]. First
if call has somehow access now [1] element.
2: Evaluate
negExpr and evaluate second if. Evaluates A == B, goes through the whole array
of A and B, even though I only need the [2] result. Returns [30, 20, 30]. First
if call has somehow access now [2] element.
End result:
[10, 20, 30]
Nesting with those redundant calculations would blow up the performance on large arrays. O(N^2) --> very bad
a) Is it possible to customize if for Arrays?
b) Is it somehow
possible, to only calculate the required operations? Maybe that the first if
call will evaluate its own child expressions only with the
needed
value on the
current index.
E.g. in index 1: Sets tempA = 1, tempB = 1
tempA == tempB => ture
Returns 20 for [1]
I assume you are probably busy right now. If my question is unclear please let me know.
If it can only be implemented with a huge amount of work, it still would be interesting to know that it is generally achievable.
Just a quick reminder, I am still very interested in an if-function
without lazy evaluation.
I am not able to do my own implementation with the style for PostfixMathCommands, because the if-function is not a normal BinaryFunction like Comparative.
> For what
its worth, there is a four argument version of if where you can do
> if( x - y , -1, 1, 0)
And just to make sure there isn’t a misunderstanding. I choosed the example
“if(A <
B, 10, if(A == B, 20, 30))”
as a nested if, to make clear, why I think there is O(N^2).
But the operator should be able to handle a unlimited amount of if-nesting.
This is my last addition, I forgot in my example that the trueval/falseval can also receive an array.
A = [1, 2, 3]
B = [0, 0, 0]
if(A<B, 0, A) = [1, 2, 3]
--
You received this message because you are subscribed to a topic in the Google Groups "Jep Java Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jep-users/gk7oBFWw6t4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jep-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jep-users/2a704b36-9d19-45a3-9205-0861d20989e6n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jep-users/3d4b43af-4bdc-4648-bc57-ab37ad5e6330n%40googlegroups.com.