module Data.SRTree.Print
( DisplayNodes(..)
, showExpr
, showTree
, printExpr
, showDefault
, showTikz
, showPython
, showLatex
)
where
import Control.Monad.Reader ( asks, runReader, Reader )
import Data.Char ( toLower )
import Data.SRTree.Internal
data DisplayNodes ix val = D
{ forall ix val. DisplayNodes ix val -> ix -> String
_displayVar :: ix -> String
, forall ix val. DisplayNodes ix val -> ix -> String
_displayPar :: ix -> String
, forall ix val. DisplayNodes ix val -> val -> String
_displayVal :: val -> String
, forall ix val. DisplayNodes ix val -> Function -> String
_displayFun :: Function -> String
, forall ix val. DisplayNodes ix val -> String
_displayPow :: String
, forall ix val. DisplayNodes ix val -> String
_displayFloatPow :: String
}
asExpr :: (Show ix, Show val) => SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr :: forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr SRTree ix val
Empty = forall (f :: * -> *) a. Applicative f => a -> f a
pure String
""
asExpr (Var ix
ix) = do
ix -> String
display <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall ix val. DisplayNodes ix val -> ix -> String
_displayVar
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ ix -> String
display ix
ix
asExpr (Param ix
ix) = do
ix -> String
display <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall ix val. DisplayNodes ix val -> ix -> String
_displayPar
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ ix -> String
display ix
ix
asExpr (Const val
val) = do
val -> String
display <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall ix val. DisplayNodes ix val -> val -> String
_displayVal
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ val -> String
display val
val
asExpr (Fun Function
f SRTree ix val
t) = do
Function -> String
display <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall ix val. DisplayNodes ix val -> Function -> String
_displayFun
String
st <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr SRTree ix val
t
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [Function -> String
display Function
f, String
"(", String
st, String
")"]
asExpr (Pow SRTree ix val
t Int
ix) = do
String
st <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr SRTree ix val
t
String
pow <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall ix val. DisplayNodes ix val -> String
_displayPow
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"(", String
st, String
")", String
pow, String
"(", forall a. Show a => a -> String
show Int
ix, String
")"]
asExpr (Add SRTree ix val
l SRTree ix val
r) = do
String
sl <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr SRTree ix val
l
String
sr <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr SRTree ix val
r
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"(", String
sl, String
") + (", String
sr, String
")"]
asExpr (Sub SRTree ix val
l SRTree ix val
r) = do
String
sl <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr SRTree ix val
l
String
sr <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr SRTree ix val
r
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"(", String
sl, String
") - (", String
sr, String
")"]
asExpr (Mul SRTree ix val
l SRTree ix val
r) = do
String
sl <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr SRTree ix val
l
String
sr <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr SRTree ix val
r
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"(", String
sl, String
") * (", String
sr, String
")"]
asExpr (Div SRTree ix val
l SRTree ix val
r) = do
String
sl <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr SRTree ix val
l
String
sr <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr SRTree ix val
r
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"(", String
sl, String
") / (", String
sr, String
")"]
asExpr (Power SRTree ix val
l SRTree ix val
r) = do
String
sl <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr SRTree ix val
l
String
sr <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr SRTree ix val
r
String
pow <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall ix val. DisplayNodes ix val -> String
_displayFloatPow
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"(", String
sl, String
")", String
pow, String
"(", String
sr, String
")"]
asExpr (LogBase SRTree ix val
l SRTree ix val
r) = do
String
sl <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr SRTree ix val
l
String
sr <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr SRTree ix val
r
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"log(", String
sl, String
",", String
sr, String
")"]
asTree :: (Show ix, Show val) => SRTree ix val -> Reader (DisplayNodes ix val) String
asTree :: forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asTree SRTree ix val
Empty = forall (f :: * -> *) a. Applicative f => a -> f a
pure String
""
asTree (Var ix
ix) = do
ix -> String
display <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall ix val. DisplayNodes ix val -> ix -> String
_displayVar
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"[", ix -> String
display ix
ix, String
"]\n"]
asTree (Param ix
ix) = do
ix -> String
display <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall ix val. DisplayNodes ix val -> ix -> String
_displayPar
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"[", ix -> String
display ix
ix, String
"]\n"]
asTree (Const val
val) = do
val -> String
display <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall ix val. DisplayNodes ix val -> val -> String
_displayVal
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"[", val -> String
display val
val, String
"]\n"]
asTree (Fun Function
f SRTree ix val
t) = do
Function -> String
display <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall ix val. DisplayNodes ix val -> Function -> String
_displayFun
String
st <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asTree SRTree ix val
t
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"[", Function -> String
display Function
f, String
"\n", String
st, String
"]\n"]
asTree (Pow SRTree ix val
t Int
ix) = do
String
st <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asTree SRTree ix val
t
String
pow <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall ix val. DisplayNodes ix val -> String
_displayPow
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"[", String
pow, String
"\n", String
st, String
"[", forall a. Show a => a -> String
show Int
ix, String
"]\n]"]
asTree (Add SRTree ix val
l SRTree ix val
r) = do
String
sl <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asTree SRTree ix val
l
String
sr <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asTree SRTree ix val
r
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"[+\n", String
sl, String
sr, String
"]\n"]
asTree (Sub SRTree ix val
l SRTree ix val
r) = do
String
sl <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asTree SRTree ix val
l
String
sr <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asTree SRTree ix val
r
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"[-\n", String
sl, String
sr, String
"]\n"]
asTree (Mul SRTree ix val
l SRTree ix val
r) = do
String
sl <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asTree SRTree ix val
l
String
sr <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asTree SRTree ix val
r
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"[×\n", String
sl, String
sr, String
"]\n"]
asTree (Div SRTree ix val
l SRTree ix val
r) = do
String
sl <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asTree SRTree ix val
l
String
sr <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asTree SRTree ix val
r
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"[÷\n", String
sl, String
sr, String
"]\n"]
asTree (Power SRTree ix val
l SRTree ix val
r) = do
String
sl <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asTree SRTree ix val
l
String
sr <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asTree SRTree ix val
r
String
pow <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall ix val. DisplayNodes ix val -> String
_displayFloatPow
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"[", String
pow, String
"\n", String
sl, String
sr, String
"]\n"]
asTree (LogBase SRTree ix val
l SRTree ix val
r) = do
String
sl <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asTree SRTree ix val
l
String
sr <- forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asTree SRTree ix val
r
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [String
"[log\n", String
sl, String
sr, String
"]\n"]
showExpr, showTree :: (Show ix, Show val) => SRTree ix val -> DisplayNodes ix val -> String
showExpr :: forall ix val.
(Show ix, Show val) =>
SRTree ix val -> DisplayNodes ix val -> String
showExpr SRTree ix val
t = forall r a. Reader r a -> r -> a
runReader (forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asExpr SRTree ix val
t)
{-# INLINE showExpr #-}
showTree :: forall ix val.
(Show ix, Show val) =>
SRTree ix val -> DisplayNodes ix val -> String
showTree SRTree ix val
t = forall r a. Reader r a -> r -> a
runReader (forall ix val.
(Show ix, Show val) =>
SRTree ix val -> Reader (DisplayNodes ix val) String
asTree SRTree ix val
t)
{-# INLINE showTree #-}
printExpr :: (Show ix, Show val) => SRTree ix val -> DisplayNodes ix val -> IO ()
printExpr :: forall ix val.
(Show ix, Show val) =>
SRTree ix val -> DisplayNodes ix val -> IO ()
printExpr SRTree ix val
t = String -> IO ()
putStrLn forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix val.
(Show ix, Show val) =>
SRTree ix val -> DisplayNodes ix val -> String
showExpr SRTree ix val
t
showDefault :: SRTree ix val -> String
showDefault SRTree ix val
t = forall ix val.
(Show ix, Show val) =>
SRTree ix val -> DisplayNodes ix val -> String
showExpr SRTree ix val
t DisplayNodes ix val
d
where
d :: DisplayNodes ix val
d = forall ix val.
(ix -> String)
-> (ix -> String)
-> (val -> String)
-> (Function -> String)
-> String
-> String
-> DisplayNodes ix val
D (\ix
ix -> forall a. Monoid a => [a] -> a
mconcat [String
"x", forall a. Show a => a -> String
show ix
ix])
(\ix
ix -> forall a. Monoid a => [a] -> a
mconcat [String
"t", forall a. Show a => a -> String
show ix
ix])
forall a. Show a => a -> String
show
forall a. Show a => a -> String
show
String
"^"
String
"**"
showTikz :: (Show ix, Show val, RealFrac val) => SRTree ix val -> String
showTikz :: forall ix val.
(Show ix, Show val, RealFrac val) =>
SRTree ix val -> String
showTikz SRTree ix val
t = forall ix val.
(Show ix, Show val) =>
SRTree ix val -> DisplayNodes ix val -> String
showTree SRTree ix val
t DisplayNodes ix val
d
where
d :: DisplayNodes ix val
d = forall ix val.
(ix -> String)
-> (ix -> String)
-> (val -> String)
-> (Function -> String)
-> String
-> String
-> DisplayNodes ix val
D (\ix
ix -> forall a. Monoid a => [a] -> a
mconcat [String
"$x_{", forall a. Show a => a -> String
show ix
ix, String
"}$"])
(\ix
ix -> forall a. Monoid a => [a] -> a
mconcat [String
"$\\theta_{", forall a. Show a => a -> String
show ix
ix, String
"}$"])
(\val
val -> forall a. Monoid a => [a] -> a
mconcat [String
"$", forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ (forall a. Fractional a => a -> a -> a
/Double
100) forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a b. (RealFrac a, Integral b) => a -> b
round forall a b. (a -> b) -> a -> b
$ val
valforall a. Num a => a -> a -> a
*val
100, String
"$"])
forall a. Show a => a -> String
show
String
"\\^{}"
String
"**"
showPython :: SRTree ix val -> String
showPython SRTree ix val
t = forall ix val.
(Show ix, Show val) =>
SRTree ix val -> DisplayNodes ix val -> String
showExpr SRTree ix val
t DisplayNodes ix val
d
where
d :: DisplayNodes ix val
d = forall ix val.
(ix -> String)
-> (ix -> String)
-> (val -> String)
-> (Function -> String)
-> String
-> String
-> DisplayNodes ix val
D (\ix
ix -> forall a. Monoid a => [a] -> a
mconcat [String
"x[:,", forall a. Show a => a -> String
show ix
ix, String
"]"])
(\ix
ix -> forall a. Monoid a => [a] -> a
mconcat [String
"t[", forall a. Show a => a -> String
show ix
ix, String
"]"])
forall a. Show a => a -> String
show
Function -> String
pyFun
String
"**"
String
"**"
pyFun :: Function -> String
pyFun Function
Id = String
""
pyFun Function
Abs = String
"np.abs"
pyFun Function
Sin = String
"np.sin"
pyFun Function
Cos = String
"np.cos"
pyFun Function
Tan = String
"np.tan"
pyFun Function
Sinh = String
"np.sinh"
pyFun Function
Cosh = String
"np.cosh"
pyFun Function
Tanh = String
"np.tanh"
pyFun Function
ASin = String
"np.asin"
pyFun Function
ACos = String
"np.acos"
pyFun Function
ATan = String
"np.atan"
pyFun Function
ASinh = String
"np.asinh"
pyFun Function
ACosh = String
"np.acosh"
pyFun Function
ATanh = String
"np.atanh"
pyFun Function
Sqrt = String
"np.sqrt"
pyFun Function
Square = String
"np.square"
pyFun Function
Log = String
"np.log"
pyFun Function
Exp = String
"np.exp"
showLatex :: (Show ix, Show val) => SRTree ix val -> String
showLatex :: forall ix val. (Show ix, Show val) => SRTree ix val -> String
showLatex SRTree ix val
Empty = String
""
showLatex (Var ix
ix) = forall a. Monoid a => [a] -> a
mconcat [String
"x_{", forall a. Show a => a -> String
show ix
ix, String
"}"]
showLatex (Param ix
ix) = forall a. Monoid a => [a] -> a
mconcat [String
"\\theta_{", forall a. Show a => a -> String
show ix
ix, String
"}"]
showLatex (Const val
val) = forall a. Show a => a -> String
show val
val
showLatex (Fun Function
Abs SRTree ix val
t) = forall a. Monoid a => [a] -> a
mconcat [String
"\\left |", forall ix val. (Show ix, Show val) => SRTree ix val -> String
showLatex SRTree ix val
t, String
"\\right |"]
showLatex (Fun Function
f SRTree ix val
t) = forall a. Monoid a => [a] -> a
mconcat [Function -> String
showLatexFun Function
f, String
"\\left(", forall ix val. (Show ix, Show val) => SRTree ix val -> String
showLatex SRTree ix val
t, String
"\\right)"]
showLatex (Pow SRTree ix val
t Int
ix) = forall a. Monoid a => [a] -> a
mconcat [String
"\\left(", forall ix val. (Show ix, Show val) => SRTree ix val -> String
showLatex SRTree ix val
t, String
"\\right)^{", forall a. Show a => a -> String
show Int
ix, String
"}"]
showLatex (Add SRTree ix val
l SRTree ix val
r) = forall a. Monoid a => [a] -> a
mconcat [String
"\\left(", forall ix val. (Show ix, Show val) => SRTree ix val -> String
showLatex SRTree ix val
l, String
"\\right) + \\left(", forall ix val. (Show ix, Show val) => SRTree ix val -> String
showLatex SRTree ix val
r, String
"\\right)"]
showLatex (Sub SRTree ix val
l SRTree ix val
r) = forall a. Monoid a => [a] -> a
mconcat [String
"\\left(", forall ix val. (Show ix, Show val) => SRTree ix val -> String
showLatex SRTree ix val
l, String
"\\right) - \\left(", forall ix val. (Show ix, Show val) => SRTree ix val -> String
showLatex SRTree ix val
r, String
"\\right)"]
showLatex (Mul SRTree ix val
l SRTree ix val
r) = forall a. Monoid a => [a] -> a
mconcat [String
"\\left(", forall ix val. (Show ix, Show val) => SRTree ix val -> String
showLatex SRTree ix val
l, String
"\\right) \\left(", forall ix val. (Show ix, Show val) => SRTree ix val -> String
showLatex SRTree ix val
r, String
"\\right)"]
showLatex (Div SRTree ix val
l SRTree ix val
r) = forall a. Monoid a => [a] -> a
mconcat [String
"\\frac{", forall ix val. (Show ix, Show val) => SRTree ix val -> String
showLatex SRTree ix val
l, String
"}{", forall ix val. (Show ix, Show val) => SRTree ix val -> String
showLatex SRTree ix val
r, String
"}"]
showLatex (Power SRTree ix val
l SRTree ix val
r) = forall a. Monoid a => [a] -> a
mconcat [String
"\\left(", forall ix val. (Show ix, Show val) => SRTree ix val -> String
showLatex SRTree ix val
l, String
"\\right)^{", forall ix val. (Show ix, Show val) => SRTree ix val -> String
showLatex SRTree ix val
r, String
"}"]
showLatex (LogBase SRTree ix val
l SRTree ix val
r) = forall a. Monoid a => [a] -> a
mconcat [String
"\\log_{", forall ix val. (Show ix, Show val) => SRTree ix val -> String
showLatex SRTree ix val
r, String
"}{", forall ix val. (Show ix, Show val) => SRTree ix val -> String
showLatex SRTree ix val
l, String
"}"]
showLatexFun :: Function -> String
showLatexFun :: Function -> String
showLatexFun Function
f = forall a. Monoid a => [a] -> a
mconcat [String
"\\operatorname{", forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show Function
f, String
"}"]