Skip to content

Commit a05c3cf

Browse files
Merge pull request #771 from AayushSabharwal/as/poly-variant
feat: add `Polyform` variant, remove `AddOrMul`, `Pow`
2 parents 2f1123f + 066712b commit a05c3cf

File tree

12 files changed

+996
-1165
lines changed

12 files changed

+996
-1165
lines changed

Project.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
99
Bijections = "e2ed5e7c-b2de-5872-ae92-c73ca462fb04"
1010
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
1111
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
12+
ConcurrentUtilities = "f0e56b4a-5159-44fe-b623-3e5288b988bb"
1213
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
1314
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
1415
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
@@ -20,6 +21,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
2021
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
2122
Moshi = "2e0e35c7-a2e4-4343-998d-7ef72827ed2d"
2223
MultivariatePolynomials = "102ac46a-7ee4-5c85-9060-abc95bfdeaa3"
24+
MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
2325
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
2426
ReadOnlyArrays = "988b38a3-91fc-5605-94a2-ee2116b3bd83"
2527
ReadOnlyDicts = "795d4caa-f5a7-4580-b5d8-c01d53451803"
@@ -48,8 +50,9 @@ ArrayInterface = "7.8"
4850
Bijections = "0.1.2, 0.2"
4951
ChainRulesCore = "1"
5052
Combinatorics = "1.0"
53+
ConcurrentUtilities = "2.5.0"
5154
ConstructionBase = "1.5.7"
52-
DataStructures = "0.18"
55+
DataStructures = "0.19"
5356
DocStringExtensions = "0.8, 0.9"
5457
DynamicPolynomials = "0.5, 0.6"
5558
EnumX = "1.0.5"
@@ -58,6 +61,7 @@ LabelledArrays = "1.5"
5861
MacroTools = "0.5.16"
5962
Moshi = "0.3.6"
6063
MultivariatePolynomials = "0.5"
64+
MutableArithmetics = "1.6.4"
6165
NaNMath = "0.3, 1.1.2"
6266
OhMyThreads = "0.7"
6367
ReadOnlyArrays = "0.2.0"
@@ -91,3 +95,8 @@ Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
9195

9296
[targets]
9397
test = ["BenchmarkTools", "Documenter", "LabelledArrays", "Pkg", "PkgBenchmark", "Random", "ReferenceTests", "ReverseDiff", "SafeTestsets", "Test", "Zygote", "OhMyThreads", "RuntimeGeneratedFunctions"]
98+
99+
[sources]
100+
DynamicPolynomials = {url = "https://github.com/AayushSabharwal/DynamicPolynomials.jl", rev = "as/new-poly-merge"}
101+
MultivariatePolynomials = {url = "https://github.com/AayushSabharwal/MultivariatePolynomials.jl", rev = "as/poly-merge-nonconcrete"}
102+
MutableArithmetics = {url = "https://github.com/AayushSabharwal/MutableArithmetics.jl", rev="as+bl/simplify_promote_type_fallback"}

src/SymbolicUtils.jl

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,71 @@ using WeakValueDicts: WeakValueDict
3131
using WeakCacheSets: WeakCacheSet, getkey!
3232
using Base: RefValue
3333
import MacroTools
34+
import MultivariatePolynomials as MP
35+
import DynamicPolynomials as DP
36+
import MutableArithmetics as MA
37+
import ConcurrentUtilities: ReadWriteLock, readlock, readunlock
3438

3539
# include("WeakCacheSets.jl")
3640

41+
macro manually_scope(val, expr, is_forced = false)
42+
@assert Meta.isexpr(val, :call)
43+
@assert val.args[1] == :(=>)
44+
45+
var_name = val.args[2]
46+
new_val = val.args[3]
47+
old_name = gensym(:old_val)
48+
cur_name = gensym(:cur_val)
49+
retval_name = gensym(:retval)
50+
close_expr = :($var_name[] = $old_name)
51+
interpolated_expr = MacroTools.postwalk(expr) do ex
52+
if Meta.isexpr(ex, :$) && length(ex.args) == 1 && ex.args[1] == :$
53+
return cur_name
54+
else
55+
return ex
56+
end
57+
end
58+
basic_result = quote
59+
$cur_name = $var_name[] = $new_val
60+
$retval_name = try
61+
$interpolated_expr
62+
finally
63+
$close_expr
64+
end
65+
end
66+
is_forced && return quote
67+
$old_name = $var_name[]
68+
$basic_result
69+
end |> esc
70+
71+
return quote
72+
$old_name = $var_name[]
73+
if $iszero($old_name)
74+
$basic_result
75+
else
76+
$cur_name = $old_name
77+
$retval_name = begin
78+
$interpolated_expr
79+
end
80+
end
81+
$retval_name
82+
end |> esc
83+
end
84+
85+
# copied from https://github.com/JuliaLang/julia/blob/80f7db8e51b2ba1dd21e913611c23a6d5b75ecab/base/lock.jl#L371-L381
86+
# and adapted for readlock/readunlock
87+
macro readlock(l, expr)
88+
quote
89+
temp = $(esc(l))
90+
$readlock(temp)
91+
try
92+
$(esc(expr))
93+
finally
94+
$readunlock(temp)
95+
end
96+
end
97+
end
98+
3799
include("cache.jl")
38100
Base.@deprecate istree iscall
39101

@@ -66,7 +128,6 @@ include("matchers.jl")
66128
include("rewriters.jl")
67129

68130
# Convert to an efficient multi-variate polynomial representation
69-
import MultivariatePolynomials as MP
70131
import DynamicPolynomials
71132
export expand
72133
include("polyform.jl")

0 commit comments

Comments
 (0)