ExprEngine compiles a string of expression to java bytecode in memory at runtime. The bytecode represents a static method or a class which implements a user defined interface. The method can be called as normal Java method and returns Java object. Some simple examples are shown in following codes.
1. Quick examples: exec() -> parse, compile and run. (Note: the Java bytecode will be generated each time when using function ExprEngine.exec(). See examples in sections 2,3 and 4 below for more efficient ways.)
importstaticio.lambdacloud.ExprEngine.exec;
...
//Basic expressionsSystem.out.println(exec("-x", newint[]{3}));
System.out.println(exec("x + y", newint[]{3,4}));
System.out.println(exec("x > y", newint[]{3,4}));
System.out.println(exec("x <= y and x > 0", newint[]{3,4}));
System.out.println(exec("a=x+y; a+2", newdouble[]{3.0,4.0}));
System.out.println(exec("(7^3) == (1<<2)"));
System.out.println(exec(" 'abc'+'def' "));
System.out.println(exec(" \"abc\"+'def' "));
System.out.println(exec("7**2"));
//If else System.out.println(exec("if(x>y) {a=x+y;a } else {a=x*2;b=y*3; if(x<0) {x-1} else {a+b} }", newdouble[]{3,4}));
System.out.println("max(3,4)="+exec("a=if(x>y) {x} else {y}; a", newint[]{3,4}));
//While for loopsSystem.out.println(exec("while(x<y) {x=x+1} x", newint[]{1,4}));
System.out.println(exec("for(i=0;i<=n;i++) {x+=i} x", newint[]{100,0}));
//ArraySystem.out.println(exec("a=[10,20,30,40]; sum=0; for(i=0;i<4;i++) { sum+=a[i] } sum"));
System.out.println(Arrays.toString(
(int[])exec("[x, y, 3, 4]", newObject[]{ 1,2 })));
System.out.println(Arrays.toString(
(String[])exec("['abc', 'def', x]", newObject[]{"ghi"})));
System.out.println(Arrays.deepToString(
(int[][])exec("[ [x], [y] ]", newObject[]{ 1,2 })));
System.out.println(Arrays.deepToString(
(int[][][])exec("[ [[x],[y]], [[x+y]] ]", newObject[]{ 1,2 })));
System.out.println(Arrays.toString(
(double[])exec("x[i+1:j-1]", newObject[]{1,4,newdouble[]{1,2,3,4,5}})));
System.out.println(Arrays.deepToString((int[][])exec("[1:3, 5:10, 100:101]")));
//List comprehensionSystem.out.println(exec("[x for x in A for y in B]",
newObject[]{ newint[]{0,1,2}, newint[]{100,101,102} }));
System.out.println(exec("[x+y for x in A for y in B if x+y>=1000]",
newObject[]{ newint[]{1,2,3}, newint[]{10,100,1000} }));
System.out.println(exec("[ x>3 for x in A ]", newObject[] {newint[]{1,2,3,4,5,6}}));
System.out.println(exec("[ x for x in A if x>3 ]", newObject[] {newint[]{1,2,3,4,5,6}}));
System.out.println(Arrays.deepToString(
((List<int[][][]>)exec("[ [ [[x],[y]], [[x+y]] ] for x in A for y in B]",
newObject[]{ newint[]{1,2}, newint[]{3,4} })).toArray()));
//Memory efficient range() function (no temp list generated)System.out.println(exec("[x+10.0 for x in range(3)]")); //0,1,2System.out.println(exec("[x+10.0 for x in 1:3]")); //1,2,3System.out.println(exec("[[1 for col in range(3)] for row in range(2)]"));
//Function and Recursive Function CalllongstartTime = System.currentTimeMillis();
System.out.println("Fibonacci(46)="+exec("def fib(n) { if(n<=1) {1L} else {fib(n-1)+fib(n-2)} } fib(46)"));
System.out.println("Time: "+(System.currentTimeMillis()-startTime)+"ms");
//Output://Fibonacci(46)=2971215073//Time: 9022ms2. Parse and generate bytecode once, call as many times as you want. Use reflection to invoke the generated static method
importstaticio.lambdacloud.ExprEngine.genStaticMethod;
...
ExprTreeBuildWalkerew = parse("x+y", double.class);
Methodm = genStaticMethod(ew, "myclass", false, "myfun");
try {
for(inti=0; i<5; i++)
System.out.println(m.invoke(null, i, 10.0));
} catch (Exceptione) {
e.printStackTrace();
}3. Parse and generate bytecode once, call as many times you want. Use method handle to invoke the generated static method
importstaticio.lambdacloud.ExprEngine.genMethodHandle;
...
ExprTreeBuildWalkerew = parse("x+y");
MethodHandlemh = genMethodHandle(ew, "myclass", false, "myfun", double.class, double.class, double.class);
try {
for(inti=0; i<5; i++)
System.out.println(mh.invoke(i, 10.0));
} catch (Throwablee) {
e.printStackTrace();
}4. Declare an interface for your function.A class which implements the interface is generated and ready to use.
//Define your interface at somewherepublicinterfaceFun2 {
doubleapply(doublex, doubley);
}
//Cast the generated object to the type of the interface you defined
...
ExprTreeBuildWalkerew = parse("x+y", Fun2.class);
Fun2f = (Fun2)ExprEngine.newInstance(ew, "myclass", true);
System.out.println(f.apply(3,4));
...https://github.com/yuemingl/ExprEngine/blob/master/src/io/lambdacloud/TestExprEngine.java
https://github.com/yuemingl/ExprEngine/blob/master/src/io/lambdacloud/ExprGrammar.g4
#Matlab Grammar
MatlabEngine.exec("A=[1 2;3 4]; b=[3 4]'; A\\b");java -jar MatlabEngine.jar file.mIn this example, it shows how the optimaztion of the compiler for function 'myfun' works. Actually, the function 'myfun' is compiled twice. The first compilation generates 'myfun' with integer type of parameters. The second compilation generated 'myfun' with double type of parameters.
function [cd] = myfun(a, b)
c=a+b
d=a-bend
myfun(10,100)
myfun(10.1,100.1)This example shows how fast it can be reached for computing Fibonacci sequence at n=46. It takes about 4 minutes on Matlab R2016. However, it takes only 9 seconds for us. The experiment is conducted on a 2.2 GHz i7 CPU with Mac OS X Yosemite.
functionr=fib(n)
ifn<=1
r=1L;
else r=fib(n-1)+fib(n-2);
endendtic
fib(46)
toc2971215073
Elapsed time is 9188 ms.Iterative version with matrix form for computing Fibonacci sequence.
A=[11; 10];
b=[11]';
ticfor i=1:46
b=A*b;
endtocbElapsed time is 1 ms.
4807526976.00
2971215073.00Some matrix operations.
A=[12345678910; 11121314151617181920; 21222324252627282930]
A(:, 0:2:8)
A(1, 0:2:8)
A(:, 9)
B=A(0:2,0:2)
C=[548; 447; 211]
b=A(:,3)
C\b 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 10.00
11.00 12.00 13.00 14.00 15.00 16.00 17.00 18.00 19.00 20.00
21.00 22.00 23.00 24.00 25.00 26.00 27.00 28.00 29.00 30.00
1.00 3.00 5.00 7.00 9.00
11.00 13.00 15.00 17.00 19.00
21.00 23.00 25.00 27.00 29.00
11.00 13.00 15.00 17.00 19.00
10.00
20.00
30.00
1.00 2.00 3.00
11.00 12.00 13.00
21.00 22.00 23.00
5.00 4.00 8.00
4.00 4.00 7.00
2.00 1.00 1.00
4.00
14.00
24.00
7.43
26.57
-17.43
Gaussian Elimination
function GE(A)
[m,n]=size(A);
U=A;
for j=1:n-1
U(j+1:n,j)=(1/U(j,j))*U(j+1:n,j);
for i=j+1:n
U(i,j+1:n)=U(i,j+1:n)-U(i,j)*U(j,j+1:n);
endend
L=U-triu(U)+eye(n)
U=triu(U)
end
GE([123; 456; 789]); 1.00 0.00 0.00
4.00 1.00 0.00
7.00 2.00 1.00
1.00 2.00 3.00
0.00 -3.00 -6.00
0.00 0.00 0.00https://github.com/yuemingl/ExprEngine/blob/master/src/io/lambdacloud/test/TestMatlabEngine.javahttps://github.com/yuemingl/ExprEngine/tree/master/test_matlab