Skip to content

Commit 3635a37

Browse files
authored
Generalize integrand f to any callable (#137)
* Remove Function requirement * Remove Function requirement * Remove Function requirement * Remove Function requirement * Remove Function requirement * Remove Function requirement * Remove Function requirement * Remove Function requirement * Remove Function requirement * Remove Function requirement * Remove Function requirement * Remove Function requirement * Remove Function requirement * Convert pi to FP * Update docstring * Update docstring * Update docstring [skip CI] * Update docstring * Update to explain callable f * Update to reflect changed README * Add testing for callable struct as f * Update function name
1 parent cba39f0 commit 3635a37

16 files changed

+84
-81
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ These solvers have support for integrand functions that produce scalars, vectors
2525
```julia
2626
integral(f, geometry)
2727
```
28-
Performs a numerical integration of some integrand function `f(p::Meshes.Point)` over the domain specified by `geometry`. A default integration method will be automatically selected according to the geometry: `GaussKronrod()` for 1D, and `HAdaptiveCubature()` for all others.
28+
Performs a numerical integration of some integrand function `f` over the domain specified by `geometry`. The integrand function can be anything callable with a method `f(::Meshes.Point)`. A default integration method will be automatically selected according to the geometry: `GaussKronrod()` for 1D, and `HAdaptiveCubature()` for all others.
2929

3030
```julia
3131
integral(f, geometry, rule)
3232
```
33-
Performs a numerical integration of some integrand function `f(p::Meshes.Point)` over the domain specified by `geometry` using the specified integration rule, e.g. `GaussKronrod()`.
33+
Performs a numerical integration of some integrand function `f` over the domain specified by `geometry` using the specified integration rule, e.g. `GaussKronrod()`. The integrand function can be anything callable with a method `f(::Meshes.Point)`.
3434

3535
Additionally, several optional keyword arguments are defined in [the API](https://juliageometry.github.io/MeshIntegrals.jl/stable/api/) to provide additional control over the integration mechanics.
3636

docs/src/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ These solvers have support for integrand functions that produce scalars, vectors
2525
```julia
2626
integral(f, geometry)
2727
```
28-
Performs a numerical integration of some integrand function `f(p::Meshes.Point)` over the domain specified by `geometry`. A default integration method will be automatically selected according to the geometry: `GaussKronrod()` for 1D, and `HAdaptiveCubature()` for all others.
28+
Performs a numerical integration of some integrand function `f` over the domain specified by `geometry`. The integrand function can be anything callable with a method `f(::Meshes.Point)`. A default integration method will be automatically selected according to the geometry: `GaussKronrod()` for 1D, and `HAdaptiveCubature()` for all others.
2929

3030
```julia
3131
integral(f, geometry, rule)
3232
```
33-
Performs a numerical integration of some integrand function `f(p::Meshes.Point)` over the domain specified by `geometry` using the specified integration rule, e.g. `GaussKronrod()`.
33+
Performs a numerical integration of some integrand function `f` over the domain specified by `geometry` using the specified integration rule, e.g. `GaussKronrod()`. The integrand function can be anything callable with a method `f(::Meshes.Point)`.
3434

3535
Additionally, several optional keyword arguments are defined in [the API](https://juliageometry.github.io/MeshIntegrals.jl/stable/api/) to provide additional control over the integration mechanics.
3636

src/integral.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ a `geometry` using a particular numerical integration `rule` with floating point
1010
precision of type `FP`.
1111
1212
# Arguments
13-
- `f`: an integrand function with a method `f(::Meshes.Point)`
13+
- `f`: an integrand function, i.e. any callable with a method `f(::Meshes.Point)`
1414
- `geometry`: some `Meshes.Geometry` that defines the integration domain
1515
- `rule`: optionally, the `IntegrationRule` used for integration (by default
1616
`GaussKronrod()` in 1D and `HAdaptiveCubature()` else)
@@ -24,7 +24,7 @@ function integral end
2424

2525
# If only f and geometry are specified, select default rule
2626
function integral(
27-
f::Function,
27+
f,
2828
geometry::Geometry,
2929
rule::I = Meshes.paramdim(geometry) == 1 ? GaussKronrod() : HAdaptiveCubature();
3030
kwargs...

src/integral_aliases.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Rule types available:
1515
- [`HAdaptiveCubature`](@ref)
1616
"""
1717
function lineintegral(
18-
f::Function,
18+
f,
1919
geometry::Geometry,
2020
rule::IntegrationRule = GaussKronrod();
2121
kwargs...
@@ -47,7 +47,7 @@ Algorithm types available:
4747
- [`HAdaptiveCubature`](@ref) (default)
4848
"""
4949
function surfaceintegral(
50-
f::Function,
50+
f,
5151
geometry::Geometry,
5252
rule::IntegrationRule = HAdaptiveCubature();
5353
kwargs...
@@ -79,7 +79,7 @@ Algorithm types available:
7979
- [`HAdaptiveCubature`](@ref) (default)
8080
"""
8181
function volumeintegral(
82-
f::Function,
82+
f,
8383
geometry::Geometry,
8484
rule::IntegrationRule = HAdaptiveCubature();
8585
kwargs...

src/specializations/BezierCurve.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
Like [`integral`](@ref) but integrates along the domain defined by `curve`.
2020
2121
# Arguments
22-
- `f`: an integrand function with a method `f(::Meshes.Point)`
22+
- `f`: an integrand function, i.e. any callable with a method `f(::Meshes.Point)`
2323
- `curve`: a `Meshes.BezierCurve` that defines the integration domain
2424
- `rule = GaussKronrod()`: optionally, the `IntegrationRule` used for integration
2525
@@ -32,13 +32,13 @@ calculating Jacobians that are used to calculate differential elements
3232
steep performance cost, especially for curves with a large number of control points.
3333
"""
3434
function integral(
35-
f::F,
35+
f,
3636
curve::Meshes.BezierCurve,
3737
rule::GaussLegendre;
3838
diff_method::DM = _default_method(curve),
3939
FP::Type{T} = Float64,
4040
alg::Meshes.BezierEvalMethod = Meshes.Horner()
41-
) where {F <: Function, DM <: DifferentiationMethod, T <: AbstractFloat}
41+
) where {DM <: DifferentiationMethod, T <: AbstractFloat}
4242
# Compute Gauss-Legendre nodes/weights for x in interval [-1,1]
4343
xs, ws = _gausslegendre(FP, rule.n)
4444

@@ -52,26 +52,26 @@ function integral(
5252
end
5353

5454
function integral(
55-
f::F,
55+
f,
5656
curve::Meshes.BezierCurve,
5757
rule::GaussKronrod;
5858
diff_method::DM = _default_method(curve),
5959
FP::Type{T} = Float64,
6060
alg::Meshes.BezierEvalMethod = Meshes.Horner()
61-
) where {F <: Function, DM <: DifferentiationMethod, T <: AbstractFloat}
61+
) where {DM <: DifferentiationMethod, T <: AbstractFloat}
6262
point(t) = curve(t, alg)
6363
integrand(t) = f(point(t)) * differential(curve, (t,), diff_method)
6464
return QuadGK.quadgk(integrand, zero(FP), one(FP); rule.kwargs...)[1]
6565
end
6666

6767
function integral(
68-
f::F,
68+
f,
6969
curve::Meshes.BezierCurve,
7070
rule::HAdaptiveCubature;
7171
diff_method::DM = _default_method(curve),
7272
FP::Type{T} = Float64,
7373
alg::Meshes.BezierEvalMethod = Meshes.Horner()
74-
) where {F <: Function, DM <: DifferentiationMethod, T <: AbstractFloat}
74+
) where {DM <: DifferentiationMethod, T <: AbstractFloat}
7575
point(t) = curve(t, alg)
7676
integrand(ts) = f(point(only(ts))) * differential(curve, ts, diff_method)
7777

src/specializations/ConeSurface.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
################################################################################
1010

1111
function integral(
12-
f::F,
12+
f,
1313
cone::Meshes.ConeSurface,
1414
rule::I;
1515
kwargs...
16-
) where {F <: Function, I <: IntegrationRule}
16+
) where {I <: IntegrationRule}
1717
# The generic method only parameterizes the sides
1818
sides = _integral(f, cone, rule; kwargs...)
1919

src/specializations/CylinderSurface.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
################################################################################
1010

1111
function integral(
12-
f::F,
12+
f,
1313
cyl::Meshes.CylinderSurface,
1414
rule::I;
1515
kwargs...
16-
) where {F <: Function, I <: IntegrationRule}
16+
) where {I <: IntegrationRule}
1717
# The generic method only parameterizes the sides
1818
sides = _integral(f, cyl, rule; kwargs...)
1919

src/specializations/FrustumSurface.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
################################################################################
1010

1111
function integral(
12-
f::F,
12+
f,
1313
frust::Meshes.FrustumSurface,
1414
rule::I;
1515
kwargs...
16-
) where {F <: Function, I <: IntegrationRule}
16+
) where {I <: IntegrationRule}
1717
# The generic method only parameterizes the sides
1818
sides = _integral(f, frust, rule; kwargs...)
1919

