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
be a non-negative integer (n = 0 returns list unchanged).
Ratios[list, {n1, n2, ...}] gives the successive n_k-th ratios at
level k of a nested list; for a matrix m, Ratios[m, n] (= Ratios[m, {n, 0}]) takes ratios of successive rows.
FoldList[Times, x, Ratios[list]] inverts Ratios.
Ratios has the attribute Protected.
Examples¶
All examples below are verified against the current Mathilda build.
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}
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 lengthl - 1;Ratios[{x1, x2}]gives{x2/x1}.Ratios[list, n]applies the ratio operatorntimes, giving lengthl - n.nmust be a non-negative integer (n = 0returnslistunchanged).Ratios[list, {n1, n2, ...}]gives the successivenk-th ratios at levelkof a nested list, and is equivalent toRatios[Ratios[list, n1], {0, n2, ...}]. Eachnkmust be a non-negative integer.- Division threads element-wise over sublists via the
ListablePower/Times, so for a matrixm,Ratios[m](=Ratios[m, 1]=Ratios[m, {1, 0}]) takes ratios of successive rows within each column, whileRatios[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
Rationalresults), machine-precision doubles, and symbolic expressions.
Attributes: Protected.
Implementation status¶
Stable — documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
References¶
- Source:
src/list.c - Specification:
docs/spec/builtins/arithmetic.md
Notes & additional examples¶
Worked examples¶
A constant ratio list is the fingerprint of a geometric sequence. Applied to the
Fibonacci numbers, Ratios produces the classic convergents of the golden
ratio, which numerically close in on φ:
In[1]:= Ratios[{1, 1, 2, 3, 5, 8, 13, 21}]
Out[1]= {1, 2, 3/2, 5/3, 8/5, 13/8, 21/13}
In[2]:= N[Ratios[{1, 1, 2, 3, 5, 8, 13, 21, 34, 55}]]
Out[2]= {1.0, 2.0, 1.5, 1.66667, 1.6, 1.625, 1.61538, 1.61905, 1.61765}
The successive ratios of factorials collapse to the integers, and FoldList
with Times reconstructs the original list from a seed and its ratios:
In[1]:= Ratios[{1, 1, 2, 6, 24, 120}]
Out[1]= {1, 2, 3, 4, 5}
In[2]:= FoldList[Times, 3, Ratios[{3, 6, 18, 36}]]
Out[2]= {3, 6, 18, 36}
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.