toy-lib-0.1.0.0
Safe HaskellSafe-Inferred
LanguageHaskell2010

Math.NTT

Description

Number-theoretic transform (integer DFT).

Synopsis

Documentation

bitRevSort :: (Vector v a, Vector v (Int, a)) => v a -> v a #

ntt :: KnownNat p => Vector (ModInt p) -> Vector (ModInt p) #

\(\Theta(N \log N)\) Number-theoretic transform (integer DFT).

>>> :set -XDataKinds
>>> ntt $ U.fromList $ map (ModInt @998244353) [1, 1, 1, 1]
[4,0,0,0]
>>> ntt $ U.fromList $ map (ModInt @469762049) [123, 0, 0, 0]
[123,123,123,123]

intt :: KnownNat p => Vector (ModInt p) -> Vector (ModInt p) #

\(\Theta(N \log N)\) Inverse number-theoretic transform (integer DFT).

convoluteMod :: forall p. KnownNat p => Vector (ModInt p) -> Vector (ModInt p) -> Vector (ModInt p) #

\(\Theta((N + M) \log (N + M))\) Convolution with mod.

\(\mathcal{F}[f*g](\omega) = \mathcal{F}[f](\omega) \mathcal{F}[g](\omega)\).

\(\mathbb{a} * \mathbb{b} = \mathcal{F^{-1}}(\mathcal{F}(\mathbb{a}) \mathcal{F}(\mathbb{b}))\)

>>> :set -XDataKinds
>>> convoluteMod @998244353 (U.fromList [1, 1, 1, 0]) (U.fromList [1, 1, 1, 0])
[1,2,3,2,1,0,0]
>>> convoluteMod @998244353 (U.fromList [1, 1, 1]) (U.fromList [1, 1, 1, 0])
[1,2,3,2,1,0]

type M1 = 754974721 :: Nat #

Modulo type variable for CRT in convolute64.

type M2 = 167772161 :: Nat #

Modulo type variable for CRT in convolute64.

type M3 = 469762049 :: Nat #

Modulo type variable for CRT in convolute64.

convolute64 :: Vector Int -> Vector Int -> Vector Int #

\(\Theta((N+M)\log(N+M))\) Convolution without mod (much heavier than convoluteMod).

See also: convolution_ll (ACL).

butterfly :: forall p m. (KnownNat p, PrimMonad m) => MVector (PrimState m) (ModInt p) -> m () #

\(\Theta(N \log N)\)

butterfly1 :: forall p m. (KnownNat p, PrimMonad m) => MVector (PrimState m) (ModInt p) -> ModInt p -> Int -> Int -> m () #

\(\Theta(N)\) Butterfly calculation performed in-place.

  • iMaxBit: Length of xs equals to bit iMaxBit
  • iBit: The length of a pair equals to the length of xs.

See also: my devlog post.

invButterfly :: forall p m. (KnownNat p, PrimMonad m) => MVector (PrimState m) (ModInt p) -> m () #

\(\Theta(N \log N)\). Does not contain scaling and index sorting.

invButterfly1 :: forall p m. (KnownNat p, PrimMonad m) => MVector (PrimState m) (ModInt p) -> ModInt p -> Int -> Int -> m () #

\(\Theta(N)\) Inverse butterfly calculation performed in-place.

grow2 :: forall a. (Num a, Unbox a) => Vector a -> Vector a #

Grow to the length of a power two.

>>> grow2 $ U.fromList [0 :: Int, 1, 2, 3, 4]
[0,1,2,3,4,0,0,0]

primRoot :: Int -> Int #

Primitive root.