/
pmat.hs
107 lines (85 loc) · 3.5 KB
/
pmat.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import System.Environment
import System.IO
import Text.ParserCombinators.Parsec
import Control.Monad
import Numeric.LinearAlgebra as LA
{--
pmat
written by Ryuichi Ueda
The MIT License
Copyright (C) 2013 Ryuichi Ueda
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
--}
showUsage :: IO ()
showUsage = do hPutStr stderr
("Usage : pmat <opt> <file>\n" ++
"Tue Jul 2 16:40:21 JST 2013\n")
version :: IO ()
version = do hPutStr stderr
("version 0.001")
main :: IO ()
main = do
args <- getArgs
case args of
["-h"] -> showUsage
["--help"] -> showUsage
["--ver"] -> version
["--v"] -> version
[opt,f] -> readData f >>= mainProc opt
[opt] -> readData "-" >>= mainProc opt
readData :: String -> IO String
readData "-" = getContents
readData f = readFile f
data NMat = NMat (String,Matrix Double) deriving Show
mainProc :: String -> String -> IO ()
mainProc opt cs = do putStr cs
doCalc (setOpt opt) (setMatrices cs)
toStr :: NMat -> String
toStr (NMat (name,mat)) = unlines [ name ++ " " ++ t | t <- tos ]
where lns = LA.toLists mat
tos = [ unwords [ show d | d <- ln ] | ln <- lns ]
-- ここを広げていく
doCalc :: Option -> [NMat] -> IO ()
doCalc opt (a:b:ms) = putStr $ toStr (matMultiply a b)
matMultiply :: NMat -> NMat -> NMat
matMultiply (NMat x) (NMat y) = NMat ((fst x) ++ "*" ++ (fst y), LA.multiply (snd x) (snd y))
setMatrices :: String -> [NMat]
setMatrices cs = [ setMatrix m | m <- (getCluster $ lines cs) ]
setMatrix :: [String] -> NMat
setMatrix lns = NMat (k,val)
where k = head (words $ head lns)
val = (row><col) (concatMap toNums lns)
toNums str = [ read n | n <- ( drop 1 $ words str) ]
row = length lns
col = (length $ words $ head lns) - 1
getCluster :: [String] -> [[String]]
getCluster [] = []
getCluster lns = [fst d] ++ getCluster (snd d)
where d = span (compKey key ) lns
key = head $ words $ head lns
compKey a b = a == (head $ words b)
data Option = Mul String String |
Error String deriving Show
setOpt :: String -> Option
setOpt str = case parse parseOption "" str of
Right opt -> opt
Left err -> Error ( show err )
parseOption :: Parser Option
parseOption = do a <- many1 letter
char '*'
b <- many1 letter
return $ Mul a b