Skip to content

Commit 629bd51

Browse files
committed
Merge pull request #3 from pwl/master
Added the option to specify the order of the derivative to polyder.
2 parents 8ac5823 + ed3f323 commit 629bd51

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/Polynomials.jl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -266,26 +266,30 @@ function polyint{T}(p::Poly{T}, k::Number=0)
266266
p2
267267
end
268268

269-
function polyder{T}(p::Poly{T})
269+
function polyder{T}(p::Poly{T}, order::Int=1)
270270
n = length(p)
271-
if n > 0
272-
p2 = Poly(Array(T, n-1), p.var)
273-
for i = 1:(n-1)
274-
p2[i-1] = p[i] * i
271+
if order < 0
272+
error("Order of derivative must be non-negative")
273+
elseif n <= order
274+
return Poly(zeros(T,0),p.var)
275+
elseif order == 0
276+
return p
277+
else
278+
p2 = Poly(Array(T, n-order), p.var)
279+
for i = order:n-1
280+
p2[i-order] = p[i] * factorial(i)/factorial(i-order)
275281
end
276282
return p2
277-
else
278-
return Poly(zeros(T, 0), p.var)
279283
end
280284
end
281285

282-
polyder{T}(a::Array{Poly{T}}) = [polyder(p) for p in a]
283-
polyint{T}(a::Array{Poly{T}}) = [polyint(p) for p in a]
286+
polyint{n,T}(a::Array{Poly{T},n}, k::Number = 0) = map(p->polyint(p,k),a)
287+
polyder{n,T}(a::Array{Poly{T},n}, order::Int = 1) = map(p->polyder(p,order),a)
284288

285289
# create a Poly object from its roots
286290
function poly{T}(r::AbstractVector{T}, var=:x)
287291
n = length(r)
288-
c = zeros(T, n+1)
292+
c = zeros(T, n+1)
289293
c[1] = 1
290294
for j = 1:n
291295
c[2:j+1] = c[2:j+1]-r[j]*c[1:j]

0 commit comments

Comments
 (0)