create changelog entry
[debian/openrocket] / core / src / de / congrace / exp4j / Example.java
1 package de.congrace.exp4j;
2
3 import java.util.List;
4
5 public class Example {
6
7         public static void main(String[] args) throws UnknownFunctionException, UnparsableExpressionException, InvalidCustomFunctionException {
8
9                 // Test 1
10                 // ======
11                 
12                 Calculable calc1 = new ExpressionBuilder("x * y - 2").withVariableNames("x", "y").build();
13                 calc1.setVariable(new Variable("x", 1.2));
14                 calc1.setVariable(new Variable("y", 2.2));
15                 
16                 System.out.println(calc1.calculate().toString());
17                 //double result = calc1.calculate().getDoubleValue();
18                 //System.out.println(result);
19                         
20                 // Test 2
21                 // ======
22                 
23                 // A function which calculates the mean of an array and scales it
24                 CustomFunction meanFn = new CustomFunction("mean",2) {
25                     public Variable applyFunction(List<Variable> vars) {
26                         
27                         double[] vals;
28                         double scale;
29                         
30                         try{
31                                 vals = vars.get(0).getArrayValue();
32                                 scale = vars.get(1).getDoubleValue();
33                         } catch (Exception e) {
34                                 return new Variable("Invalid");
35                         }
36                         
37                         double subtotal = 0;
38                         for (int i = 0; i < vals.length; i++ ){
39                                 subtotal += vals[i];
40                         }
41                         
42                         subtotal = scale * subtotal / vals.length;
43                         return new Variable("double MEAN result, ", subtotal);
44                         
45                     }
46                 };
47                                 
48                 ExpressionBuilder b = new ExpressionBuilder("mean(x,y)");
49                 b.withCustomFunction(meanFn);
50                 b.withVariable(new Variable("x", new double[] {1.1,2,10,3,2.4,10.2}));
51                 b.withVariable(new Variable("y", 2));
52                 Calculable calc2 = b.build();
53                 
54                 System.out.println( calc2.calculate().toString() );
55                 
56                 // Test 3
57                 // ======
58                 
59                 Calculable calc3 = new ExpressionBuilder("x * y - 2").withVariableNames("x", "y").build();
60                 calc3.setVariable(new Variable("x", new double[]{1.2, 10, 20, 15}));
61                 calc3.setVariable(new Variable("y", new double[]{2.2, 5.2, 12, 9 }));
62                 
63                 //double result3 = calc3.calculate().getDoubleValue();
64                 System.out.println(calc3.calculate().toString());
65                 
66
67                 // Test 4
68                 // ======
69                                 
70                 Calculable calc4 = new ExpressionBuilder("log10(sqrt(x) * abs(y))").withVariableNames("x", "y").build();
71                 calc4.setVariable(new Variable("x", new double[]{1.2, 10, 10, 15}));
72                 calc4.setVariable(new Variable("y", new double[]{2.2, -5.2, 5.2, 9 }));
73                 
74                 //double result3 = calc3.calculate().getDoubleValue();
75                 System.out.println(calc4.calculate().toString());               
76         }
77 }