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
Don't report -1 as a factor #101
Conversation
@@ -192,7 +192,7 @@ a .^ e | |||
factorise :: GaussianInteger -> [(GaussianInteger, Int)] | |||
factorise g = helper (Factorisation.factorise $ norm g) g | |||
where | |||
helper [] g' = if g' == 1 then [] else [(g', 1)] -- include the unit, if it isn't 1 | |||
helper [] g' = if abs g' == 1 then [] else [(g', 1)] -- include the unit, if it isn't 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the first argument is an empty list, abs g'
is guaranteed to be 1
, so we can just return []
.
In fact, now helper
does not need to carry the second argument at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've reduced the base case to
helper [] _ = []
but I'm less certain about how to rework the other case to remove the need for g'
, assuming that's what you mean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My fault, it is non-trivial to remove the need for g'
. Definitely out-of-scope for this PR.
@@ -84,9 +84,9 @@ import Math.NumberTheory.Utils | |||
-- manner from the bit-pattern of @n@. | |||
factorise :: Integer -> [(Integer,Int)] | |||
factorise n | |||
| n < 0 = (-1,1):factorise (-n) | |||
| abs n == 1 = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep code aligned by =
.
@@ -35,6 +35,7 @@ factoriseProperty1 :: Integer -> Integer -> Bool | |||
factoriseProperty1 x y | |||
= x == 0 && y == 0 | |||
|| g == g' | |||
|| abs g == abs g' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we can safely remove check g == g'
, because it is a specific instance of abs g == abs g'
.
Thanks for your contribution! Please find review suggestions above. |
Updated, except as commented above. EDIT: Review commit squashed now that it seems we're all good. :) |
@@ -84,10 +84,10 @@ import Math.NumberTheory.Utils | |||
-- manner from the bit-pattern of @n@. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please amend the haddock comment to reflect your changes.
@@ -136,7 +136,7 @@ sieveFactor (FS bnd sve) = check | |||
check 0 = error "0 has no prime factorisation" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please amend the haddock comment to reflect your changes.
@@ -192,7 +192,7 @@ a .^ e | |||
factorise :: GaussianInteger -> [(GaussianInteger, Int)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is worth to clarify in haddock comment that unit factors are not included.
Haddock comments updated. |
Excellent! There is one remaining area to improve: instance UniqueFactorisation Int where
unPrime = coerce wordToInt
- factorise m' = if m <= 1
- then []
- else map (coerce integerToWord *** intToWord) . F.factorise' . intToInteger $ m
- where
- m = abs m'
+ factorise = map (coerce integerToWord *** intToWord) . F.factorise . intToInteger Make sure to rebase your branch on latest |
I'll chase down the test failures later on. |
Just replace |
I looked into two remaining failures. There are several ways to fix them, but the most fundamental one is to change default implementation of |
@Bodigrim Such a definition also implies a |
That is why I suggested enabling isPrime :: a -> Maybe (Prime a)
default isPrime :: (Eq a, Num a) => a -> Maybe (Prime a)
isPrime 0 = Nothing
isPrime n = case factorise n of
[(p, 1)] -> Just p
_ -> Nothing Then for types, satisfying Another option is proceed further with your approach, removing default implementation of |
Fixes #95 Special case for property Montgomery factorise Chasing test failures Edits according to review Update haddocks Simplify UniqueFactorisation instances Clear nearly all failures Special case UFD isPrime for Int Default isPrime with Eq and Num instances
I've gone with the |
Yeah, it is quite possible that another hierarchy will work better, especially when we get quadratic numbers (#82) merged. Merged, many thanks for your awesome work. |
Fixes #95
Not sure if
abs g == abs g'
is the best way to go about this in the property. Feels a little too forgetful, but I'm not sure how else to reformulate the test to get it to pass.