@@ -136,8 +136,10 @@ function _add_variables!(p::PolyType, q::PolyType)
136
136
return p
137
137
end
138
138
139
- function monoeval (z:: Vector{Int} , vals:: AbstractVector )
140
- @assert length (z) == length (vals)
139
+ function _mono_eval (z:: Union{Vector{Int},Tuple} , vals:: AbstractVector )
140
+ if length (z) != length (vals)
141
+ error (" Cannot evaluate a polynomial of `$(length (z)) ` variables with only `$(length (vals)) ` values." )
142
+ end
141
143
if isempty (z)
142
144
return one (eltype (vals))^ 1
143
145
end
@@ -154,24 +156,24 @@ function monoeval(z::Vector{Int}, vals::AbstractVector)
154
156
return val
155
157
end
156
158
157
- _subs (st , :: Variable , vals) = monoeval ([ 1 ], vals:: AbstractVector )
158
- _subs (st , m:: Monomial , vals) = monoeval (m. z, vals:: AbstractVector )
159
- function _subs (st, t:: _Term , vals)
160
- return MP. coefficient (t) * monoeval ( MP. monomial (t). z , vals:: AbstractVector )
159
+ MP . substitute ( :: MP.AbstractSubstitutionType , :: Variable , vals:: AbstractVector ) = _mono_eval (( 1 ,), vals)
160
+ MP . substitute ( :: MP.AbstractSubstitutionType , m:: Monomial , vals:: AbstractVector ) = _mono_eval (m. z, vals)
161
+ function MP . substitute (st:: MP.AbstractSubstitutionType , t:: _Term , vals:: AbstractVector )
162
+ return MP. coefficient (t) * MP . substitute (st, MP. monomial (t), vals)
161
163
end
162
- function _subs (
164
+ function MP . substitute (
163
165
:: MP.Eval ,
164
166
p:: Polynomial{V,M,T} ,
165
167
vals:: AbstractVector{S} ,
166
168
) where {V,M,T,S}
167
169
# I need to check for iszero otherwise I get : ArgumentError: reducing over an empty collection is not allowed
168
170
if iszero (p)
169
- zero (Base . promote_op (* , S, T))
171
+ zero (MA . promote_operation (* , S, T))
170
172
else
171
- sum (i -> p. a[i] * monoeval (p. x. Z[i], vals), eachindex (p. a))
173
+ sum (i -> p. a[i] * _mono_eval (p. x. Z[i], vals), eachindex (p. a))
172
174
end
173
175
end
174
- function _subs (
176
+ function MP . substitute (
175
177
:: MP.Subs ,
176
178
p:: Polynomial{V,M,T} ,
177
179
vals:: AbstractVector{S} ,
@@ -182,7 +184,7 @@ function _subs(
182
184
mergevars_of (Variable{V,M}, vals)[1 ],
183
185
)
184
186
for i in eachindex (p. a)
185
- MA. operate! (+ , q, p. a[i] * monoeval (p. x. Z[i], vals))
187
+ MA. operate! (+ , q, p. a[i] * _mono_eval (p. x. Z[i], vals))
186
188
end
187
189
return q
188
190
end
@@ -197,12 +199,30 @@ function MA.promote_operation(
197
199
return MA. promote_operation (* , U, Monomial{V,M})
198
200
end
199
201
202
+ function MP. substitute (
203
+ st:: MP.AbstractSubstitutionType ,
204
+ p:: PolyType ,
205
+ s:: MP.AbstractSubstitution... ,
206
+ )
207
+ return MP. substitute (st, p, subsmap (st, MP. variables (p), s))
208
+ end
209
+
210
+ # TODO resolve ambiguity. Can remove after:
211
+ # https://github.com/JuliaAlgebra/MultivariatePolynomials.jl/pull/305
212
+ function MP. substitute (
213
+ st:: MP.AbstractSubstitutionType ,
214
+ p:: PolyType ,
215
+ s:: MP.AbstractMultiSubstitution ,
216
+ )
217
+ return MP. substitute (st, p, subsmap (st, MP. variables (p), (s,)))
218
+ end
219
+
200
220
function MP. substitute (
201
221
st:: MP.AbstractSubstitutionType ,
202
222
p:: PolyType ,
203
223
s:: MP.Substitutions ,
204
224
)
205
- return _subs (st, p, subsmap (st, MP. variables (p), s))
225
+ return MP . substitute (st, p, subsmap (st, MP. variables (p), s))
206
226
end
207
227
208
228
(v:: Variable )(s:: MP.AbstractSubstitution... ) = MP. substitute (MP. Eval (), v, s)
@@ -215,20 +235,20 @@ function (p::Monomial)(x::NTuple{N,<:Number}) where {N}
215
235
return MP. substitute (MP. Eval (), p, variables (p) => x)
216
236
end
217
237
function (p:: Monomial )(x:: AbstractVector{<:Number} )
218
- return MP. substitute (MP. Eval (), p, variables (p) => x)
238
+ return MP. substitute (MP. Eval (), p, x)
219
239
end
220
240
(p:: Monomial )(x:: Number... ) = MP. substitute (MP. Eval (), p, variables (p) => x)
221
241
function (p:: _Term )(x:: NTuple{N,<:Number} ) where {N}
222
242
return MP. substitute (MP. Eval (), p, variables (p) => x)
223
243
end
224
244
function (p:: _Term )(x:: AbstractVector{<:Number} )
225
- return MP. substitute (MP. Eval (), p, variables (p) => x)
245
+ return MP. substitute (MP. Eval (), p, x)
226
246
end
227
247
(p:: _Term )(x:: Number... ) = MP. substitute (MP. Eval (), p, variables (p) => x)
228
248
function (p:: Polynomial )(x:: NTuple{N,<:Number} ) where {N}
229
249
return MP. substitute (MP. Eval (), p, variables (p) => x)
230
250
end
231
251
function (p:: Polynomial )(x:: AbstractVector{<:Number} )
232
- return MP. substitute (MP. Eval (), p, variables (p) => x)
252
+ return MP. substitute (MP. Eval (), p, x)
233
253
end
234
254
(p:: Polynomial )(x:: Number... ) = MP. substitute (MP. Eval (), p, variables (p) => x)
0 commit comments