File: functions.html

package info (click to toggle)
jep 2.4.1%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,748 kB
  • sloc: java: 28,213; xml: 206; makefile: 15; sh: 6
file content (383 lines) | stat: -rw-r--r-- 19,699 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<html><!-- #BeginTemplate "/Templates/maintemplate.dwt" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- #BeginEditable "doctitle" --> 
<title>JEP - Java Math Expression Parser</title>
<!-- #EndEditable --> 
<link rel="stylesheet" type="text/css" href="main.css" title="style1">
</head>

<body>



<!-- NAVIGATION ---------------------------------------------------- -->
<div id="navcontainer"> 
  <ul>
    <li><a href="../javadoc/index.html" target="_blank">JavaDoc <img src="img/new-window-icon.gif" width="15" height="11"></a></li>
  </ul>
  <h1>JEP</h1>
  <ul>
    <li><a href="index.html">Basic Usage</a></li>
    <li><a href="variables.html">Variables</a></li>
    <li><a href="types.html">Data types</a></li>
    <li><a href="operators.html">Operators</a></li>
    <li><a href="functions.html">Functions</a></li>
    <li><a href="advanced.html">Advanced Features</a></li>
    <li><a href="grammar.html">Grammar</a></li>
    <li><a href="faq.html">FAQ</a></li>
    <li><a href="version.html">Version History</a></li>
  </ul>
  <h1>Extensions</h1>
  <ul>
    <li><a href="extensions/index.html">Overview</a></li>
    <li><a href="extensions/xjep.html">XJep</a></li>
    <li><a href="extensions/djep.html">Differentiation</a></li>
    <li><a href="extensions/vectorjep.html">Vectors and Matrices</a></li>
    <li><a href="extensions/groupjep.html">Groups</a></li>
    <li><a href="extensions/version.html">Version History</a></li>
  </ul>
</div>



