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,(<->))
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 $ makeMatrix <$> parse (XYZ.equivalentPositions XYZ.ratio) input input
fromXYZ' :: Integral a => String -> Maybe (Matrix (Ratio a))
fromXYZ' input = get $ makeMatrix <$> parse (XYZ.equivalentPositions XYZ.ratio) input input
fromABC :: Integral a => String -> Matrix (Ratio a)
fromABC input = unsafeGet $ makeMatrix <$> parse (XYZ.transformPpABC XYZ.ratio) input input
makeMatrix :: Num a => [[a]] -> Matrix a
makeMatrix m = 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 $ makeMatrix' <$> parse (XY.equivalentPositions XY.ratio) input input
fromXY' :: Integral a =>
String
-> Maybe (Matrix (Ratio a))
fromXY' input = get $ makeMatrix' <$> parse (XY.equivalentPositions XY.ratio) input input
fromAB :: Integral a =>
String
-> Matrix (Ratio a)
fromAB input = unsafeGet $ makeMatrix' <$> parse (XY.transformPpAB XY.ratio) input input
makeMatrix' :: Num a => [[a]] -> Matrix a
makeMatrix' m = 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