Skip to content

Commit

Permalink
Sort monomials on constructing polynomials
Browse files Browse the repository at this point in the history
  • Loading branch information
iokasimov committed May 15, 2024
1 parent 1febd3a commit ea1663a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/ZkFold/Base/Algebra/Polynomials/Multivariate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ polynomial ::
P c i j (Map i j) [(c, M i j (Map i j))]
polynomial = foldr (\(c, m) x -> if c == zero then x else P [(c, m)] + x) zero

instance Ord i => IsList (M i j (Map i j)) where
type Item (M i j (Map i j)) = (i, j)
toList (M m) = toList m
fromList m = M $ fromList m

instance (Ord i, Eq c, Field c, Ord j, Semiring j) => IsList (P c i j (Map i j) [(c, M i j (Map i j))]) where
type Item (P c i j (Map i j) [(c, M i j (Map i j))]) = (c, Map i j)
toList (P p) = second (\(M m) -> m) <$> p
fromList p = polynomial $ second monomial <$> p

-- | @'var' i@ is a polynomial \(p(x) = x_i\)
var :: Polynomial c i j => i -> P c i j (Map i j) [(c, M i j (Map i j))]
var x = polynomial [(one, monomial $ fromList [(x, one)])]
Expand Down
5 changes: 0 additions & 5 deletions src/ZkFold/Base/Algebra/Polynomials/Multivariate/Monomial.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ type Monomial i j = (Variable i, Ord j, Semiring j)
newtype M i j m = M m
deriving (Generic, NFData, FromJSON, ToJSON)

instance Ord i => IsList (M i j (Map i j)) where
type Item (M i j (Map i j)) = (i, j)
toList (M m) = toList m
fromList m = M $ fromList m

instance (Show i, Show j, Monomial i j) => Show (M i j (Map i j)) where
show (M m) = intercalate "" . map showVar $ toList m
where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ type Polynomial c i j = (Eq c, Field c, Monomial i j)
newtype P c i j m p = P p
deriving (Generic, NFData, FromJSON, ToJSON)

instance IsList (P c i j (Map i j) [(c, M i j (Map i j))]) where
type Item (P c i j (Map i j) [(c, M i j (Map i j))]) = (c, Map i j)
toList (P p) = second (\(M m) -> m) <$> p
fromList p = P $ second M <$> p

instance (Show c, Show i, Show j, Monomial i j) => Show (P c i j (Map i j) [(c, M i j (Map i j))]) where
show (P p) = intercalate " + "
$ p <&> \(c, m) -> show c <> "" <> show (m :: M i j (Map i j))
Expand Down

0 comments on commit ea1663a

Please sign in to comment.