Code Examples
A repository of 155 code examples for BeepBeep
Polynomial.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2018 Sylvain HallĂ©
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package functions;
19 
20 import ca.uqac.lif.cep.functions.StreamVariable;
21 import ca.uqac.lif.cep.functions.Constant;
22 import ca.uqac.lif.cep.functions.Function;
23 import ca.uqac.lif.cep.functions.FunctionException;
24 import ca.uqac.lif.cep.functions.FunctionTree;
25 import ca.uqac.lif.cep.functions.PassthroughFunction;
26 import ca.uqac.lif.cep.util.Numbers;
27 
28 /**
29  * Use an {@link StreamVariable} and a {@link FunctionTree} to create
30  * the polynomial function <i>x</i><sup>2</sup>+3.
31  *
32  * @author Sylvain HallĂ©
33  */
34 public class Polynomial extends PassthroughFunction
35 {
36  /*
37  * We need to implement the getFunction() method, which must
38  * return an object of type Function. Here, we create a FunctionTree
39  * that computes x^2+3.
40  */
41  @Override
42  public Function getFunction()
43  {
44  /* At the top level, the polynomial is an addition of two
45  * terms. Hence the top of the function tree is the function
46  * Addition, followed by two arguments. */
47  return new FunctionTree(Numbers.addition,
48  /* The first argument is itself a FunctionTree that computes
49  * the square of the argument. */
50  new FunctionTree(Numbers.power,
51  /* This is done by using the Power function. StreamVariable.X
52  * refers by default to the first argument passed to a function. */
53  StreamVariable.X,
54  new Constant(2)),
55  /* The second term of the addition is the constant 3 */
56  new Constant(3));
57  }
58 
59  /*
60  * Small main() to illustrate the concept
61  */
62  public static void main(String[] args) throws FunctionException
63  {
64  Polynomial poly = new Polynomial();
65  Object[] value = new Object[1];
66  poly.evaluate(new Integer[]{3}, value);
67  System.out.printf("Return value of the function: %f\n", value[0]);
68  poly.evaluate(new Integer[]{8}, value);
69  System.out.printf("Return value of the function: %f\n", value[0]);
70  }
71 }
Use an StreamVariable and a FunctionTree to create the polynomial function x2+3.
Definition: Polynomial.java:34