SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.misc
Class SuanShuUtils

java.lang.Object
  extended by com.numericalmethod.suanshu.misc.SuanShuUtils

public class SuanShuUtils
extends java.lang.Object

This class collects some miscellaneous utility functions that are commonly used. These functions are static and stateless.


Method Summary
static void assertArgument(boolean condition, java.lang.String errorMessage)
          Check if an argument condition is satisfied.
static void assertOrThrow(java.lang.RuntimeException error)
          Throw the argument RuntimeException if it is not null.
static double autoEpsilon(double... inputs)
          Compute a reasonable precision parameter.
static double autoEpsilon(double[]... inputs)
          Compute a reasonable precision parameter.
static double autoEpsilon(Matrix A)
          Compute a reasonable precision parameter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

assertArgument

public static void assertArgument(boolean condition,
                                  java.lang.String errorMessage)
Check if an argument condition is satisfied. Throw IllegalArgumentException if not. This is a convenience method for checking input arguments.

Parameters:
condition - the argument condition to be checked
errorMessage - the error message if the condition is not satisfied

assertOrThrow

public static void assertOrThrow(java.lang.RuntimeException error)
Throw the argument RuntimeException if it is not null.

This function is slow because the RuntimeException is always created and passed in as an argument to a function. The object creation is often unnecessary.

To efficiently use this method for checking whether a condition is satisified, a sample usage is the following.

assertOrThrow(cond ? null : new RuntimeException("msg"));

It is important that we use lazy evaluation of the if statement. This is to avoid the expensive object (exception) creation for each checking.

Parameters:
error - the error thrown if the condition is not satisfied

autoEpsilon

public static double autoEpsilon(double... inputs)
Compute a reasonable precision parameter. In numerical computing, we often need to 'guess' how small a number needs to be for it to be considered 0. Changing the threshold often changes the results, e.g., computing the rank of a matrix.

This method suggests a more 'objective' way to determine the 'correct' epsilon from the inputs. Roughly,

auto ε = |max(inputs)| * sqrt(number of inputs) * machine ε * 10

Parameters:
inputs - doubles
Returns:
a precision parameter

autoEpsilon

public static double autoEpsilon(double[]... inputs)
Compute a reasonable precision parameter. In numerical computing, we often need to 'guess' how small a number is for it to be considered 0. Changing the threshold often changes the results, e.g., computing the rank of a matrix.

This method suggests a more 'objective' way to determine the 'correct' epsilon from the inputs. Roughly,

auto ε = |max(inputs)| * sqrt(number of inputs) * machine ε * 10

Parameters:
inputs - arrays of doubles
Returns:
a precision parameter

autoEpsilon

public static double autoEpsilon(Matrix A)
Compute a reasonable precision parameter. In numerical computing, we often need to 'guess' how small a number is for it to be considered 0. Changing the threshold often changes the results, e.g., computing the rank of a matrix.

This method suggests a more 'objective' way to determine the 'correct' epsilon from the inputs. Roughly,

auto ε = |max(inputs)| * sqrt(number of inputs) * machine ε * 10

Parameters:
A - a matrix
Returns:
a precision parameter

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.