Skip to content

Commit b01b668

Browse files
authored
Fixes for SIMDPolynomials (#267)
* Fixes for SIMDPolynomials * Fix format
1 parent bcc6d7a commit b01b668

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

src/monomial.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ end
101101
102102
Returns whether the monomial of `t` is constant.
103103
"""
104-
isconstant(t::AbstractTermLike) = all(iszero, exponents(t))
104+
isconstant(t::AbstractTerm) = isconstant(monomial(t))
105+
isconstant(t::AbstractMonomialLike) = all(iszero, exponents(t))
105106
isconstant(v::AbstractVariable) = false
106107

107108
"""

src/show.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ function _show(io::IO, mime::MIME, var::AbstractVariable)
3434
print_subscript(io, mime, indices)
3535
end
3636
end
37-
function _show(io::IO, mime::MIME"text/print", var::AbstractVariable)
38-
return print(io, name(var))
39-
end
4037

38+
function print_subscript(io::IO, ::MIME"text/print", index)
39+
return print(io, "[", join(index, ","), "]")
40+
end
4141
function print_subscript(io::IO, ::MIME"text/latex", index)
4242
return print(io, "_{", join(index, ","), "}")
4343
end

src/substitution.jl

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,39 @@ function powersubstitute(
8181
)
8282
return powersubstitute(st, s, p) * powersubstitute(st, s, p2...)
8383
end
84+
85+
function _promote_subs(S, T, s::Substitution)
86+
# `T` is a constant
87+
return T
88+
end
89+
90+
function _promote_subs(S, T::Type{<:Union{RationalPoly,_APL}}, s::Substitution)
91+
return MA.promote_operation(substitute, S, T, typeof(s))
92+
end
93+
94+
function _promote_subs(S, T, s::AbstractMultiSubstitution)
95+
return _promote_subs(
96+
S,
97+
T,
98+
pair_zip(_monomial_vector_to_variable_tuple(s))...,
99+
)
100+
end
101+
102+
function _promote_subs(
103+
S,
104+
T,
105+
head::AbstractSubstitution,
106+
tail::Vararg{AbstractSubstitution,N},
107+
) where {N}
108+
return _promote_subs(S, _promote_subs(S, T, head), tail...)
109+
end
110+
84111
function substitute(st::_AST, m::AbstractMonomial, s::Substitutions)
85-
return powersubstitute(st, s, powers(m)...)
112+
if isconstant(m)
113+
return one(_promote_subs(typeof(st), typeof(m), s...))
114+
else
115+
return powersubstitute(st, s, powers(m)...)
116+
end
86117
end
87118

88119
## Terms
@@ -92,11 +123,20 @@ end
92123

93124
function MA.promote_operation(
94125
::typeof(substitute),
95-
::Type{Subs},
126+
::Type{Eval},
127+
::Type{M},
128+
::Type{Pair{V,T}},
129+
) where {M<:AbstractMonomial,V<:AbstractVariable,T}
130+
return MA.promote_operation(*, T, T)
131+
end
132+
133+
function MA.promote_operation(
134+
::typeof(substitute),
135+
::Type{S},
96136
::Type{T},
97137
args::Vararg{Type,N},
98-
) where {T<:AbstractTerm,N}
99-
M = MA.promote_operation(substitute, Subs, monomial_type(T), args...)
138+
) where {S<:AbstractSubstitutionType,T<:AbstractTerm,N}
139+
M = MA.promote_operation(substitute, S, monomial_type(T), args...)
100140
U = coefficient_type(T)
101141
return MA.promote_operation(*, U, M)
102142
end
@@ -121,11 +161,11 @@ end
121161

122162
function MA.promote_operation(
123163
::typeof(substitute),
124-
::Type{Subs},
164+
::Type{S},
125165
::Type{P},
126166
args::Vararg{Type,N},
127-
) where {P<:AbstractPolynomial,N}
128-
T = MA.promote_operation(substitute, Subs, term_type(P), args...)
167+
) where {S<:AbstractSubstitutionType,P<:AbstractPolynomial,N}
168+
T = MA.promote_operation(substitute, S, term_type(P), args...)
129169
return MA.promote_operation(+, T, T)
130170
end
131171

test/substitution.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
using Test
12
import Test: @inferred
23

4+
import MutableArithmetics as MA
5+
36
@testset "Substitution" begin
47
Mod.@polyvar x[1:3]
58

@@ -28,6 +31,8 @@ import Test: @inferred
2831
p = x[1] + x[2] + 2 * x[1]^2 + 3 * x[1] * x[2]^2
2932
#@inferred p((x[1], x[2]) => (1.0, 2.0))
3033

34+
m = x[1] * x[2]
35+
@inferred subs(m, x[2] => 2.0)
3136
@inferred subs(p, x[2] => 2.0)
3237
@test subs(p, x[2] => 2.0) == 13x[1] + 2 + 2x[1]^2
3338
@inferred subs(p, x[2] => x[1])
@@ -77,4 +82,9 @@ import Test: @inferred
7782
@test t == @inferred subs(t, x => 1.0x)
7883
@test t == @inferred subs(t, x => 1.0)
7984
end
85+
86+
for (p, s) in [(x^1, x => 2x)]
87+
@test MA.promote_operation(substitute, Subs, typeof(p), typeof(s)) ==
88+
typeof(subs(p, s))
89+
end
8090
end

0 commit comments

Comments
 (0)