{-# LANGUAGE NoImplicitPrelude #-}
module OAlg.AbelianGroup.Euclid
( gcd, gcds, lcm, lcms, euclid, mod0, (//)
)
where
import OAlg.Prelude
import Data.List (foldl)
import OAlg.Data.Canonical
import OAlg.Structure.Additive
import OAlg.Structure.Multiplicative
import OAlg.Structure.Number
mod0 :: Z -> N -> Z
mod0 :: Z -> N -> Z
mod0 Z
z N
0 = Z
z
mod0 Z
z N
n = Z -> Z -> Z
forall a. Integral a => a -> a -> a
mod Z
z (N -> Z
forall a b. Embeddable a b => a -> b
inj N
n)
infix 7 //
(//) :: Z -> N -> Z
Z
0 // :: Z -> N -> Z
// N
0 = Z
1
Z
_ // N
0 = Z
0
Z
z // N
n = Z -> Z -> Z
forall a. Integral a => a -> a -> a
div Z
z (N -> Z
forall a b. Embeddable a b => a -> b
inj N
n)
euclid :: Z -> Z -> (N,Z,Z)
euclid :: Z -> Z -> (N, Z, Z)
euclid Z
a Z
b = (Z -> N
forall a b. Projectible a b => b -> a
prj Z
r,Z -> Z
forall r. Number r => r -> r
signum Z
a Z -> Z -> Z
forall c. Multiplicative c => c -> c -> c
* Z
s,Z -> Z
forall r. Number r => r -> r
signum Z
b Z -> Z -> Z
forall c. Multiplicative c => c -> c -> c
* Z
t) where
(Z
r,Z
s,Z
t) = (Z, Z, Z) -> (Z, Z, Z) -> (Z, Z, Z)
forall {c}.
(Num c, Abelian c, Integral c) =>
(c, c, c) -> (c, c, c) -> (c, c, c)
eval (Z -> Z
forall r. Number r => r -> r
abs Z
a,Z
1,Z
0) (Z -> Z
forall r. Number r => r -> r
abs Z
b,Z
0,Z
1)
eval :: (c, c, c) -> (c, c, c) -> (c, c, c)
eval (c, c, c)
u (c
0 ,c
_ ,c
_ ) = (c, c, c)
u
eval (c
r'',c
s'',c
t'') (c
r',c
s',c
t')
= c
s' c -> (c, c, c) -> (c, c, c)
forall a b. a -> b -> b
`seq` c
t' c -> (c, c, c) -> (c, c, c)
forall a b. a -> b -> b
`seq` (c, c, c) -> (c, c, c) -> (c, c, c)
eval (c
r',c
s',c
t') (c
r,c
s'' c -> c -> c
forall a. Abelian a => a -> a -> a
- c
qc -> c -> c
forall c. Multiplicative c => c -> c -> c
*c
s',c
t'' c -> c -> c
forall a. Abelian a => a -> a -> a
- c
qc -> c -> c
forall c. Multiplicative c => c -> c -> c
*c
t') where (c
q,c
r) = c -> c -> (c, c)
forall a. Integral a => a -> a -> (a, a)
divMod c
r'' c
r'
gcd :: N -> N -> N
gcd :: N -> N -> N
gcd N
a N
b = N
g where (N
g,Z
_,Z
_) = Z -> Z -> (N, Z, Z)
euclid (N -> Z
forall a b. Embeddable a b => a -> b
inj N
a) (N -> Z
forall a b. Embeddable a b => a -> b
inj N
b)
gcds :: [N] -> N
gcds :: [N] -> N
gcds = (N -> N -> N) -> N -> [N] -> N
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl N -> N -> N
gcd N
0
lcm :: N -> N -> N
lcm :: N -> N -> N
lcm N
0 N
0 = N
0
lcm N
a N
b = (N
a N -> N -> N
forall c. Multiplicative c => c -> c -> c
* N
b) N -> N -> N
forall a. Integral a => a -> a -> a
`div` N -> N -> N
gcd N
a N
b
lcms :: [N] -> N
lcms :: [N] -> N
lcms = (N -> N -> N) -> N -> [N] -> N
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl N -> N -> N
lcm N
1