-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsubstitution.jl
239 lines (204 loc) · 6.99 KB
/
substitution.jl
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# Base on the TypedPolynomials/abstract/substition.jl written by Robin Deits
# TODO Vararg{<:...} -> Vararg{...}
const Substitution = Pair{<:AbstractVariable}
const MultiSubstitution{N} =
Pair{<:Tuple{Vararg{AbstractVariable,N}},<:Tuple{Vararg{Any,N}}}
const MultiVectorSubstitution =
Pair{<:Tuple{Vararg{AbstractVariable}},<:AbstractVector}
# When the variables are promoted to be in the same vector they could be promoted into a monomial
const VectorMultiSubstitution =
Pair{<:AbstractVector{<:AbstractMonomialLike},<:Tuple}
const VectorMultiVectorSubstitution =
Pair{<:AbstractVector{<:AbstractMonomialLike},<:AbstractVector}
function _monomial_vector_to_variable_tuple(
s::Pair{<:AbstractVector{<:AbstractMonomial}},
)
return variable.(Tuple(s.first)) => s.second
end
_monomial_vector_to_variable_tuple(s) = s
const AbstractMultiSubstitution = Union{
MultiSubstitution,
MultiVectorSubstitution,
VectorMultiVectorSubstitution,
VectorMultiSubstitution,
}
const AbstractSubstitution = Union{Substitution,AbstractMultiSubstitution}
const Substitutions = Tuple{Vararg{AbstractSubstitution}}
abstract type AbstractSubstitutionType end
struct Subs <: AbstractSubstitutionType end
struct Eval <: AbstractSubstitutionType end
const _AST = AbstractSubstitutionType
"""
subs(polynomial, (x, y)=>(1, 2))
is equivalent to:
subs(polynomial, (x=>1, y=>2))
"""
function substitute(st::_AST, p::_APL, s::AbstractMultiSubstitution)
return substitute(st, p, _flatten_subs(s))
end
## Variables
function substitute(st::_AST, v::AbstractVariable, s::Substitutions)
return substitute(st, v, s...)
end
## Monomials
function powersubstitute(
st::_AST,
s::Substitutions,
p::Tuple{AbstractVariable,Integer},
)
return substitute(st, p[1], s...)^p[2]
end
function powersubstitute(
st::_AST,
s::Substitutions,
p::Tuple{AbstractVariable,Integer},
p2...,
)
return powersubstitute(st, s, p) * powersubstitute(st, s, p2...)
end
function _promote_subs(
S,
::Type{V},
s::Substitution,
) where {V<:AbstractVariable}
return MA.promote_operation(substitute, S, V, typeof(s))
end
function _flatten_subs(s::AbstractMultiSubstitution)
return pair_zip(_monomial_vector_to_variable_tuple(s))
end
function _flatten_subs(s::Substitution)
return (s,)
end
# Turn a tuple of `AbstractSubstitution` into a `Tuple` if `Substitution`
function _flatten_subs(
s::AbstractSubstitution,
tail::Vararg{AbstractSubstitution,N},
) where {N}
return (_flatten_subs(s)..., _flatten_subs(tail...)...)
end
function power_promote(
S,
::Type{V},
s::Substitutions,
) where {V<:AbstractVariable}
T = MA.promote_operation(substitute, S, V, typeof.(_flatten_subs(s...))...)
return MA.promote_operation(*, T, T)
end
function power_promote(
S,
::Vector{V},
s::Substitutions,
) where {V<:AbstractVariable}
return power_promote(S, V, s)
end
function power_promote(
S,
::Tuple{V},
s::Substitutions,
) where {V<:AbstractVariable}
return power_promote(S, V, s)
end
function power_promote(
S,
vars::Tuple{V,Vararg{AbstractVariable,N}},
s::Substitutions,
) where {V<:AbstractVariable,N}
return MA.promote_operation(
*,
power_promote(S, V, s),
power_promote(S, Base.tail(vars), s),
)
end
function substitute(st::_AST, m::AbstractMonomial, s::Substitutions)
if isconstant(m)
return one(power_promote(typeof(st), variables(m), s))
else
return powersubstitute(st, s, powers(m)...)
end
end
## Terms
function substitute(st::_AST, t::AbstractTerm, s::Substitutions)
return coefficient(t) * substitute(st, monomial(t), s)
end
function MA.promote_operation(
::typeof(substitute),
::Type{Eval},
::Type{M},
::Type{Pair{V,T}},
) where {M<:AbstractMonomial,V<:AbstractVariable,T}
return MA.promote_operation(*, T, T)
end
function MA.promote_operation(
::typeof(substitute),
::Type{S},
::Type{T},
args::Vararg{Type,N},
) where {S<:AbstractSubstitutionType,T<:AbstractTerm,N}
M = MA.promote_operation(substitute, S, monomial_type(T), args...)
U = coefficient_type(T)
return MA.promote_operation(*, U, M)
end
## Polynomials
_polynomial(α) = α
_polynomial(p::_APL) = polynomial(p)
function substitute(st::_AST, p::AbstractPolynomial, s::Substitutions)
if iszero(p)
_polynomial(substitute(st, zero_term(p), s))
else
ts = terms(p)
r1 = substitute(st, ts[1], s)
R = MA.promote_operation(+, typeof(r1), typeof(r1))
result::R = convert(R, r1)
for i in 2:length(ts)
result += substitute(st, ts[i], s)
end
result
end
end
function MA.promote_operation(
::typeof(substitute),
::Type{S},
::Type{P},
args::Vararg{Type,N},
) where {S<:AbstractSubstitutionType,P<:AbstractPolynomial,N}
T = MA.promote_operation(substitute, S, term_type(P), args...)
return MA.promote_operation(+, T, T)
end
## Fallbacks
function substitute(st::_AST, p::_APL, s::Substitutions)
return substitute(st, polynomial(p), s)
end
function substitute(st::_AST, q::RationalPoly, s::Substitutions)
return substitute(st, q.num, s) / substitute(st, q.den, s)
end
# subs(x, x=>x+y, y=>2) would call substitute(Subs(), x+y, y=>2)
#substitute(st::_AST, p::Union{_APL, RationalPoly}, s::AbstractSubstitution...) = substitute(st, p, s)
# Substitute Arrays
function substitute(st::_AST, A::AbstractArray{<:_APL}, s::Substitutions)
return map(p -> substitute(st, p, s), A)
end
## Everything else
substitute(::_AST, x, s::Substitutions) = x
# subs(x, x=>1, y=>2) would call substitute(Subs(), 1, y=>2)
#substitute(::_AST, x, s::AbstractSubstitution...) = x
"""
subs(p, s::AbstractSubstitution...)
Apply the substitutions `s` to `p`.
Use `p(s...)` if we are sure that all the variables are substited in `s`.
The allowed substutions are:
* `v => p` where `v` is a variable and `p` a polynomial, e.g. `x => 1` or `x => x^2*y + x + y`.
* `V => P` where `V` is a tuple or vector of variables and `P` a tuple or vector of polynomials, e.g. `(x, y) => (y, x)` or `(y, x) => (2, 1)`.
The order of the variables is lexicographic with the name with TypedPolynomials and by order of creation with DynamicPolynomials.
Since there is no guarantee on the order of the variables, substitution directly with a tuple or a vector is not allowed.
You can use `p(variables(p) => (1, 2))` instead if you are sure of the order of the variables (e.g. the name order matches the creation order).
### Examples
```julia
p = 3x^2*y + x + 2y + 1
p(x => 2, y => 1) # Return type is Int
subs(p, x => 2, y => 1) # Return type is Int in TypedPolynomials but is a polynomial of Int coefficients in DynamicPolynomials
subs(p, y => x*y^2 + 1)
p(y => 2) # Do not do that, this works fine with TypedPolynomials but it will not return a correct result with DynamicPolynomials since it thinks that the return type is `Int`.
```
"""
subs(p, s::AbstractSubstitution...) = substitute(Subs(), p, s)
(p::RationalPoly)(s::AbstractSubstitution...) = p.num(s...) / p.den(s...)