HNumeric 0.3.2.0 → 0.3.3.0
raw patch · 3 files changed
+70/−11 lines, 3 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- HNum.CSV: class Functor f => CSVtize f
- HNum.CSV: instance HNum.CSV.CSVtize HNum.CSV.DataFrame
- HNum.CSV: instance HNum.CSV.CSVtize HNum.Vector.Matrix
- HNum.CSV: instance HNum.CSV.CSVtize HNum.Vector.Vector
- HNum.CSV: write :: (CSVtize f, Show a) => String -> f a -> IO ()
+ HNum.CSV: class Functor f => Writable f
+ HNum.CSV: instance HNum.CSV.Writable HNum.CSV.DataFrame
+ HNum.CSV: instance HNum.CSV.Writable HNum.Vector.Matrix
+ HNum.CSV: instance HNum.CSV.Writable HNum.Vector.Vector
+ HNum.CSV: readCSV :: String -> IO (DataFrame String)
+ HNum.CSV: rmQuot :: String -> String
+ HNum.CSV: splitWith :: Char -> String -> [String]
+ HNum.CSV: writeCSV :: (Writable f, Show a) => String -> f a -> IO ()
+ HNum.Stats: sma :: Fractional a => Int -> Vector a -> Vector a
- HNum.CSV: toString :: (CSVtize f, Show a) => f a -> String
+ HNum.CSV: toString :: (Writable f, Show a) => f a -> String
Files
- HNumeric.cabal +2/−2
- src/HNum/CSV.hs +51/−9
- src/HNum/Stats.hs +17/−0
HNumeric.cabal view
@@ -2,10 +2,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: c5d1ade7480449723b6215fe9d6fe260a29e127d440ff4511d5d3f926c680586+-- hash: 35b947c6b91e1d075b0700a52de393b87f6749f5649ab43e268623c898e675e3 name: HNumeric-version: 0.3.2.0+version: 0.3.3.0 synopsis: Haskell Numeric Library with pure functionality, R & MATLAB Syntax. description: Please see the README on GitHub at <https://github.com/Axect/HNumeric#readme> category: HNum, library, Numeric, LinearAlgebra, Statistics, bsd3
src/HNum/CSV.hs view
@@ -10,6 +10,9 @@ import HNum.Vector +-----------------------------------------------+-- Declaration+----------------------------------------------- -- | Type Aliases for convenience type Header = [String] @@ -29,29 +32,68 @@ instance Functor DataFrame where fmap f df = df { dat = fmap f (dat df) } +-----------------------------------------------+-- Write to CSV+----------------------------------------------- -- | Class to write csv file-class Functor f => CSVtize f where+class Functor f => Writable f where+ -- | Object to String (Different to Show) toString :: Show a => f a -> String- write :: Show a => String -> f a -> IO ()+ -- | Write as CSV+ writeCSV :: Show a => String -> f a -> IO () -instance CSVtize Vector where+instance Writable Vector where toString v = foldr (\x y -> x ++ "\n" ++ y) "" (show <$> v)- write title v = writeFile title (toString v)+ writeCSV title v = writeFile title (toString v) -instance CSVtize Matrix where+instance Writable Matrix where toString m = foldr ((\x y -> x ++ "\n" ++ y) . cm) "" m1 where m1 = matForm (show <$> m)- write title m = writeFile title (toString m)+ writeCSV title m = writeFile title (toString m) -instance CSVtize DataFrame where+instance Writable DataFrame where toString (DataFrame h m) = h' ++ "\n" ++ m' where h' = cm h m' = toString (transpose m)- write title df = writeFile title (toString df)+ writeCSV title df = writeFile title (toString df) +-----------------------------------------------+-- Read from CSV+-----------------------------------------------+-- | From CSV to DataFrame+readCSV :: String -> IO (DataFrame String)+readCSV filepath = do+ r <- readFile filepath+ let dat = splitWith '\n' (rmQuot r)+ hd = splitWith ',' (head dat)+ bd = map (splitWith ',') (tail dat)+ return DataFrame {header = hd, dat = matrix bd} --- | Convenient +-----------------------------------------------+-- Backend Function+-----------------------------------------------+-- | For Convenient cm :: [String] -> String cm [] = [] cm (x : xs) | null xs = x | otherwise = x ++ "," ++ cm xs++-- | Split With Seperator+splitWith :: Char -> String -> [String]+splitWith _ [] = []+splitWith sep str | null temp'' = [temp]+ | otherwise = temp : splitWith sep temp'+ where+ temp = takeWhile (/= sep) str+ temp'' = dropWhile (/= sep) str+ temp' = tail temp''++-- | Remove Quotation Symbol+rmQuot :: String -> String+rmQuot [] = []+rmQuot x | null temp = clean+ | otherwise = clean ++ rmQuot clean'+ where+ clean = takeWhile (/= '"') x+ temp = dropWhile (/= '"') x+ clean' = tail temp
src/HNum/Stats.hs view
@@ -167,6 +167,23 @@ rse x y = sqrt (1 / fromIntegral (length x - 2) * rss x y) --------------------------------------------------------+-- Technical Analysis+--------------------------------------------------------++-- | Simple Moving Average+sma :: Fractional a => Int -> Vector a -> Vector a+sma p v = vec $ take (p - 1) v' ++ sma' p v'+ where+ v' = toList v+ sma' :: Fractional a => Int -> [a] -> [a]+ sma' p x+ | length x < p+ = []+ | otherwise+ = let m = sum (take p x) / fromIntegral p in m : sma' p (tail x)+++-------------------------------------------------------- -- Backend Functions --------------------------------------------------------