JuliaMath / Calculus.jl

Calculus functions in Julia
Other
276 stars 78 forks source link

Remove call to `eval` in `simplify` #111

Open sglyon opened 7 years ago

sglyon commented 7 years ago

I have some code where I want to use Calculus.jl to differentiate symbolic expressions inside a @generated function. This currently does not work because after differentiating Calculus.jl calls simplify, which in turn often ends up calling eval. As of right now @generated functions are not allowed to call eval.

I'm opening this issue to see if those who know the internals here better think it would be possible/feasible to remove the call to eval inside simplify?

ref: https://github.com/EconForge/Dolang/pull/31#issuecomment-307791343

JanisErdmanis commented 6 years ago

I had exactly the same issue today. Looking in the symbolic.jl I saw that it was only used for doing arithmetics. Thus replaced eval with a following code

    if all(isnumber, ex.args[2:end]) && length(ex.args) > 1
        ### Doing numerical stuff!!!
        ################ A Patch Here #################
        op = ex.args[1]
        nums = ex.args[2:end]
        if op==:+
            return +(nums...)
        elseif op==:-
            return -(nums...)
        elseif op==:/
            return /(nums...)
        elseif op==:*
            return *(nums...)
        else
            error("Operation $op not implemented")
        end

        ############# Replaces ######################
        #return eval(current_module(), ex)
        #############################################
    end

which seems to work fine ;)