Skip to content

Commit 15e1d37

Browse files
authored
Merge pull request #39 from tkelman/nolazy
Refactor eval_univariate to avoid depending on Lazy.jl
2 parents 0f5bde7 + 95b349c commit 15e1d37

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

REQUIRE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
julia 0.5
22
Calculus
3-
Lazy
43
DataStructures
54
MathProgBase
65
NaNMath 0.2.1

src/ReverseDiffSparse.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module ReverseDiffSparse
33
using Base.Meta
44
using ForwardDiff
55
import Calculus
6-
import Lazy
76
import MathProgBase
87
# Override basic math functions to return NaN instead of throwing errors.
98
# This is what NLP solvers expect, and

src/forward.jl

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,21 +336,38 @@ end
336336
export forward_eval_ϵ
337337

338338

339-
switchblock = Expr(:block)
339+
exprs = Expr[]
340340
for i = 1:length(univariate_operators)
341341
op = univariate_operators[i]
342342
deriv_expr = univariate_operator_deriv[i]
343-
ex = :(return $op(x), $deriv_expr::T)
344-
push!(switchblock.args,i,ex)
343+
ex = :(return $op(x), $deriv_expr::T)
344+
push!(exprs, ex)
345345
end
346-
switchexpr = Expr(:macrocall, Expr(:.,:Lazy,quot(Symbol("@switch"))), :operator_id,switchblock)
346+
347+
function binaryswitch(ids, exprs)
348+
if length(exprs) <= 3
349+
out = Expr(:if, Expr(:call, :(==), :operator_id, ids[1]), exprs[1])
350+
if length(exprs) > 1
351+
push!(out.args, binaryswitch(ids[2:end], exprs[2:end]))
352+
end
353+
return out
354+
else
355+
mid = length(exprs) >>> 1
356+
return Expr(:if, Expr(:call, :(<=), :operator_id, ids[mid]),
357+
binaryswitch(ids[1:mid], exprs[1:mid]),
358+
binaryswitch(ids[mid+1:end], exprs[mid+1:end]))
359+
end
360+
end
361+
switchexpr = binaryswitch(1:length(exprs), exprs)
347362

348363
@eval @inline function eval_univariate{T}(operator_id,x::T)
349364
$switchexpr
365+
error("No match for operator_id")
350366
end
351367

352368
# TODO: optimize sin/cos/exp
353-
switchblock = Expr(:block)
369+
ids = Int[]
370+
exprs = Expr[]
354371
for i = 1:length(univariate_operators)
355372
op = univariate_operators[i]
356373
if op == :asec || op == :acsc || op == :asecd || op == :acscd || op == :acsch || op == :trigamma
@@ -366,11 +383,13 @@ for i = 1:length(univariate_operators)
366383
else
367384
deriv_expr = Calculus.differentiate(univariate_operator_deriv[i],:x)
368385
end
369-
ex = :(return $deriv_expr::T)
370-
push!(switchblock.args,i,ex)
386+
ex = :(return $deriv_expr::T)
387+
push!(ids, i)
388+
push!(exprs, ex)
371389
end
372-
switchexpr = Expr(:macrocall, Expr(:.,:Lazy,quot(Symbol("@switch"))), :operator_id,switchblock)
390+
switchexpr = binaryswitch(ids, exprs)
373391

374392
@eval @inline function eval_univariate_2nd_deriv{T}(operator_id,x::T,fval::T)
375393
$switchexpr
394+
error("No match for operator_id")
376395
end

0 commit comments

Comments
 (0)