forked from PSORLab/SourceCodeMcCormick.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarize.jl
More file actions
38 lines (38 loc) · 1.08 KB
/
binarize.jl
File metadata and controls
38 lines (38 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#=
Rules for transforming narity operations into arity 1 operations
=#
function binarize!(ex::BasicSymbolic)
exprtype(ex) in (SYM, TERM, DIV, POW) && return nothing
if exprtype(ex)==ADD
skipfirst = iszero(ex.coeff)
newdict = Dict{Any, Number}()
for (key, val) in ex.dict
if skipfirst
skipfirst = false
continue
end
newdict[key] = val
delete!(ex.dict, key)
end
a = SymbolicUtils.Add(Real, 0, newdict)
binarize!(a)
ex.dict[a] = 1
return nothing
elseif exprtype(ex)==MUL
skipfirst = isone(ex.coeff)
newdict = Dict{Any, Number}()
for (key, val) in ex.dict
if skipfirst
skipfirst = false
continue
end
newdict[key] = val
delete!(ex.dict, key)
end
a = SymbolicUtils.Mul(Real, 1, newdict)
binarize!(a)
ex.dict[a] = 1
return nothing
end
end
binarize!(a::Real) = error("Attempting to apply binarize!() to a Real")