| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Math example source code file (utilities.xml)

This example Commons Math source code file (utilities.xml) is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Math tags/keywords

apache, continued, license, license, mathutils, pi, pi, see, see, the, the, then, to, you

The Commons Math utilities.xml source code

<?xml version="1.0"?>

   Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You 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

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   See the License for the specific language governing permissions and
   limitations under the License.
<?xml-stylesheet type="text/xsl" href="./xdoc.xsl"?>
<!-- $Revision: 726459 $ $Date: 2008-12-14 09:56:50 -0500 (Sun, 14 Dec 2008) $ -->
<document url="utilities.html">

    <title>The Commons Math User Guide - Utilites


<section name="6 Utilities">

<subsection name="6.1 Overview" href="overview">
    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

<subsection name="6.2 Double array utilities" href="arrays">
    To maintain statistics based on a "rolling" window of values, a resizable 
    array implementation was developed and is provided for reuse in the 
    <code>util package.  The core functionality provided is described in
    the documentation for the interface, 
    <a href="../apidocs/org/apache/commons/math/util/DoubleArray.html">
    org.apache.commons.math.util.DoubleArray.</a>  This interface adds one
    method, <code>addElementRolling(double) to basic list accessors. 
    The <code>addElementRolling method adds an element 
    (the actual parameter) to the end of the list and removes the first element
     in the list.
    The <a href="../apidocs/org/apache/commons/math/util/ResizableDoubleArray.html">
    org.apache.commons.math.util.ResizableDoubleArray</a> class provides a
    configurable, array-backed implementation of the <code>DoubleArray 
    interface.  When <code>addElementRolling is invoked, the underlying
    array is expanded if necessary, the new element is added to the end of the
    array and the "usable window" of the array is moved forward, so that
    the first element is effectively discarded, what was the second becomes the
    first, and so on.  To efficiently manage storage, two maintenance
    operations need to be periodically performed -- orphaned elements at the
    beginning of the array need to be reclaimed and space for new elements at
    the end needs to be created.  Both of these operations are handled
    automatically, with frequency / effect driven by the configuration
    properties <code>expansionMode, expansionFactor and
    <code>contractionCriteria.  See 
    <a href="../apidocs/org/apache/commons/math/util/ResizableDoubleArray.html">
    for details. 

<subsection name="6.3 int/double hash map" href="int_double_hash_map">
    The <a href="../apidocs/org/apache/commons/math/util/OpenIntToDoubleHashMap.html">
    org.apache.commons.math.util.OpenIntToDoubleHashMap</a> class provides a specialized
    hash map implementation for int/double. This implementation has a much smaller memory
    overhead than standard <code>java.util.HashMap class. It uses open addressing
    and primitive arrays, which greatly reduces the number of intermediate objects and
    improve data locality.

<subsection name="6.4 Continued Fractions" href="continued_fractions">
    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 and
    override the <code>getA and getB methods which return
    the continued fraction terms.  The precise definition of these terms is
    explained in <a href="">
    Continued Fraction, equation (1)</a> from MathWorld.
    As an example, the constant Pi could be computed using the continued fraction
    defined at <a href=""></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;
    Then, to evalute Pi, simply call any of the <code>evalute methods
    (Note, the point of evalution in this example is meaningless since Pi is a
    For a more practical use of continued fractions, consider the exponential
    function with the continued fraction definition of
    <a href=""></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;
    Then, to evalute <i>ex for any value x, simply call any of the
    <code>evalute methods.

<subsection name="6.5 binomial coefficients, factorials and other common math functions" href="math_utils">
    A collection of reusable math functions is provided in the
    <a href="../apidocs/org/apache/commons/math/util/MathUtils.html">MathUtils
    utility class.  MathUtils currently includes methods to compute the following: <ul>
    Binomial coeffiecients -- "n choose k" available as an (exact) long value,  
    <code>binomialCoefficient(int, int) for small n, k; as a double,
    <code>binomialCoefficientDouble(int, int) for larger values; and in
    a "super-sized" version, <code>binomialCoefficientLog(int, int) 
    that returns the natural logarithm of the value.</li>
    Factorials -- like binomial coefficients, these are available as exact long
    values, <code>factorial(int);  doubles, 
    <code>factorialDouble(int); or logs, factorialLog(int). 
    Hyperbolic sine and cosine functions -- 
    <code>cosh(double), sinh(double)
    sign (+1 if argument > 0, 0 if x = 0, and -1 if x < 0) and 
    indicator (+1.0 if argument  >= 0 and -1.0 if argument < 0) functions
    for variables of all primitive numeric types.</li>
    a hash function, <code>hash(double), returning a long-valued
    hash code for a double value.
    Convience methods to round floating-point number to arbitrary precision.
    Least common multiple and greatest common denominator functions.



Other Commons Math examples (source code examples)

Here is a short list of links related to this Commons Math utilities.xml source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.