Skip to content

Commit 8dd49dd

Browse files
authored
Merge pull request #39 from blegat/fix38
Fix #38
2 parents 6019e9c + e0e10b3 commit 8dd49dd

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ language: julia
33
os:
44
- linux
55
julia:
6-
- 0.5
76
- 0.6
87
- nightly
98
notifications:

appveyor.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
environment:
22
matrix:
3-
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
4-
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
53
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
64
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
75
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"

src/diff.jl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,28 @@ differentiate(p::MatPolynomial, x) = differentiate(Polynomial(p), x)
3838

3939
differentiate(p::RationalPoly, x::PolyVar) = (differentiate(p.num, x) * p.den - p.num * differentiate(p.den, x)) / p.den^2
4040

41-
function differentiate{C}(p::PolyType{C}, xs::Vector{PolyVar{C}})
41+
# even if I annotate with ::Array{_diff_promote_op(T, PolyVar{C}), N+1}, it cannot detect the type since it seems to be unable to determine the dimension N+1 :(
42+
function differentiate{N, C, T<:PolyType{C}}(ps::AbstractArray{T, N}, xs::Union{AbstractVector{PolyVar{C}}, Tuple})
43+
qs = Array{_diff_promote_op(T, PolyVar{C}), N+1}(length(xs), size(ps)...)
44+
for (i, x) in enumerate(xs)
45+
for j in linearindices(ps)
46+
J = ind2sub(ps, j)
47+
qs[i, J...] = differentiate(ps[J...], x)
48+
end
49+
end
50+
qs
51+
end
52+
function differentiate{C}(p::PolyType{C}, xs::Union{AbstractVector{PolyVar{C}}, Tuple})
4253
[differentiate(p, x) for x in xs]
4354
end
4455

4556
# In Julia v0.5, Base.promote_op returns Any for PolyVar, Monomial and MatPolynomial
57+
# Even on Julia v0.6 and Polynomial, Base.promote_op returns Any...
4658
_diff_promote_op(S, T) = Base.promote_op(differentiate, S, T)
47-
_diff_promote_op{C}(::Type{PolyVar{C}}, ::Type{PolyVar{C}}) = Term{true, Int}
48-
_diff_promote_op{C}(::Type{Monomial{C}}, ::Type{PolyVar{C}}) = Term{true, Int}
49-
_diff_promote_op{C, T}(::Type{MatPolynomial{C, T}}, ::Type{PolyVar{C}}) = Polynomial{true, Base.promote_op(*, T, Int)}
59+
_diff_promote_op{C}(::Union{Type{PolyVar{C}}, Type{Monomial{C}}}, ::Type{PolyVar{C}}) = Term{true, Int}
60+
_diff_promote_op{C, T}(::Union{Type{Polynomial{C, T}}, Type{MatPolynomial{C, T}}}, ::Type{PolyVar{C}}) = Polynomial{true, Base.promote_op(*, T, Int)}
5061

51-
function differentiate(p::PolyType, x, deg::Int)
62+
function differentiate{T<:PolyType}(p::Union{T, AbstractArray{T}}, x, deg::Int)
5263
if deg < 0
5364
throw(DomainError())
5465
elseif deg == 0

test/diff.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
@inferred differentiate(true*x+true*x^2, y)
55
@test differentiate(MatPolynomial{true, Int}((i,j)->1, [x]), y) == 0
66
@test differentiate(x*y + 3y^2 , [x, y]) == [y, x+6y]
7+
@inferred differentiate(x*y + 3y^2 , x)
78
@inferred differentiate(x*y + 3y^2 , [x, y])
89
@test differentiate(1 / x , [x, y]) == [-1/x^2, 0]
910
@test differentiate((x - y) / (x * y) , [x, y]) == [y^2 / (x * y)^2, -x^2 / (x * y)^2]
@@ -21,4 +22,11 @@
2122
@inferred differentiate(2x^2, x, 2)
2223
@test differentiate(MatPolynomial{true, Int}((i,j)->1, [x, y]), y, 1) == 2x + 2y
2324
@inferred differentiate(MatPolynomial{true, Int}((i,j)->1, [x, y]), y, 0)
25+
@inferred differentiate(2x^2 + x*y + y^2, [x, y])
26+
p = differentiate(2x^2 + 3x*y + y^2, [x, y], 2)
27+
@test isa(p, Matrix{Polynomial{true,Int}})
28+
@test p == [4 3; 3 2]
29+
p = differentiate(2x^2 + 3x*y^2 + 4y^3 + 2.0, (x, y), 2)
30+
@test isa(p, Matrix{Polynomial{true,Float64}})
31+
@test p == [4.0 6.0y; 6.0y 6.0x+24.0y]
2432
end

0 commit comments

Comments
 (0)