srtree-2.0.0.0: A general library to work with Symbolic Regression expression trees.
Copyright(c) Fabricio Olivetti 2021 - 2024
LicenseBSD3
Maintainerfabricio.olivetti@gmail.com
Stabilityexperimental
PortabilityFlexibleInstances, DeriveFunctor, ScopedTypeVariables, ConstraintKinds
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.SRTree

Description

Expression tree for Symbolic Regression

Synopsis

Documentation

data SRTree val #

Tree structure to be used with Symbolic Regression algorithms. This structure is a fixed point of a n-ary tree.

Constructors

Var Int

index of the variables

Param Int

index of the parameter

Const Double

constant value, can be converted to a parameter | IConst Int -- TODO: integer constant | RConst Ratio -- TODO: rational constant

Uni Function val

univariate function

Bin Op val val

binary operator

Instances

Instances details
Functor SRTree # 
Instance details

Defined in Data.SRTree.Internal

Methods

fmap :: (a -> b) -> SRTree a -> SRTree b #

(<$) :: a -> SRTree b -> SRTree a #

IsString (Fix SRTree) #

the instance of IsString allows us to create a tree using a more practical notation:

>>> :t  "x0" + "t0" * sin("x1" * "t1")
Fix SRTree
Instance details

Defined in Data.SRTree.Internal

Floating (Fix SRTree) # 
Instance details

Defined in Data.SRTree.Internal

Num (Fix SRTree) # 
Instance details

Defined in Data.SRTree.Internal

Fractional (Fix SRTree) # 
Instance details

Defined in Data.SRTree.Internal

Show val => Show (SRTree val) # 
Instance details

Defined in Data.SRTree.Internal

Methods

showsPrec :: Int -> SRTree val -> ShowS #

show :: SRTree val -> String #

showList :: [SRTree val] -> ShowS #

Eq val => Eq (SRTree val) # 
Instance details

Defined in Data.SRTree.Internal

Methods

(==) :: SRTree val -> SRTree val -> Bool #

(/=) :: SRTree val -> SRTree val -> Bool #

Ord val => Ord (SRTree val) # 
Instance details

Defined in Data.SRTree.Internal

Methods

compare :: SRTree val -> SRTree val -> Ordering #

(<) :: SRTree val -> SRTree val -> Bool #

(<=) :: SRTree val -> SRTree val -> Bool #

(>) :: SRTree val -> SRTree val -> Bool #

(>=) :: SRTree val -> SRTree val -> Bool #

max :: SRTree val -> SRTree val -> SRTree val #

min :: SRTree val -> SRTree val -> SRTree val #

data Op #

Supported operators

Constructors

Add 
Sub 
Mul 
Div 
Power 

Instances

Instances details
Enum Op # 
Instance details

Defined in Data.SRTree.Internal

Methods

succ :: Op -> Op #

pred :: Op -> Op #

toEnum :: Int -> Op #

fromEnum :: Op -> Int #

enumFrom :: Op -> [Op] #

enumFromThen :: Op -> Op -> [Op] #

enumFromTo :: Op -> Op -> [Op] #

enumFromThenTo :: Op -> Op -> Op -> [Op] #

Read Op # 
Instance details

Defined in Data.SRTree.Internal

Show Op # 
Instance details

Defined in Data.SRTree.Internal

Methods

showsPrec :: Int -> Op -> ShowS #

show :: Op -> String #

showList :: [Op] -> ShowS #

Eq Op # 
Instance details

Defined in Data.SRTree.Internal

Methods

(==) :: Op -> Op -> Bool #

(/=) :: Op -> Op -> Bool #

Ord Op # 
Instance details

Defined in Data.SRTree.Internal

Methods

compare :: Op -> Op -> Ordering #

(<) :: Op -> Op -> Bool #

(<=) :: Op -> Op -> Bool #

(>) :: Op -> Op -> Bool #

