srtree-2.0.1.4: A general library to work with Symbolic Regression expression trees.
Copyright(c) Fabricio Olivetti 2021 - 2024
LicenseBSD3
Maintainer[email protected]
Stabilityexperimental
PortabilityFlexibleInstances, DeriveFunctor, ScopedTypeVariables, ConstraintKinds
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.SRTree

Description

Expression tree for Symbolic Regression

Synopsis

Documentation

data SRTree val Source #

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 Source # 
Instance details

Defined in Data.SRTree.Internal

Methods

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

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

Foldable SRTree Source # 
Instance details

Defined in Data.SRTree.Internal

Methods

fold :: Monoid m => SRTree m -> m #

foldMap :: Monoid m => (a -> m) -> SRTree a -> m #

foldMap' :: Monoid m => (a -> m) -> SRTree a -> m #

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

foldr' :: (a -> b -> b) -> b -> SRTree a -> b #

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

foldl' :: (b -> a -> b) -> b -> SRTree a -> b #

foldr1 :: (a -> a -> a) -> SRTree a -> a #

foldl1 :: (a -> a -> a) -> SRTree a -> a #

toList :: SRTree a -> [a] #

null :: SRTree a -> Bool #

length :: SRTree a -> Int #

elem :: Eq a => a -> SRTree a -> Bool #

maximum :: Ord a => SRTree a -> a #

minimum :: Ord a => SRTree a -> a #

sum :: Num a => SRTree a -> a #

product :: Num a => SRTree a -> a #

Traversable SRTree Source # 
Instance details

Defined in Data.SRTree.Internal

Methods

traverse :: Applicative f => (a -> f b) -> SRTree a -> f (SRTree b) #

sequenceA :: Applicative f => SRTree (f a) -> f (SRTree a) #

mapM :: Monad m => (a -> m b) -> SRTree a -> m (SRTree b) #

sequence :: Monad m => SRTree (m a) -> m (SRTree a) #

Hashable ENode Source # 
Instance details

Defined in Algorithm.EqSat.Egraph

Methods

hashWithSalt :: Int -> ENode -> Int #

hash :: ENode -> Int #

Binary (SRTree EClassId) Source # 
Instance details

Defined in Algorithm.EqSat.Egraph

Binary (SRTree ()) Source # 
Instance details

Defined in Algorithm.EqSat.Egraph

Methods

put :: SRTree () -> Put #

get :: Get (SRTree ()) #

putList :: [SRTree ()] -> Put #

IsString (Fix SRTree) Source #

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) Source # 
Instance details

Defined in Data.SRTree.Internal

Num (Fix SRTree) Source # 
Instance details

Defined in Data.SRTree.Internal

Fractional (Fix SRTree) Source # 
Instance details

Defined in Data.SRTree.Internal

Show val => Show (SRTree val) Source # 
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) Source # 
Instance details

Defined in Data.SRTree.Internal

Methods

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

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

Ord val => Ord (SRTree val) Source # 
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 #

Generic (EClassId, ENode) Source # 
Instance details

Defined in Algorithm.EqSat.Egraph

Associated Types

type Rep (a, b)

Since: base-4.6.0.0

Instance details

Defined in GHC.Internal.Generics

Methods

from :: (EClassId, ENode) -> Rep (EClassId, ENode) x #

to :: Rep (EClassId, ENode) x -> (EClassId, ENode) #

data Function Source #

Supported functions

data Op Source #

Supported operators

Constructors

Add 
Sub 
Mul 
Div 
Power 
PowerAbs 
AQ 

Instances

Instances details
Enum Op Source # 
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 Source # 
Instance details

Defined in Data.SRTree.Internal

Show Op Source # 
Instance details

Defined in Data.SRTree.Internal

Methods

showsPrec :: Int -> Op -> ShowS #

show :: Op -> String #

showList :: [Op] -> ShowS #

Eq Op Source # 
Instance details

Defined in Data.SRTree.Internal

Methods

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

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

Ord Op Source # 
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 Source #

create a tree with a single node representing a parameter

var :: Int -> Fix SRTree Source #

create a tree with a single node representing a variable

constv :: Double -> Fix SRTree Source #

create a tree with a single node representing a constant value

arity :: Fix SRTree -> Int Source #

Arity of the current node

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

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

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

childrenOf :: SRTree a -> [a] Source #

Get the children of an unfixed node

replaceChildren :: [a] -> SRTree b -> SRTree a Source #

replaces the children with elements from a list

getOperator :: SRTree a -> SRTree () Source #

returns a node containing the operator and () as children

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

Count the number of nodes in a tree.

>>> countNodes $ "x0" + 2
3

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

Count the number of Var nodes

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

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

Count the number of const nodes

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

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

Count the number of Param nodes

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

countParamsUniq :: Fix SRTree -> Int Source #

Count the unique occurrences of Param nodes

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

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

Count the occurrences of variable indexed as ix

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

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

counts the number of unique tokens

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

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

return the number of unique variables

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

getIntConsts :: Fix SRTree -> [Double] Source #

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 Source #

Relabel the parameters indices incrementaly starting from 0

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

relabelParamsOrder :: Fix SRTree -> Fix SRTree Source #

Reorder the labels of the parameters indices

>>> showExpr . relabelParamsOrder $ "x0" + "t1" * sin ("t3" + "x1") - "t1"
"x0" + "t0" * sin ("t1" + "x1") - "t0"

relabelVars :: Fix SRTree -> Fix SRTree Source #

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]) Source #

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]) Source #

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 Source #

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 :: Type -> Type) Source #

Constructors

Fix 

Fields

Instances

Instances details
IsString (Fix SRTree) Source #

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) Source # 
Instance details

Defined in Data.SRTree.Internal

Num (Fix SRTree) Source # 
Instance details

Defined in Data.SRTree.Internal

Fractional (Fix SRTree) Source # 
Instance details

Defined in Data.SRTree.Internal