<div id="centercontent">
<!-- CENTER CONTENT -------------------------------------------------- -->
<!-- #BeginEditable "Text" --> 
  <h1><a name="top"></a>Functions</h1>
  <p>Note that you can always add new <a href="#custom">custom functions</a>. 
    Each of the following functions can be applied to objects of the types indicated. 
  </p>
  Functions added with <b>addStandardFunctions()</b>: 
  <table border="0" cellspacing="1" cellpadding="2" width="100%" bgcolor="#CCCCCC">
    <tr bgcolor="#FFFFFF"> 
      <td width="225">&nbsp;</td>
      <td width="91">&nbsp;</td>
      <td width="76"><b>Double</b></td>
      <td width="81"><b>Complex</b></td>
      <td width="91"><b>String</b></td>
      <td width="91"><b>Vector</b></td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Sine</td>
      <td width="91">sin(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Cosine</td>
      <td width="91">cos(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Tangent</td>
      <td width="91">tan(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Arc Sine</td>
      <td width="91">asin(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Arc Cosine</td>
      <td width="91">acos(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Arc Tangent</td>
      <td width="91">atan(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Arc Tangent (with 2 parameters)</td>
      <td width="91">atan2(y, x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Hyperbolic Sine</td>
      <td width="91">sinh(x)</td>
      <td width="76" bordercolor="#CCCCCC" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" bordercolor="#CCCCCC" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" bordercolor="#CCCCCC" align="center">&nbsp;</td>
      <td width="91" bordercolor="#CCCCCC" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Hyperbolic Cosine</td>
      <td width="91">cosh(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Hyperbolic Tangent</td>
      <td width="91">tanh(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Inverse Hyperbolic Sine</td>
      <td width="91">asinh(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Inverse Hyperbolic Cosine</td>
      <td width="91">acosh(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Inverse Hyperbolic Tangent</td>
      <td width="91">atanh(x)</td>
      <td width="76" bordercolor="#CCCCCC" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Natural Logarithm</td>
      <td width="91">ln(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Logarithm base 10</td>
      <td width="91">log(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Exponential (e^x)</td>
      <td width="91">exp(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Absolute Value / Magnitude</td>
      <td width="91">abs(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Random number (between 0 and 1)</td>
      <td width="91">rand()</td>
      <td width="76" align="center">&nbsp;</td>
      <td width="81" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Modulus</td>
      <td width="91">mod(x,y) = x % y</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Square Root</td>
      <td width="91">sqrt(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Sum</td>
      <td width="91">sum(x,y,z)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">If</td>
      <td width="91">if(cond,trueval,falseval)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Str (number to string)</td>
      <td width="91">str(x)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Binomial coefficients</td>
      <td width="91">binom(n,i)</td>
      <td width="76" align="center">Integer values</td>
      <td width="81" align="center"></td>
      <td width="91" align="center"></td>
      <td width="91" align="center"></td>
    </tr>
  </table>
  <br>
  Functions added with <b>addComplex()</b>: 
  <table border="0" cellspacing="1" cellpadding="2" width="100%" bgcolor="#CCCCCC">
    <tr bgcolor="#FFFFFF"> 
      <td width="225">&nbsp;</td>
      <td width="91">&nbsp;</td>
      <td width="76"><b>Double</b></td>
      <td width="81"><b>Complex</b></td>
      <td width="91"><b>String</b></td>
      <td width="91"><b>Vector</b></td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Real Component</td>
      <td width="91">re(c)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Imaginary Component</td>
      <td width="91">im(c)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Complex Modulus (Absolute Value)</td>
      <td width="91">cmod(c)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Argument (Angle of complex value, in radians)</td>
      <td width="91">arg(c)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Complex conjugate</td>
      <td width="91">conj(c)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Complex, constructs a complex number from real and imaginar 
        parts</td>
      <td width="91">complex(x,y)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="225">Polar, constructs a complex number from modulus and argument</td>
      <td width="91">polar(r,theta)</td>
      <td width="76" align="center"><img src="img/checkmark.gif" width="10" height="10" border="0" alt="Check"></td>
      <td width="81" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
      <td width="91" align="center">&nbsp;</td>
    </tr>
  </table>
  <!-- #BeginLibraryItem "/Library/top bar.lbi" -->
<div class="topbar"><a href="#top"><img src="img/top.gif" width="38" height="15" name="top"></a></div>
<!-- #EndLibraryItem --> 
  <h2><a name="custom"></a>Custom Functions</h2>
  <p>The following is an example of how a custom function can be added.</p>
  <p>Assume you want to add a function &quot;half&quot; to divide a number by 
    two (for demonstration purposes).</p>
  <ol>
    <li>Create a class that extends PostfixMathCommand (in the org.nfunk.jep.function 
      package). In this example we will name it &quot;Half&quot;</li>
    <li>In the constructor set the number of arguments to be taken. In our case 
      this is one. Do this by writing &quot;numberOfParameters = 1;&quot;<br>
      If you want to allow any number of parameters, initialize the numberOfParameters 
      value to -1. It is highly recommended to study the Sum.java code as an example 
      of a function that accepts any number of parameters.</li>
    <li> Implement the run(Stack inStack) method. The parameters are passed in 
      a Stack object. This same stack is used as the output for the function. 
      So we first need to pop the parameter off the stack, calculate the result, 
      and finally pop the result back on the stack. 
      <pre>public void run(Stack inStack) throws ParseException {

   <font color="#999999">// check the stack</font>
   checkStack(inStack);
   
   <font color="#999999">// get the parameter from the stack</font>
   Object param = inStack.pop();

   <font color="#999999">// check whether the argument is of the right type</font><br>   if (param instanceof Double) {
      <font color="#999999">// calculate the result</font><br>      double r = ((Double)param).doubleValue() / 2;
      <font color="#999999">// push the result on the inStack</font><br>      inStack.push(new Double(r)); <br>   } else {<br>      throw new ParseException(&quot;Invalid parameter type&quot;);
   }
}</pre>
    </li>
    <li>In your main program or applet that is using the parser, add the new function. 
      Do this by writing<br>
      <pre>parser.addFunction("half", new Half());</pre>
    </li>
    <li>If numberOfParameters == -1 you may wish to overwrite the <tt>boolean 
      checkNumberOfParameters(int n)</tt> to catch an illegal number of arguments 
      at parse time.</li>
  </ol>
  <p><b>Source files</b></p>
  <ul>
    <li><a href="CustFunc.java">CustFunc.java</a> (a testing class)</li>
    <li><a href="Half.java">Half.java</a> (the Half function class)</li>
  </ul>
  <!-- #EndEditable -->




<!-- FOOTER ---------------------------------------------------------- -->
<div id="footer">
<a href="http://sourceforge.net/tracker/?func=add&group_id=24711&atid=382402">Report bugs / documentation errors</a><br/>
<br/>
&copy; 2006 <a href="http://www.singularsys.com" target="_blank">Singular Systems</a>
</div>
</div> <!-- centercontent -->

</body>
<!-- #EndTemplate --></html>