Imaginary base numbers: Difference between revisions
Content deleted Content added
Line 1,085: | Line 1,085: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<lang Haskell> |
<lang Haskell> |
||
import Data.Maybe |
|||
import Data.List |
import Data.List |
||
import Data.Complex |
import Data.Complex |
||
Line 1,092: | Line 1,093: | ||
base = 0 :+ 2 |
base = 0 :+ 2 |
||
quotRemPositive |
quotRemPositive a b = |
||
quotRemPositive a b= |
|||
let (q, r) = quotRem a b in |
let (q, r) = quotRem a b in |
||
if (r < 0) then (1 + q, (floor $ realPart (- base^^2)) + r) else (q, r) |
if (r < 0) then (1 + q, (floor $ realPart (- base^^2)) + r) else (q, r) |
||
digitToIntQI :: Char -> Int |
|||
digitToIntQI c = if (isDigit c) then (digitToInt c) else ((ord c) - (ord 'a') + 10) |
digitToIntQI c = if (isDigit c) then (digitToInt c) else ((ord c) - (ord 'a') + 10) |
||
shiftRight :: [Char] -> [Char] |
|||
shiftRight n = let (l, h) = (last n, init n) in |
shiftRight n = let (l, h) = (last n, init n) in |
||
if (l == '0') then h else h ++ "." ++ [l] |
if (l == '0') then h else h ++ "." ++ [l] |
||
intToDigitQI :: Int -> Char |
|||
intToDigitQI i = if (elem i [0..9]) then (intToDigit i) else (chr (i - 10 + (ord 'a'))) |
intToDigitQI i = if (elem i [0..9]) then (intToDigit i) else (chr (i - 10 + (ord 'a'))) |
||
fromQItoComplex :: (RealFloat a) => [Char] -> Complex a -> Complex a |
|||
fromQItoComplex num b = |
fromQItoComplex num b = |
||
let dot = fromMaybe (length num) (elemIndex '.' num) in |
let dot = fromMaybe (length num) (elemIndex '.' num) in |
||
Line 1,116: | Line 1,112: | ||
(delete '.' num) |
(delete '.' num) |
||
euclidEr |
euclidEr 0 b l = l |
||
euclidEr a b l = |
euclidEr a b l = let (q, r) = quotRemPositive a b in euclidEr q b (0:r:l) |
||
if (a == 0) then l else let (q, r) = quotRemPositive a b in euclidEr q b (0:r:l) |
|||
fromIntToQI :: Int -> [Int] |
|||
fromIntToQI 0 = [0] |
fromIntToQI 0 = [0] |
||
fromIntToQI x = tail (euclidEr x (floor $ realPart (base^^2)) []) |
fromIntToQI x = tail (euclidEr x (floor $ realPart (base^^2)) []) |
||
getCuid :: Integral a => Complex a -> a |
|||
getCuid c = (imagPart c)*(floor $ imagPart (-base)) |
getCuid c = (imagPart c)*(floor $ imagPart (-base)) |
||
qizip :: Complex Int -> [Int] |
|||
qizip c = let (r, i) = (fromIntToQI (realPart c) ++ [0], fromIntToQI (getCuid c)) in |
qizip c = let (r, i) = (fromIntToQI (realPart c) ++ [0], fromIntToQI (getCuid c)) in |
||
let m = min (length r) (length i) in |
let m = min (length r) (length i) in |
||
Line 1,133: | Line 1,125: | ||
++ reverse (zipWith (+) (take m (reverse r)) (take m (reverse i))) |
++ reverse (zipWith (+) (take m (reverse r)) (take m (reverse i))) |
||
fromComplexToQI :: Complex Int -> [Char] |
|||
fromComplexToQI c = shiftRight (map intToDigitQI (qizip c)) |
fromComplexToQI c = shiftRight (map intToDigitQI (qizip c)) |
||