Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linear diophantine equations #220

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Math/NumberTheory/Diophantine.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
-- Module for Diophantine Equations and related functions

{-# LANGUAGE PartialTypeSignatures #-}
{-# OPTIONS_GHC -Wno-partial-type-signatures #-}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it difficult to provide full type signatures?


module Math.NumberTheory.Diophantine
( cornacchiaPrimitive
, cornacchia
, LinearSolution (..)
, linear
, runLinearSolution
)
where

Expand All @@ -14,6 +20,9 @@ import Math.NumberTheory.Primes ( factorise
import Math.NumberTheory.Roots ( integerSquareRoot )
import Math.NumberTheory.Utils.FromIntegral

import Control.Monad (guard)
import Data.Euclidean (gcdExt)

-- | See `cornacchiaPrimitive`, this is the internal algorithm implementation
-- | as described at https://en.wikipedia.org/wiki/Cornacchia%27s_algorithm
cornacchiaPrimitive' :: Integer -> Integer -> [(Integer, Integer)]
Expand Down Expand Up @@ -64,3 +73,31 @@ cornacchia d m
where
candidates = map (\sf -> (sf, m `div` (sf * sf))) (squareFactors m)
solve (sf, m') = map (\(x, y) -> (x * sf, y * sf)) (cornacchiaPrimitive d m')

----

-- | A solution to a linear equation
data LinearSolution a = LS { base1,base2,scale1,scale2 :: a }
deriving
( Show, Eq, Ord
)

-- | Solves a linear diophantine equation
-- | ax + by = c
-- | where `x` and `y` are unknown
linear :: _ => a -> a -> a -> Maybe (LinearSolution a)
linear a b c =
LS x y v u <$ guard (b /= 0 && q == 0)
where
(d, e) = gcdExt a b
(h, q) = divMod c d
f = div (a*e-d) (-b)
(x, y) = (e*h, f*h)
(u, v) = (quot a d, quot b d)

-- | Produces an unique solution given any
-- | arbitrary number k
runLinearSolution :: _ => LinearSolution a -> a -> (a, a)
runLinearSolution (LS x y v u) k =
( x + k*v, y - k*u )

26 changes: 25 additions & 1 deletion test-suite/Math/NumberTheory/DiophantineTests.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- Tests for Math.NumberTheory.Diophantine

{-# LANGUAGE CPP #-}
{-# LANGUAGE GADTs #-}

{-# OPTIONS_GHC -fno-warn-type-defaults #-}

Expand All @@ -15,6 +16,7 @@ import Test.Tasty
import Math.NumberTheory.Diophantine
import Math.NumberTheory.Roots (integerSquareRoot)
import Math.NumberTheory.TestUtils
import Math.NumberTheory.Primes

cornacchiaTest :: Positive Integer -> Positive Integer -> Bool
cornacchiaTest (Positive d) (Positive a) = gcd d m /= 1 || all checkSoln (cornacchia d m)
Expand All @@ -33,8 +35,30 @@ cornacchiaBruteForce (Positive d) (Positive a) = gcd d m /= 1 || findSolutions [
where x2 = m - d*y*y
x = integerSquareRoot x2

linearTest :: (a ~ Integer) => a -> a -> a -> a -> Bool
linearTest a b c k =
case linear a b c of
Nothing -> True -- Disproving this would require a counter example
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that solveLinear = const Nothing passes this test. Can we come up with a test which requires solveLinear return Just{} at least in some cases?

Copy link
Author

@BlackCapCoder BlackCapCoder Nov 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! I think this is easier to see in terms of my Pogo problem D = H*L (mod C) where H is the solution. All we have to do is make sure L is coprime to C, because sort [mod (h*l) c | h <- [0..pred c]] == [0..pred c], which includes D.

Just ls | (x, y) <- runLinearSolution ls k
-> a*x + b*y == c

linearTest' :: (a ~ Integer) => Prime a -> Prime a -> a -> a -> Bool
linearTest' l c' d k =
case linear a b c of
Nothing -> l == c'
Just ls | (x, y) <- runLinearSolution ls k
-> a*x + b*y == c
where
a = unPrime l
b = unPrime c'
c = d


testSuite :: TestTree
testSuite = testGroup "Diophantine"
[ testSmallAndQuick "Cornacchia correct" cornacchiaTest
, testSmallAndQuick "Cornacchia same solutions as brute force" cornacchiaBruteForce
]
, testSmallAndQuick "Linear correct" linearTest
, testSmallAndQuick "Linear correct #2" linearTest'
]