Imaginary base numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: Suggestion (OP may prefer to revert): signatures restored, hlint and hindent applied, imports specified.)
Line 1,085: Line 1,085:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang Haskell>import Data.Char (chr, digitToInt, intToDigit, isDigit, ord)
<lang Haskell>
import Data.Maybe
import Data.Complex (Complex(..), imagPart, realPart)
import Data.List
import Data.List (elemIndex, delete)
import Data.Complex
import Data.Maybe (fromMaybe)
import Data.Char



base :: Complex Float
base = 0 :+ 2
base = 0 :+ 2


quotRemPositive a b =
quotRemPositive
:: (Integral a)
let (q, r) = quotRem a b in
=> a -> a -> (a, a)
if (r < 0) then (1 + q, (floor $ realPart (- base^^2)) + r) else (q, r)
quotRemPositive a b
| r < 0 = (1 + q, floor (realPart (-base ^^ 2)) + r)
| otherwise = (q, r)
where
(q, r) = quotRem a b


digitToIntQI :: Char -> Int
digitToIntQI c = if (isDigit c) then (digitToInt c) else ((ord c) - (ord 'a') + 10)
digitToIntQI c
| isDigit c = digitToInt c
| otherwise = ord c - ord 'a' + 10


shiftRight n = let (l, h) = (last n, init n) in
shiftRight :: String -> String
shiftRight n
if (l == '0') then h else h ++ "." ++ [l]
| l == '0' = h
| otherwise = h ++ "." ++ [l]
where
(l, h) = (last n, init n)


intToDigitQI :: Int -> Char
intToDigitQI i = if (elem i [0..9]) then (intToDigit i) else (chr (i - 10 + (ord 'a')))
intToDigitQI i
| i `elem` [0 .. 9] = intToDigit i
| otherwise = chr (i - 10 + ord 'a')


fromQItoComplex :: String -> Complex Float -> Complex Float
fromQItoComplex num b =
fromQItoComplex num b =
let dot = fromMaybe (length num) (elemIndex '.' num) in
let dot = fromMaybe (length num) (elemIndex '.' num)
fst $ foldl
in fst $
foldl
(\(acc, indx) x ->
(acc + (fromIntegral $ digitToIntQI x)*(b^^(dot - indx)), indx + 1))
(\(a, indx) x ->
(a + fromIntegral (digitToIntQI x) * (b ^^ (dot - indx)), indx + 1))
(0, 1)
(delete '.' num)
(0, 1)
(delete '.' num)


euclidEr 0 b l = l
euclidEr :: Int -> Int -> [Int] -> [Int]
euclidEr a b l
euclidEr a b l = let (q, r) = quotRemPositive a b in euclidEr q b (0:r:l)
| a == 0 = l
| otherwise =
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
getCuid c = (imagPart c)*(floor $ imagPart (-base))
:: Integral a
=> Complex a -> a
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 m = min (length r) (length i) in
(take ((length r) - m) r) ++ (take ((length i) - m) i)
let (r, i) = (fromIntToQI (realPart c) ++ [0], fromIntToQI (getCuid c))
++ reverse (zipWith (+) (take m (reverse r)) (take m (reverse i)))
in let m = min (length r) (length i)
in take (length r - m) r ++
take (length i - m) i ++
reverse (zipWith (+) (take m (reverse r)) (take m (reverse i)))


fromComplexToQI c = shiftRight (map intToDigitQI (qizip c))
fromComplexToQI :: Complex Int -> String
fromComplexToQI = shiftRight . fmap intToDigitQI . qizip


main :: IO ()
main = print (fromComplexToQI (35 :+ 23)) >>
print (fromQItoComplex "10.2" base)
main = print (fromComplexToQI (35 :+ 23)) >> print (fromQItoComplex "10.2" base)</lang>
</lang>
{{out}}
{{out}}
<pre>
<pre>"121003.2"
"121003.2"
0.0 :+ 1.0
0.0 :+ 1.0
</pre>
</pre>
Line 1,138: Line 1,166:
<pre>
<pre>
"3z.8"
"3z.8"
0.0 :+ 7.75
0.0 :+ 7.75</pre>
</pre>


=={{header|Java}}==
=={{header|Java}}==