Family Day update

This commit is contained in:
Ingy döt Net 2020-02-17 23:21:07 -08:00
parent aac6731f2c
commit 9ad63ea473
2442 changed files with 39761 additions and 8255 deletions

View file

@ -1,11 +1,9 @@
import Control.Applicative (liftA2)
nthRoot :: Double -> Double -> Double
nthRoot n x =
fst $
until
(uncurry (==))
(((,) <*> ((/ n) . liftA2 (+) ((n - 1) *) ((x /) . (** (n - 1))))) . snd)
(((,) <*> ((/ n) . ((+) <$> ((n - 1) *) <*> (x /) . (** (n - 1))))) . snd)
(x, x / n)
-- TESTS --------------------------------------------------
@ -23,6 +21,6 @@ main =
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
fTable s xShow fxShow f xs =
let w = maximum (length . xShow <$> xs)
rjust n c = liftA2 drop length (replicate n c ++)
rjust n c = drop . length <*> (replicate n c ++)
in unlines $
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs

View file

@ -0,0 +1,21 @@
iroot(_, 0, 0) :- !.
iroot(M, N, R) :-
M > 1,
(N > 0 ->
irootpos(M, N, R)
;
N /\ 1 =:= 1,
NegN is -N, irootpos(M, NegN, R0), R is -R0).
irootpos(N, A, R) :-
X0 is 1 << (msb(A) div N), % initial guess is 2^(log2(A) / N)
newton(N, A, X0, X1),
iroot_loop(A, X1, N, A, R).
iroot_loop(X1, X2, _, _, X1) :- X1 =< X2, !.
iroot_loop(_, X1, N, A, R) :-
newton(N, A, X1, X2),
iroot_loop(X1, X2, N, A, R).
newton(2, A, X0, X1) :- X1 is (X0 + A div X0) >> 1, !. % fast special case
newton(N, A, X0, X1) :- X1 is ((N - 1)*X0 + A div X0**(N - 1)) div N.

View file

@ -0,0 +1,111 @@
'''Nth Root'''
from decimal import Decimal, getcontext
from operator import eq
# nthRoot :: Int -> Int -> Int -> Real
def nthRoot(precision):
'''The nth root of x at the given precision.'''
def go(n, x):
getcontext().prec = precision
dcn = Decimal(n)
def same(ab):
return eq(*ab)
def step(ab):
a, b = ab
predn = pred(dcn)
return (
b,
reciprocal(dcn) * (
predn * a + (
x / (a ** predn)
)
)
)
return until(same)(step)(
(x / dcn, 1)
)[0]
return lambda n: lambda x: go(n, x)
# --------------------------TEST---------------------------
def main():
'''Nth roots at various precisions'''
def xShow(tpl):
p, n, x = tpl
return rootName(n) + (
' of ' + str(x) + ' at precision ' + str(p)
)
def f(tpl):
p, n, x = tpl
return nthRoot(p)(n)(x)
print(
fTable(main.__doc__ + ':\n')(xShow)(str)(f)(
[(10, 5, 34), (20, 10, 42), (30, 2, 5)]
)
)
# -------------------------DISPLAY-------------------------
# fTable :: String -> (a -> String) ->
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
'''Heading -> x display function -> fx display function ->
f -> xs -> tabular string.
'''
def go(xShow, fxShow, f, xs):
ys = [xShow(x) for x in xs]
w = max(map(len, ys))
return s + '\n' + '\n'.join(map(
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
xs, ys
))
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
xShow, fxShow, f, xs
)
# -------------------------GENERIC-------------------------
# rootName :: Int -> String
def rootName(n):
'''English ordinal suffix.'''
return ['identity', 'square root', 'cube root'][n - 1] if (
4 > n or 1 > n
) else (str(n) + 'th root')
# pred :: Enum a => a -> a
def pred(x):
'''The predecessor of a value. For numeric types, (- 1).'''
return x - 1
# reciprocal :: Num -> Num
def reciprocal(x):
'''Arithmetic reciprocal of x.'''
return 1 / x
# until :: (a -> Bool) -> (a -> a) -> a -> a
def until(p):
'''The result of repeatedly applying f until p holds.
The initial seed value is x.
'''
def go(f, x):
v = x
while not p(v):
v = f(v)
return v
return lambda f: lambda x: go(f, x)
if __name__ == '__main__':
main()

View file

@ -0,0 +1,36 @@
extension FloatingPoint where Self: ExpressibleByFloatLiteral {
@inlinable
public func power(_ e: Int) -> Self {
var res = Self(1)
for _ in 0..<e {
res *= self
}
return res
}
@inlinable
public func root(n: Int, epsilon: Self = 2.220446049250313e-16) -> Self {
var d = Self(0)
var res = Self(1)
guard self != 0 else {
return 0
}
guard n >= 1 else {
return .nan
}
repeat {
d = (self / res.power(n - 1) - res) / Self(n)
res += d
} while d >= epsilon * 10 || d <= -epsilon * 10
return res
}
}
print(81.root(n: 4))
print(13.root(n: 5))