AnyLogic
Expand
Font size
All Known Implementing Classes:
Agent, Experiment, ExperimentCompareRuns, ExperimentMultipleRuns, ExperimentOptimization, ExperimentParamVariation, ExperimentRunFast, ExperimentSimulation, FlowchartBlock, Utilities

public interface UtilitiesMath
Various math utilities
Author:
AnyLogic North America, LLC https://anylogic.com

Field Summary

Modifier and TypeFieldDescription
static final doubleinfinity
A constant holding the positive infinity of type double.
If you want to get negative infinity, please write -infinity

Method Summary

Modifier and TypeMethodDescription
static doubleatan2fast(double y, double x)
Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).
static doubledifference(BasicDataSet ds1, BasicDataSet ds2)
Difference function which is always not-negative and reflects difference between 2 given data sets in their common arguments range
static doubledifference(BasicDataSet ds, TableFunction f)
Difference function which is always not-negative and reflects difference between given data set and table function in their common arguments range
static doublegammaLog(double x)
Returns the natural logarithm of the gamma function of x:
ln(Γ(x)).
The gamma function is an extension of the factorial function that works on all positive values of x.
If n is a positive integer, then: Γ(n) = (n - 1)!.

The gammaLog function may be useful in System Dynamics models for computing combinatorial factors.
static booleanisFinite(double v)
Returns true if the given value is finite (not +/-infinity or NaN)
static doublelimit(double min, double x, double max)
Returns x if it is within [min,max] interval, otherwise returns the closest bound.
static intlimit(int min, int x, int max)
Returns x if it is within [min,max] interval, otherwise returns the closest bound.
static doublelimitMax(double x, double max)
Returns x if it is less or equal to max, otherwise returns max.
static intlimitMax(int x, int max)
Returns x if it is less or equal to max, otherwise returns max.
static doublelimitMin(double min, double x)
Returns x if it is greater or equal to min, otherwise returns min.
static intlimitMin(int min, int x)
Returns x if it is greater or equal to min, otherwise returns min.
static doublequantum(double value, double quantizer)
Returns the number smaller (by absolute value) than or equal to value that is an integer multiple of quantizer.
If quantizer is less than or equal to zero, then value is returned unchanged.
For example, quantum(PI, 0.01) will return 3.14
static doubleroundToDecimal(double v, int nDecimalDigits)
Rounds the value to the given precision.
static introundToInt(double v)
Returns int closest to the given value.
static doublesqr(double v)
Returns the square of the given value (v2)
static doublexidz(double a, double b, double x)
Tries to divide the first argument by the second.
static doublezidz(double a, double b)
Tries to divide the first argument by the second.

Field Details

infinity

static final double infinity
A constant holding the positive infinity of type double.
If you want to get negative infinity, please write -infinity
See Also:
Constant Field Values

Method Details

sqr

static double sqr(double v)
Returns the square of the given value (v2)
Parameters:
v - the value
Returns:
v * v

isFinite

static boolean isFinite(double v)
Returns true if the given value is finite (not +/-infinity or NaN)
Parameters:
v - the value
Returns:
the result of -infinity < v && v < +infinity

roundToInt

static int roundToInt(double v)
Returns int closest to the given value. Unlike Math.round(double), returns int value (instead of long) which may be more convenient for some operations.
The result is equal to the value of the expression:

(int) floor(v + 0.5)
Special cases:
  • If the argument is NaN, the result is 0.
  • If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE.
  • If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.
Parameters:
v - floating-point value to be rounded to a int.
Returns:
the value rounded to the nearest int value.
See Also:
Integer.MAX_VALUEInteger.MIN_VALUE

roundToDecimal

static double roundToDecimal(double v,
 int nDecimalDigits)
Rounds the value to the given precision. Returns the number closest to the given value, retaining the given number of decimal digits in the fractional / integer part.
The result is equal to the value of the expression:

floor(v * 10nDecimalDigits+ 0.5) / 10nDecimalDigits
Parameters:
v - floating-point value to be rounded to the given precision.
nDecimalDigits - if positive, fractional part will be affected, if negative, integer part will be
Returns:
the value rounded to the given precision
Since:
7.3

