Skip to content

Commit 0cd335d

Browse files
mikeingoldJoshuaLampertgithub-actions[bot]
authored
Implement _zeros and _ones utils (#135)
* Implement _zeros and _ones utils * Change handling of negative _ones * Generalize integrand arg * Fix typo * Add new tests * Drop leaving zero Co-authored-by: Joshua Lampert <[email protected]> * Apply bot formatting suggestion Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: Joshua Lampert <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 1bccd58 commit 0cd335d

File tree

8 files changed

+27
-8
lines changed

8 files changed

+27
-8
lines changed

src/integral.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function _integral(
100100
# Create a wrapper that returns only the value component in those units
101101
uintegrand(ts) = Unitful.ustrip.(integrandunits, integrand(ts))
102102
# Integrate only the unitless values
103-
value = HCubature.hcubature(uintegrand, zeros(FP, N), ones(FP, N); rule.kwargs...)[1]
103+
value = HCubature.hcubature(uintegrand, _zeros(FP, N), _ones(FP, N); rule.kwargs...)[1]
104104

105105
# Reapply units
106106
return value .* integrandunits

src/specializations/BezierCurve.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function integral(
8282
# Create a wrapper that returns only the value component in those units
8383
uintegrand(ts) = Unitful.ustrip.(integrandunits, integrand(ts))
8484
# Integrate only the unitless values
85-
value = HCubature.hcubature(uintegrand, zeros(FP, 1), ones(FP, 1); rule.kwargs...)[1]
85+
value = HCubature.hcubature(uintegrand, _zeros(FP, 1), _ones(FP, 1); rule.kwargs...)[1]
8686

8787
# Reapply units
8888
return value .* integrandunits

src/specializations/Line.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ function integral(
6868

6969
# Integrate f along the Line
7070
differential(line, x) = t′(x) * _units(line(0))
71-
integrand(x::AbstractVector) = f(line(t(x[1]))) * differential(line, x[1])
71+
integrand(xs) = f(line(t(xs[1]))) * differential(line, xs[1])
7272

7373
# HCubature doesn't support functions that output Unitful Quantity types
7474
# Establish the units that are output by f
75-
testpoint_parametriccoord = FP[0.5]
75+
testpoint_parametriccoord = (FP(0.5),)
7676
integrandunits = Unitful.unit.(integrand(testpoint_parametriccoord))
7777
# Create a wrapper that returns only the value component in those units
7878
uintegrand(uv) = Unitful.ustrip.(integrandunits, integrand(uv))

src/specializations/Plane.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ function integral(
9191
# Create a wrapper that returns only the value component in those units
9292
uintegrand(uv) = Unitful.ustrip.(integrandunits, integrand(uv))
9393
# Integrate only the unitless values
94-
value = HCubature.hcubature(uintegrand, -ones(FP, 2), ones(FP, 2); rule.kwargs...)[1]
94+
a = .-_ones(FP, 2)
95+
b = _ones(FP, 2)
96+
value = HCubature.hcubature(uintegrand, a, b; rule.kwargs...)[1]
9597

9698
# Reapply units
9799
return value .* integrandunits

src/specializations/Ray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function integral(
8282
# Create a wrapper that returns only the value component in those units
8383
uintegrand(uv) = Unitful.ustrip.(integrandunits, integrand(uv))
8484
# Integrate only the unitless values
85-
value = HCubature.hcubature(uintegrand, zeros(FP, 1), ones(FP, 1); rule.kwargs...)[1]
85+
value = HCubature.hcubature(uintegrand, _zeros(FP, 1), _ones(FP, 1); rule.kwargs...)[1]
8686

8787
# Reapply units
8888
return value .* integrandunits

src/specializations/Triangle.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function integral(
8787
v = R * (1 - b / (a + b))
8888
return f(triangle(u, v)) * R / (a + b)^2
8989
end
90-
= HCubature.hcubature(integrand, zeros(FP, 2), FP[1, π / 2], rule.kwargs...)[1]
90+
= HCubature.hcubature(integrand, _zeros(FP, 2), (FP(1), FP(π / 2)), rule.kwargs...)[1]
9191

9292
# Apply a linear domain-correction factor 0.5 ↦ area(triangle)
9393
return 2 * Meshes.area(triangle) .*

src/utils.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ function _error_unsupported_combination(geometry, rule)
1414
throw(ArgumentError(msg))
1515
end
1616

17+
# Return an NTuple{N, T} of zeros; same interface as Base.zeros() but faster
18+
_zeros(T::DataType, N::Int64) = ntuple(_ -> zero(T), N)
19+
_zeros(N::Int) = _zeros(Float64, N)
20+
21+
# Return an NTuple{N, T} of ones; same interface as Base.ones() but faster
22+
_ones(T::DataType, N::Int64) = ntuple(_ -> one(T), N)
23+
_ones(N::Int) = _ones(Float64, N)
24+
1725
################################################################################
1826
# DifferentiationMethod
1927
################################################################################

test/utils.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
@testitem "Utilities" setup=[Setup] begin
22
using LinearAlgebra: norm
3+
using MeshIntegrals: _units, _zeros, _ones
34

45
# _KVector
56
v = Meshes.Vec(3, 4)
67
@test norm(MeshIntegrals._KVector(v)) 5.0u"m"
78

89
# _units
910
p = Point(1.0u"cm", 2.0u"mm", 3.0u"m")
10-
@test MeshIntegrals._units(p) == u"m"
11+
@test _units(p) == u"m"
12+
13+
# _zeros
14+
@test _zeros(2) == (0.0, 0.0)
15+
@test _zeros(Float32, 2) == (0.0f0, 0.0f0)
16+
17+
# _ones
18+
@test _ones(2) == (1.0, 1.0)
19+
@test _ones(Float32, 2) == (1.0f0, 1.0f0)
1120
end
1221

1322
@testitem "DifferentiationMethod" setup=[Setup] begin

0 commit comments

Comments
 (0)