Module Math
In: lib/standard/facets/math/sqsolve.rb
lib/standard/facets/math/factorial.rb
lib/standard/facets/math/pow.rb
lib/standard/facets/math/variance.rb
lib/standard/facets/math/asech.rb
lib/standard/facets/math/lcm.rb
lib/standard/facets/math/coth.rb
lib/standard/facets/math/summed_sqdevs.rb
lib/standard/facets/math/atkinson_index.rb
lib/standard/facets/math/sech.rb
lib/standard/facets/math/tgamma.rb
lib/standard/facets/math/theil_index.rb
lib/standard/facets/math/floor.rb
lib/standard/facets/math/percentile.rb
lib/standard/facets/math/acsch.rb
lib/standard/facets/math/log2.rb
lib/standard/facets/math/ceil.rb
lib/standard/facets/math/min.rb
lib/standard/facets/math/acsc.rb
lib/standard/facets/math/gcd.rb
lib/standard/facets/math/approx_equal.rb
lib/standard/facets/math/asec.rb
lib/standard/facets/math/sign.rb
lib/standard/facets/math/epsilon.rb
lib/standard/facets/math/tau.rb
lib/standard/facets/math/sum.rb
lib/standard/facets/math/root.rb
lib/standard/facets/math/csc.rb
lib/standard/facets/math/median.rb
lib/standard/facets/math/distance.rb
lib/standard/facets/math/lngamma.rb
lib/standard/facets/math/sinc.rb
lib/standard/facets/math/sqr.rb
lib/standard/facets/math/beta.rb
lib/standard/facets/math/cot.rb
lib/standard/facets/math/amd.rb
lib/standard/facets/math/acoth.rb
lib/standard/facets/math/linsolve.rb
lib/standard/facets/math/acot.rb
lib/standard/facets/math/ec.rb
lib/standard/facets/math/round.rb
lib/standard/facets/math/abs.rb
lib/standard/facets/math/exp10.rb
lib/standard/facets/math/rmd.rb
lib/standard/facets/math/delta.rb
lib/standard/facets/math/kldivergence.rb
lib/standard/facets/math/csch.rb
lib/standard/facets/math/std.rb
lib/standard/facets/math/cdf.rb
lib/standard/facets/math/exp2.rb
lib/standard/facets/math/sec.rb
lib/standard/facets/math/mean.rb
lib/standard/facets/math/gini_coefficient.rb

Methods

abs   acosec   acot   acoth   acsc   acsch   amd   approx_equal   asec   asech   atkinson_index   beta   cdf   ceil   cosec   cosech   cot   coth   csc   csch   delta   distance   epsilon   exp10   exp2   factorial   floor   gcd   gini_coefficient   kldivergence   lcm   linsolve   ln_gamma   lngamma   log2   max   mean   median   min   percentile   pow   pstd   pvariance   pwr   rmd   root   round   sec   sech   sgn   sign   sinc   sqr   sqsolve   std   stderr   sum   summed_sqdevs   tgamma   theil_index   unit_step   variance   variance2  

Constants