zidz

static double zidz(double a,
 double b)
Tries to divide the first argument by the second. If the result is infinity or not a number, returns 0, otherwise returns the division result.
Parameters:
a - first value
b - second value
Returns:
division result or 0

xidz

static double xidz(double a,
 double b,
 double x)
Tries to divide the first argument by the second. If the result is infinity or not a number, returns the third argument, otherwise returns the division result.
Parameters:
a - first value
b - second value
x - defines return result in case of unsuccessful division
Returns:
division result or x

limitMin

static double limitMin(double min,
 double x)
Returns x if it is greater or equal to min, otherwise returns min. May be more self-explanatory than the function max as explicitly says "x is left-limited by min".
Parameters:
min - left bound
x - the value
Returns:
x or min

limitMin

static int limitMin(int min,
 int x)
Returns x if it is greater or equal to min, otherwise returns min. May be more self-explanatory than the function max as explicitly says "x is left-limited by min".
Parameters:
min - left bound
x - the value
Returns:
x or min

limitMax

static double limitMax(double x,
 double max)
Returns x if it is less or equal to max, otherwise returns max. May be more self-explanatory than the function min as explicitly says "x is right-limited by max".
Parameters:
x - the value
max - right bound
Returns:
x or max

limitMax

static int limitMax(int x,
 int max)
Returns x if it is less or equal to max, otherwise returns max. May be more self-explanatory than the function min as explicitly says "x is right-limited by max".
Parameters:
x - the value
max - right bound
Returns:
x or max

limit

static double limit(double min,
 double x,
 double max)
Returns x if it is within [min,max] interval, otherwise returns the closest bound.
Parameters:
min - left bound
x - the value
max - right bound
Returns:
min, x or max

limit

static int limit(int min,
 int x,
 int max)
Returns x if it is within [min,max] interval, otherwise returns the closest bound.
Parameters:
min - left bound
x - the value
max - right bound
Returns:
min, x or max

gammaLog

static double gammaLog(double x)
Returns the natural logarithm of the gamma function of x:
ln(Γ(x)).
The gamma function is an extension of the factorial function that works on all positive values of x.
If n is a positive integer, then: Γ(n) = (n - 1)!.

The gammaLog function may be useful in System Dynamics models for computing combinatorial factors.
Parameters:
x - the value.
Returns:
ln(Γ(x))

quantum

static double quantum(double value,
 double quantizer)
Returns the number smaller (by absolute value) than or equal to value that is an integer multiple of quantizer.
If quantizer is less than or equal to zero, then value is returned unchanged.
For example, quantum(PI, 0.01) will return 3.14
Parameters:
value -
quantizer -
Returns:
quantized value

difference

static double difference(BasicDataSet ds,
 TableFunction f)
Difference function which is always not-negative and reflects difference between given data set and table function in their common arguments range
Parameters:
ds - data set
f - table function with linear interpolation
Returns:
square root of the average of square of difference between linearly interpolated data set and table function
The integration range is the intersection of argument ranges of data set and function

difference

static double difference(BasicDataSet ds1,
 BasicDataSet ds2)
Difference function which is always not-negative and reflects difference between 2 given data sets in their common arguments range
Parameters:
ds1 - data set
ds2 - data set
Returns:
square root of the average of square of difference between linearly interpolated data sets
The integration range is the intersection of argument ranges of data sets

atan2fast

static double atan2fast(double y,
 double x)
Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). This method computes the phase theta by computing an arc tangent of y/x in the range of -pi to pi. Comparing with Math.atan2(double, double), instead of computation of value every time, this method uses precomputed table of values Math.atan2(double, double). Therefore atan2fast is approximately 20 times faster than Math.atan2(double, double). Average error (comparing to Math.atan2(double, double)) is 3e-5 radians, max error is 1e-4 radians. This Method does not accept special arguments like NaN or Infinities. It's recommended to use when you don't need extremely high accuracy and have assurance that your arguments are finite.
Parameters:
y -
x -
Returns:
arctangent(y/x)