(>=) :: Op -> Op -> Bool #

max :: Op -> Op -> Op #

min :: Op -> Op -> Op #

param :: Int -> Fix SRTree #

create a tree with a single node representing a parameter

var :: Int -> Fix SRTree #

create a tree with a single node representing a variable

constv :: Double -> Fix SRTree #

create a tree with a single node representing a constant value

arity :: Fix SRTree -> Int #

Arity of the current node

getChildren :: Fix SRTree -> [Fix SRTree] #

Get the children of a node. Returns an empty list in case of a leaf node.

>>> map showExpr . getChildren $ "x0" + 2
["x0", 2]

countNodes :: Num a => Fix SRTree -> a #

Count the number of nodes in a tree.

>>> countNodes $ "x0" + 2
3

countVarNodes :: Num a => Fix SRTree -> a #

Count the number of Var nodes

>>> countVarNodes $ "x0" + 2 * ("x0" - sin "x1")
3

countConsts :: Num a => Fix SRTree -> a #

Count the number of const nodes

>>> countConsts $ "x0"* 2 + 3 * sin "x0"
2

countParams :: Num a => Fix SRTree -> a #

Count the number of Param nodes

>>> countParams $ "x0" + "t0" * sin ("t1" + "x1") - "t0"
3

countOccurrences :: Num a => Int -> Fix SRTree -> a #

Count the occurrences of variable indexed as ix

>>> countOccurrences 0 $ "x0"* 2 + 3 * sin "x0" + "x1"
2

countUniqueTokens :: Num a => Fix SRTree -> a #

counts the number of unique tokens

>>> countUniqueTokens $ "x0" + ("x1" * "x0" - sin ("x0" ** 2))
8

numberOfVars :: Num a => Fix SRTree -> a #

return the number of unique variables

>>> numberOfVars $ "x0" + 2 * ("x0" - sin "x1")
2

getIntConsts :: Fix SRTree -> [Double] #

returns the integer constants. We assume an integer constant as those values in which `floor x == ceiling x`.

>>> getIntConsts $ "x0" + 2 * "x1" ** 3 - 3.14
[2.0,3.0]

relabelParams :: Fix SRTree -> Fix SRTree #

Relabel the parameters indices incrementaly starting from 0

>>> showExpr . relabelParams $ "x0" + "t0" * sin ("t1" + "x1") - "t0"
"x0" + "t0" * sin ("t1" + "x1") - "t2" 

constsToParam :: Fix SRTree -> (Fix SRTree, [Double]) #

Change constant values to a parameter, returning the changed tree and a list of parameter values

>>> snd . constsToParam $ "x0" * 2 + 3.14 * sin (5 * "x1")
[2.0,3.14,5.0]

floatConstsToParam :: Fix SRTree -> (Fix SRTree, [Double]) #

Same as constsToParam but does not change constant values that can be converted to integer without loss of precision

>>> snd . floatConstsToParam $ "x0" * 2 + 3.14 * sin (5 * "x1")
[3.14]

paramsToConst :: [Double] -> Fix SRTree -> Fix SRTree #

Convert the parameters into constants in the tree

>>> showExpr . paramsToConst [1.1, 2.2, 3.3] $ "x0" + "t0" * sin ("t1" * "x0" - "t2")
x0 + 1.1 * sin(2.2 * x0 - 3.3)

newtype Fix f #

Constructors

Fix 

Fields

Instances

Instances details
IsString (Fix SRTree) #

the instance of IsString allows us to create a tree using a more practical notation:

>>> :t  "x0" + "t0" * sin("x1" * "t1")
Fix SRTree
Instance details

Defined in Data.SRTree.Internal

Floating (Fix SRTree) # 
Instance details

Defined in Data.SRTree.Internal

Num (Fix SRTree) # 
Instance details

Defined in Data.SRTree.Internal

Fractional (Fix SRTree) # 
Instance details

Defined in Data.SRTree.Internal