Skip to content

Commit

Permalink
Introduce Monom type, a strict pair
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodigrim committed Oct 14, 2024
1 parent d1c6078 commit 0665a66
Show file tree
Hide file tree
Showing 20 changed files with 389 additions and 282 deletions.
3 changes: 2 additions & 1 deletion poly.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ library
Data.Poly.Internal.Multi.Field
Data.Poly.Internal.Multi.GcdDomain
Data.Poly.Internal.Multi.Laurent
Data.Poly.Internal.Multi.Monom

build-depends:
base >= 4.12 && < 5,
deepseq >= 1.1 && < 1.6,
primitive >= 0.6,
semirings >= 0.5.2,
vector >= 0.12.0.2
vector >= 0.13

if flag(sparse)
build-depends:
Expand Down
18 changes: 9 additions & 9 deletions src/Data/Poly/Internal/Convert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ import qualified Data.Poly.Internal.Multi as Sparse
--
-- @since 0.5.0.0
denseToSparse
:: (Eq a, Num a, G.Vector v a, G.Vector v (SU.Vector 1 Word, a))
:: (Eq a, Num a, G.Vector v a, G.Vector v (Sparse.Monom 1 a))
=> Dense.Poly v a
-> Sparse.Poly v a
denseToSparse = denseToSparseInternal 0

denseToSparse'
:: (Eq a, Semiring a, G.Vector v a, G.Vector v (SU.Vector 1 Word, a))
:: (Eq a, Semiring a, G.Vector v a, G.Vector v (Sparse.Monom 1 a))
=> Dense.Poly v a
-> Sparse.Poly v a
denseToSparse' = denseToSparseInternal zero

denseToSparseInternal
:: (Eq a, G.Vector v a, G.Vector v (SU.Vector 1 Word, a))
:: (Eq a, G.Vector v a, G.Vector v (Sparse.Monom 1 a))
=> a
-> Dense.Poly v a
-> Sparse.Poly v a
denseToSparseInternal z = Sparse.MultiPoly . G.imapMaybe (\i c -> if c == z then Nothing else Just (fromIntegral i, c)) . Dense.unPoly
denseToSparseInternal z = Sparse.MultiPoly . G.imapMaybe (\i c -> if c == z then Nothing else Just (Sparse.Monom (fromIntegral i) c)) . Dense.unPoly

-- | Convert from sparse to dense polynomials.
--
Expand All @@ -60,31 +60,31 @@ denseToSparseInternal z = Sparse.MultiPoly . G.imapMaybe (\i c -> if c == z then
--
-- @since 0.5.0.0
sparseToDense
:: (Num a, G.Vector v a, G.Vector v (SU.Vector 1 Word, a))
:: (Num a, G.Vector v a, G.Vector v (Sparse.Monom 1 a))
=> Sparse.Poly v a
-> Dense.Poly v a
sparseToDense = sparseToDenseInternal 0

sparseToDense'
:: (Semiring a, G.Vector v a, G.Vector v (SU.Vector 1 Word, a))
:: (Semiring a, G.Vector v a, G.Vector v (Sparse.Monom 1 a))
=> Sparse.Poly v a
-> Dense.Poly v a
sparseToDense' = sparseToDenseInternal zero

sparseToDenseInternal
:: (G.Vector v a, G.Vector v (SU.Vector 1 Word, a))
:: (G.Vector v a, G.Vector v (Sparse.Monom 1 a))
=> a
-> Sparse.Poly v a
-> Dense.Poly v a
sparseToDenseInternal z (Sparse.MultiPoly xs)
| G.null xs = Dense.Poly G.empty
| otherwise = runST $ do
let len = fromIntegral (SU.head (fst (G.unsafeLast xs)) + 1)
let len = fromIntegral (SU.head (Sparse.monomPower (G.unsafeLast xs)) + 1)
ys <- MG.unsafeNew len
MG.set ys z
let go xi yi
| xi >= G.length xs = pure ()
| (yi', c) <- G.unsafeIndex xs xi
| Sparse.Monom yi' c <- G.unsafeIndex xs xi
, yi == fromIntegral (SU.head yi')
= MG.unsafeWrite ys yi c >> go (xi + 1) (yi + 1)
| otherwise = go xi (yi + 1)
Expand Down
Loading

0 comments on commit 0665a66

Please sign in to comment.