Commit 510e5fda authored by Han-Miru Kim's avatar Han-Miru Kim
Browse files

asdf

parent 04dcb144
...@@ -27,36 +27,10 @@ or calculate the entire cycle ...@@ -27,36 +27,10 @@ or calculate the entire cycle
[(-1,6,-3),(2,4,-1),(5,8,2),(6,12,5),(5,12,6),(2,8,5),(5,8,2)] [(-1,6,-3),(2,4,-1),(5,8,2),(6,12,5),(5,12,6),(2,8,5),(5,8,2)]
#+END_SRC #+END_SRC
** Interesting stuff ** Interesting stuff
*** How many are reduced? How long does it take to reduce forms?
This question obviously not well-defined, but I wanted to know "how many bqfs are reduced and how many are not?"
#+BEGIN_SRC haskell :tangle no #+BEGIN_SRC haskell :tangle no
ghci> interval = [-5 .. 5] ghci> interval = [-10 .. 10]
ghci> forms = filter (\f -> discriminant f /= 0) [BQF (a,b,c) | a <- interval, b <- interval, c <- interval] ghci> forms = filter (\f -> discriminant f /= 0) [BQF (a,b,c) | a <- interval, b <- interval, c <- interval]
ghci> length forms
1294
ghci> reducedForms = filter isReduced forms
ghci> length reducedForms
150
ghci> 150 / 1294
0.1159...
-- and with interval = [-10 .. 10]
ghci> length forms
9176
ghci> length reducedForms
1000
ghci> 1000/9176
0.1090...
-- interval = [-200 .. 200]
ghci> 6727000/64478328
0.1043...
#+END_SRC
So it looks like the ratio is decreasing as the range of coeffcients gets larger, but does not appear to decrease very quickly.
Does it converge to some value?
*** How long does it take to reduce?
#+BEGIN_SRC haskell :tangle no
-- with limit = 10
ghci> steps = map stepsTillReduced forms ghci> steps = map stepsTillReduced forms
14 14
ghci> [(length . filter (== x)) steps | x <- [0 .. maximum steps]] ghci> [(length . filter (== x)) steps | x <- [0 .. maximum steps]]
...@@ -96,22 +70,18 @@ instance Show BQF where ...@@ -96,22 +70,18 @@ instance Show BQF where
= "(" ++ show a ++ "," ++ show b ++ "," ++ show c ++ ")" = "(" ++ show a ++ "," ++ show b ++ "," ++ show c ++ ")"
#+END_SRC #+END_SRC
** Equivalence ** Basic functions
We put an equivalence class on the set of binary quadratic forms.
If $f(x,y) = ax^2 + bxy + cy^2$ is a form, we can act on it by elements of $\text{SL}(2,\mathbb{Z})$, preserving the structure of integer solutions.
One can show that the discriminant is an invariant in each equivalence class.
#+BEGIN_SRC haskell #+BEGIN_SRC haskell
discriminant :: BQF -> Int discriminant :: BQF -> Int
discriminant (BQF (a,b,c)) = b^2 - 4 * a * c discriminant (BQF (a,b,c)) = b^2 - 4 * a * c
#+END_SRC #+END_SRC
moreover, for any discriminant $D$ with $D = 1$ or $D = 4$ modulo $4$, there exists a form with such a discriminant, called the basic form (Grundform) For any discriminant $D$ with $D = 1$ or $D = 4$ modulo $4$, there exists a form with such a discriminant, called the basic form (Grundform)
#+BEGIN_SRC haskell #+BEGIN_SRC haskell
basicForm :: Int -> BQF basicForm :: Int -> BQF
basicForm d basicForm d
| (d `mod` 4) == 0 = BQF (1,0,round (-(fromIntegral d)/4)) | (d `mod` 4) == 0 = BQF (1,0,round (-(fromIntegral d)/4))
| (d `mod` 4) == 1 = BQF (1,1,round (fromIntegral (1-d)/4)) | (d `mod` 4) == 1 = BQF (1,1,round (fromIntegral (1-d)/4))
| otherwise = error "invalid discriminant" | otherwise = error "invalid discriminant"
#+END_SRC #+END_SRC
** Basic Functions ** Basic Functions
...@@ -127,7 +97,7 @@ A definite form (D < 0) is reduced if ~-|a| < b <= |a| <= |c|~. ...@@ -127,7 +97,7 @@ A definite form (D < 0) is reduced if ~-|a| < b <= |a| <= |c|~.
ie2 = b <= abs a ie2 = b <= abs a
ie3 = abs a <= abs c ie3 = abs a <= abs c
#+END_SRC #+END_SRC
an indefinite form (D > 0) is reduced if $a,c,(b - a -c) > 0$ an indefinite form (D > 0) is reduced if ~a > 0, c > 0, b > a + c~
#+BEGIN_SRC haskell #+BEGIN_SRC haskell
isIndefiniteReduced :: BQF -> Bool isIndefiniteReduced :: BQF -> Bool
isIndefiniteReduced (BQF (a,b,c)) isIndefiniteReduced (BQF (a,b,c))
...@@ -147,9 +117,9 @@ combining these, we get the general definition of reduced forms ...@@ -147,9 +117,9 @@ combining these, we get the general definition of reduced forms
d = discriminant f d = discriminant f
#+END_SRC #+END_SRC
** Reduction Theory ** Reduction Theory
Reduction is done by first finding the ~n~ for which ~n > (b + sqrt D)/2a > n-1~ Reduction is done by first finding the ~n~ for which ~n > (b + sqrt D)/2a > n-1~.
The transformation is done by applying the transformation ~T = S_n = [[n,1],[-1,0]]~.
#+BEGIN_SRC haskell #+BEGIN_SRC haskell
redcoeff :: BQF -> Int redcoeff :: BQF -> Int
redcoeff (BQF (a,b,c)) = ceiling $ (b' + d') / (2 * a') redcoeff (BQF (a,b,c)) = ceiling $ (b' + d') / (2 * a')
...@@ -166,18 +136,23 @@ Reduction is done by first finding the ~n~ for which ~n > (b + sqrt D)/2a > n-1~ ...@@ -166,18 +136,23 @@ Reduction is done by first finding the ~n~ for which ~n > (b + sqrt D)/2a > n-1~
c' = a c' = a
n = redcoeff (BQF (a,b,c)) n = redcoeff (BQF (a,b,c))
-- appplies reduction steps to f, until there is repetition -- appplies reduction steps to f
-- and including -- until the first reduced element repeats
reduction :: BQF -> [BQF] reduction :: BQF -> [BQF]
reduction f = aux (iterate reduceStep f) [] reduction f = aux (iterate reduceStep f) Nothing
where where
aux (x:xs) y -- second argument stores the first reduced element
| x `elem` y = y ++ [x] aux :: [BQF] -> Maybe BQF -> [BQF]
| otherwise = aux xs (y ++ [x]) aux (x:xs) Nothing
| isReduced x = [x] ++ aux xs (Just x)
| otherwise = [x] ++ aux xs Nothing
aux (x:xs) (Just y)
| x == y = [x]
| otherwise = [x] ++ aux xs (Just y)
reduce :: BQF -> BQF reduce :: BQF -> BQF
reduce = last . reduction reduce = head . (dropWhile (not . isReduced)) . (iterate reduceStep)
-- how many reduction stepts does it take to reduce f -- how many reduction stepts does it take to reduce f
stepsTillReduced :: BQF -> Int stepsTillReduced :: BQF -> Int
...@@ -188,8 +163,30 @@ Reduction is done by first finding the ~n~ for which ~n > (b + sqrt D)/2a > n-1~ ...@@ -188,8 +163,30 @@ Reduction is done by first finding the ~n~ for which ~n > (b + sqrt D)/2a > n-1~
* Continued Fractions * Continued Fractions
** Evaluation ** Evaluation
#+BEGIN_SRC haskell #+BEGIN_SRC haskell
eval :: (Fractional a) => [a] -> a eval :: [Double] -> Double
eval [] = 0 eval [] = 0
eval (x:[]) = x eval (x:[]) = x
eval (x:xs) = x - 1/(eval xs) eval (x:xs) = x - 1/(eval xs)
-- implementation with foldr
eval' :: [Double] -> Double
eval' = (\x -> 1/x) . foldr (\n -> \x -> 1/(n-x)) 0
#+END_SRC
Convert real number to continued fraction
#+BEGIN_SRC haskell
next :: (Double,Double) -> (Double,Double)
next (w,n) = (x,fromIntegral (floor x) + 1)
where
x = 1/(n - w)
realToConfrac :: Double -> [Double]
realToConfrac w = map snd $ iterate next (w,fromIntegral (floor w) + 1)
#+END_SRC
Convert back to real number after n steps
#+BEGIN_SRC haskell
reconvert :: Double -> Int -> Double
reconvert x n = (eval . (take n) . realToConfrac) x
#+END_SRC #+END_SRC
...@@ -27,7 +27,7 @@ basicForm :: Int -> BQF ...@@ -27,7 +27,7 @@ basicForm :: Int -> BQF
basicForm d basicForm d
| (d `mod` 4) == 0 = BQF (1,0,round (-(fromIntegral d)/4)) | (d `mod` 4) == 0 = BQF (1,0,round (-(fromIntegral d)/4))
| (d `mod` 4) == 1 = BQF (1,1,round (fromIntegral (1-d)/4)) | (d `mod` 4) == 1 = BQF (1,1,round (fromIntegral (1-d)/4))
| otherwise = error "invalid discriminant" | otherwise = error "invalid discriminant"
isDefiniteReduced :: BQF -> Bool isDefiniteReduced :: BQF -> Bool
isDefiniteReduced (BQF (a,b,c)) isDefiniteReduced (BQF (a,b,c))
...@@ -69,24 +69,42 @@ reduceStep (BQF (a,b,c)) = BQF (a',b',c') ...@@ -69,24 +69,42 @@ reduceStep (BQF (a,b,c)) = BQF (a',b',c')
c' = a c' = a
n = redcoeff (BQF (a,b,c)) n = redcoeff (BQF (a,b,c))
-- appplies reduction steps to f
-- until the first reduced element repeats
reduction :: BQF -> [BQF] reduction :: BQF -> [BQF]
  reduction f = aux (iterate reduceStep f) Nothing
