Skip to content

Commit 7a7702a

Browse files
committed
Fix differentiate of AbstractArrays
Previously `differentiate([x+y,y+z],[x,y,z])` would return a 3 x 2 matrix instead of the more commonly used 2 x 3 matrix.
1 parent a789f50 commit 7a7702a

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/differentiation.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ differentiate(p::RationalPoly, v::AbstractVariable) = (differentiate(p.num, v) *
4040
const ARPL = Union{APL, RationalPoly}
4141

4242
function differentiate(ps::AbstractArray{PT}, xs::AbstractArray) where {PT <: ARPL}
43-
differentiate.(reshape(ps, (1, size(ps)...)), reshape(xs, :))
43+
differentiate.(reshape(ps, (size(ps)..., 1)), reshape(xs, 1, :))
4444
end
4545

4646
function differentiate(ps::AbstractArray{PT}, xs::Tuple) where {PT <: ARPL}
47-
differentiate.(reshape(ps, (1, size(ps)...)), xs)
47+
differentiate(ps, collect(xs))
4848
end
4949

5050

test/differentiation.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@testset "Differentiation" begin
2-
Mod.@polyvar x y
2+
Mod.@polyvar x y z
33
@test differentiate(3, y) == 0
44
@test differentiate.([x, y], y) == [0, 1]
55
@test differentiate([x, y], (x, y)) == Matrix(1.0I, 2, 2) # TODO: this can be just `I` on v0.7 and above
@@ -34,6 +34,10 @@
3434
@test isa(p, Matrix{<:AbstractPolynomial{Float64}})
3535
@test p == [4.0 6.0y; 6.0y 6.0x+24.0y]
3636

37+
f = [x^2+y, z^2+4x]
38+
@test differentiate(f, [x, y, z]) == [2x 1 0; 4 0 2z]
39+
@test differentiate(f, (x, y, z)) == [2x 1 0; 4 0 2z]
40+
3741
@testset "differentiation with Val{}" begin
3842
@test @inferred(differentiate(x, x, Val{0}())) == x
3943
@test @inferred(differentiate(x, x, Val{1}())) == 1

0 commit comments

Comments
 (0)