Skip to content

Commit de47945

Browse files
committed
Inplace: Val{true} -> Val(true)
1 parent 39da4b5 commit de47945

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

src/gradients.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ function GradientCache(
1010
x,
1111
fdtype = Val(:central),
1212
returntype = eltype(df),
13-
inplace = Val{true})
13+
inplace = Val(true))
1414

1515
fdtype isa Type && (fdtype = fdtype())
16+
inplace isa Type && (inplace = inplace())
1617
if typeof(x)<:AbstractArray # the vector->scalar case
1718
if fdtype!=Val(:complex) # complex-mode FD only needs one cache, for x+eps*im
1819
if typeof(x)<:StridedVector
@@ -58,18 +59,19 @@ function finite_difference_gradient(
5859
x,
5960
fdtype = Val(:central),
6061
returntype = eltype(x),
61-
inplace = Val{true},
62+
inplace = Val(true),
6263
fx = nothing,
6364
c1 = nothing,
6465
c2 = nothing;
6566
relstep=default_relstep(fdtype, eltype(x)),
6667
absstep=relstep,
6768
dir=true)
6869

70+
inplace isa Type && (inplace = inplace())
6971
if typeof(x) <: AbstractArray
7072
df = zero(returntype) .* x
7173
else
72-
if inplace == Val{true}
74+
if inplace == Val(true)
7375
if typeof(fx)==Nothing && typeof(c1)==Nothing && typeof(c2)==Nothing
7476
error("In the scalar->vector in-place map case, at least one of fx, c1 or c2 must be provided, otherwise we cannot infer the return size.")
7577
else
@@ -92,7 +94,7 @@ function finite_difference_gradient!(
9294
x,
9395
fdtype=Val(:central),
9496
returntype=eltype(df),
95-
inplace=Val{true},
97+
inplace=Val(true),
9698
fx=nothing,
9799
c1=nothing,
98100
c2=nothing;
@@ -321,21 +323,21 @@ function finite_difference_gradient!(
321323
# c1 denotes fx1, c2 is fx2, sizes guaranteed by the cache constructor
322324
fx, c1, c2 = cache.fx, cache.c1, cache.c2
323325

324-
if inplace == Val{true}
326+
if inplace == Val(true)
325327
_c1, _c2 = c1, c2
326328
end
327329

328330
if fdtype == Val(:forward)
329331
epsilon = compute_epsilon(Val(:forward), x, relstep, absstep, dir)
330-
if inplace == Val{true}
332+
if inplace == Val(true)
331333
f(c1, x+epsilon)
332334
else
333335
_c1 = f(x+epsilon)
334336
end
335337
if typeof(fx) != Nothing
336338
@. df = (_c1 - fx) / epsilon
337339
else
338-
if inplace == Val{true}
340+
if inplace == Val(true)
339341
f(c2, x)
340342
else
341343
_c2 = f(x)
@@ -344,7 +346,7 @@ function finite_difference_gradient!(
344346
end
345347
elseif fdtype == Val(:central)
346348
epsilon = compute_epsilon(Val(:central), x, relstep, absstep, dir)
347-
if inplace == Val{true}
349+
if inplace == Val(true)
348350
f(c1, x+epsilon)
349351
f(c2, x-epsilon)
350352
else
@@ -354,7 +356,7 @@ function finite_difference_gradient!(
354356
@. df = (_c1 - _c2) / (2*epsilon)
355357
elseif fdtype == Val(:complex) && returntype <: Real
356358
epsilon_complex = eps(real(eltype(x)))
357-
if inplace == Val{true}
359+
if inplace == Val(true)
358360
f(c1, x+im*epsilon_complex)
359361
else
360362
_c1 = f(x+im*epsilon_complex)

src/hessians.jl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@ end
77

88
function HessianCache(xpp,xpm,xmp,xmm,
99
fdtype=Val(:hcentral),
10-
inplace = x isa StaticArray ? Val{false} : Val{true})
10+
inplace = x isa StaticArray ? Val(false) : Val(true))
1111
fdtype isa Type && (fdtype = fdtype())
12+
inplace isa Type && (inplace = inplace())
1213
HessianCache{typeof(xpp),fdtype,inplace}(xpp,xpm,xmp,xmm)
1314
end
1415

1516
function HessianCache(x, fdtype=Val(:hcentral),
16-
inplace = x isa StaticArray ? Val{false} : Val{true})
17+
inplace = x isa StaticArray ? Val(false) : Val(true))
1718
cx = copy(x)
1819
fdtype isa Type && (fdtype = fdtype())
20+
inplace isa Type && (inplace = inplace())
1921
HessianCache{typeof(cx),fdtype,inplace}(cx, copy(x), copy(x), copy(x))
2022
end
2123

2224
function finite_difference_hessian(f, x,
2325
fdtype = Val(:hcentral),
24-
inplace = x isa StaticArray ? Val{false} : Val{true};
26+
inplace = x isa StaticArray ? Val(false) : Val(true);
2527
relstep = default_relstep(fdtype, eltype(x)),
2628
absstep = relstep)
2729

@@ -43,7 +45,7 @@ end
4345
function finite_difference_hessian!(H,f,
4446
x,
4547
fdtype = Val(:hcentral),
46-
inplace = x isa StaticArray ? Val{false} : Val{true};
48+
inplace = x isa StaticArray ? Val(false) : Val(true);
4749
relstep=default_relstep(fdtype, eltype(x)),
4850
absstep=relstep)
4951

@@ -61,15 +63,15 @@ function finite_difference_hessian!(H,f,x,
6163
xpp, xpm, xmp, xmm = cache.xpp, cache.xpm, cache.xmp, cache.xmm
6264
fx = f(x)
6365

64-
if inplace === Val{true}
66+
if inplace === Val(true)
6567
_xpp, _xpm, _xmp, _xmm = xpp, xpm, xmp, xmm
6668
end
6769

6870
for i = 1:n
6971
xi = ArrayInterface.allowed_getindex(x,i)
7072
epsilon = compute_epsilon(Val(:hcentral), xi, relstep, absstep)
7173

72-
if inplace === Val{true}
74+
if inplace === Val(true)
7375
ArrayInterface.allowed_setindex!(xpp,xi + epsilon,i)
7476
ArrayInterface.allowed_setindex!(xmm,xi - epsilon,i)
7577
else
@@ -82,7 +84,7 @@ function finite_difference_hessian!(H,f,x,
8284
xp = xi + epsiloni
8385
xm = xi - epsiloni
8486

85-
if inplace === Val{true}
87+
if inplace === Val(true)
8688
ArrayInterface.allowed_setindex!(xpp,xp,i)
8789
ArrayInterface.allowed_setindex!(xpm,xp,i)
8890
ArrayInterface.allowed_setindex!(xmp,xm,i)
@@ -100,7 +102,7 @@ function finite_difference_hessian!(H,f,x,
100102
xp = xj + epsilonj
101103
xm = xj - epsilonj
102104

103-
if inplace === Val{true}
105+
if inplace === Val(true)
104106
ArrayInterface.allowed_setindex!(xpp,xp,j)
105107
ArrayInterface.allowed_setindex!(xpm,xm,j)
106108
ArrayInterface.allowed_setindex!(xmp,xp,j)
@@ -114,7 +116,7 @@ function finite_difference_hessian!(H,f,x,
114116

115117
ArrayInterface.allowed_setindex!(H,(f(_xpp) - f(_xpm) - f(_xmp) + f(_xmm))/(4*epsiloni*epsilonj),i,j)
116118

117-
if inplace === Val{true}
119+
if inplace === Val(true)
118120
ArrayInterface.allowed_setindex!(xpp,xj,j)
119121
ArrayInterface.allowed_setindex!(xpm,xj,j)
120122
ArrayInterface.allowed_setindex!(xmp,xj,j)
@@ -127,7 +129,7 @@ function finite_difference_hessian!(H,f,x,
127129
end
128130
end
129131

130-
if inplace === Val{true}
132+
if inplace === Val(true)
131133
ArrayInterface.allowed_setindex!(xpp,xi,i)
132134
ArrayInterface.allowed_setindex!(xpm,xi,i)
133135
ArrayInterface.allowed_setindex!(xmp,xi,i)

src/jacobians.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ function JacobianCache(
1010
x,
1111
fdtype :: Union{Val{T1},Type{T1}} = Val(:forward),
1212
returntype :: Type{T2} = eltype(x);
13-
inplace :: Type{Val{T3}} = Val{true},
13+
inplace :: Union{Val{T3},Type{T3}} = Val(true),
1414
colorvec = 1:length(x),
1515
sparsity = nothing) where {T1,T2,T3}
1616

1717
fdtype isa Type && (fdtype = fdtype())
18+
inplace isa Type && (inplace = inplace())
1819
if eltype(x) <: Real && fdtype==Val(:complex)
1920
x1 = false .* im .* x
2021
_fx = false .* im .* x
@@ -37,11 +38,12 @@ function JacobianCache(
3738
fx,
3839
fdtype :: Union{Val{T1},Type{T1}} = Val(:forward),
3940
returntype :: Type{T2} = eltype(x);
40-
inplace :: Type{Val{T3}} = Val{true},
41+
inplace :: Union{Val{T3},Type{T3}} = Val(true),
4142
colorvec = 1:length(x),
4243
sparsity = nothing) where {T1,T2,T3}
4344

4445
fdtype isa Type && (fdtype = fdtype())
46+
inplace isa Type && (inplace = inplace())
4547
if eltype(x) <: Real && fdtype==Val(:complex)
4648
x1 = false .* im .* x
4749
else
@@ -69,11 +71,12 @@ function JacobianCache(
6971
fx1,
7072
fdtype :: Union{Val{T1},Type{T1}} = Val(:forward),
7173
returntype :: Type{T2} = eltype(fx);
72-
inplace :: Type{Val{T3}} = Val{true},
74+
inplace :: Union{Val{T3},Type{T3}} = Val(true),
7375
colorvec = 1:length(x1),
7476
sparsity = nothing) where {T1,T2,T3}
7577

7678
fdtype isa Type && (fdtype = fdtype())
79+
inplace isa Type && (inplace = inplace())
7780
if fdtype==Val(:complex)
7881
!(returntype<:Real) && fdtype_error(returntype)
7982

0 commit comments

Comments
 (0)