Skip to content

Commit 3f7ef75

Browse files
authored
Merge pull request #105 from JuliaAlgebra/st/arrays
Better support arrays of polynomials.
2 parents fd3c42e + 8729bdd commit 3f7ef75

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

src/operators.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ for (op, fun) in [(:+, :plusconstant), (:-, :minusconstant), (:*, :multconstant)
2727
@eval Base.$op(p::APL, α) = $fun(p, α)
2828
@eval Base.$op(α, p::APL) = $fun(α, p)
2929
end
30+
31+
# Special case AbstractArrays of APLs
32+
# We add these instead of relying on the broadcasting API since the above method definitions are very wide.
33+
# In particular, there is support for Matrices as coefficents. In order to avoid issues like #104 we therefore
34+
# explicitly define this (instead of implictly getting unexpected results).
35+
for op in [:+, :-, :*]
36+
@eval Base.$op(p::APL, A::AbstractArray{<:APL}) = map(f -> $op(p, f), A)
37+
@eval Base.$op(A::AbstractArray{<:APL}, p::APL) = map(f -> $op(f, p), A)
38+
end
39+
Base.:/(A::AbstractArray{<:APL}, p::APL) = map(f -> f / p, A)
40+
3041
Base.isapprox(p::APL, α; kwargs...) = isapprox(promote(p, α)...; kwargs...)
3142
Base.isapprox(α, p::APL; kwargs...) = isapprox(promote(p, α)...; kwargs...)
3243

src/substitution.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ substitute(st::AST, q::RationalPoly, s::Substitutions) = substitute(st, q.num, s
7575
# subs(x, x=>x+y, y=>2) would call substitute(Subs(), x+y, y=>2)
7676
#substitute(st::AST, p::Union{APL, RationalPoly}, s::AbstractSubstitution...) = substitute(st, p, s)
7777

78+
# Substitute Arrays
79+
function substitute(st::AST, A::AbstractArray{<:APL}, s::Substitutions)
80+
map(p -> substitute(st, p, s), A)
81+
end
7882
## Everything else
7983
substitute(::AST, x, s::Substitutions) = x
8084
# subs(x, x=>1, y=>2) would call substitute(Subs(), 1, y=>2)

test/algebra.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,14 @@
6565

6666
@test iszero((x+x-2*x) * (x * (x^2 + y^2)))
6767
@test iszero((0*x) * (x*y * (x^2 + y^2)))
68+
69+
@testset "Scalar - Array" begin
70+
Mod.@polyvar x y
71+
@test x + [x^2+y y; y x*y] == [x+x^2+y x+y; x+y x+x*y]
72+
@test [x^2+y y; y x*y] + x == [x^2+x+y y+x; y+x x*y+x]
73+
74+
@test x * [1+y y] == [x*(1+y) x*y]
75+
@test [1+y y] * x == [(1+y)*x y*x]
76+
@test [1+y y] / x == [(1+y)/x y/x]
77+
end
6878
end

test/substitution.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,8 @@ import Test: @inferred
6363
@test p(variables(p) => valsv) == 13
6464
@test p(varst => valst) == 13
6565
@test p(varsv => valsv) == 13
66+
67+
68+
Mod.@polyvar x y
69+
@test subs([x^2 + y, x + y], x => y) == [y^2+y, 2y]
6670
end

0 commit comments

Comments
 (0)