Skip to content

Commit ce6841a

Browse files
authored
Merge pull request #112 from jverzani/polyint
Polyint
2 parents 7dc8b85 + d938391 commit ce6841a

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/Polynomials.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ polyval(p::Poly, v::AbstractArray) = map(x->polyval(p, x), v)
427427
@compat (p::Poly)(x) = polyval(p, x)
428428

429429
"""
430+
Indefinite integral of a polynomial.
430431
431432
* `polyint(p::Poly, k::Number=0)`: Integrate the polynomial `p` term
432433
by term, optionally adding constant term `k`. The order of the
@@ -438,6 +439,7 @@ polyint(Poly([1, 0, -1])) # Poly(x - 0.3333333333333333x^3)
438439
polyint(Poly([1, 0, -1]), 2) # Poly(2.0 + x - 0.3333333333333333x^3)
439440
```
440441
442+
See also `polyint(p, a, b)` for a definite integral over `[a,b]`.
441443
"""
442444
# if we do not have any initial condition, assume k = zero(Int)
443445
polyint{T}(p::Poly{T}) = polyint(p, 0)
@@ -476,6 +478,22 @@ end
476478

477479
"""
478480
481+
* `polyint(p::Poly, a::Number, b::Number)`: Definite integral of the polynomial `p` over the interval `[a,b]`.
482+
483+
Examples:
484+
```
485+
x = variable()
486+
p = 1 - x^2
487+
polyint(p, 0, 1)
488+
```
489+
"""
490+
function polyint(p::Poly, a::Number, b::Number)
491+
P = polyint(p)
492+
P(b) - P(a)
493+
end
494+
495+
"""
496+
479497
* `polyder(p::Poly)`: Differentiate the polynomial `p` term by
480498
term. The order of the resulting polynomial is one lower than the
481499
order of `p`.

test/runtests.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ sprint(show, pNULL)
5151
@test polyval(poly([1//2, 3//2]), 1//2) == 0//1
5252
@test polyder(polyint(pN)) == pN
5353
@test polyder(pR) == Poly([-2//1,2//1])
54+
@test polyder(p3) == Poly([2,2])
55+
@test polyder(p1) == polyder(p0) == polyder(pNULL) == pNULL
5456
@test_throws ErrorException polyder(pR, -1)
5557
@test polyint(pNULL,1) == p1
5658
@test polyint(Poly(Rational[1,2,3])) == Poly(Rational[0, 1, 1, 1])
57-
@test polyder(p3) == Poly([2,2])
58-
@test polyder(p1) == polyder(p0) == polyder(pNULL) == pNULL
59+
@test polyint(p2, 0, 2) == 4.0
60+
5961

6062
@test pN(-.125) == 276.9609375
6163
@test pN([0.1, 0.2, 0.3]) == polyval(pN, [0.1, 0.2, 0.3])

0 commit comments

Comments
 (0)