Math

Math

Math provides access to many common mathematical functions and constants. Most of the functions are static (i.e.: they do not require an instance of the class.) Several of the methods are not static and may be accessed through the commonly available variable ‘MATH’.

Static Methods

These methods may be invoked as follows:

double sinX = Math.Sin(90.0);
static double E()

Returns the value of Euler’s Number (e).

static double PI()

Returns the value of PI.

static double PI_OVER_2()

Returns the value of PI/2.

static double PI_OVER_4()

Returns the value of PI/4.

static double TWO_PI()

Returns the value of 2*PI.

static double FOUR_PI()

Returns the value of 4*PI.

static double RAD_PER_DEG()

Returns the number of radians per degree.

static double DEG_PER_RAD()

Returns the number of degrees per radian.

static double M_PER_FT()

Returns the number of meters per foot.

static double M_PER_MI()

Returns the number of meters per mile.

static double M_PER_NM()

Returns the number of meters per nautical mile.

static double FT_PER_M()

Returns the number of feet per meter.

static double FT_PER_MI()

Returns the number of feet per mile.

static double FT_PER_NM()

Returns the number of feet per nautical mile.

static double NM_PER_FT()

Returns the number of nautical miles per foot.

static double MPS_PER_MPH()

Returns the number of meters/second per mile/hour.

static double MPH_PER_MPS()

Returns the number of miles/hour per meters/second.

static double MPS_PER_NMPH()

Returns the number of meters/second per nmiles/hour.

static double NMPH_PER_MPS()

Returns the number of nmiles/hour per meters/second.

static double LB_PER_KG()

Returns the number of pounds (mass) per kilogram.

static double LB_PER_NT()

Returns the number of pounds (force) per Newton.

static double KG_PER_LB()

Returns the number of kilograms per pound (mass).

static double NT_PER_LB()

Returns the number of Newtons per pound (force).

static double LIGHT_SPEED()

Returns the speed of light (meters per second)

static double BOLTZMANN_CONSTANT()

Returns Boltzmann constant, kb (Joules per degree Kelvin)

static double GRAVITATIONAL_CONSTANT()

Returns Newton’s gravitational constant, G (N-m^2 / kg^2).

static double Sin(double aAngle)

Returns the sine of the provided angle (degrees).

static double ASin(double aNumber)

Returns the arc sine [-90 … -90] of the provided number.

static double Cos(double aAngle)

Returns the cosine of the provided angle (degrees).

static double ACos(double aNumber)

Returns the arc cosine [0 … 180] of the provided number.

static double Tan(double aAngle)

Returns the tangent of the provided angle (degrees).

static double ATan(double aNumber)

Returns the arc tangent [-90 … 90] of the provided number.

static double ATan2(double aY, double aX)

Returns the arc tangent of (Y/X) [-180 … 180] of the provided number.

static double Sqrt(double aNumber)

Returns the square root of the provided number.

static double Fabs(double aNumber)

Returns the absolute value of the provided number.

static double Log10(double aNumber)

Returns the base-10 logarithm of the provided number.

static double Log(double aNumber)
static double Log(double aNumber, double aBase)
static double Ln(double aNumber)
static double Ln(double aNumber, double aBase)

Returns the natural logarithm of the provided number. Uses the base if provided as a second parameter.

static double Pow(double aX, double aY)

Returns the value of X raised to the power of Y.

static double Floor(double aNumber)

Returns the largest integer not greater than the provided number.

static double Ceil(double aNumber)

Returns the smallest integer not less than the provided number.

static double Fmod(double aX, double aY)

Returns the floating-point remainder of X divided by Y.

static int Mod(int aX, int aY)

Returns the integer remainder of X divided by Y.

static double Max(double aX, double aY)

Returns the maximum of X and Y.

static double Min(double aX, double aY)

Returns the minimum of X and Y.

static double Lerp(double aValue, double aOrigRangeMin, double aOrigRangeMax, double aNewRangeMin, double aNewRangeMax)

Interpolates the given value from one scale to another using linear interpolation. y = y0 + ((x-x0)*(y1-y0)/(x1-x0))

static double Limit(double aVal, double aBound)
static double Limit(double aVal, double aLowerBound, double aUpperBound)

Returns the value bounded by the range [aLowerBound &<= aVal &<= aUpperBound] or [-aBound &<= aVal &<= aBound].

static bool AngleIsBetween(double aAngle, double aLowerLimit, double aUpperLimit)

Returns true if aAngle is within the lower and upper limit. The angles are in degrees and will be normalized before comparing.

static bool AngleWithinTolerance(double aAngle, double aTolerance)

Returns true if aAngle is within the specified tolerance. (degrees)

static double NormalizeAngleMinus180_180(double aAngle)

Returns the angle normalized to be between -180 and 180 degrees.

static double NormalizeAngle0_360(double aAngle)

Returns the angle normalized to be between 0 and 360 degrees.

static double Sign(double aNumber)

Returns the sign of the number, -1 or +1

static double LinearToDB(double aNumber)

Returns a decibel scaled value given a linear value.

static double DB_ToLinear(double aNumber)

Returns a linear scaled value given a decibel value.

static double SafeLinearToDB(double aNumber)

Returns a decibel scaled value given a linear value.

static double StringToLat(string aLat)

Returns a degree decimal equivalent of a string formatted latitude value.

static double StringToLon(string aLon)

Returns a degree decimal equivalent of a string formatted longitude value.

static string LatToString(double aLat)

Returns a string formatted latitude value given the degree decimal equivalent.

static string LonToString(double aLon)

Returns a string formatted longitude value given the degree decimal equivalent

static double DOUBLE_MAX()

