Skip to content

Handle conditionals and abs #568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 5, 2020
Merged

Handle conditionals and abs #568

merged 4 commits into from
Sep 5, 2020

Conversation

ChrisRackauckas
Copy link
Member

It's time we finally handle #531, https://github.com/SciML/ModelingToolkit.jl/issues/532, and https://discourse.julialang.org/t/differentiation-method-for-element-wise-abs-function-applied-on-operation-types-in-modelingtoolkit-jl/45867/4 . This PR creates a ModelingToolkit.ifelse with derivative fixes for abs to make it so standard conditional code can work. While tracing cannot correctly make these operations, this would at least allow directly written symbolic code to handle conditions, so we're at least as good (or bad) as TensorFlow.

It's time we finally handle #531, https://github.com/SciML/ModelingToolkit.jl/issues/532, and https://discourse.julialang.org/t/differentiation-method-for-element-wise-abs-function-applied-on-operation-types-in-modelingtoolkit-jl/45867/4 . This PR creates a `ModelingToolkit.ifelse` with derivative fixes for `abs` to make it so standard conditional code can work. While tracing cannot correctly make these operations, this would at least allow directly written symbolic code to handle conditions, so we're at least as good (or bad) as TensorFlow.
@ChrisRackauckas
Copy link
Member Author

ChrisRackauckas commented Sep 2, 2020

@chriselrod should we make a IfElse.jl package so that way we use the same one as LoopVectorization.jl? @c42f should we inject this as Base.ifelse?

@ChrisRackauckas
Copy link
Member Author

using ModelingToolkit
function func!(du,u)
    du = abs.(u)
end
@variables du[1:2] u[1:2]
func!(du,u)
sjac= ModelingToolkit.sparsejacobian(vec(du),vec(u));

function func!(du,u)
    for j = 1:2
        du[j] = abs(u[j])
    end
 end
@variables du[1:2] u[1:2]
func!(du,u)
sjac= ModelingToolkit.sparsejacobian(vec(du),vec(u));

works with this PR.

Copy link
Member

@YingboMa YingboMa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I agree that it's better to make a new "IfElse.jl" package so that we can overload the same function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants