HiRISE

HiRISE.HiPlan.Utilities
Class Numerical

java.lang.Object
  extended by HiRISE.HiPlan.Utilities.Numerical

public class Numerical
extends Object

A collection of numerical analysis routines. The collection includes a few pretty basic functions as well, like minimum(double[]), maximum(double[]), and vectorLength(double[]).

Version:
1.11
Author:
Christian Schaller - UA/PIRL

Field Summary
static String ID
          Class identification name with source code version and date.
 
Method Summary
static Point2D boxMuller()
          Computes a pair of normally distributed random numbers using the Box-Muller polar algorithm.
static double interpolate(double[] f, double x)
          Computes an interpolated value for the tabulated function f = f(x).
static double[] interpolate(double[] f, double[] x)
          Computes a one-dimensional array of linearly interpolated values for a tabulated function f = f(x).
static void main(String[] args)
          Performs simple tests of the numerical analysis functions.
static double maximum(double[] values)
          Finds the maximum value of an array of values.
static double minimum(double[] values)
          Finds the minimum value of an array of values.
static double nearestElement(double[] array, double value)
          Finds the element of the input array nearest to the input value.
static double newtonCotes(double[] x, double[] y)
          Integrates the tabulated function y = y(x) using a five-point Newton-Cotes method.
static double[] spline(double[] x, double[] y)
          Computes the second derivatives of the interpolating function for the tabulated function y = f(x).
static double[] spline(double[] x, double[] y, double yp0, double ypn_1)
          Computes the second derivatives of the interpolating function for the tabulated function y = f(x).
static double[] splint(double[] xa, double[] ya, double[] x)
          Computes the cubic-spline interpolated values at x for the tabulated function ya = f(xa).
static double splint(double[] xa, double[] ya, double[] y2, double x)
          Computes the cubic-spline interpolated value at x for the tabulated function ya = f(xa).
static double[] splint(double[] xa, double[] ya, double yp0, double ypn_1, double[] x)
          Computes the cubic-spline interpolated values at x for the tabulated function ya = f(xa).
static double vectorLength(double[] v)
          Computes the length of a vector.
static double vincentyInverse(double lon1, double lat1, double lon2, double lat2)
          Computes the distance between two points on Mars using the Vincenty inverse formula.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ID

public static final String ID
Class identification name with source code version and date.

See Also:
Constant Field Values
Method Detail

spline

public static double[] spline(double[] x,
                              double[] y,
                              double yp0,
                              double ypn_1)
Computes the second derivatives of the interpolating function for the tabulated function y = f(x).

If either yp0 or ypn_1 (the first derivative of the interpolating function at point 0 and n - 1, respectively) is Double.MAX_VALUE, the corresponding boundary condition is set for a natural spline, with a second derivative of 0 on that boundary.

This function is adapted from the function spline in Numerical Recipes in C (1988).

Parameters:
x - the points for which the tabulated function y has been calculated.
y - the tabulated function for interpolation.
yp0 - the first derivative of the interpolating function at point 0.
ypn_1 - the first derivative of the interpolating function at point n - 1.
Returns:
the second derivatives of the interpolating function.

spline

public static double[] spline(double[] x,
                              double[] y)
Computes the second derivatives of the interpolating function for the tabulated function y = f(x). The boundary points 0 and n - 1 of the interpolating function are set for a natural spline, with a second derivative of 0 at those boundaries.

This function is adapted from the function spline in Numerical Recipes in C (1988).

Parameters:
x - the points for which the tabulated function y has been calculated.
y - the tabulated function for interpolation.
Returns:
the second derivatives of the interpolating function.
See Also:
spline(double[], double[], double, double)

splint

public static double splint(double[] xa,
                            double[] ya,
                            double[] y2,
                            double x)
Computes the cubic-spline interpolated value at x for the tabulated function ya = f(xa).

This function requires the second derivatives of the interpolating function, which may be computed via the spline function.

This function is adapted from the function splint in Numerical Recipes in C (1988).

Parameters:
xa - the points for which the tabulated function ya has been calculated.
ya - the tabulated function for interpolation.
y2 - the second derivatives of the interpolating function; may be computed via the spline function.
x - the point at which to compute the interpolated value.
Returns:
the interpolated value at x.

splint

public static double[] splint(double[] xa,
                              double[] ya,
                              double yp0,
                              double ypn_1,
                              double[] x)
Computes the cubic-spline interpolated values at x for the tabulated function ya = f(xa).

The second derivatives of the interpolating function are computed via the spline function with the given first derivatives at point 0 and n - 1. If either derivative is Double.MAX_VALUE, the corresponding boundary condition is set for a natural spline, with a second derivative of 0 on that boundary.

This function is adapted from the function splint in Numerical Recipes in C (1988).

Parameters:
xa - the points for which the tabulated function ya has been calculated.
ya - the tabulated function for interpolation.
yp0 - the first derivative of the interpolating function at point 0.
ypn_1 - the first derivative of the interpolating function at point n - 1.
x - the points at which to compute the interpolated values.
Returns:
the interpolated values at each element of x.

