@@ -13,143 +13,141 @@ function Integrals.__solvebp_call(prob::IntegralProblem,
1313 mid = (lb + ub) / 2
1414
1515 # we get to pick fdim or not based on the IntegralFunction and its output dimensions
16- y = if prob. f isa BatchIntegralFunction
17- isinplace (prob. f) ? prob. f. integrand_prototype :
18- mid isa Number ? prob. f (eltype (mid)[], p) :
19- prob. f (Matrix {eltype(mid)} (undef, length (mid), 0 ), p)
20- else
21- # we evaluate the oop function to decide whether the output should be vectorized
22- isinplace (prob. f) ? prob. f. integrand_prototype : prob. f (mid, p)
23- end
16+ f = prob. f
17+ prototype = Integrals. get_prototype (prob)
2418
25- @assert eltype (y )<: Real " Cubature.jl is only compatible with real-valued integrands"
19+ @assert eltype (prototype )<: Real " Cubature.jl is only compatible with real-valued integrands"
2620
27- if prob . f isa BatchIntegralFunction
28- if y isa AbstractVector # this branch could be omitted since the following one should work similarly
29- if isinplace (prob )
21+ if f isa BatchIntegralFunction
22+ if prototype isa AbstractVector # this branch could be omitted since the following one should work similarly
23+ if isinplace (f )
3024 # dx is a Vector, but we provide the integrand a vector of the same type as
3125 # y, which needs to be resized since the number of batch points changes.
32- dy = similar (y)
33- f = (x, dx) -> begin
34- resize! (dy, length (dx))
35- prob. f (dy, x, p)
36- dx .= dy
26+ _f = let y = similar (prototype)
27+ (u, v) -> begin
28+ resize! (y, length (v))
29+ f (y, u, p)
30+ v .= y
31+ end
3732 end
3833 else
39- f = (x, dx ) -> (dx .= prob . f (x , p))
34+ _f = (u, v ) -> (v .= f (u , p))
4035 end
4136 if mid isa Number
4237 if alg isa CubatureJLh
43- val, err = Cubature. hquadrature_v (f , lb, ub;
38+ val, err = Cubature. hquadrature_v (_f , lb, ub;
4439 reltol = reltol, abstol = abstol,
4540 maxevals = maxiters)
4641 else
47- val, err = Cubature. pquadrature_v (f , lb, ub;
42+ val, err = Cubature. pquadrature_v (_f , lb, ub;
4843 reltol = reltol, abstol = abstol,
4944 maxevals = maxiters)
5045 end
5146 else
5247 if alg isa CubatureJLh
53- val, err = Cubature. hcubature_v (f , lb, ub;
48+ val, err = Cubature. hcubature_v (_f , lb, ub;
5449 reltol = reltol, abstol = abstol,
5550 maxevals = maxiters)
5651 else
57- val, err = Cubature. pcubature_v (f , lb, ub;
52+ val, err = Cubature. pcubature_v (_f , lb, ub;
5853 reltol = reltol, abstol = abstol,
5954 maxevals = maxiters)
6055 end
6156 end
62- elseif y isa AbstractArray
63- bfsize = size (y )[begin : (end - 1 )]
64- bfdim = prod (bfsize )
65- if isinplace (prob )
57+ elseif prototype isa AbstractArray
58+ fsize = size (prototype )[begin : (end - 1 )]
59+ fdim = prod (fsize )
60+ if isinplace (f )
6661 # dx is a Matrix, but to provide a buffer of the same type as y, we make
6762 # would like to make views of a larger buffer, but CubatureJL doesn't set
6863 # a hard limit for max_batch, so we allocate a new buffer with the needed size
69- f = (x, dx) -> begin
70- dy = similar (y, bfsize... , size (dx, 2 ))
71- prob. f (dy, x, p)
72- dx .= reshape (dy, bfdim, size (dx, 2 ))
64+ _f = let fsize = fsize
65+ (u, v) -> begin
66+ y = similar (prototype, fsize... , size (v, 2 ))
67+ f (y, u, p)
68+ v .= reshape (y, fdim, size (v, 2 ))
69+ end
7370 end
7471 else
75- f = (x, dx ) -> (dx .= reshape (prob . f (x , p), bfdim , size (dx , 2 )))
72+ _f = (u, v ) -> (v .= reshape (f (u , p), fdim , size (v , 2 )))
7673 end
7774 if mid isa Number
7875 if alg isa CubatureJLh
79- val_, err = Cubature. hquadrature_v (bfdim, f , lb, ub;
76+ val_, err = Cubature. hquadrature_v (fdim, _f , lb, ub;
8077 reltol = reltol, abstol = abstol,
8178 maxevals = maxiters, error_norm = alg. error_norm)
8279 else
83- val_, err = Cubature. pquadrature_v (bfdim, f , lb, ub;
80+ val_, err = Cubature. pquadrature_v (fdim, _f , lb, ub;
8481 reltol = reltol, abstol = abstol,
8582 maxevals = maxiters, error_norm = alg. error_norm)
8683 end
8784 else
8885 if alg isa CubatureJLh
89- val_, err = Cubature. hcubature_v (bfdim, f , lb, ub;
86+ val_, err = Cubature. hcubature_v (fdim, _f , lb, ub;
9087 reltol = reltol, abstol = abstol,
9188 maxevals = maxiters, error_norm = alg. error_norm)
9289 else
93- val_, err = Cubature. pcubature_v (bfdim, f , lb, ub;
90+ val_, err = Cubature. pcubature_v (fdim, _f , lb, ub;
9491 reltol = reltol, abstol = abstol,
9592 maxevals = maxiters, error_norm = alg. error_norm)
9693 end
9794 end
98- val = reshape (val_, bfsize ... )
95+ val = reshape (val_, fsize ... )
9996 else
10097 error (" BatchIntegralFunction integrands must be arrays for Cubature.jl" )
10198 end
10299 else
103- if y isa Real
100+ if prototype isa Real
104101 # no inplace in this case, since the integrand_prototype would be mutable
105- f = x -> prob . f (x , p)
102+ _f = u -> f (u , p)
106103 if lb isa Number
107104 if alg isa CubatureJLh
108- val, err = Cubature. hquadrature (f , lb, ub;
105+ val, err = Cubature. hquadrature (_f , lb, ub;
109106 reltol = reltol, abstol = abstol,
110107 maxevals = maxiters)
111108 else
112- val, err = Cubature. pquadrature (f , lb, ub;
109+ val, err = Cubature. pquadrature (_f , lb, ub;
113110 reltol = reltol, abstol = abstol,
114111 maxevals = maxiters)
115112 end
116113 else
117114 if alg isa CubatureJLh
118- val, err = Cubature. hcubature (f , lb, ub;
115+ val, err = Cubature. hcubature (_f , lb, ub;
119116 reltol = reltol, abstol = abstol,
120117 maxevals = maxiters)
121118 else
122- val, err = Cubature. pcubature (f , lb, ub;
119+ val, err = Cubature. pcubature (_f , lb, ub;
123120 reltol = reltol, abstol = abstol,
124121 maxevals = maxiters)
125122 end
126123 end
127- elseif y isa AbstractArray
128- fsize = size (y )
129- fdim = length (y )
124+ elseif prototype isa AbstractArray
125+ fsize = size (prototype )
126+ fdim = length (prototype )
130127 if isinplace (prob)
131- dy = similar (y)
132- f = (x, v) -> (prob. f (dy, x, p); v .= vec (dy))
128+ _f = let y = similar (prototype)
129+ (u, v) -> (f (y, u, p); v .= vec (y))
130+ end
133131 else
134- f = (x , v) -> (v .= vec (prob . f (x , p)))
132+ _f = (u , v) -> (v .= vec (f (u , p)))
135133 end
136134 if mid isa Number
137135 if alg isa CubatureJLh
138- val_, err = Cubature. hquadrature (fdim, f , lb, ub;
136+ val_, err = Cubature. hquadrature (fdim, _f , lb, ub;
139137 reltol = reltol, abstol = abstol,
140138 maxevals = maxiters, error_norm = alg. error_norm)
141139 else
142- val_, err = Cubature. pquadrature (fdim, f , lb, ub;
140+ val_, err = Cubature. pquadrature (fdim, _f , lb, ub;
143141 reltol = reltol, abstol = abstol,
144142 maxevals = maxiters, error_norm = alg. error_norm)
145143 end
146144 else
147145 if alg isa CubatureJLh
148- val_, err = Cubature. hcubature (fdim, f , lb, ub;
146+ val_, err = Cubature. hcubature (fdim, _f , lb, ub;
149147 reltol = reltol, abstol = abstol,
150148 maxevals = maxiters, error_norm = alg. error_norm)
151149 else
152- val_, err = Cubature. pcubature (fdim, f , lb, ub;
150+ val_, err = Cubature. pcubature (fdim, _f , lb, ub;
153151 reltol = reltol, abstol = abstol,
154152 maxevals = maxiters, error_norm = alg. error_norm)
155153 end
0 commit comments