de.erichseifert.gral.util
Class MathUtils

java.lang.Object
  extended by de.erichseifert.gral.util.MathUtils

public abstract class MathUtils
extends Object

Abstract class that provides utility functions which are useful for mathematical calculations.


Constructor Summary
MathUtils()
           
 
Method Summary
static boolean almostEqual(double a, double b, double delta)
          Check whether two floating point values match with a given precision.
static int binarySearch(double[] a, double key)
          Perform a binary search on a sorted array a to find the element with the nearest element to key.
static int binarySearchCeil(double[] a, double key)
          Perform a binary search on a sorted array a to find the element with the smallest distance to key.
static int binarySearchFloor(double[] a, double key)
          Perform a binary search on a sorted array a to find the element with the smallest distance to key.
static double ceil(double a, double precision)
          Returns a rounded number larger than a with a defined precision.
static double floor(double a, double precision)
          Returns a rounded number smaller than a with a defined precision.
static
<T extends Comparable<T>>
T
limit(T value, T min, T max)
          Clamps a value to specified limits: if value is greater than max then max will be returned.
static
<T extends Comparable<T>>
int
randomizedSelect(List<T> a, int lower, int upper, int i)
          Perform a randomized search on an unsorted array a to find the ith smallest element.
static double round(double a, double precision)
          Mathematically rounds a number with a defined precision.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MathUtils

public MathUtils()
Method Detail

almostEqual

public static boolean almostEqual(double a,
                                  double b,
                                  double delta)
Check whether two floating point values match with a given precision.

Parameters:
a - First value
b - Second value
delta - Precision
Returns:
true if the difference of a and b is smaller or equal than delta, otherwise false

round

public static double round(double a,
                           double precision)
Mathematically rounds a number with a defined precision.

Parameters:
a - Value
precision - Precision
Returns:
Rounded value

floor

public static double floor(double a,
                           double precision)
Returns a rounded number smaller than a with a defined precision.

Parameters:
a - Value
precision - Precision
Returns:
Rounded value

ceil

public static double ceil(double a,
                          double precision)
Returns a rounded number larger than a with a defined precision.

Parameters:
a - Value
precision - Precision
Returns:
Rounded value

binarySearch

public static int binarySearch(double[] a,
                               double key)
Perform a binary search on a sorted array a to find the element with the nearest element to key.

Parameters:
a - Array with ascending values
key - Pivot value
Returns:
Index of the array element whose value is nearly or exactly key

binarySearchFloor

public static int binarySearchFloor(double[] a,
                                    double key)
Perform a binary search on a sorted array a to find the element with the smallest distance to key. The returned element's value is always less than or equal to key.

Parameters:
a - Array with ascending values
key - Pivot value
Returns:
Index of the array element whose value is less than or equal to key

binarySearchCeil

public static int binarySearchCeil(double[] a,
                                   double key)
Perform a binary search on a sorted array a to find the element with the smallest distance to key. The returned element's value is always greater than or equal to key.

Parameters:
a - Array with ascending values
key - Pivot value
Returns:
Index of the array element whose value is greater than or equal to key

limit

public static <T extends Comparable<T>> T limit(T value,
                                                T min,
                                                T max)
Clamps a value to specified limits: if value is greater than max then max will be returned. If value is greater than min then min will be returned.

Type Parameters:
T - Data type of the Value to limit.
Parameters:
value - Value to be clamped
min - Minimum
max - Maximum
Returns:
Clamped value

randomizedSelect

public static <T extends Comparable<T>> int randomizedSelect(List<T> a,
                                                             int lower,
                                                             int upper,
                                                             int i)

Perform a randomized search on an unsorted array a to find the ith smallest element. The array contents are be modified during the operation!

See Cormen et al. (2001): Introduction to Algorithms. 2nd edition. p. 186

Type Parameters:
T - Data type of the array
Parameters:
a - Unsorted array
lower - Starting index
upper - End index
i - Smallness rank of value to search
Returns:
Index of the element that is the ith smallest in array a


Copyright © 2009-2010. All Rights Reserved.