Skip to content

Commit

Permalink
Merge pull request #66 from SymbolicML/statictools
Browse files Browse the repository at this point in the history
(BREAKING) Refactor code to be more idiomatic
  • Loading branch information
MilesCranmer authored Feb 21, 2024
2 parents 1f1ad6c + 7397885 commit 4bd83e9
Show file tree
Hide file tree
Showing 18 changed files with 477 additions and 363 deletions.
9 changes: 4 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
name = "DynamicExpressions"
uuid = "a40a106e-89c9-4ca8-8020-a735e8728b6b"
authors = ["MilesCranmer <[email protected]>"]
version = "0.15.0"
version = "0.16.0"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
PackageExtensionCompat = "65ce6f38-6b18-4e1d-a461-8949797d7930"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Expand All @@ -34,8 +32,8 @@ Bumper = "0.6"
Compat = "3.37, 4"
Enzyme = "^0.11.12"
LoopVectorization = "0.12"
Optim = "0.19, 1"
MacroTools = "0.4, 0.5"
Optim = "0.19, 1"
PackageExtensionCompat = "1"
PrecompileTools = "1"
Reexport = "1"
Expand All @@ -48,6 +46,7 @@ Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Bumper = "8ce10254-0962-460f-a3d8-1f77fea1446e"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890"
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Expand All @@ -58,4 +57,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[targets]
test = ["Test", "SafeTestsets", "Aqua", "Bumper", "Enzyme", "ForwardDiff", "LoopVectorization", "Optim", "SpecialFunctions", "StaticArrays", "SymbolicUtils", "Zygote"]
test = ["Test", "SafeTestsets", "Aqua", "Bumper", "Enzyme", "ForwardDiff", "LinearAlgebra", "LoopVectorization", "Optim", "SpecialFunctions", "StaticArrays", "SymbolicUtils", "Zygote"]
2 changes: 1 addition & 1 deletion ext/DynamicExpressionsBumperExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function bumper_eval_tree_array(
leaf_node -> begin
ar = @alloc(T, n)
ok = if leaf_node.constant
v = leaf_node.val::T
v = leaf_node.val
ar .= v
isfinite(v)
else
Expand Down
22 changes: 11 additions & 11 deletions ext/DynamicExpressionsLoopVectorizationExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ function deg1_l2_ll0_lr0_eval(
tree::AbstractExpressionNode{T}, cX::AbstractMatrix{T}, op::F, op_l::F2, ::Val{true}
) where {T<:Number,F,F2}
if tree.l.l.constant && tree.l.r.constant
val_ll = tree.l.l.val::T
val_lr = tree.l.r.val::T
val_ll = tree.l.l.val
val_lr = tree.l.r.val
@return_on_check val_ll cX
@return_on_check val_lr cX
x_l = op_l(val_ll, val_lr)::T
Expand All @@ -51,7 +51,7 @@ function deg1_l2_ll0_lr0_eval(
@return_on_check x cX
return ResultOk(fill_similar(x, cX, axes(cX, 2)), true)
elseif tree.l.l.constant
val_ll = tree.l.l.val::T
val_ll = tree.l.l.val
@return_on_check val_ll cX
feature_lr = tree.l.r.feature
cumulator = similar(cX, axes(cX, 2))
Expand All @@ -63,7 +63,7 @@ function deg1_l2_ll0_lr0_eval(
return ResultOk(cumulator, true)
elseif tree.l.r.constant
feature_ll = tree.l.l.feature
val_lr = tree.l.r.val::T
val_lr = tree.l.r.val
@return_on_check val_lr cX
cumulator = similar(cX, axes(cX, 2))
@turbo for j in axes(cX, 2)
Expand All @@ -89,7 +89,7 @@ function deg1_l1_ll0_eval(
tree::AbstractExpressionNode{T}, cX::AbstractMatrix{T}, op::F, op_l::F2, ::Val{true}
) where {T<:Number,F,F2}
if tree.l.l.constant
val_ll = tree.l.l.val::T
val_ll = tree.l.l.val
@return_on_check val_ll cX
x_l = op_l(val_ll)::T
@return_on_check x_l cX
Expand All @@ -112,16 +112,16 @@ function deg2_l0_r0_eval(
tree::AbstractExpressionNode{T}, cX::AbstractMatrix{T}, op::F, ::Val{true}
) where {T<:Number,F}
if tree.l.constant && tree.r.constant
val_l = tree.l.val::T
val_l = tree.l.val
@return_on_check val_l cX
val_r = tree.r.val::T
val_r = tree.r.val
@return_on_check val_r cX
x = op(val_l, val_r)::T
@return_on_check x cX
return ResultOk(fill_similar(x, cX, axes(cX, 2)), true)
elseif tree.l.constant
cumulator = similar(cX, axes(cX, 2))
val_l = tree.l.val::T
val_l = tree.l.val
@return_on_check val_l cX
feature_r = tree.r.feature
@turbo for j in axes(cX, 2)
Expand All @@ -132,7 +132,7 @@ function deg2_l0_r0_eval(
elseif tree.r.constant
cumulator = similar(cX, axes(cX, 2))
feature_l = tree.l.feature
val_r = tree.r.val::T
val_r = tree.r.val
@return_on_check val_r cX
@turbo for j in axes(cX, 2)
x = op(cX[feature_l, j], val_r)
Expand Down Expand Up @@ -160,7 +160,7 @@ function deg2_l0_eval(
::Val{true},
) where {T<:Number,F}
if tree.l.constant
val = tree.l.val::T
val = tree.l.val
@return_on_check val cX
@turbo for j in eachindex(cumulator)
x = op(val, cumulator[j])
Expand All @@ -185,7 +185,7 @@ function deg2_r0_eval(
::Val{true},
) where {T<:Number,F}
if tree.r.constant
val = tree.r.val::T
val = tree.r.val
@return_on_check val cX
@turbo for j in eachindex(cumulator)
x = op(cumulator[j], val)
Expand Down
15 changes: 8 additions & 7 deletions ext/DynamicExpressionsSymbolicUtilsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function parse_tree_to_eqs(
) where {T}
if tree.degree == 0
# Return constant if needed
tree.constant && return subs_bad(tree.val::T)
tree.constant && return subs_bad(tree.val)
return SymbolicUtils.Sym{LiteralReal}(Symbol("x$(tree.feature)"))
end
# Collect the next children
Expand Down Expand Up @@ -93,10 +93,10 @@ function split_eq(
else
ind = findoperation(op, operators.binops)
end
return constructorof(N)(
ind,
convert(N, args[1], operators; variable_names=variable_names),
convert(N, op(args[2:end]...), operators; variable_names=variable_names),
return constructorof(N)(;
op=ind,
l=convert(N, args[1], operators; variable_names=variable_names),
r=convert(N, op(args[2:end]...), operators; variable_names=variable_names),
)
end

Expand Down Expand Up @@ -157,8 +157,9 @@ function Base.convert(
findoperation(op, operators.unaops)
end

return constructorof(N)(
ind, map(x -> convert(N, x, operators; variable_names=variable_names), args)...
return constructorof(N)(;
op=ind,
children=map(x -> convert(N, x, operators; variable_names=variable_names), args),
)
end

Expand Down
4 changes: 2 additions & 2 deletions src/DynamicExpressions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ include("ExtensionInterface.jl")
include("OperatorEnum.jl")
include("Equation.jl")
include("EquationUtils.jl")
include("Strings.jl")
include("EvaluateEquation.jl")
include("EvaluateEquationDerivative.jl")
include("EvaluationHelpers.jl")
Expand All @@ -19,8 +20,6 @@ import Reexport: @reexport
AbstractExpressionNode,
GraphNode,
Node,
string_tree,
print_tree,
copy_node,
set_node!,
tree_mapreduce,
Expand All @@ -39,6 +38,7 @@ import .EquationModule: constructorof, preserve_sharing
set_constants!,
get_constant_refs,
set_constant_refs!
@reexport import .StringsModule: string_tree, print_tree
@reexport import .OperatorEnumModule: AbstractOperatorEnum
@reexport import .OperatorEnumConstructionModule:
OperatorEnum, GenericOperatorEnum, @extend_operators, set_default_variable_names!
Expand Down
Loading

4 comments on commit 4bd83e9

@MilesCranmer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/102579

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.16.0 -m "<description of version>" 4bd83e97d23ebb3fbde57f720b0265c5ecc33881
git push origin v0.16.0

@MilesCranmer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/102579

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.16.0 -m "<description of version>" 4bd83e97d23ebb3fbde57f720b0265c5ecc33881
git push origin v0.16.0

Please sign in to comment.