Common Java Cookbook

Edition: 0.19

Download PDF or Read on Scribd

Download Examples (ZIP)

8.5. Obtaining Commons Math

8.5.1. Problem

You need to use Commons Math to work with complex numbers, matrices, statistics, or linear equations.

8.5.2. Solution

To use Commons Math in a Maven 2 project, add the following dependency to your project's pom.xml:

Example 8.1. Adding a Dependency on Commons Math

    <dependency>
        <groupId>commons-math</groupId>
        <artifactId>commons-math</artifactId>
        <version>1.2</version>
    </dependency>

If you are not sure what this means, I'd suggest reading Maven: The Definitive Guide. When you depend on a library in Maven 2, all you need to do is add the dependency groupId, artifactId, and version to your project's dependencies. Once you do this, Maven 2 will download the dependency and make it available on your project's classpath.

8.5.3. Discussion

Commons Math was created to provide some more advanced mathematical capabilities under an Apache-style license. Commons Math provides classes to work with complex numbers, utilities to calculate statistics, a matrix implementation, special functions, continued fractions, root-finding, interpolation, and bivariate regression.

While the previous four recipes demonstrated classes and utilities available in Commons Lang, the next five recipes demonstrate classes and utilities from Commons Math. Simpler math utilities, which have wide application, will frequently be included in Commons Lang, and more complex utilities will be added to Commons Math. As both components continue to evolve, you may notice some overlap between the feature-set of Commons Lang and Commons Math.

8.5.4. See Also

For more information about the Commons Math project, see http://commons.apache.org/math/.

For the authoritative cookbook of mathematics, pick up a copy of Numerical Recipes in C++ or Numerical Recipes in C (Cambridge University Press). These classic tomes contain a huge library of code and examples, but be forewarned, the mathematics will quickly intimidate the faint of math. More information about this indispensable text can be found at the Numerical Recipes website (http://www.nr.com/). Unlike all the components described throughout this book, the code and examples from both of these books is covered under a very restrictive license described at http://www.numerical-recipes.com/infotop.html#distinfo.


Creative Commons License
Common Java Cookbook by Tim O'Brien is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Permissions beyond the scope of this license may be available at http://www.discursive.com/books/cjcook/reference/jakartackbk-PREFACE-1.html. Copyright 2009. Common Java Cookbook Chunked HTML Output. Some Rights Reserved.