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

Methods

abs   acot   acoth   acsc   acsch   amd   approx_equal   asec   asech   atkinson_index   beta   cdf   ceil   cot   coth   csc   csch   delta   epsilon   exp10   exp2   factorial   floor   gamma   gcd   gini_coefficient   kldivergence   lcm   lgamma   linsolve   log2   max   mean   median   min   pow   pstd   pvariance   rmd   root   sec   sech   sign   sinc   sqr   sqsolve   std   stderr   sum   summed_sqdevs   theil_index   variance   variance2  

Constants

EPSILON = 0.000000001  
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)

External Aliases

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

Public Class methods

Absolute value 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.

Cotangens of x

Cotangens hyperbolicus of x

Cosecans of x

Cosecans hyperbolicus of x

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

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.

Logarithmus naturalis of gamma function of x

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.

Logarithmus dualis of x.

Mean average.

x to the power y

Standard deviation of a population.

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

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.

Secans of x.

Secans hyperbolicus of x

Sign of x. Returns -1 for negative x, +1 for positive x and zero for 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.

Solving second order equations

In order to solve ax**2 + bx + c = d +Extmath.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 +Extmath.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 +Extmath.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.
    

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.

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

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]