10
|
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
9
|
|
10
|
|
11
|
|
12
|
|
13 <html xmlns="http://www.w3.org/1999/xhtml">
|
|
14 <head>
|
|
15 <title>Math - The Commons Math User Guide - Utilites</title>
|
|
16 <style type="text/css" media="all">
|
|
17 @import url("../css/maven-base.css");
|
|
18 @import url("../css/maven-theme.css");
|
|
19 @import url("../css/site.css");
|
|
20 </style>
|
|
21 <link rel="stylesheet" href="../css/print.css" type="text/css" media="print" />
|
|
22 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
|
|
23 </head>
|
|
24 <body class="composite">
|
|
25 <div id="banner">
|
|
26 <span id="bannerLeft">
|
|
27
|
|
28 Commons Math User Guide
|
|
29
|
|
30 </span>
|
|
31 <div class="clear">
|
|
32 <hr/>
|
|
33 </div>
|
|
34 </div>
|
|
35 <div id="breadcrumbs">
|
|
36
|
|
37
|
|
38
|
|
39
|
|
40
|
|
41
|
|
42
|
|
43
|
|
44 <div class="xright">
|
|
45
|
|
46
|
|
47
|
|
48
|
|
49
|
|
50
|
|
51
|
|
52 </div>
|
|
53 <div class="clear">
|
|
54 <hr/>
|
|
55 </div>
|
|
56 </div>
|
|
57 <div id="leftColumn">
|
|
58 <div id="navcolumn">
|
|
59
|
|
60
|
|
61
|
|
62
|
|
63
|
|
64
|
|
65
|
|
66
|
|
67 <h5>User Guide</h5>
|
|
68 <ul>
|
|
69
|
|
70 <li class="none">
|
|
71 <a href="../userguide/index.html">Contents</a>
|
|
72 </li>
|
|
73
|
|
74 <li class="none">
|
|
75 <a href="../userguide/overview.html">Overview</a>
|
|
76 </li>
|
|
77
|
|
78 <li class="none">
|
|
79 <a href="../userguide/stat.html">Statistics</a>
|
|
80 </li>
|
|
81
|
|
82 <li class="none">
|
|
83 <a href="../userguide/random.html">Data Generation</a>
|
|
84 </li>
|
|
85
|
|
86 <li class="none">
|
|
87 <a href="../userguide/linear.html">Linear Algebra</a>
|
|
88 </li>
|
|
89
|
|
90 <li class="none">
|
|
91 <a href="../userguide/analysis.html">Numerical Analysis</a>
|
|
92 </li>
|
|
93
|
|
94 <li class="none">
|
|
95 <a href="../userguide/special.html">Special Functions</a>
|
|
96 </li>
|
|
97
|
|
98 <li class="none">
|
|
99 <strong>Utilities</strong>
|
|
100 </li>
|
|
101
|
|
102 <li class="none">
|
|
103 <a href="../userguide/complex.html">Complex Numbers</a>
|
|
104 </li>
|
|
105
|
|
106 <li class="none">
|
|
107 <a href="../userguide/distribution.html">Distributions</a>
|
|
108 </li>
|
|
109
|
|
110 <li class="none">
|
|
111 <a href="../userguide/fraction.html">Fractions</a>
|
|
112 </li>
|
|
113
|
|
114 <li class="none">
|
|
115 <a href="../userguide/transform.html">Transform Methods</a>
|
|
116 </li>
|
|
117
|
|
118 <li class="none">
|
|
119 <a href="../userguide/geometry.html">3D Geometry</a>
|
|
120 </li>
|
|
121
|
|
122 <li class="none">
|
|
123 <a href="../userguide/optimization.html">Optimization</a>
|
|
124 </li>
|
|
125
|
|
126 <li class="none">
|
|
127 <a href="../userguide/ode.html">Ordinary Differential Equations</a>
|
|
128 </li>
|
|
129
|
|
130 <li class="none">
|
|
131 <a href="../userguide/genetics.html">Genetic Algorithms</a>
|
|
132 </li>
|
|
133 </ul>
|
|
134 <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
|
|
135 <img alt="Built by Maven" src="../images/logos/maven-feather.png"></img>
|
|
136 </a>
|
|
137
|
|
138
|
|
139
|
|
140
|
|
141
|
|
142
|
|
143
|
|
144
|
|
145 </div>
|
|
146 </div>
|
|
147 <div id="bodyColumn">
|
|
148 <div id="contentBox">
|
|
149 <div class="section"><h2><a name="a6_Utilities"></a>6 Utilities</h2>
|
|
150 <div class="section"><h3><a name="a6.1_Overview"></a>6.1 Overview</h3>
|
|
151 <p>
|
|
152 The <a href="../apidocs/org/apache/commons/math/util/package-summary.html">
|
|
153 org.apache.commons.math.util</a> package collects a group of array utilities,
|
|
154 value transformers, and numerical routines used by implementation classes in
|
|
155 commons-math.
|
|
156 </p>
|
|
157 </div>
|
|
158 <div class="section"><h3><a name="a6.2_Double_array_utilities"></a>6.2 Double array utilities</h3>
|
|
159 <p>
|
|
160 To maintain statistics based on a "rolling" window of values, a resizable
|
|
161 array implementation was developed and is provided for reuse in the
|
|
162 <code>util</code> package. The core functionality provided is described in
|
|
163 the documentation for the interface,
|
|
164 <a href="../apidocs/org/apache/commons/math/util/DoubleArray.html">
|
|
165 org.apache.commons.math.util.DoubleArray.</a> This interface adds one
|
|
166 method, <code>addElementRolling(double)</code> to basic list accessors.
|
|
167 The <code>addElementRolling</code> method adds an element
|
|
168 (the actual parameter) to the end of the list and removes the first element
|
|
169 in the list.
|
|
170 </p>
|
|
171 <p>
|
|
172 The <a href="../apidocs/org/apache/commons/math/util/ResizableDoubleArray.html">
|
|
173 org.apache.commons.math.util.ResizableDoubleArray</a> class provides a
|
|
174 configurable, array-backed implementation of the <code>DoubleArray</code>
|
|
175 interface. When <code>addElementRolling</code> is invoked, the underlying
|
|
176 array is expanded if necessary, the new element is added to the end of the
|
|
177 array and the "usable window" of the array is moved forward, so that
|
|
178 the first element is effectively discarded, what was the second becomes the
|
|
179 first, and so on. To efficiently manage storage, two maintenance
|
|
180 operations need to be periodically performed -- orphaned elements at the
|
|
181 beginning of the array need to be reclaimed and space for new elements at
|
|
182 the end needs to be created. Both of these operations are handled
|
|
183 automatically, with frequency / effect driven by the configuration
|
|
184 properties <code>expansionMode</code>, <code>expansionFactor</code> and
|
|
185 <code>contractionCriteria.</code> See
|
|
186 <a href="../apidocs/org/apache/commons/math/util/ResizableDoubleArray.html">
|
|
187 ResizableDoubleArray</a>
|
|
188 for details.
|
|
189 </p>
|
|
190 </div>
|
|
191 <div class="section"><h3><a name="a6.3_intdouble_hash_map"></a>6.3 int/double hash map</h3>
|
|
192 <p>
|
|
193 The <a href="../apidocs/org/apache/commons/math/util/OpenIntToDoubleHashMap.html">
|
|
194 org.apache.commons.math.util.OpenIntToDoubleHashMap</a> class provides a specialized
|
|
195 hash map implementation for int/double. This implementation has a much smaller memory
|
|
196 overhead than standard <code>java.util.HashMap</code> class. It uses open addressing
|
|
197 and primitive arrays, which greatly reduces the number of intermediate objects and
|
|
198 improve data locality.
|
|
199 </p>
|
|
200 </div>
|
|
201 <div class="section"><h3><a name="a6.4_Continued_Fractions"></a>6.4 Continued Fractions</h3>
|
|
202 <p>
|
|
203 The <a href="../apidocs/org/apache/commons/math/util/ContinuedFraction.html">
|
|
204 org.apache.commons.math.util.ContinuedFraction</a> class provides a generic
|
|
205 way to create and evaluate continued fractions. The easiest way to create a
|
|
206 continued fraction is to subclass <code>ContinuedFraction</code> and
|
|
207 override the <code>getA</code> and <code>getB</code> methods which return
|
|
208 the continued fraction terms. The precise definition of these terms is
|
|
209 explained in <a href="http://mathworld.wolfram.com/ContinuedFraction.html" class="externalLink">
|
|
210 Continued Fraction, equation (1)</a> from MathWorld.
|
|
211 </p>
|
|
212 <p>
|
|
213 As an example, the constant Pi could be computed using the continued fraction
|
|
214 defined at <a href="http://functions.wolfram.com/Constants/Pi/10/0002/" class="externalLink">
|
|
215 http://functions.wolfram.com/Constants/Pi/10/0002/</a>. The following
|
|
216 anonymous class provides the implementation:
|
|
217 <div class="source"><pre>ContinuedFraction c = new ContinuedFraction() {
|
|
218 public double getA(int n, double x) {
|
|
219 switch(n) {
|
|
220 case 0: return 3.0;
|
|
221 default: return 6.0;
|
|
222 }
|
|
223 }
|
|
224
|
|
225 public double getB(int n, double x) {
|
|
226 double y = (2.0 * n) - 1.0;
|
|
227 return y * y;
|
|
228 }
|
|
229 }</pre>
|
|
230 </div>
|
|
231 </p>
|
|
232 <p>
|
|
233 Then, to evalute Pi, simply call any of the <code>evalute</code> methods
|
|
234 (Note, the point of evalution in this example is meaningless since Pi is a
|
|
235 constant).
|
|
236 </p>
|
|
237 <p>
|
|
238 For a more practical use of continued fractions, consider the exponential
|
|
239 function with the continued fraction definition of
|
|
240 <a href="http://functions.wolfram.com/ElementaryFunctions/Exp/10/" class="externalLink">
|
|
241 http://functions.wolfram.com/ElementaryFunctions/Exp/10/</a>. The
|
|
242 following anonymous class provides its implementation:
|
|
243 <div class="source"><pre>ContinuedFraction c = new ContinuedFraction() {
|
|
244 public double getA(int n, double x) {
|
|
245 if (n % 2 == 0) {
|
|
246 switch(n) {
|
|
247 case 0: return 1.0;
|
|
248 default: return 2.0;
|
|
249 }
|
|
250 } else {
|
|
251 return n;
|
|
252 }
|
|
253 }
|
|
254
|
|
255 public double getB(int n, double x) {
|
|
256 if (n % 2 == 0) {
|
|
257 return -x;
|
|
258 } else {
|
|
259 return x;
|
|
260 }
|
|
261 }
|
|
262 }</pre>
|
|
263 </div>
|
|
264 </p>
|
|
265 <p>
|
|
266 Then, to evalute <i>e</i><sup>x</sup> for any value x, simply call any of the
|
|
267 <code>evalute</code> methods.
|
|
268 </p>
|
|
269 </div>
|
|
270 <div class="section"><h3><a name="a6.5_binomial_coefficients_factorials_and_other_common_math_functions"></a>6.5 binomial coefficients, factorials and other common math functions</h3>
|
|
271 <p>
|
|
272 A collection of reusable math functions is provided in the
|
|
273 <a href="../apidocs/org/apache/commons/math/util/MathUtils.html">MathUtils</a>
|
|
274 utility class. MathUtils currently includes methods to compute the following: <ul><li>
|
|
275 Binomial coeffiecients -- "n choose k" available as an (exact) long value,
|
|
276 <code>binomialCoefficient(int, int)</code> for small n, k; as a double,
|
|
277 <code>binomialCoefficientDouble(int, int)</code> for larger values; and in
|
|
278 a "super-sized" version, <code>binomialCoefficientLog(int, int)</code>
|
|
279 that returns the natural logarithm of the value.</li>
|
|
280 <li>
|
|
281 Factorials -- like binomial coefficients, these are available as exact long
|
|
282 values, <code>factorial(int)</code>; doubles,
|
|
283 <code>factorialDouble(int)</code>; or logs, <code>factorialLog(int)</code>. </li>
|
|
284 <li>
|
|
285 Hyperbolic sine and cosine functions --
|
|
286 <code>cosh(double), sinh(double)</code></li>
|
|
287 <li>
|
|
288 sign (+1 if argument > 0, 0 if x = 0, and -1 if x < 0) and
|
|
289 indicator (+1.0 if argument >= 0 and -1.0 if argument < 0) functions
|
|
290 for variables of all primitive numeric types.</li>
|
|
291 <li>
|
|
292 a hash function, <code>hash(double),</code> returning a long-valued
|
|
293 hash code for a double value.
|
|
294 </li>
|
|
295 <li>
|
|
296 Convience methods to round floating-point number to arbitrary precision.
|
|
297 </li>
|
|
298 <li>
|
|
299 Least common multiple and greatest common denominator functions.
|
|
300 </li>
|
|
301 </ul>
|
|
302 </p>
|
|
303 </div>
|
|
304 </div>
|
|
305
|
|
306 </div>
|
|
307 </div>
|
|
308 <div class="clear">
|
|
309 <hr/>
|
|
310 </div>
|
|
311 <div id="footer">
|
|
312 <div class="xright">©
|
|
313 2003-2010
|
|
314
|
|
315
|
|
316
|
|
317
|
|
318
|
|
319
|
|
320
|
|
321
|
|
322
|
|
323 </div>
|
|
324 <div class="clear">
|
|
325 <hr/>
|
|
326 </div>
|
|
327 </div>
|
|
328 </body>
|
|
329 </html>
|