{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE NoMonomorphismRestriction #-}

module Algebra.Prelude.Core
  ( (%),
    Scalar (..),
    (.*.),
    logBase2,
    ceilingLogBase2,
    module AlgebraicPrelude,
    module Algebra.Ring.Polynomial.Internal,
    module Algebra.Ring.Ideal,
    module Algebra.Normed,
    module Algebra.Internal,
  )
where

import Algebra.Internal
import Algebra.Normed
import Algebra.Ring.Ideal
import Algebra.Ring.Polynomial.Internal
import Algebra.Scalar
import AlgebraicPrelude hiding (lex, (%))
import Data.Bits (Bits (..), FiniteBits (..))

(%) ::
  (IsPolynomial poly, Division (Coefficient poly)) =>
  Coefficient poly ->
  Coefficient poly ->
  poly
Coefficient poly
n % :: Coefficient poly -> Coefficient poly -> poly
% Coefficient poly
m = Coefficient poly -> poly
forall poly. IsPolynomial poly => Coefficient poly -> poly
injectCoeff (Coefficient poly
n Coefficient poly -> Coefficient poly -> Coefficient poly
forall r. Division r => r -> r -> r
/ Coefficient poly
m)

infixl 7 %

logBase2 :: Int -> Int
logBase2 :: Int -> Int
logBase2 Int
x = Int -> Int
forall b. FiniteBits b => b -> Int
finiteBitSize Int
x Int -> Int -> Int
forall r. Group r => r -> r -> r
- Int
1 Int -> Int -> Int
forall r. Group r => r -> r -> r
- Int -> Int
forall b. FiniteBits b => b -> Int
countLeadingZeros Int
x
{-# INLINE logBase2 #-}

ceilingLogBase2 :: Int -> Int
ceilingLogBase2 :: Int -> Int
ceilingLogBase2 Int
n =
  if Int -> Int
forall a. Bits a => a -> Int
popCount Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1
    then Int -> Int
logBase2 Int
n
    else Int -> Int
logBase2 Int
n Int -> Int -> Int
forall r. Additive r => r -> r -> r
+ Int
1