Returns the maximum value a double can contain. (1.7976931348623158e+308)

static double DOUBLE_MIN()

Returns the minimum value a double can contain. (2.2250738585072014e-308)

static double INTEGER_MAX()

Returns the maximum value an integer can contain. (2147483647)

static double INTEGER_MIN()

Returns the minimum value an integer can contain. (-2147483648)

Non-Static Methods

These methods must be accessed using the ‘MATH’ variable that is available to every script:

int seed = MATH.Seed();

Note

The following random functions don’t use the random_seed command. They are intended to be used as a separate, repeatable random number generator from the simulation’s main random number generator, WsfRandom.

Note

Multiple versions of each method with different numbers of arguments allow for default values of the distribution parameters to be used when none are specified.

void SetSeed(int aSeed)

Sets the random number generator’s seed.

int Seed()

Returns the random number generator’s seed.

double RandomUniform()
double RandomUniform(double aMinValue, double aMaxValue)

Returns a random number using a uniform distribution.

  • aMinValue Minimum value of range, default is 0.0.

  • aMaxValue Maximum value of range, default is 1.0.

bool RandomBernoulli()
bool RandomBernoulli(double aP)

Returns a random true or false result using a Bernoulli distribution.

  • aP The probability of returning a true result, default is 0.5.

int RandomBinomial()
int RandomBinomial(int aT)
int RandomBinomial(int aT, double aP)

Returns a random number using a Binomial distribution.

  • aT The number of trials, default is 1.

  • aP The probability of a trial generating true, default is 0.5.

int RandomNegativeBinomial()
int RandomNegativeBinomial(int aK)
int RandomNegativeBinomial(int aK, double aP)

Returns a random number using a Negative Binomial distribution.

  • aK The number of desired true outcomes, default is 1.

  • aP The probability of a trial generating true, default is 0.5.

int RandomGeometric()
int RandomGeometric(double aP)

Returns a random number using a Geometric distribution, equivalent to the Negative Binomial distribution with aK = 1.

  • aP The probability of a trial generating true, default is 0.5.

int RandomPoisson()
int RandomPoisson(double aMean)

Returns a random number according to a Poisson distribution.

  • aMean The mean number of occurrences of the event, default is 1.0.

double RandomExponential()
double RandomExponential(double aLambda)

Returns a random number according to an exponential distribution. This function is useful for drawing a random number associated with the waiting times between independent Poisson-random events (such as in bank queuing or radioactive decay).

  • aLambda Defines the rate of events per unit, default is 1.0.

double RandomGamma()
double RandomGamma(double aAlpha)
double RandomGamma(double aAlpha, double aBeta)

Returns a random number using a Gamma distribution.

  • aAlpha The shape parameter, default is 1.0.

  • aBeta The scale parameter, default is 1.0.

double RandomWeibull()
double RandomWeibull(double aA)
double RandomWeibull(double aA, double aB)

Returns a random number using a Weibull distribution.

  • aA The shape parameter, default is 1.0.

  • aB The scale parameter, default is 1.0.

double RandomExtremeValue(double aA, double aB)

Returns a random number using an ExtremeValue distribution.

  • aA The location parameter, default is 1.0.

  • aB The scale parameter, default is 0.0.

double RandomGaussian()
double RandomGaussian(double aMean)
double RandomGaussian(double aMean, double aStdDev)
double RandomNormal()
double RandomNormal(double aMean)
double RandomNormal(double aMean, double aStdDev)

Returns a random number using a Normal (or Gaussian) distribution.

  • aMean The mean distribution parameter, default is 0.0.

  • aStdDev The standard deviation parameter, default is 1.0.

double RandomLogNormal()
double RandomLogNormal(double aM)
double RandomLogNormal(double aM, double aS)

Returns a random number using a LogNormal distribution.

  • aM The log-mean distribution parameter, default is 0.0.

  • aS The log-deviation distribution parameter, default is 1.0.

double RandomChiSquared()
double RandomChiSquared(double aN)

Returns a random number using a ChiSquared distribution.

  • aN The degrees of freedom parameter, default is 1.0.

double RandomCauchy()
double RandomCauchy(double aA)
double RandomCauchy(double aA, double aB)

Returns a random number using a Cauchy distribution.

  • aA The location parameter, default is 0.0.

  • aB The scale parameter, default is 1.0.

double RandomFisherF()
double RandomFisherF(double aM)
double RandomFisherF(double aM, double aN)

Returns a random number using a FisherF distribtution.

  • aM The first degree of freedom parameter, default is 1.0.

  • aN The second degree of freedom parameter, default is 1.0.

double RandomStudentT()
double RandomStudentT(double aN)

Returns a random number using a StudentT distribution with the specified degrees of freedom.

  • aN The degrees of freedom, default is 1

double RandomRayleigh(double aRadius)

Returns a random number using a Rayleigh distribution using the specified radius.

int Roll(int aLowerLimit, int aUpperLimit)

Returns a random number using a uniform distribution between the specified lower and upper limits.

Array<int> AssignMaxProfit(Array<Array<int>> values)
Array<int> AssignMaxProfit(Array<Array<double>> values)

Returns the maximum profit assignment of rows to columns, assuming that values[a][b] holds the profit obtained from assigning a -> b. Example:

Array<Array<int> > vals = { {6,4,3},
                            {3,1,2},
                            {3,0,0} };
Array<int> assigns = MATH.AssignMaxProfit(vals);   //expect [1,2,0] returned
Array<int> AssignMinCost(Array<Array<int>> values)
Array<int> AssignMinCost(Array<Array<double>> values)

Returns the minimum cost assignment of rows to columns, assuming that values[a][b] holds the cost incurred by assigning a -> b.