module Data.Sparse.PPrint (prd, prd0, PrintDense, newline, PPrintOptions, prdef, prepD, prepC, printDN, printCN) where
import Data.Complex
import Data.List
import Text.Printf
import Numeric.Eps
class PrintDense a where
prd :: a -> IO ()
prd0 :: a -> IO ()
newline :: IO ()
newline = putStrLn ""
data PPrintOptions =
PPOpts {
pprintInt :: Int,
pprintDec :: Int
} deriving (Eq, Show)
prdef :: PPrintOptions
prdef = PPOpts 1 2
prepD opts x = pstr
where
pstr | nearZero x = "_"
| abs x >= 10 || abs x < 0.1 = s ++ "e"
| otherwise = s ++ "f"
where
s = concat ["%" , show ni, ".", show nd]
nd = pprintDec opts
ni = pprintInt opts
prepC :: (Epsilon t, Ord t) => PPrintOptions -> Complex t -> String
prepC opts (r :+ i) = prepD opts r ++ oi where
oi | isNz i = concat [s, prepD opts i', "i"]
| otherwise = []
s | signum i >= 0 = "+"
| otherwise = "-"
i' = abs i
printDN
:: (PrintfArg t1, PrintfType t, Ord t1, Epsilon t1) =>
PPrintOptions -> [t1] -> t
printDN opts xl
| null xl = printf "\n"
| null xs = printf (prepD opts x) x
| n==1 = let [x1]=xs in printf (commas (pr <$> xl)++"\n") x x1
| n==2 = let [x1,x2]=xs in printf (commas (pr <$> xl)++"\n") x x1 x2
| n==3 = let [x1,x2,x3]=xs in printf (commas (pr <$> xl)++"\n") x x1 x2 x3
| n==4 = let [x1,x2,x3,x4]=xs in printf (commas (pr <$> xl)++"\n") x x1 x2 x3 x4
| otherwise = let xs@[x,x1,x2,x3]=take 4 xl
xfin=last xl
in printf (commas (pr <$> xs) ++ ", ... , " ++ pr xfin++"\n") x x1 x2 x3 xfin
where
(x:xs) = xl
n = length xs
pr = prepD opts
printCN
:: (PrintfArg t1, PrintfType t, Epsilon t1, Ord t1) =>
PPrintOptions -> [Complex t1] -> t
printCN opts xl
| null xl = printf "\n"
| null xs = printf (pr x) (re x) (aim x)
| n==1 = let [x1]=xs
in printf (commas (pr <$> xl)++"\n") (re x) (aim x) (re x1) (aim x1)
| n==2 = let [x1,x2]=xs
in printf (commas (pr <$> xl) ++"\n") (re x) (aim x) (re x1) (aim x1) (re x2) (aim x2)
| n==3 = let [x1,x2,x3]=xs
in printf (commas (pr <$> xl)++"\n") (re x) (aim x) (re x1) (aim x1) (re x2) (aim x2) (re x3) (aim x3)
| n==4 = let [x1,x2,x3,x4]=xs
in printf (commas (pr <$> xl)++"\n") (re x) (aim x) (re x1) (aim x1) (re x2) (aim x2) (re x3) (aim x3) (re x4) (aim x4)
| otherwise = let xs@[x,x1,x2,x3]=take 4 xl
xfin = last xl
in printf (commas (pr <$> xs) ++ ", ... , " ++ pr xfin++"\n") (re x) (aim x) (re x1) (aim x1) (re x2) (aim x2) (re x3) (aim x3) (re xfin) (aim xfin)
where
(x:xs) = xl
pr = prepC opts
n = length xs
commas :: [String] -> String
commas = intercalate ", "
re :: Complex a -> a
re = realPart
aim :: Num a => Complex a -> a
aim = abs . imagPart