Skip to content

Commit fba0bf2

Browse files
committed
add definite integral interface for polyint
1 parent 0da3d97 commit fba0bf2

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
@@ -430,6 +430,7 @@ polyval(p::Poly, v::AbstractArray) = map(x->polyval(p, x), v)
430430
@compat (p::Poly)(x) = polyval(p, x)
431431

432432
"""
433+
Indefinite integral of a polynomial.
433434
434435
* `polyint(p::Poly, k::Number=0)`: Integrate the polynomial `p` term
435436
by term, optionally adding constant term `k`. The order of the
@@ -441,6 +442,7 @@ polyint(Poly([1, 0, -1])) # Poly(x - 0.3333333333333333x^3)
441442
polyint(Poly([1, 0, -1]), 2) # Poly(2.0 + x - 0.3333333333333333x^3)
442443
```
443444
445+
See also `polyint(p, a, b)` for a definite integral over `[a,b]`.
444446
"""
445447
# if we do not have any initial condition, assume k = zero(Int)
446448
polyint{T}(p::Poly{T}) = polyint(p, 0)
@@ -479,6 +481,22 @@ end
479481

480482
"""
481483
484+
* `polyint(p::Poly, a::Number, b::Number)`: Definite integral of the polynomial `p` over the interval `[a,b]`.
485+
486+
Examples:
487+
```
488+
x = variable()
489+
p = 1 - x^2
490+
polyint(p, 0, 1)
491+
```
492+
"""
493+
function polyint(p::Poly, a::Number, b::Number)
494+
P = polyint(p)
495+
P(b) - P(a)
496+
end
497+
498+
"""
499+
482500
* `polyder(p::Poly)`: Differentiate the polynomial `p` term by
483501
term. The order of the resulting polynomial is one lower than the
484502
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)