5.2 The Functions

   

 
Java Number Cruncher: The Java Programmer's Guide to Numerical Computing
By Ronald  Mak

Table of Contents
Chapter  5.   Finding Roots

5.2 The Functions

Before we start looking at the algorithms, we need to create some functions whose roots we're going to find and enter them all into a global function table. Listing 5-0a shows the abstract base class Function in package numbercruncher.mathutils.

Listing 5-0a The abstract base Function class.
 package numbercruncher.mathutils; /**  * The base class for functions that can have derivatives.  */ public abstract class Function implements Evaluatable {     /**      * Return the value of the function at x.      * (Implementation of Evaluatable.)      * @param x the value of x      * @return the function value      */     public abstract float at(float x);     /**      * Return the value of the function's derivative at x.      * @param x the value of x      * @return the derivative value      */     public float derivativeAt(float x) { return 0; } } 

This base class delegates to its subclasses the implementation of method at() , which will return the value of the function at x. By default, method derivativeAt() returns 0. This method may be overridden by a subclass, where it can return the value of the function's first derivative at x.

Listing 5-0b Interface Evaluatable.
 package numbercruncher.mathutils; /**  * Interface implement by function classes.  */ public interface Evaluatable {     /**      * Return the value of the function at x.      * @param x the value of x      * @return the value of the function at x      */     float at(float x); } 

The base class implements interface Evaluatable in package numbercruncher. mathutils. See Listing 5-0b. This interface specifies the at() method. The interface will be useful when we develop classes that work with different function classes, such as the regression and interpolation function classes in Chapter 6.

Now we're ready to create some Function objects. Listing 5-0c shows parts of class RootFunctions in package numbercruncher.rootutils. It statically creates the function objects and enters them into the global hash table named TABLE.

Listing 5-0c Parts of class RootFunctions , which creates Function objects and enters them into a global hash table.
 package numbercruncher.rootutils; import java.util.Hashtable; import numbercruncher.mathutils.Function; /**  * Load into a global table the functions whose roots we want to find.  */ public class RootFunctions {     /** global function table */     private static Hashtable TABLE = new Hashtable(32);     // Enter the functions into the global function table.     static {        enterFunctions();     }     /**      * Return the function with the given hash key      * @param key the hash key      * @return the function      */     public static Function function(String key)     {         return (Function) TABLE.get(key);     }     /**      * Enter all the functions into the global function table.      */     private static void enterFunctions()     {         // Function f(x)  = x^2 - 4         //          f'(x) = 2x         TABLE.put(             "x^2 - 4",             new Function()             {                 public float at(float x)                 {                     return x*x - 4;                 }                 public float derivativeAt(float x)                 {                     return 2*x;                 }             }); ...         // Function g(x) = (x + 4/x)/2         TABLE.put(             "(x + 4/x)/2",             new Function()             {                 public float at(float x)                 {                     return (x + 4/x)/2;                 }             }); ... } 

The class enters each Function object into TABLE , using a string representation of the function's expression as the hash key. Some of the functions also have a defined first derivative f' ( x ). Given a key, the function() method returns the corresponding Function object from TABLE , or null if the key is invalid.

Table 5-1 shows all the functions created by class RootFunctions. The reason some of the functions are named g will be explained later in this chapter.


   
Top
 


Java Number Cruncher. The Java Programmer's Guide to Numerical Computing
Java Number Cruncher: The Java Programmers Guide to Numerical Computing
ISBN: 0130460419
EAN: 2147483647
Year: 2001
Pages: 141
Authors: Ronald Mak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net