HaskellForMaths 0.4.2 → 0.4.3
raw patch · 6 files changed
+19/−19 lines, 6 files
Files
- HaskellForMaths.cabal +1/−1
- Math/Algebra/Field/Extension.hs +7/−7
- Math/Algebra/LinearAlgebra.hs +3/−3
- Math/Algebra/NonCommutative/NCPoly.hs +2/−2
- Math/Combinatorics/FiniteGeometry.hs +4/−4
- Math/Projects/KnotTheory/LaurentMPoly.hs +2/−2
HaskellForMaths.cabal view
@@ -1,5 +1,5 @@ Name: HaskellForMaths - Version: 0.4.2 + Version: 0.4.3 Category: Math Description: A library of maths code in the areas of combinatorics, group theory, commutative algebra, and non-commutative algebra. The library is mainly intended as an educational resource, but does have efficient implementations of several fundamental algorithms. Synopsis: Combinatorics, group theory, commutative algebra, non-commutative algebra
Math/Algebra/Field/Extension.hs view
@@ -1,6 +1,6 @@ -- Copyright (c) David Amos, 2008. All rights reserved. -{-# LANGUAGE MultiParamTypeClasses, TypeSynonymInstances, ScopedTypeVariables, EmptyDataDecls #-} +{-# LANGUAGE MultiParamTypeClasses, TypeSynonymInstances, ScopedTypeVariables, EmptyDataDecls, FlexibleInstances #-} module Math.Algebra.Field.Extension where @@ -19,7 +19,7 @@ x = UP [0,1] :: UPoly Integer -instance (Show a, Num a) => Show (UPoly a) where +instance (Eq a, Show a, Num a) => Show (UPoly a) where -- show (UP []) = "0" show (UP as) = showUP "x" as @@ -40,7 +40,7 @@ | i == 1 = v -- "x" | i > 1 = v ++ "^" ++ show i -- "x^" ++ show i -instance Num a => Num (UPoly a) where +instance (Eq a, Num a) => Num (UPoly a) where UP as + UP bs = toUPoly $ as <+> bs negate (UP as) = UP $ map negate as UP as * UP bs = toUPoly $ as <*> bs @@ -79,7 +79,7 @@ monomial a i = UP $ replicate i 0 ++ [a] -- quotRem for UPolys over a field -quotRemUP :: (Num k, Fractional k) => UPoly k -> UPoly k -> (UPoly k, UPoly k) +quotRemUP :: (Eq k, Fractional k) => UPoly k -> UPoly k -> (UPoly k, UPoly k) quotRemUP f g = qr 0 f where qr q r = if deg r < deg_g then (q,r) @@ -106,12 +106,12 @@ data ExtensionField k poly = Ext (UPoly k) deriving (Eq,Ord) -instance Num k => Show (ExtensionField k poly) where +instance (Eq k, Show k, Num k) => Show (ExtensionField k poly) where -- show (Ext f) = show f -- show (Ext (UP [])) = "0" show (Ext (UP as)) = showUP "a" as -instance (Num k, Fractional k, PolynomialAsType k poly) => Num (ExtensionField k poly) where +instance (Eq k, Fractional k, PolynomialAsType k poly) => Num (ExtensionField k poly) where Ext x + Ext y = Ext $ (x+y) -- `modUP` pvalue (undefined :: (k,poly)) Ext x * Ext y = Ext $ (x*y) `modUP` pvalue (undefined :: (k,poly)) negate (Ext x) = Ext $ negate x @@ -119,7 +119,7 @@ abs _ = error "Prelude.Num.abs: inappropriate abstraction" signum _ = error "Prelude.Num.signum: inappropriate abstraction" -instance (Num k, Fractional k, PolynomialAsType k poly) => Fractional (ExtensionField k poly) where +instance (Eq k, Fractional k, PolynomialAsType k poly) => Fractional (ExtensionField k poly) where recip 0 = error "ExtensionField.recip 0" recip (Ext f) = let g = pvalue (undefined :: (k,poly)) (u,v,d@(UP [c])) = extendedEuclidUP f g
Math/Algebra/LinearAlgebra.hs view
@@ -136,7 +136,7 @@ -- |The inverse of a matrix (over a field), if it exists -inverse :: (Fractional a) => [[a]] -> Maybe [[a]] +inverse :: (Eq a, Fractional a) => [[a]] -> Maybe [[a]] inverse m = let d = length m -- the dimension i = idMx d @@ -176,7 +176,7 @@ r:_ -> rowEchelonForm (((x:xs) <+> r) : rs) rowEchelonForm zs@([]:_) = zs -reducedRowEchelonForm :: (Fractional a) => [[a]] -> [[a]] +reducedRowEchelonForm :: (Eq a, Fractional a) => [[a]] -> [[a]] reducedRowEchelonForm m = reverse $ reduce $ reverse $ rowEchelonForm m where reduce (r:rs) = let r':rs' = reduceStep (r:rs) in r' : reduce rs' -- is this scanl or similar? reduce [] = [] @@ -230,7 +230,7 @@ -- t (M m) = M (L.transpose m) -- |The determinant of a matrix (over a field) -det :: (Fractional a) => [[a]] -> a +det :: (Eq a, Fractional a) => [[a]] -> a det [[x]] = x det ((x:xs):rs) = if x /= 0
Math/Algebra/NonCommutative/NCPoly.hs view
@@ -58,7 +58,7 @@ then "+(" ++ c:cs ++ ")" else if c == '-' then c:cs else '+':c:cs -instance (Ord v, Show v, Num r) => Num (NPoly r v) where +instance (Eq r, Num r, Ord v, Show v) => Num (NPoly r v) where NP ts + NP us = NP (mergeTerms ts us) negate (NP ts) = NP $ map (\(m,c) -> (m,-c)) ts NP ts * NP us = NP $ collect $ L.sortBy cmpTerm $ [(g*h,c*d) | (g,c) <- ts, (h,d) <- us] @@ -85,7 +85,7 @@ -- Fractional instance so that we can enter fractional coefficients -- Only lets us divide by field elements (with unit monomial), not any other polynomials -instance (Ord v, Show v, Fractional r) => Fractional (NPoly r v) where +instance (Eq k, Fractional k, Ord v, Show v) => Fractional (NPoly k v) where recip (NP [(1,c)]) = NP [(1, recip c)] recip _ = error "NPoly.recip: only supported for (non-zero) constants"
Math/Combinatorics/FiniteGeometry.hs view
@@ -132,7 +132,7 @@ -- The returned flats are represented as matrices in reduced row echelon form, -- the rows of which are the points that generate the flat. -- The full set of points in the flat can be recovered by calling 'closurePG' -flatsPG :: (Num a) => Int -> [a] -> Int -> [[[a]]] +flatsPG :: (Eq a, Num a) => Int -> [a] -> Int -> [[[a]]] flatsPG n fq k = concatMap substStars $ rrefs (n+1) (k+1) where substStars (r:rs) = [r':rs' | r' <- substStars' r, rs' <- substStars rs] substStars [] = [[]] @@ -146,7 +146,7 @@ -- flatsAG :: (FiniteField a) => Int -> [a] -> Int -> [[[a]]] -- |flatsAG n fq k returns the k-flats in AG(n,Fq), where fq are the elements of Fq. -flatsAG :: (Num a) => Int -> [a] -> Int -> [[[a]]] +flatsAG :: (Eq a, Num a) => Int -> [a] -> Int -> [[[a]]] flatsAG n fq k = [map tail (r : map (r <+>) rs) | r:rs <- flatsPG n fq k, head r == 1] -- The head r == 1 condition is saying that we want points which are in the "finite" part of PG(n,Fq), not points at infinity -- The reason we add r to each of the rs is to bring them into the "finite" part @@ -156,13 +156,13 @@ -- linesPG :: (FiniteField a) => Int -> [a] -> [[[a]]] -- |The lines (1-flats) in PG(n,fq) -linesPG :: (Num a) => Int -> [a] -> [[[a]]] +linesPG :: (Eq a, Num a) => Int -> [a] -> [[[a]]] linesPG n fq = flatsPG n fq 1 -- linesAG :: (FiniteField a) => Int -> [a] -> [[[a]]] -- |The lines (1-flats) in AG(n,fq) -linesAG :: (Num a) => Int -> [a] -> [[[a]]] +linesAG :: (Eq a, Num a) => Int -> [a] -> [[[a]]] linesAG n fq = flatsAG n fq 1
Math/Projects/KnotTheory/LaurentMPoly.hs view
@@ -77,7 +77,7 @@ else if c == '-' then c:cs else '+':c:cs -- we don't attempt sign reversal within brackets in case we have expressions like t^-1 inside the brackets -instance Num r => Num (LaurentMPoly r) where +instance (Eq r, Num r) => Num (LaurentMPoly r) where LP ts + LP us = LP (mergeTerms ts us) negate (LP ts) = LP $ map (\(m,c)->(m,-c)) ts LP ts * LP us = LP $ collect $ sortBy cmpTerm $ [(g*h,c*d) | (g,c) <- ts, (h,d) <- us] @@ -104,7 +104,7 @@ -- Fractional instance so that we can enter fractional coefficients -- Only lets us divide by single terms, not any other polynomials -instance Fractional r => Fractional (LaurentMPoly r) where +instance (Eq r, Fractional r) => Fractional (LaurentMPoly r) where recip (LP [(m,c)]) = LP [(recip m, recip c)] recip _ = error "LaurentMPoly.recip: only supported for (non-zero) constants or monomials"