Skip to content

Ratios

Status: Stable

documented, exercised by the test suite and/or worked examples, with no known limitations recorded.

Description

Ratios[list]

gives the successive ratios list[[k+1]]/list[[k]] of the elements of list (length l - 1).

Ratios[list, n] gives the n-th iterated ratios (length l - n); n must

Ratios[list, {n1, n2, ...}] gives the successive n_k-th ratios at

FoldList[Times, x, Ratios[list]] inverts Ratios.

Notes be a non-negative integer (n = 0 returns list unchanged). level k of a nested list; for a matrix m, Ratios\[m, n\] (= Ratios\[m, {n, 0}\]) takes ratios of successive rows. Ratios has the attribute Protected.

Examples (10)

Every input below was run against the current Mathilda build and its output recorded.

Basic examples (5)

In[1]:= Ratios[{a, b, c, d, e}]
Out[1]= {b/a, c/b, d/c, e/d}

In[2]:= Ratios[{a, b, c, d, e}, 2]
Out[2]= {(a c)/b^2, (b d)/c^2, (c e)/d^2}

In[3]:= Ratios[Table[2^i, {i, 0, 10}]]
Out[3]= {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}

In[4]:= Ratios[{{a11, a12, a13}, {a21, a22, a23}}, {0, 1}]
Out[4]= {{a12/a11, a13/a12}, {a22/a21, a23/a22}}

In[5]:= FoldList[Times, a, Ratios[{a, b, c, d, e}]]
Out[5]= {a, b, c, d, e}

Applications (5)

In[6]:= Ratios[{1, 2, 4, 8, 16}]
Out[6]= {2, 2, 2, 2}

In[7]:= Ratios[{1, 1, 2, 3, 5, 8, 13, 21}]
Out[7]= {1, 2, 3/2, 5/3, 8/5, 13/8, 21/13}

In[8]:= N[Ratios[{1, 1, 2, 3, 5, 8, 13, 21, 34, 55}]]
Out[8]= {1.0, 2.0, 1.5, 1.66667, 1.6, 1.625, 1.61538, 1.61905, 1.61765}

In[9]:= Ratios[{1, 1, 2, 6, 24, 120}]
Out[9]= {1, 2, 3, 4, 5}

In[10]:= FoldList[Times, 3, Ratios[{3, 6, 18, 36}]]
Out[10]= {3, 6, 18, 36}

Options & behaviour

Packed arrays. Ratios[list] divides in place on a real buffer. An integer buffer takes the ordinary path: Ratios[{1, 2, 3}] is {2, 3/2}, exact Rationals that no buffer holds.

Implementation notes

builtin_ratios returns successive ratios of list elements: Ratios[{a, b, c, ...}] gives {b/a, c/b, ...}. Ratios[list, n] applies the ratio operation n times (ratio_n); Ratios[list, {n1, n2, ...}] takes ratios along the given levels (ratio_levels). Each ratio is formed as a division, so the usual numeric/symbolic simplification follows. Level/count specs must be non-negative integers; bad shapes return NULL.

  • Protected.
  • Ratios[list] divides successive elements by preceding ones, giving {list[[2]]/list[[1]], list[[3]]/list[[2]], ...} of length l - 1; Ratios[{x1, x2}] gives {x2/x1}.
  • Ratios[list, n] applies the ratio operator n times, giving length l - n. n must be a non-negative integer (n = 0 returns list unchanged).
  • Ratios[list, {n1, n2, ...}] gives the successive nk-th ratios at level k of a nested list, and is equivalent to Ratios[Ratios[list, n1], {0, n2, ...}]. Each nk must be a non-negative integer.
  • Division threads element-wise over sublists via the Listable Power/Times, so for a matrix m, Ratios[m] (= Ratios[m, 1] = Ratios[m, {1, 0}]) takes ratios of successive rows within each column, while Ratios[m, {0, 1}] takes ratios of columns within each row.
  • The head of the input is preserved. A list shorter than the reduction yields the empty list. First ratios are constant for a geometric sequence.
  • Works on machine integers, GMP arbitrary-precision integers (exact Rational results), machine-precision doubles, and symbolic expressions.

Attributes: Protected.

References

See also: Differences, Power, Times, Rational

Notes & additional examples

Notes

Ratios[list] gives the successive ratios list[[k+1]]/list[[k]] (length l - 1). Ratios[list, n] iterates n times; Ratios[list, {n1, n2, ...}] takes the n_k-th ratios at level k of a nested list. FoldList[Times, x, Ratios[list]] inverts Ratios.