FACTORIALS = [ 1, 1, 2, 6, 24, 120, 720, 5_040, 40_320, 362_880, 3_628_800, 39_916_800, 479_001_600, 6_227_020_800, 87_178_291_200, 1_307_674_368_000   First 16 factorials.
INVERSE_LN_2 = 1.0 / ::Math.log(2.0)
EPSILON = 0.000000001  
TAU = 2 * PI   See tauday.com/tau-manifesto
EC = 0.577_215_664_901_532_861   Euler‘s constant.

External Aliases

amd -> absolute_mean_difference
rmd -> relative_mean_difference
std -> standard_deviation
mean -> mean_average

Public Class methods

Absolute value of x.

Arcus cosecans of `x`.

Arcus cotangens of x

Area cotangens hyperbolicus of x

Arcus cosecans of `x`.

The average absolute difference of two independent values drawn from the sample. Equal to the RMD * mean.

Approximately equal.

TODO: Use core extension Numeric#approx? instead (?)

Arcus secans of x

Closely related to the Theil index and easily expressible in terms of it.

AI = 1-e^{theil_index}

en.wikipedia.org/wiki/Atkinson_index

Beta function of `x` and `y`.

    beta(x, y) = tgamma(x) * tgamma(y) / tgamma(x + y)

Returns the Cumulative Density Function of this sample (normalised to a fraction of 1.0).

Smallest integer not smaller than x.

Cosecans of `x`.

Cosecans hyperbolicus of `x`.

Cotangens of x

Cotangens hyperbolicus of x

Cosecans of `x`.

Cosecans hyperbolicus of `x`.

Kronecker symbol of i and j. Returns 1 if i and j are equal, 0 otherwise.

Calculates the Euclidean Distance between points p and q.

`p`, `q` is assumed to described coordinates in N-dimensions, e. g.:

   Math.distance([1, 1], [2, 2])          # 2D coordinates
   Math.distance([1, 1, 1], [2, 2, 2])    # 3D coordinates

If N is 1, then `::distance` may also be invoked like so:

   Math.distance(1, 1)

Levi-Civita symbol of i, j, and k - 1 if (i, j, k) is (1, 2, 3), (2, 3, 1), or (3, 1, 2), -1 if it is (1, 3, 2), (2, 1, 3), or (3, 2, 1), 0 as long as i, j, and k are all elements of {1, 2, 3}, otherwise returns nil.

10 to the power x

2 to the power x

1 * 2 * … * n, nil for negative numbers

Largest integer not larger than x.

Greatest common divisor of m and n, nil for non-positive numbers - gcd is computed by means of the Euclidian algorithm.

Calculates the Gini Coefficient (a measure of inequality of a distribution based on the area between the Lorenz curve and the uniform curve).

en.wikipedia.org/wiki/Gini_coefficient

This is a slightly cleaner way of calculating the Gini Coefficient then the previous implementationj.

  GC = \frac{\sum_{i=1}^N (2i-N-1)x_i}{N^2-\bar{x}}

The Kullback-Leibler divergence from this array to that of q.

NB: You will possibly want to sort both P and Q before calling this depending on what you‘re actually trying to measure.

en.wikipedia.org/wiki/Kullback-Leibler_divergence

Least common multiple of m and n, computed by multiplying m and n and dividing the product by the gcd of m and n, nil for non-positive numbers.

Returns real solution(s) of +a+x + b = c or nil if no or an infinite number of solutions exist. If c is missing it is assumed to be 0.

@author Josef Schugt

Old name used by Extmath library.

Logarithmus naturalis of gamma function of `x`.

Notice the use of `ln` prefix to differentiate from Ruby‘s built-in `lgamma` function which returns an Array.

Logarithmus dualis of x.

Mean average.

Returns the numerical median for the an array of values; or nil if array is empty.

Returns the percentile value for percentile pcnt; nil if array is empty.

pcnt should be expressed as an integer, e.g. `percentile(90)` returns the 90th percentile of the array.

Algorithm from NIST

NOTE: This is not a common core extension and is not loaded automatically when using require ‘facets‘.

CREDIT: Ben Koski

@non-core

  require 'facets/array/precentile'

`x` to the power `y`.

Standard deviation of a population.

Variance of a population. Variance of 0 or 1 elements is 0.0.

`x` to the power `y`.

Calculates the relative mean difference of this sample. Makes use of the fact that the Gini Coefficient is half the RMD.

The `y` root of `x`.

Round number to an integer.

Secans of x.

Secans hyperbolicus of x

Sign of `x`. This function returns `-1.0` if `x` is negative, `+1.0` if `x` is positive `x`, and `0.0` if `x = 0`.

Sinc function of x.

Square of number.

Returns array of real solution of ax**2 + bx + c = d or nil if no or an infinite number of solutions exist. If d is missing it is assumed to be 0.

In order to solve ax**2 + bx + c = d sqsolve identifies several cases:

  • a == 0: The equation to be solved is the linear equation bx + c = d. sqsolve> delegates the computation to linsolve>. If it results in nil, nil is returned (not [nil]!). Otherwise a one-element array containing result of linsolve is returned.
  • a != 0:
     The equation to be solved actually is a second order one.
     * <code>c == d</code>
       The equation to be solved is <code>ax**2 + bx = 0</code>. One solution of this equation obviously is
       <code>x = 0</code>, the second one solves <code>ax + b = 0</code>. The solution of the latter is
       delegated to +linsolve+. An array containing both results in ascending order is returned.
     * <code>c != d</code>
       The equation cannot be separated into <code>x</code> times some factor.
       * <code>b == 0</code>
         The equation to be solved is <code>ax**2 + c = d</code>. This can be written as the linear equation
         <code>ay + c = d</code> with <code>y = x ** 2</code>. The solution of the linear equation is delegated
         to +linsolve+. If the returned value for +y+ is +nil+, that becomes the overall return value.
         Otherwise an array containing the negative and positive squareroot of +y+ is returned
       * <code>b != 0 </code>
         The equation cannot be reduced to simpler cases. We now first have to compute what is called the
         discriminant <code>x = b**2 + 4a(d - c)</code> (that's what we need to compute the square root of).
         If the descriminant is negative no real solution exists and <code>nil</code> is returned. The ternary
         operator checking whether <code>b</code> is negative does ensure better numerical stability --only one
         of the two solutions is computed using the widely know formula for solving second order equations.
         The second one is computed from the fact that the product of both solutions is <code>(c - d) / a</code>.
         Take a look at a book on numerical mathematics if you don't understand why this should be done.
    

@author Josef Schugt

Standard deviation of a sample.

Calculates the standard error of a sample.

Returns sum. When a block is given, summation is taken over the each result of block evaluation.

The sum of the squared deviations from the mean.

Exp of LGamma.

Calculates the Theil index (a statistic used to measure economic inequality).

TI = \sum_{i=1}^N \frac{x_i}{\sum_{j=1}^N x_j} ln \frac{x_i}{\bar{x}}

  http://en.wikipedia.org/wiki/Theil_index

The *Heaviside step function*, also called the the *unit step function*. This functions works like `Math.sign` but by default returns `1.0` for zero.

Variance of the sample. Variance of 0 or 1 elements is 0.0.

TODO: Same as variance? Then choose one.

Public Instance methods

Area cosecans hyperbolicus of x

Area secans hyperbolicus of x

[Validate]