You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Дмитрий Белавенцев <di...@gmail.com> on 2011/11/27 15:49:35 UTC

[JEXL] Evaluating math expression

Hi! I try to use JEXL to compute math expressions like this:

(x1-2)^4+(x1-2*x2)^2

But here is the problem - "^" mean xor, but not Math.pow() function. So, I
want to use the following code to compute a math expressions:

JexlEngine jexl = new JexlEngine();
Expression func = jexl.createExpression("(x1-2)^4+(x1-2*x2)^2");
MapContext mc = new MapContext();
mc.set("x1", 2);
mc.set("x2", 1);
System.out.println(func.evaluate(mc)); // prints "6" - WRONG ANSWER!

The question is the following. How can I use JEXL as math interpreter (so
"^" will be and Math.pow(), etc...)? Or it's impossible?

Re: [JEXL] Evaluating math expression

Posted by Дмитрий Белавенцев <di...@gmail.com>.
Hm, I steel have wrong answer. Here is my code:

1. Test class http://pastebin.com/92ufGBW1
2. NelderMead class http://pastebin.com/Y7tP80Gd
3. Class overriding XOR with Math.pow() http://pastebin.com/BGaX4g7R

Could you point me on my mistake?

TIA!

---
Best regards,
Dmitry!

2011/11/27 henrib <he...@apache.org>

> Hi;
> You can use JEXL as a math interpreter but you need to derive
> JexlArithmetic
> to fit your purpose.
> this means, create your own JexlArithmeticWithPow class, overload the
> method
> bitwiseXor to perform pow and create your JexlEngine instance with an
> instance of that class.
> For example:
> <code>
>    public static class WithPow extends
> org.apache.commons.jexl2.JexlArithmetic {
>        public WithPow(boolean lenient) {
>            super(lenient);
>        }
>
>        public Object bitwiseXor(Object left, Object right) {
>            double l = toDouble(left);
>            double r = toDouble(right);
>            return Math.pow(l, r);
>        }
>    }
>
>    public void test120() throws Exception {
>        JexlEngine jexl = new JexlEngine(null, new WithPow(true), null,
> null);
>         Expression func = jexl.createExpression("(x1-2)^4+(x1-2*x2)^2");
>        MapContext mc = new MapContext();
>        mc.set("x1", 2);
>        mc.set("x2", 1);
>         assertEquals(0, func.evaluate(mc));
>    }
> </code>
> Hope this helps,
> Regards
> Henrib
>
> --
> View this message in context:
> http://apache-commons.680414.n4.nabble.com/JEXL-Evaluating-math-expression-tp4112606p4112799.html
> Sent from the Commons - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

Re: [JEXL] Evaluating math expression

Posted by henrib <he...@apache.org>.
Hi;
You can use JEXL as a math interpreter but you need to derive JexlArithmetic
to fit your purpose.
this means, create your own JexlArithmeticWithPow class, overload the method
bitwiseXor to perform pow and create your JexlEngine instance with an
instance of that class.
For example:
<code>
    public static class WithPow extends
org.apache.commons.jexl2.JexlArithmetic {
        public WithPow(boolean lenient) {
            super(lenient);
        }

        public Object bitwiseXor(Object left, Object right) {
            double l = toDouble(left);
            double r = toDouble(right);
            return Math.pow(l, r);
        }
    }

    public void test120() throws Exception {
        JexlEngine jexl = new JexlEngine(null, new WithPow(true), null,
null);
        Expression func = jexl.createExpression("(x1-2)^4+(x1-2*x2)^2");
        MapContext mc = new MapContext();
        mc.set("x1", 2);
        mc.set("x2", 1);
        assertEquals(0, func.evaluate(mc));
    }
</code>
Hope this helps,
Regards
Henrib

--
View this message in context: http://apache-commons.680414.n4.nabble.com/JEXL-Evaluating-math-expression-tp4112606p4112799.html
Sent from the Commons - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org