Module | Math

In:

lib/standard/facets/math/abs.rb
lib/standard/facets/math/acot.rb
lib/standard/facets/math/acoth.rb
lib/standard/facets/math/acsc.rb
lib/standard/facets/math/acsch.rb
lib/standard/facets/math/amd.rb
lib/standard/facets/math/approx_equal.rb
lib/standard/facets/math/asec.rb
lib/standard/facets/math/asech.rb
lib/standard/facets/math/atkinson_index.rb
lib/standard/facets/math/beta.rb
lib/standard/facets/math/cdf.rb
lib/standard/facets/math/ceil.rb
lib/standard/facets/math/cot.rb
lib/standard/facets/math/coth.rb
lib/standard/facets/math/csc.rb
lib/standard/facets/math/csch.rb
lib/standard/facets/math/delta.rb
lib/standard/facets/math/epsilon.rb
lib/standard/facets/math/exp10.rb
lib/standard/facets/math/exp2.rb
lib/standard/facets/math/factorial.rb
lib/standard/facets/math/floor.rb
lib/standard/facets/math/gamma.rb
lib/standard/facets/math/gcd.rb
lib/standard/facets/math/gini_coefficient.rb
lib/standard/facets/math/kldivergence.rb
lib/standard/facets/math/lcm.rb
lib/standard/facets/math/lgamma.rb
lib/standard/facets/math/linsolve.rb
lib/standard/facets/math/log2.rb
lib/standard/facets/math/mean.rb
lib/standard/facets/math/median.rb
lib/standard/facets/math/min.rb
lib/standard/facets/math/pow.rb
lib/standard/facets/math/rmd.rb
lib/standard/facets/math/root.rb
lib/standard/facets/math/sec.rb
lib/standard/facets/math/sech.rb
lib/standard/facets/math/sign.rb
lib/standard/facets/math/sinc.rb
lib/standard/facets/math/sqr.rb
lib/standard/facets/math/sqsolve.rb
lib/standard/facets/math/std.rb
lib/standard/facets/math/sum.rb
lib/standard/facets/math/summed_sqdevs.rb
lib/standard/facets/math/theil_index.rb
lib/standard/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

abs(x)

Absolute value of `x`.

acot(x)

Arcus cotangens of `x`

acoth(x)

Area cotangens hyperbolicus of `x`

acsc(x)

Arcus cosecans of `x`

amd(array)

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

approx_equal(a, b, epsilon=EPSILON)

Approximately equal.

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

asec(x)

Arcus secans of `x`

atkinson_index(array)

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(x, y)

Beta function of `x` and `y` - `beta(x, y) = tgamma(x) * tgamma(y) / tgamma(x + y)`

cdf(array, normalised=1.0)

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

ceil(x)

Smallest integer not smaller than `x`.

cot(x)

Cotangens of `x`

coth(x)

Cotangens hyperbolicus of `x`

csc(x)

Cosecans of `x`

csch(x)

Cosecans hyperbolicus of `x`

delta(i, j)

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

epsilon(i, j, k)

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`.

exp10(x)

10 to the power `x`

exp2(x)

2 to the power `x`

factorial(n)

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

floor(x)

Largest integer not larger than `x`.

gamma(x)
gcd(m, n)

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

gini_coefficient(array)

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}}

kldivergence(array, q)

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

lcm(m, n)

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.

lgamma(x)

Logarithmus naturalis of gamma function of `x`

linsolve(a, b, c = 0.0)

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.

log2(x)

Logarithmus dualis of `x`.

max(array, block) {|max| ...}
mean(array, &blk)

Mean average.

median(array)
min(array) {|min| ...}
pow(x, y)

`x` to the power `y`

pstd(array, &block)

Standard deviation of a population.

pvariance(array)

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

rmd(array)

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

root(x, y)

The `y` root of `x`.

sec(x)

Secans of `x`.

sech(x)

Secans hyperbolicus of `x`

sign(x)

Sign of `x`. Returns -1 for negative x, +1 for positive x and zero for x = 0

sinc(x)

Sinc function of `x`.

sqr(x)

Square of number.

sqsolve(a, b, c, d = 0.0)

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.

std(array, &block)

Standard deviation of a sample.

stderr(array)

Calculates the standard error of a sample.

sum(array) {|| ...}

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

summed_sqdevs(array)

The sum of the squared deviations from the mean.

theil_index(array)

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(array, &block)
variance2(array)

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

TODO: Same as variance? Then choose one.

### Public Instance methods

acsch(x)

Area cosecans hyperbolicus of `x`

asech(x)

Area secans hyperbolicus of `x`

[Validate]