3434# Gauss-Legendre
3535# ###############################################################################
3636
37+ # Generalized method
38+ function _integral_1d (f, geometry, settings:: GaussLegendre )
39+ # Compute Gauss-Legendre nodes/weights for x in interval [-1,1]
40+ xs, ws = gausslegendre (settings. n)
41+
42+ # Change of variables: x [-1,1] ↦ t [0,1]
43+ t (x) = 0.5 x + 0.5
44+ point (x) = geometry (t (x))
45+
46+ function paramfactor (x)
47+ J = jacobian (geometry,[t (x)])
48+ return norm (J[1 ])
49+ end
50+
51+ # Integrate f along the geometry and apply a domain-correction factor for [-1,1] ↦ [0, 1]
52+ integrand ((w,x)) = w * f (point (x)) * paramfactor (x)
53+ return 0.5 * sum (integrand, zip (ws, xs))
54+ end
55+
3756"""
3857 integral(f, curve::Meshes.BezierCurve, ::GaussLegendre; alg=Meshes.Horner())
3958
@@ -73,15 +92,7 @@ function integral(
7392 # A Box{1,T} is definitionally embedded in 1D-space
7493 _validate_integrand (f,1 ,T)
7594
76- # Compute Gauss-Legendre nodes/weights for x in interval [-1,1]
77- xs, ws = gausslegendre (settings. n)
78-
79- # Change of variables: x [-1,1] ↦ t [0,1]
80- t (x) = 0.5 x + 0.5
81- point (x) = line (t (x))
82-
83- # Integrate f along the line and apply a domain-correction factor for [-1,1] ↦ [0, length]
84- return 0.5 * length (line) * sum (w .* f (point (x)) for (w,x) in zip (ws, xs))
95+ return _integral_1d (f, line, settings)
8596end
8697
8798function integral (
@@ -93,16 +104,7 @@ function integral(
93104 # A Circle is definitionally embedded in 3D-space
94105 _validate_integrand (f,3 ,T)
95106
96- # Compute Gauss-Legendre nodes/weights for x in interval [-1,1]
97- xs, ws = gausslegendre (settings. n)
98-
99- # Change of variables: x [-1,1] ↦ t [0,1]
100- t (x) = 0.5 x + 0.5
101- point (x) = circle (t (x))
102-
103- # Integrate f along the circle's rim and apply a domain-correction
104- # factor for [-1,1] ↦ [0, circumference]
105- return 0.5 * length (circle) * sum (w .* f (point (x)) for (w,x) in zip (ws, xs))
107+ return _integral_1d (f, circle, settings)
106108end
107109
108110function integral (
@@ -135,15 +137,7 @@ function integral(
135137 # Validate the provided integrand function
136138 _validate_integrand (f,Dim,T)
137139
138- # Compute Gauss-Legendre nodes/weights for x in interval [-1,1]
139- xs, ws = gausslegendre (settings. n)
140-
141- # Change of variables: x [-1,1] ↦ t [0,1]
142- t (x) = 0.5 x + 0.5
143- point (x) = segment (t (x))
144-
145- # Integrate f along the line and apply a domain-correction factor for [-1,1] ↦ [0, length]
146- return 0.5 * length (segment) * sum (w .* f (point (x)) for (w,x) in zip (ws, xs))
140+ return _integral_1d (f, segment, settings)
147141end
148142
149143function integral (
@@ -155,23 +149,25 @@ function integral(
155149 # A Sphere{2,T} is simply a circle in 2D-space
156150 _validate_integrand (f,2 ,T)
157151
158- # Compute Gauss-Legendre nodes/weights for x in interval [-1,1]
159- xs, ws = gausslegendre (settings. n)
160-
161- # Change of variables: x [-1,1] ↦ t [0,1]
162- t (x) = 0.5 x + 0.5
163- point (x) = circle (t (x))
164-
165- # Integrate f along the circle's rim and apply a domain-correction
166- # factor for [-1,1] ↦ [0, circumference]
167- return 0.5 * length (circle) * sum (w .* f (point (x)) for (w,x) in zip (ws, xs))
152+ return _integral_1d (f, circle, settings)
168153end
169154
170155
171156# ###############################################################################
172157# Gauss-Kronrod
173158# ###############################################################################
174159
160+ # Generalized method
161+ function _integral_1d (f, geometry, settings:: GaussKronrod )
162+ function paramfactor (t)
163+ J = jacobian (geometry,[t])
164+ return norm (J[1 ])
165+ end
166+
167+ integrand (t) = f (geometry (t)) * paramfactor (t)
168+ return QuadGK. quadgk (integrand, 0 , 1 ; settings. kwargs... )[1 ]
169+ end
170+
175171"""
176172 integral(f, curve::BezierCurve, ::GaussKronrod; alg=Horner(), kws...)
177173
@@ -205,9 +201,7 @@ function integral(
205201 # A Box is definitionally embedded in 1D-space
206202 _validate_integrand (f,1 ,T)
207203
208- len = length (line)
209- point (t) = line (t)
210- return QuadGK. quadgk (t -> len * f (point (t)), 0 , 1 ; settings. kwargs... )[1 ]
204+ return _integral_1d (f, line, settings)
211205end
212206
213207function integral (
@@ -219,9 +213,7 @@ function integral(
219213 # A Circle is definitionally embedded in 3D-space
220214 _validate_integrand (f,3 ,T)
221215
222- len = length (circle)
223- point (t) = circle (t)
224- return QuadGK. quadgk (t -> len * f (point (t)), 0 , 1 ; settings. kwargs... )[1 ]
216+ return _integral_1d (f, circle, settings)
225217end
226218
227219function integral (
@@ -248,9 +240,7 @@ function integral(
248240 # Validate the provided integrand function
249241 _validate_integrand (f,Dim,T)
250242
251- len = length (segment)
252- point (t) = segment (t)
253- return QuadGK. quadgk (t -> len * f (point (t)), 0 , 1 ; settings. kwargs... )[1 ]
243+ return _integral_1d (f, segment, settings)
254244end
255245
256246function integral (
@@ -262,16 +252,25 @@ function integral(
262252 # A Sphere{2,T} is simply a circle in 2D-space
263253 _validate_integrand (f,2 ,T)
264254
265- len = length (circle)
266- point (t) = circle (t)
267- return QuadGK. quadgk (t -> len * f (point (t)), 0 , 1 ; settings. kwargs... )[1 ]
255+ return _integral_1d (f, circle, settings)
268256end
269257
270258
271259# ###############################################################################
272260# HCubature
273261# ###############################################################################
274262
263+ # Generalized method
264+ function _integral_1d (f, geometry, settings:: HAdaptiveCubature )
265+ function paramfactor (t)
266+ J = jacobian (geometry,t)
267+ return norm (J[1 ])
268+ end
269+
270+ integrand (t) = f (geometry (t[1 ])) * paramfactor (t)
271+ return HCubature. hcubature (integrand, [0 ], [1 ]; settings. kwargs... )[1 ]
272+ end
273+
275274"""
276275 integral(f, curve::BezierCurve, ::HAdaptiveCubature; alg=Horner(), kws...)
277276
@@ -305,9 +304,7 @@ function integral(
305304 # A Box is definitionally embedded in 1D-space
306305 _validate_integrand (f,1 ,T)
307306
308- len = length (line)
309- point (t) = line (t)
310- return hcubature (t -> len * f (point (t[1 ])), [0 ], [1 ]; settings. kwargs... )[1 ]
307+ return _integral_1d (f, line, settings)
311308end
312309
313310function integral (
@@ -319,9 +316,7 @@ function integral(
319316 # A Circle is definitionally embedded in 3D-space
320317 _validate_integrand (f,3 ,T)
321318
322- len = length (circle)
323- point (t) = circle (t)
324- return hcubature (t -> len * f (point (t[1 ])), [0 ], [1 ]; settings. kwargs... )[1 ]
319+ return _integral_1d (f, circle, settings)
325320end
326321
327322function integral (
@@ -352,9 +347,7 @@ function integral(
352347 # Validate the provided integrand function
353348 _validate_integrand (f,Dim,T)
354349
355- len = length (segment)
356- point (t) = segment (t)
357- return hcubature (t -> len * f (point (t[1 ])), [0 ], [1 ]; settings. kwargs... )[1 ]
350+ return _integral_1d (f, segment, settings)
358351end
359352
360353function integral (
@@ -366,7 +359,5 @@ function integral(
366359 # A Sphere{2,T} is simply a circle in 2D-space
367360 _validate_integrand (f,2 ,T)
368361
369- len = length (circle)
370- point (t) = circle (t)
371- return hcubature (t -> len * f (point (t[1 ])), [0 ], [1 ]; settings. kwargs... )[1 ]
362+ return _integral_1d (f, circle, settings)
372363end
0 commit comments