src/specializations/Line.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
################################################################################
99

1010
function integral(
11-
f::F,
11+
f,
1212
line::Meshes.Line,
1313
rule::GaussLegendre;
1414
diff_method::DM = Analytical(),
1515
FP::Type{T} = Float64
16-
) where {F <: Function, DM <: DifferentiationMethod, T <: AbstractFloat}
16+
) where {DM <: DifferentiationMethod, T <: AbstractFloat}
1717
_guarantee_analytical(Meshes.Line, diff_method)
1818

1919
# Compute Gauss-Legendre nodes/weights for x in interval [-1,1]
@@ -33,12 +33,12 @@ function integral(
3333
end
3434

3535
function integral(
36-
f::F,
36+
f,
3737
line::Meshes.Line,
3838
rule::GaussKronrod;
3939
diff_method::DM = Analytical(),
4040
FP::Type{T} = Float64
41-
) where {F <: Function, DM <: DifferentiationMethod, T <: AbstractFloat}
41+
) where {DM <: DifferentiationMethod, T <: AbstractFloat}
4242
_guarantee_analytical(Meshes.Line, diff_method)
4343

4444
# Normalize the Line s.t. line(t) is distance t from origin point
@@ -51,12 +51,12 @@ function integral(
5151
end
5252

5353
function integral(
54-
f::F,
54+
f,
5555
line::Meshes.Line,
5656
rule::HAdaptiveCubature;
5757
diff_method::DM = Analytical(),
5858
FP::Type{T} = Float64
59-
) where {F <: Function, DM <: DifferentiationMethod, T <: AbstractFloat}
59+
) where {DM <: DifferentiationMethod, T <: AbstractFloat}
6060
_guarantee_analytical(Meshes.Line, diff_method)
6161

