org.opensourcephysics.numerics

## Class Util

• java.lang.Object
• org.opensourcephysics.numerics.Util

• ```public final class Util
extends java.lang.Object```
A utility class for numerical analysis. This class cannot be subclassed or instantiated because all methods are static.
Version:
1.0
Author:
Wolfgang Christian
• ### Field Summary

Fields
Modifier and Type Field and Description
`static double` `defaultNumericalPrecision`
The default precision for numerical analysis.
`static double` `LOG10`
`static double` `SQRT2PI`
• ### Method Summary

Methods
Modifier and Type Method and Description
`static int` `checkSorting(double[] array)`
Checks if an array is sorted.
`static double` ```computeAverage(double[] array, int start, int num)```
Computes the average value of a subset of an array.
`static Function` `constantFunction(double c)`
Creates a function having a constant value.
`static double` `evalMath(java.lang.String str)`
Evalautes a mathematical expression without variables.
`static java.lang.String` `f2(double d)`
Convert a double to a string, printing two decimal places.
`static java.lang.String` `f3(double d)`
Convert a double to a string, printing three decimal places.
`static java.lang.String` `f4(double d)`
Convert a double to a string, printing four decimal places.
`static double[]` ```functionFill(Function f, double start, double stop, double[] data)```
Fills the given double[n] array with f(x) on the interval [start, stop].
`static double[][]` ```functionFill(Function f, double start, double stop, double[][] data)```
Fills the given double[2][n] array with x and f(x) values on interval [start, stop].
`static Function` ```gaussian(double x0, double sigma)```
Creates a Guassian (normal) distribution function.
`static double[]` ```getRange(Function f, double a, double b, int n)```
Gets the approximate range of a function within the given domain.
`static Function` ```linearFunction(double m, double b)```
Creates a linear function with the given slope and intercept.
`static double` ```relativePrecision(double epsilon, double result)```
Computes the relativePrecision except near zero where the absolute precision is returned.
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Field Detail

• #### SQRT2PI

`public static final double SQRT2PI`
• #### LOG10

`public static final double LOG10`
• #### defaultNumericalPrecision

`public static final double defaultNumericalPrecision`
The default precision for numerical analysis.
• ### Method Detail

• #### f2

`public static java.lang.String f2(double d)`
Convert a double to a string, printing two decimal places.
Parameters:
`d` - Input double
• #### f3

`public static java.lang.String f3(double d)`
Convert a double to a string, printing three decimal places.
Parameters:
`d` - Input double
• #### f4

`public static java.lang.String f4(double d)`
Convert a double to a string, printing four decimal places.
Parameters:
`d` - Input double
• #### relativePrecision

```public static double relativePrecision(double epsilon,
double result)```
Computes the relativePrecision except near zero where the absolute precision is returned.
Parameters:
`epsilon` - the absolute error
`result` - the result
Returns:
the relative error
• #### checkSorting

`public static int checkSorting(double[] array)`
Checks if an array is sorted. Returns: Positive integer if array is sorted in increasing value. Negative integer if array is sorted in decreasing value. Zero if array is not sorted.
Parameters:
`array` - double[]
Returns:
int 1,0,-1 based on sorting
• #### getRange

```public static double[] getRange(Function f,
double a,
double b,
int n)```
Gets the approximate range of a function within the given domain. The range is deterermiend by evaluating the function at n points and finding the minimum and maximum values.
Parameters:
`f` - Function
`a` - double
`b` - double
`n` - int
Returns:
double[]
• #### functionFill

```public static double[][] functionFill(Function f,
double start,
double stop,
double[][] data)```
Fills the given double[2][n] array with x and f(x) values on interval [start, stop].
Parameters:
`f` - Function
`start` - double
`stop` - double
`data` - double[][]
Returns:
double[][]
• #### functionFill

```public static double[] functionFill(Function f,
double start,
double stop,
double[] data)```
Fills the given double[n] array with f(x) on the interval [start, stop].
Parameters:
`f` - Function
`start` - double starting value of x
`stop` - double stopping value of x
`data` - double[]
Returns:
double[]
• #### computeAverage

```public static double computeAverage(double[] array,
int start,
int num)```
Computes the average value of a subset of an array.
Parameters:
`array` - the data to be averaged
`start` - the index of the first point to be averaged
`num` - the total number of points to be averaged
Returns:
• #### constantFunction

`public static Function constantFunction(double c)`
Creates a function having a constant value.
Parameters:
`c` -
Returns:
• #### linearFunction

```public static Function linearFunction(double m,
double b)```
Creates a linear function with the given slope and intercept.
Parameters:
`m` - double slope
`b` - double intercept
Returns:
Function
• #### gaussian

```public static Function gaussian(double x0,
double sigma)```
Creates a Guassian (normal) distribution function. The distribution is normalized. The full width at half maximum is 2*sigma*Math.sqrt(2 Math.log(2)) ~ 2.3548*sigma
Parameters:
`x0` - double center of the distribution
`sigma` - double width of the distributuon
Returns:
Function
• #### evalMath

`public static double evalMath(java.lang.String str)`
Evalautes a mathematical expression without variables.
Parameters:
`str` - String
Returns:
double