Packagecom.masterbaboon
Classpublic class AdvancedMath
InheritanceAdvancedMath Inheritance Object

Collection of advanced math functions for AS3



Public Methods
 MethodDefined By
  
add(x:Array, y:Array):Array
[static] Add two arrays elementwise.
AdvancedMath
  
factorial(x:int):Number
[static] Computes the factorial of x.
AdvancedMath
  
gamma(x:Number):Number
[static] Lanczos implementation of the Gamma function, cf.
AdvancedMath
  
lnGamma(x:Number):Number
[static] Lanczos implementation of the log Gamma function, cf.
AdvancedMath
  
mean(x:Array):Number
[static] Compute the mean of the input array x.
AdvancedMath
  
sampleDirichlet(alpha:Array):Array
[static] Sample from a Dirichlet distribution.
AdvancedMath
  
sampleGamma(k:Number, theta:Number):Number
[static] Sample from a Gamma(k, theta) distribution.
AdvancedMath
  
sampleMultinomial(p:Array):int
[static] Sample from a multinomial distribution.
AdvancedMath
  
sum(x:Array):Number
[static] Compute the sum of the elements of the input array x
AdvancedMath
  
variance(x:Array):Number
[static] Compute the variance of the input array x.
AdvancedMath
  
zeros(dim:int, val:Number = 0):Array
[static] Create a new array with elements initialized to a particular value.
AdvancedMath
Public Constants
 ConstantDefined By
  FACTORIAL_MAXEXACT : Number = 20
[static]
AdvancedMath
Method Detail
add()method
public static function add(x:Array, y:Array):Array

Add two arrays elementwise. They must be of the same length.

Parameters

x:Array
 
y:Array

Returns
Array
factorial()method 
public static function factorial(x:int):Number

Computes the factorial of x. Requested values are cached to speed-up future calls. If x>FACTORIAL_MAXEXACT, the result is approximated using the Gamma function, as x! = Gamma(x+1) .

Parameters

x:int

Returns
Number
gamma()method 
public static function gamma(x:Number):Number

Lanczos implementation of the Gamma function, cf. Numerical Recipes in C (2nd edition, p.213). Reference: Lanczos, C. 1964, SIAM Journal on Numerical Analysis, ser. B, vol. 1, pp. 86–96.

Parameters

x:Number

Returns
Number
lnGamma()method 
public static function lnGamma(x:Number):Number

Lanczos implementation of the log Gamma function, cf. Numerical Recipes in C (2nd edition, p.213). Reference: Lanczos, C. 1964, SIAM Journal on Numerical Analysis, ser. B, vol. 1, pp. 86–96.

Parameters

x:Number

Returns
Number
mean()method 
public static function mean(x:Array):Number

Compute the mean of the input array x.

Parameters

x:Array

Returns
Number
sampleDirichlet()method 
public static function sampleDirichlet(alpha:Array):Array

Sample from a Dirichlet distribution. Dirichlet distribution can be used as prior for multinomial variables.

Parameters

alpha:Array — Array of parameters for the Dirichlet distribution

Returns
Array

See also

sampleDirichlet2
sampleGamma()method 
public static function sampleGamma(k:Number, theta:Number):Number

Sample from a Gamma(k, theta) distribution. The definition and the sample generating algorithm are as at http://en.wikipedia.org/wiki/Gamma_distribution . For the Matlab Gamma(alpha, beta) definition, call Gamma(alpha, 1/beta).

Parameters

k:Number
 
theta:Number

Returns
Number
sampleMultinomial()method 
public static function sampleMultinomial(p:Array):int

Sample from a multinomial distribution. In other words, choose randomly from a set of N choices with probabilities p[0], ..., p[N-1]

Parameters

p:Array — An array of probabilities, one for each element. p must sum to 1.

Returns
int
sum()method 
public static function sum(x:Array):Number

Compute the sum of the elements of the input array x

Parameters

x:Array

Returns
Number
variance()method 
public static function variance(x:Array):Number

Compute the variance of the input array x.

Parameters

x:Array

Returns
Number
zeros()method 
public static function zeros(dim:int, val:Number = 0):Array

Create a new array with elements initialized to a particular value.

Parameters

dim:int — Number of dimensions
 
val:Number (default = 0) — Value to initialize the array to. Default: 0

Returns
Array
Constant Detail
FACTORIAL_MAXEXACTConstant
public static const FACTORIAL_MAXEXACT:Number = 20