splint

public static double[] splint(double[] xa,
                              double[] ya,
                              double[] x)
Computes the cubic-spline interpolated values at x for the tabulated function ya = f(xa).

The second derivatives of the interpolating function are computed via the spline function. The boundary points 0 and n - 1 of the interpolating function are set for a natural spline, with a second derivative of 0 at those boundaries.

This function is adapted from the function splint in Numerical Recipes in C (1988).

Parameters:
xa - the points for which the tabulated function ya has been calculated.
ya - the tabulated function for interpolation.
x - the points at which to compute the interpolated values.
Returns:
the interpolated values at each element of x.

interpolate

public static double interpolate(double[] f,
                                 double x)
Computes an interpolated value for the tabulated function f = f(x).

The function f is assumed to have been computed from a function f(x) at regular intervals from 0 to n - 1, where n is the length of the array f. Thus, f[0] is f(0), f[1] is f(1), and so on.

The returned value is computed such if x is outside the range 0, n - 1, it is set to the value of the nearest element of the array f. Otherwise it is linearly interpolated.

Example:

If f is {1.0, 2.0, 4.0}, then for x in {-0.5, 0.5, 1.75, 2.5}, interpolate(f, x) returns 1.0, 1.5, 3.5, and 4.0.

Parameters:
f - a one-dimensional array of function values.
x - the point for interpolation.
Returns:
the interpolated value.

interpolate

public static double[] interpolate(double[] f,
                                   double[] x)
Computes a one-dimensional array of linearly interpolated values for a tabulated function f = f(x).

The function f is assumed to have been computed from a function f(x) at regular intervals from 0 to n - 1, where n is the length of the array f. Thus, f[0] is f(0), f[1] is f(1), and so on.

The returned array of interpolated values is computed such that input values in the array x that are outside the range 0, n - 1 are set to the value of the nearest element of the array f. All other values are linearly interpolated.

Example:

If f is {1.0, 2.0, 4.0} and x is {-0.5, 0.5, 1.75, 2.5}, then interpolate(f, x) returns {1.0, 1.5, 3.5, 4.0}.

Parameters:
f - a one-dimensional array of function values.
x - a one-dimensional array of points for interpolation.
Returns:
a one-dimensional array containing the interpolated values.

newtonCotes

public static double newtonCotes(double[] x,
                                 double[] y)
Integrates the tabulated function y = y(x) using a five-point Newton-Cotes method. A cubic-spline interpolation is used in the integration to re-grid the function into regularly gridded segments.

Parameters:
x - the points for which the tabulated function y has been calculated.
y - the tabulated function.
Returns:
the area under the curve represented by the tabulated function.

vincentyInverse

public static double vincentyInverse(double lon1,
                                     double lat1,
                                     double lon2,
                                     double lat2)
Computes the distance between two points on Mars using the Vincenty inverse formula. The equatorial radius of Mars is 3396.19 km; the polar radius is 3376.2 km.

Parameters:
lon1 - the first point's longitude in radians.
lat1 - the first point's latitude in radians.
lon2 - the second point's longitude in radians.
lat2 - the second point's latitude in radians.
Returns:
the distance in km; if the function fails to converge, it returns NaN.
See Also:
Vincenty Formula, Wikipedia Entry

minimum

public static double minimum(double[] values)
Finds the minimum value of an array of values.

Parameters:
values - the array of values.
Returns:
the minimum value.

maximum

public static double maximum(double[] values)
Finds the maximum value of an array of values.

Parameters:
values - the array of values.
Returns:
the maximum value.

vectorLength

public static double vectorLength(double[] v)
Computes the length of a vector.

Parameters:
v - the input vector.
Returns:
the length of the vector.

nearestElement

public static double nearestElement(double[] array,
                                    double value)
Finds the element of the input array nearest to the input value. The nearest element is defined as the one for which the following is minimized:
abs(ai - v)

where a is the input array and v is the input test value.

Parameters:
array - the input array of values.
value - the test value.
Returns:
the closest value.

boxMuller

public static Point2D boxMuller()
Computes a pair of normally distributed random numbers using the Box-Muller polar algorithm. The algorithm uses a uniformly distributed random function on the closed interval [-1, +1] and transforms it into a normally distributed function with a mean of 0.0 and a standard deviation of 1.0.

This function uses the built-in Random object to generate the uniform distribution to be sampled.

The Box-Muller algorithm uses two input values to derive two output values. The two output values are contained in a single Point2D object for convenience.

Returns:
a pair of normally distributed random numbers.

main

public static void main(String[] args)
Performs simple tests of the numerical analysis functions.

Compare the results of these tests with the equivalent calls of the IDL routines on which they're based.

Usage

Numerical

Command-line arguments are ignored.

Parameters:
args - command-line arguments, ignored.

HiRISE

Copyright (C) Arizona Board of Regents on behalf of the Planetary Image Research Laboratory, Lunar and Planetary Laboratory at the University of Arizona