123 lines
4.0 KiB
XML
123 lines
4.0 KiB
XML
<?xml version="1.0"?>
|
|
|
|
<!--
|
|
Copyright 2003-2004 The Apache Software Foundation
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
-->
|
|
|
|
<?xml-stylesheet type="text/xsl" href="./xdoc.xsl"?>
|
|
<!-- $Revision: 1.8 $ $Date: 2004/05/17 05:58:35 $ -->
|
|
<document url="utilities.html">
|
|
|
|
<properties>
|
|
<title>The Commons Math User Guide - Utilites</title>
|
|
</properties>
|
|
|
|
<body>
|
|
|
|
<section name="6 Utilities">
|
|
|
|
<subsection name="6.1 Overview" href="overview">
|
|
<p>
|
|
The <a href="../apidocs/org/apache/commons/math/util/package-summary.html">
|
|
org.apache.commons.math.util</a> package collects a group of array utilities,
|
|
value transformers, and numerical routines used by implementation classes in
|
|
commons-math.
|
|
</p>
|
|
</subsection>
|
|
|
|
<subsection name="6.2 Double array utilities" href="arrays">
|
|
<p>
|
|
This is yet to be written. Any contributions will be gratefully accepted!
|
|
</p>
|
|
</subsection>
|
|
|
|
<subsection name="6.3 Continued Fractions" href="continued_fractions">
|
|
<p>
|
|
The <a href="../apidocs/org/apache/commons/math/util/ContinuedFraction.html">
|
|
org.apache.commons.math.util.ContinuedFraction</a> class provides a generic
|
|
way to create and evaluate continued fractions. The easiest way to create a
|
|
continued fraction is to subclass <code>ContinuedFraction</code> and
|
|
override the <code>getA</code> and <code>getB</code> methods which return
|
|
the continued fraction terms. The precise definition of these terms is
|
|
explained in <a href="http://mathworld.wolfram.com/ContinuedFraction.html">
|
|
Continued Fraction, equation (1)</a> from MathWorld.
|
|
</p>
|
|
<p>
|
|
As an example, the constant Pi could be computed using the continued fraction
|
|
defined at <a href="http://functions.wolfram.com/Constants/Pi/10/0002/">
|
|
http://functions.wolfram.com/Constants/Pi/10/0002/</a>. The following
|
|
anonymous class provides the implementation:
|
|
<source>ContinuedFraction c = new ContinuedFraction() {
|
|
public double getA(int n, double x) {
|
|
switch(n) {
|
|
case 0: return 3.0;
|
|
default: return 6.0;
|
|
}
|
|
}
|
|
|
|
public double getB(int n, double x) {
|
|
double y = (2.0 * n) - 1.0;
|
|
return y * y;
|
|
}
|
|
}</source>
|
|
</p>
|
|
<p>
|
|
Then, to evalute Pi, simply call any of the <code>evalute</code> methods
|
|
(Note, the point of evalution in this example is meaningless since Pi is a
|
|
constant).
|
|
</p>
|
|
<p>
|
|
For a more practical use of continued fractions, consider the exponential
|
|
function with the continued fraction definition of
|
|
<a href="http://functions.wolfram.com/ElementaryFunctions/Exp/10/">
|
|
http://functions.wolfram.com/ElementaryFunctions/Exp/10/</a>. The
|
|
following anonymous class provides its implementation:
|
|
<source>ContinuedFraction c = new ContinuedFraction() {
|
|
public double getA(int n, double x) {
|
|
if (n % 2 == 0) {
|
|
switch(n) {
|
|
case 0: return 1.0;
|
|
default: return 2.0;
|
|
}
|
|
} else {
|
|
return n;
|
|
}
|
|
}
|
|
|
|
public double getB(int n, double x) {
|
|
if (n % 2 == 0) {
|
|
return -x;
|
|
} else {
|
|
return x;
|
|
}
|
|
}
|
|
}</source>
|
|
</p>
|
|
<p>
|
|
Then, to evalute <i>e</i><sup>x</sup> for any value x, simply call any of the
|
|
<code>evalute</code> methods.
|
|
</p>
|
|
</subsection>
|
|
|
|
<subsection name="6.4 binomial coefficients, factorials and other common math functions" href="math_utils">
|
|
<p>
|
|
This is yet to be written. Any contributions will be gratefully accepted!
|
|
</p>
|
|
</subsection>
|
|
|
|
</section>
|
|
|
|
</body>
|
|
</document> |