where where
reduction f = aux (iterate reduceStep f) [] -- second argument stores the first reduced element
where aux :: [BQF] -> Maybe BQF -> [BQF]
aux (x:xs) y aux (x:xs) Nothing
| isReduced x = [x] ++ aux xs (Just x)
| otherwise = [x] ++ aux xs Nothing
aux (x:xs) (Just y)
| x == y = [x]
| otherwise = [x] ++ aux xs (Just y)
reduce :: BQF -> BQF reduce :: BQF -> BQF
  reduce = head . (dropWhile (not . isReduced)) . reduction
-- how many reduction stepts does it take to reduce f -- how many reduction stepts does it take to reduce f
stepsTillReduced :: BQF -> Int stepsTillReduced :: BQF -> Int
stepsTillReduced = (length . (takeWhile (not . isReduced)) . reduction) stepsTillReduced = (length . (takeWhile (not . isReduced)) . reduction)
stepsTillReduced = (length . (takeWhile (not . isReduced)) . reduction) eval :: [Double] -> Double
eval [] = 0 eval [] = 0
eval (x:[]) = x eval (x:[]) = x
eval (x:xs) = x - 1/(eval xs) eval (x:xs) = x - 1/(eval xs)
-- implementation with foldr
eval' :: [Double] -> Double
eval' = (\x -> 1/x) . foldr (\n -> \x -> 1/(n-x)) 0
next :: (Double,Double) -> (Double,Double)
next (w,n) = (x,fromIntegral (floor x) + 1)
where
x = 1/(n - w)
realToConfrac :: Double -> [Double]
realToConfrac w = map snd $ iterate next (w,fromIntegral (floor w) + 1)
reconvert :: Double -> Int -> Double
reconvert x n = (eval . (take n) . realToConfrac) x
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment