@@ -5,6 +5,7 @@ import ..UtilsModule: @memoize_on, @with_memoize, deprecate_varmap
55
66const DEFAULT_NODE_TYPE = Float32
77
8+ # ! format: off
89"""
910 Node{T}
1011
@@ -15,16 +16,16 @@ nodes, you can evaluate or print a given expression.
1516
1617# Fields
1718
18- - `degree::Int `: Degree of the node. 0 for constants, 1 for
19+ - `degree::UInt8 `: Degree of the node. 0 for constants, 1 for
1920 unary operators, 2 for binary operators.
2021- `constant::Bool`: Whether the node is a constant.
2122- `val::T`: Value of the node. If `degree==0`, and `constant==true`,
2223 this is the value of the constant. It has a type specified by the
2324 overall type of the `Node` (e.g., `Float64`).
24- - `feature::Int` (optional) : Index of the feature to use in the
25+ - `feature::UInt16` : Index of the feature to use in the
2526 case of a feature node. Only used if `degree==0` and `constant==false`.
2627 Only defined if `degree == 0 && constant == false`.
27- - `op::Int `: If `degree==1`, this is the index of the operator
28+ - `op::UInt8 `: If `degree==1`, this is the index of the operator
2829 in `operators.unaops`. If `degree==2`, this is the index of the
2930 operator in `operators.binops`. In other words, this is an enum
3031 of the operators, and is dependent on the specific `OperatorEnum`
@@ -36,36 +37,32 @@ nodes, you can evaluate or print a given expression.
3637 argument to the binary operator.
3738"""
3839mutable struct Node{T}
39- degree:: Int # 0 for constant/variable, 1 for cos/sin, 2 for +/* etc.
40+ degree:: UInt8 # 0 for constant/variable, 1 for cos/sin, 2 for +/* etc.
4041 constant:: Bool # false if variable
4142 val:: Union{T,Nothing} # If is a constant, this stores the actual value
4243 # ------------------- (possibly undefined below)
43- feature:: Int # If is a variable (e.g., x in cos(x)), this stores the feature index.
44- op:: Int # If operator, this is the index of the operator in operators.binary_operators , or operators.unary_operators
44+ feature:: UInt16 # If is a variable (e.g., x in cos(x)), this stores the feature index.
45+ op:: UInt8 # If operator, this is the index of the operator in operators.binops , or operators.unaops
4546 l:: Node{T} # Left child node. Only defined for degree=1 or degree=2.
4647 r:: Node{T} # Right child node. Only defined for degree=2.
4748
4849 # ################
4950 # # Constructors:
5051 # ################
51- Node (d:: Int , c:: Bool , v:: _T ) where {_T} = new {_T} (d, c, v)
52- Node (:: Type{_T} , d:: Int , c:: Bool , v:: _T ) where {_T} = new {_T} (d, c, v)
53- Node (:: Type{_T} , d:: Int , c:: Bool , v:: Nothing , f:: Int ) where {_T} = new {_T} (d, c, v, f)
54- function Node (d:: Int , c:: Bool , v:: Nothing , f:: Int , o:: Int , l:: Node{_T} ) where {_T}
55- return new {_T} (d, c, v, f, o, l)
56- end
57- function Node (
58- d:: Int , c:: Bool , v:: Nothing , f:: Int , o:: Int , l:: Node{_T} , r:: Node{_T}
59- ) where {_T}
60- return new {_T} (d, c, v, f, o, l, r)
61- end
52+ Node (d:: Integer , c:: Bool , v:: _T ) where {_T} = new {_T} (UInt8 (d), c, v)
53+ Node (:: Type{_T} , d:: Integer , c:: Bool , v:: _T ) where {_T} = new {_T} (UInt8 (d), c, v)
54+ Node (:: Type{_T} , d:: Integer , c:: Bool , v:: Nothing , f:: Integer ) where {_T} = new {_T} (UInt8 (d), c, v, UInt16 (f))
55+ Node (d:: Integer , c:: Bool , v:: Nothing , f:: Integer , o:: Integer , l:: Node{_T} ) where {_T} = new {_T} (UInt8 (d), c, v, UInt16 (f), UInt8 (o), l)
56+ Node (d:: Integer , c:: Bool , v:: Nothing , f:: Integer , o:: Integer , l:: Node{_T} , r:: Node{_T} ) where {_T} = new {_T} (UInt8 (d), c, v, UInt16 (f), UInt8 (o), l, r)
57+
6258end
6359# ###############################################################################
60+ # ! format: on
6461
6562include (" base.jl" )
6663
6764"""
68- Node([::Type{T}]; val=nothing, feature::Int =nothing) where {T}
65+ Node([::Type{T}]; val=nothing, feature::Union{Integer,Nothing} =nothing) where {T}
6966
7067Create a leaf node: either a constant, or a variable.
7168
@@ -115,18 +112,18 @@ function Node(
115112end
116113
117114"""
118- Node(op::Int , l::Node)
115+ Node(op::Integer , l::Node)
119116
120117Apply unary operator `op` (enumerating over the order given) to `Node` `l`
121118"""
122- Node (op:: Int , l:: Node{T} ) where {T} = Node (1 , false , nothing , 0 , op, l)
119+ Node (op:: Integer , l:: Node{T} ) where {T} = Node (1 , false , nothing , 0 , op, l)
123120
124121"""
125- Node(op::Int , l::Node, r::Node)
122+ Node(op::Integer , l::Node, r::Node)
126123
127124Apply binary operator `op` (enumerating over the order given) to `Node`s `l` and `r`
128125"""
129- function Node (op:: Int , l:: Node{T1} , r:: Node{T2} ) where {T1,T2}
126+ function Node (op:: Integer , l:: Node{T1} , r:: Node{T2} ) where {T1,T2}
130127 # Get highest type:
131128 if T1 != T2
132129 T = promote_type (T1, T2)
141138
142139Create a variable node, using the format `"x1"` to mean feature 1
143140"""
144- Node (var_string:: String ) = Node (; feature= parse (Int , var_string[2 : end ]))
141+ Node (var_string:: String ) = Node (; feature= parse (UInt16 , var_string[2 : end ]))
145142
146143"""
147144 Node(var_string::String, variable_names::Array{String, 1})
@@ -261,7 +258,7 @@ Convert an equation to a string.
261258
262259# Keyword Arguments
263260- `bracketed`: (optional) whether to put brackets around the outside.
264- - `f_variable`: (optional) function to convert a variable to a string, of the form `(feature::Int , variable_names)`.
261+ - `f_variable`: (optional) function to convert a variable to a string, of the form `(feature::UInt8 , variable_names)`.
265262- `f_constant`: (optional) function to convert a constant to a string, of the form `(val, bracketed::Bool)`
266263- `variable_names::Union{Array{String, 1}, Nothing}=nothing`: (optional) what variables to print for each feature.
267264"""
0 commit comments