Faulhaber's triangle: Difference between revisions
Content added Content deleted
m (→{{header|Python}}: Pruned one import, updated primitives, tidied.) |
|||
Line 2,267: | Line 2,267: | ||
<lang python>'''Faulhaber's triangle''' |
<lang python>'''Faulhaber's triangle''' |
||
from itertools import |
from itertools import accumulate, chain, count, islice |
||
from fractions import |
from fractions import Fraction |
||
Line 2,275: | Line 2,275: | ||
'''List of rows of Faulhaber fractions.''' |
'''List of rows of Faulhaber fractions.''' |
||
def go(rs, n): |
def go(rs, n): |
||
def f(x, y): |
|||
return Fraction(n, x) * y |
|||
xs = list(map(f, islice(count(2), m), rs)) |
|||
⚫ | |||
return [Fraction(1 - sum(xs), 1)] + xs |
return [Fraction(1 - sum(xs), 1)] + xs |
||
return list(accumulate( |
return list(accumulate( |
||
[[]] + list(islice(count(0), 1 + m)), |
[[]] + list(islice(count(0), 1 + m)), |
||
Line 2,291: | Line 2,291: | ||
positive integers. |
positive integers. |
||
''' |
''' |
||
def go(x, y): |
|||
⚫ | |||
return sum( |
return sum( |
||
⚫ | |||
list(starmap( |
|||
⚫ | |||
⚫ | |||
⚫ | |||
) |
) |
||
# |
# ------------------------- TEST ------------------------- |
||
def main(): |
def main(): |
||
'''Tests''' |
'''Tests''' |
||
Line 2,319: | Line 2,319: | ||
# |
# ----------------------- DISPLAY ------------------------ |
||
# fTable :: String -> (a -> String) -> |
# fTable :: String -> (a -> String) -> |
||
# |
# (b -> String) -> (a -> b) -> [a] -> String |
||
def fTable(s): |
def fTable(s): |
||
'''Heading -> x |
'''Heading -> x display function -> |
||
fx display function -> f -> xs -> tabular string. |
|||
''' |
''' |
||
def |
def gox(xShow): |
||
def gofx(fxShow): |
|||
def gof(f): |
|||
def goxs(xs): |
|||
ys = [xShow(x) for x in xs] |
|||
w = max(map(len, ys)) |
|||
⚫ | |||
def arrowed(x, y): |
|||
return lambda xShow: lambda fxShow: lambda f: lambda xs: go( |
|||
return y.rjust(w, ' ') + ' -> ' + ( |
|||
xShow, fxShow, f, xs |
|||
fxShow(f(x)) |
|||
⚫ | |||
) |
|||
return s + '\n' + '\n'.join( |
|||
map(arrowed, xs, ys) |
|||
⚫ | |||
return goxs |
|||
return gof |
|||
⚫ | |||
return gox |
|||
# |
# ----------------------- GENERIC ------------------------ |
||
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c |
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c |
||
Line 2,351: | Line 2,359: | ||
def concat(xs): |
def concat(xs): |
||
'''The concatenation of all the elements |
'''The concatenation of all the elements |
||
in a list or iterable. |
in a list or iterable. |
||
⚫ | |||
def f(ys): |
def f(ys): |
||
zs = list(chain(*ys)) |
zs = list(chain(*ys)) |
||
Line 2,368: | Line 2,377: | ||
f lifted to a function over a list. |
f lifted to a function over a list. |
||
''' |
''' |
||
def go(xs): |
|||
return list(map(f, xs)) |
|||
return go |
|||
Line 2,386: | Line 2,398: | ||
representation of the ratio r. |
representation of the ratio r. |
||
''' |
''' |
||
def go(n |
def go(n): |
||
def f(r): |
|||
d = r.denominator |
|||
return str(r.numerator).rjust(m, ' ') + ( |
|||
' |
('/' + str(d).ljust(n, ' ')) if 1 != d else ( |
||
' ' * (1 + n) |
|||
⚫ | |||
) |
) |
||
return f |
|||
return |
return go |
||