<commit_before>{-# LANGUAGE DataKinds, EmptyDataDecls, FlexibleContexts, FlexibleInstances #-}
{-# LANGUAGE GADTs, MultiParamTypeClasses, NoMonomorphismRestriction        #-}
{-# LANGUAGE ParallelListComp, PolyKinds, RankNTypes, ScopedTypeVariables   #-}
{-# LANGUAGE TypeFamilies, TypeOperators, UndecidableInstances              #-}
module Algebra.Field.Galois (GF0(), IsGF0, modPoly, modVec, reifyGF0,
                             withGF0, GF'(), Conway, primitive, conway,
                             conwayFile, addConwayPolynomials)  where
import           Algebra.Field.Finite
import           Algebra.Field.Galois.Conway
import           Algebra.Internal
import           Algebra.Prelude
import           Control.Lens                 (imap)
import           Control.Monad                (replicateM)
import           Control.Monad.Loops          (iterateUntil)
import           Control.Monad.Random         (MonadRandom)
import           Control.Monad.Random         (uniform)
import qualified Data.Ratio                   as Rat
import           Data.Reflection
import           Data.Singletons.Prelude.Enum (PEnum (..), SEnum (..))
import           Data.Singletons.TypeLits     (withKnownNat)
import           Data.Type.Natural            hiding (one, zero)
import qualified Data.Type.Natural.Builtin    as TN
import qualified Data.Vector                  as V
import           Data.Vector.Sized            (Vector ((:-), Nil))
import qualified Data.Vector.Sized            as SV
import qualified GHC.TypeLits                 as TL
import           Numeric.Decidable.Units
import           Numeric.Decidable.Zero
import           Numeric.Semiring.Integral    (IntegralSemiring)
import           Prelude                      hiding (Fractional (..), Num (..),
                                               gcd, quot, rem, sum, (^))
import qualified Prelude                      as P

-- | Galois field of order @p^n@.
--   @f@ stands for the irreducible polynomial over @F_p@ of degree @n@.
data GF0 p (n :: TL.Nat) (f :: *) = GF0 { runGF0 :: SV.Vector (F p) (TN.ToPeano n) } deriving (Eq)
type Unipol a = OrderedPolynomial a Grevlex One

-- | Galois Field of order @p^n@. This uses conway polynomials
--   as canonical minimal polynomial and it should be known at
--   compile-time (i.e. @Reifies (Conway p n) (Unipol (F n))@
--   instances should be defined to use field operations).
type GF' (p :: TL.Nat) n = GF0 p n (Conway p n)

modPoly :: forall p n f. (SingI n, Reifies p Integer) => Unipol (F p) -> GF0 p n f
modPoly = withSingI (TN.sToPeano (sing :: Sing n)) $ GF0 . polyToVec

modVec :: Vector (F p) (TN.ToPeano n) -> GF0 p n f
modVec = GF0

instance (Reifies p Integer, Show (F p), SingI n) => Show (GF0 p n f)  where
  showsPrec _ (GF0 Nil) = showString "0"
  showsPrec d (GF0 (v :- vs)) =
    if SV.all isZero vs
    then showsPrec d v
    else showChar '<' . shows (vecToPoly $ v :- vs) . showChar '>'

vecToPoly :: (CoeffRing r)
          => SV.Vector r n -> Unipol r
vecToPoly v = sum $ imap (\i c -> injectCoeff c * varX^fromIntegral i) $ SV.toList v

polyToVec :: forall n r. (CoeffRing r, SingI n) => Unipol r -> SV.Vector r n
polyToVec f = SV.unsafeFromList' [ coeff (leadingMonomial $ (varX ^ i) `asTypeOf` f) f
                                 | i <- [0..sNatToInt (sing :: SNat n)]]

instance Reifies p Integer => Additive (GF0 p n f)  where
  GF0 v + GF0 u = GF0 $ SV.zipWithSame (+) v u

instance (Reifies p Integer, SingI n) => Monoidal (GF0 p n f) where
  zero = withSingI (TN.sToPeano (sing :: Sing n)) $ GF0 $ SV.replicate' zero

instance Reifies p Integer => LeftModule Natural (GF0 p n f) where
  n .* GF0 v = GF0 $ SV.map (n .*) v

instance Reifies p Integer => RightModule Natural (GF0 p n f) where
  GF0 v *. n = GF0 $ SV.map (*. n) v

instance Reifies p Integer => LeftModule Integer (GF0 p n f) where
  n .* GF0 v = GF0 $ SV.map (n .*) v

instance Reifies p Integer => RightModule Integer (GF0 p n f) where
  GF0 v *. n = GF0 $ SV.map (*. n) v

instance (SingI n, Reifies p Integer) => Group (GF0 p n f) where
  negate (GF0 v) = GF0 $ SV.map negate v
  GF0 u - GF0 v  = GF0 $ SV.zipWithSame (-) u v<commit_msg>Changed the way to display Galois field<commit_after>{-# LANGUAGE DataKinds, EmptyDataDecls, FlexibleContexts, FlexibleInstances #-}
{-# LANGUAGE GADTs, MultiParamTypeClasses, NoMonomorphismRestriction        #-}
{-# LANGUAGE ParallelListComp, PolyKinds, RankNTypes, ScopedTypeVariables   #-}
{-# LANGUAGE TypeFamilies, TypeOperators, UndecidableInstances              #-}
module Algebra.Field.Galois (GF0(), IsGF0, modPoly, modVec, reifyGF0,
                             withGF0, GF'(), Conway, primitive, conway,
                             conwayFile, addConwayPolynomials)  where
import           Algebra.Field.Finite
import           Algebra.Field.Galois.Conway
import           Algebra.Internal
import           Algebra.Prelude
import           Control.Lens                 (imap)
import           Control.Monad                (replicateM)
import           Control.Monad.Loops          (iterateUntil)
import           Control.Monad.Random         (MonadRandom)
import           Control.Monad.Random         (uniform)
import qualified Data.Ratio                   as Rat
import           Data.Reflection
import           Data.Singletons.Prelude.Enum (PEnum (..), SEnum (..))
import           Data.Singletons.TypeLits     (withKnownNat)
import           Data.Type.Natural            hiding (one, zero)
import qualified Data.Type.Natural.Builtin    as TN
import qualified Data.Vector                  as V
import           Data.Vector.Sized            (Vector ((:-), Nil))
import qualified Data.Vector.Sized            as SV
import qualified GHC.TypeLits                 as TL
import           Numeric.Decidable.Units
import           Numeric.Decidable.Zero
import           Numeric.Semiring.Integral    (IntegralSemiring)
import           Prelude                      hiding (Fractional (..), Num (..),
                                               gcd, quot, rem, sum, (^))
import qualified Prelude                      as P

-- | Galois field of order @p^n@.
--   @f@ stands for the irreducible polynomial over @F_p@ of degree @n@.
data GF0 p (n :: TL.Nat) (f :: *) = GF0 { runGF0 :: SV.Vector (F p) (TN.ToPeano n) } deriving (Eq)
type Unipol a = OrderedPolynomial a Grevlex One

-- | Galois Field of order @p^n@. This uses conway polynomials
--   as canonical minimal polynomial and it should be known at
--   compile-time (i.e. @Reifies (Conway p n) (Unipol (F n))@
--   instances should be defined to use field operations).
type GF' (p :: TL.Nat) n = GF0 p n (Conway p n)

modPoly :: forall p n f. (SingI n, Reifies p Integer) => Unipol (F p) -> GF0 p n f
modPoly = withSingI (TN.sToPeano (sing :: Sing n)) $ GF0 . polyToVec

modVec :: Vector (F p) (TN.ToPeano n) -> GF0 p n f
modVec = GF0

instance (Reifies p Integer, Show (F p), SingI n) => Show (GF0 p n f)  where
  showsPrec _ (GF0 Nil) = showString "0"
  showsPrec d (GF0 (v :- vs)) =
    if SV.all isZero vs
    then showsPrec d v
    else showChar '<' . showString (showPolynomialWithVars [(0, "ξ")] $ vecToPoly $ v :- vs) . showChar '>'

vecToPoly :: (CoeffRing r)
          => SV.Vector r n -> Unipol r
vecToPoly v = sum $ imap (\i c -> injectCoeff c * varX^fromIntegral i) $ SV.toList v

polyToVec :: forall n r. (CoeffRing r, SingI n) => Unipol r -> SV.Vector r n
polyToVec f = SV.unsafeFromList' [ coeff (leadingMonomial $ (varX ^ i) `asTypeOf` f) f
                                 | i <- [0..sNatToInt (sing :: SNat n)]]

instance Reifies p Integer => Additive (GF0 p n f)  where
  GF0 v + GF0 u = GF0 $ SV.zipWithSame (+) v u

instance (Reifies p Integer, SingI n) => Monoidal (GF0 p n f) where
  zero = withSingI (TN.sToPeano (sing :: Sing n)) $ GF0 $ SV.replicate' zero

instance Reifies p Integer => LeftModule Natural (GF0 p n f) where
  n .* GF0 v = GF0 $ SV.map (n .*) v

instance Reifies p Integer => RightModule Natural (GF0 p n f) where
  GF0 v *. n = GF0 $ SV.map (*. n) v

instance Reifies p Integer => LeftModule Integer (GF0 p n f) where
  n .* GF0 v = GF0 $ SV.map (n .*) v

instance Reifies p Integer => RightModule Integer (GF0 p n f) where
  GF0 v *. n = GF0 $ SV.map (*. n) v

instance (SingI n, Reifies p Integer) => Group (GF0 p n f) where
  negate (GF0 v) = GF0 $ SV.map negate v
  GF0 u - GF0 v  = GF0 $ SV.zipWithSame (-) u v
{'fraction_non_alphanumeric': 0.10879160205177144, 'fraction_numerical': 0.009900990099009901, 'mean_word_length': 2.215957038741849, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6778385', 'n_tokens_mistral': 2815, 'n_tokens_neox': 2711, 'n_words': 946}