module Data.Matrix.AsXYZ (
fromXYZ,
fromXYZ',
fromABC,
prettyXYZ,
prettyABC,
fromXY,
fromXY',
fromAB,
prettyXY,
prettyAB,
) where
import Control.Monad (join)
import Data.Char (isAlpha)
import Data.List (intercalate)
import Data.Ratio (Ratio,(%))
import Data.Matrix (Matrix,fromList,fromLists,toLists,identity,zero,(<->),submatrix)
import Text.ParserCombinators.Parsec (parse,ParseError)
import Data.Ratio.Slash (getRatio,Slash(..))
import qualified Data.Matrix.AsXYZ.ParseXYZ as XYZ(equivalentPositions,transformPpABC,ratio)
import qualified Data.Matrix.AsXYZ.ParseXY as XY (equivalentPositions,transformPpAB)
import qualified Data.Matrix.AsXYZ.ParseXYZ as XY(ratio)
import qualified Data.Matrix.AsXYZ.Plain as Plain (showAs,showAs',xyzLabel,abcLabel)
fromXYZ :: Integral a => String -> Matrix (Ratio a)
fromXYZ input = unsafeGet $ makeMatrixS <$> parse (XYZ.equivalentPositions XYZ.ratio) input input
fromXYZ' :: Integral a => String -> Maybe (Matrix (Ratio a))
fromXYZ' input = get $ makeMatrixS <$> parse (XYZ.equivalentPositions XYZ.ratio) input input
fromABC :: Integral a => String -> Matrix (Ratio a)
fromABC input = unsafeGet $ makeMatrixS <$> parse (XYZ.transformPpABC XYZ.ratio) input input
makeMatrixS :: Num a => [[a]] -> Matrix a
makeMatrixS m = (submatrix 1 3 1 4 . fromLists) m <-> fromLists [[0,0,0,1]]
unsafeGet :: Either ParseError a -> a
unsafeGet e = case e of
Left s -> error $ show s
Right m -> m
get :: Either ParseError a -> Maybe a
get e = case e of
Left s -> Nothing
Right m -> Just m
prettyXYZ :: (Integral a) =>
Matrix (Ratio a)
-> String
prettyXYZ = Plain.showAs Plain.xyzLabel
prettyABC :: (Integral a) =>
Matrix (Ratio a)
-> String
prettyABC = Plain.showAs Plain.abcLabel
fromXY :: Integral a =>
String
-> Matrix (Ratio a)
fromXY input = unsafeGet $ makeMatrixP <$> parse (XY.equivalentPositions XY.ratio) input input
fromXY' :: Integral a =>
String
-> Maybe (Matrix (Ratio a))
fromXY' input = get $ makeMatrixP <$> parse (XY.equivalentPositions XY.ratio) input input
fromAB :: Integral a =>
String
-> Matrix (Ratio a)
fromAB input = unsafeGet $ makeMatrixP <$> parse (XY.transformPpAB XY.ratio) input input
makeMatrixP :: Num a => [[a]] -> Matrix a
makeMatrixP m = (submatrix 1 2 1 3 . fromLists) m <-> fromLists [[0,0,1]]
prettyXY :: (Integral a) =>
Matrix (Ratio a)
-> String
prettyXY = Plain.showAs' Plain.xyzLabel
prettyAB :: (Integral a) =>
Matrix (Ratio a)
-> String
prettyAB = Plain.showAs' Plain.abcLabel