6262
# Normalize the Line s.t. line(t) is distance t from origin point

src/specializations/Plane.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
################################################################################
1010

1111
function integral(
12-
f::F,
12+
f,
1313
plane::Meshes.Plane,
1414
rule::GaussLegendre;
1515
diff_method::DM = Analytical(),
1616
FP::Type{T} = Float64
17-
) where {F <: Function, DM <: DifferentiationMethod, T <: AbstractFloat}
17+
) where {DM <: DifferentiationMethod, T <: AbstractFloat}
1818
_guarantee_analytical(Meshes.Plane, diff_method)
1919

2020
# Get Gauss-Legendre nodes and weights for a 2D region [-1,1]²
@@ -40,12 +40,12 @@ function integral(
4040
end
4141

4242
function integral(
43-
f::F,
43+
f,
4444
plane::Meshes.Plane,
4545
rule::GaussKronrod;
4646
diff_method::DM = Analytical(),
4747
FP::Type{T} = Float64
48-
) where {F <: Function, DM <: DifferentiationMethod, T <: AbstractFloat}
48+
) where {DM <: DifferentiationMethod, T <: AbstractFloat}
4949
_guarantee_analytical(Meshes.Plane, diff_method)
5050

5151
# Normalize the Plane's orthogonal vectors
@@ -61,12 +61,12 @@ function integral(
6161
end
6262

6363
function integral(
64-
f::F,
64+
f,
6565
plane::Meshes.Plane,
6666
rule::HAdaptiveCubature;
6767
diff_method::DM = Analytical(),
6868
FP::Type{T} = Float64
69-
) where {F <: Function, DM <: DifferentiationMethod, T <: AbstractFloat}
69+
) where {DM <: DifferentiationMethod, T <: AbstractFloat}
7070
_guarantee_analytical(Meshes.Plane, diff_method)
7171

7272
# Normalize the Plane's orthogonal vectors
@@ -80,8 +80,8 @@ function integral(
8080

8181
# Integrate f over the Plane
8282
domainunits = _units(uplane(0, 0))
83-
function integrand(x::AbstractVector)
84-
f(uplane(t(x[1]), t(x[2]))) * t′(x[1]) * t′(x[2]) * domainunits^2
83+
function integrand(xs)
84+
f(uplane(t(xs[1]), t(xs[2]))) * t′(xs[1]) * t′(xs[2]) * domainunits^2
8585
end
8686

8787
# HCubature doesn't support functions that output Unitful Quantity types

0 commit comments

Comments
 (0)