Java API By Example, From Geeks To Geeks.

# Java > Open Source Codes > JSci > maths > NumericalMath

 `1 package JSci.maths;2 3 import JSci.maths.analysis.RealFunction;4 import JSci.maths.analysis.RealFunction2D;5 6 /**7 * The numerical math library.8 * This class cannot be subclassed or instantiated because all methods are static.9 * @version 0.910 * @author Mark Hale11 */12 public final class NumericalMath extends AbstractMath {13         private NumericalMath() {}14 15         /**16         * Calculates the roots of the quadratic equation17         * ax2+bx+c=0.18         * @return an array containing the two roots.19         */20         public static double[] solveQuadratic(final double a,final double b,final double c) {21                 final double roots[]=new double[2];22                 double q;23                 if(b < 0.0)24                         q = (-b+Math.sqrt(b*b-4.0*a*c))/2.0;25                 else26                         q = (-b-Math.sqrt(b*b-4.0*a*c))/2.0;27                 roots[0] = q/a;28                 roots[1] = c/q;29                 return roots;30         }31         /**32         * Finds a root using the bisection method.33         * @param a lower bound.34         * @param b upper bound.35         */36         public static double bisection(Mapping func, double a, double b, final double tol) {37                 final int signa = ExtraMath.sign(func.map(a));38                 final int signb = ExtraMath.sign(func.map(b));39                 if(signa == signb)40                         throw new IllegalArgumentException ("Bounds do not bracket a root.");41                 double x;42                 do {43                         x = (a + b)/2.0;44                         int signx = ExtraMath.sign(func.map(x));45                         if(signx == signa) {46                                 a = x;47                         } else if(signx == signb) {48                                 b = x;49                         } else {50                                 a = x;51                                 b = x;52                         }53                 } while(Math.abs(a-b) > tol);54                 return x;55         }56         /**57         * Finds a root using the false position method.58         * @param a lower bound.59         * @param b upper bound.60         */61         public static double falsePosition(Mapping func, double a, double b, final double tol) {62                 double fa = func.map(a);63                 double fb = func.map(b);64                 final int signa = ExtraMath.sign(fa);65                 final int signb = ExtraMath.sign(fb);66                 if(signa == signb)67                         throw new IllegalArgumentException ("Bounds do not bracket a root.");68                 double x;69                 double delta;70                 do {71                         x = a - (b-a)*fa/(fb-fa);72                         double fx = func.map(x);73                         int signx = ExtraMath.sign(fx);74                         if(signx == signa) {75                                 delta = x-a;76                                 a = x;77                                 fa = fx;78                         } else if(signx == signb) {79                                 delta = b-x;80                                 b = x;81                                 fb = fx;82                         } else {83                                 delta = 0.0;84                         }85                 } while(Math.abs(delta) > tol);86                 return x;87         }88         /**89         * Finds a root using the Newton-Raphson method.90         * @param x initial guess.91         */92         public static double newtonRaphson(RealFunction func, double x, final int maxIter, final double tol) throws MaximumIterationsExceededException {93                 RealFunction deriv = func.differentiate();94                 double delta;95                 int n = 0;96                 do {97                         delta = -func.map(x)/deriv.map(x);98                         x += delta;99                         if(++n > maxIter)100                                 throw new MaximumIterationsExceededException("No convergence after "+maxIter+" iterations.");101                 } while(Math.abs(delta) > tol);102                 return x;103         }104         /**105         * Uses the Euler method to solve an ODE.106         * @param y an array to be filled with y values, set y[0] to initial condition.107         * @param func dy/dt as a function of y.108         * @param dt step size.109         * @return y.110         */111         public static double[] euler(final double y[],final Mapping func,final double dt) {112                 for(int i=0;iij=dfi/dxj.343         */344         public static double[][] differentiate(final MappingND func,final double x[],final double dx[]) {345                 final double xplus[]=new double[x.length];346                 final double xminus[]=new double[x.length];347                 System.arraycopy(x,0,xplus,0,x.length);348                 System.arraycopy(x,0,xminus,0,x.length);349                 xplus[0]+=dx[0];350                 xminus[0]-=dx[0];351                 double funcdiff[]=ArrayMath.scalarMultiply(0.5/dx[0],ArrayMath.subtract(func.map(xplus),func.map(xminus)));352                 final double diff[][]=new double[funcdiff.length][x.length];353                 for(int i=0;i