# Differentiation method for element-wise "abs"-function applied on "Operation"-types in ModelingToolKit.jl?

**URL:** <https://discourse.julialang.org/t/differentiation-method-for-element-wise-abs-function-applied-on-operation-types-in-modelingtoolkit-jl/45867>\
**Category:** General Usage\
**Created:** [August 31, 2020, 9:53pm UTC](https://discourse.julialang.org/t/differentiation-method-for-element-wise-abs-function-applied-on-operation-types-in-modelingtoolkit-jl/45867 "2020-08-31T21:53:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luis\_munoz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luis_munoz/32/8146_2.png) [@luis\_munoz](https://discourse.julialang.org/u/luis_munoz)\
**Post date:** [August 31, 2020, 9:53pm UTC](https://discourse.julialang.org/t/differentiation-method-for-element-wise-abs-function-applied-on-operation-types-in-modelingtoolkit-jl/45867/1 "2020-08-31T21:53:05Z")

</div>

Hi,

I am trying to apply `ModelingToolkit.sparsejacobian()` to my differential function including element-wise calls of the absolute function `abs()`. How can I appropriately define the derivative of `abs()` to be used with the `Operation` type in ModelingToolKit.jl? Simply using the standard method provided by DiffRules.jl leads to an error due to the `signbit()` function stating

```julia
ERROR: TypeError: non-boolean (Operation) used in boolean context
Stacktrace:
 [1] signbit at ./REPL[49959]:3 [inlined]
 [2] _abs_deriv at .julia/packages/DiffRules/5QwtC/src/rules.jl:72 [inlined]

```

Any help is much appreciated!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [September 1, 2020, 2:49pm UTC](https://discourse.julialang.org/t/differentiation-method-for-element-wise-abs-function-applied-on-operation-types-in-modelingtoolkit-jl/45867/2 "2020-09-01T14:49:38Z")

</div>

@shashi is there a reason this derivative isn’t already defined?

---

<div class="post-metadata">

**Author:** ![luis\_munoz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luis_munoz/32/8146_2.png) [@luis\_munoz](https://discourse.julialang.org/u/luis_munoz)\
**Post date:** [September 1, 2020, 4:56pm UTC](https://discourse.julialang.org/t/differentiation-method-for-element-wise-abs-function-applied-on-operation-types-in-modelingtoolkit-jl/45867/3 "2020-09-01T16:56:43Z")

</div>

Hi @ChrisRackauckas,

Thank you for the (always prompt) reply. Just to clarify, this minimal example works (also for less trivial cases as this one):

```julia
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));

```

while the element-wise approach (similar to my case) does not:

```julia
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));

```

and leads to the error message `ERROR: MethodError: no method matching signbit(::Operation)` where the `signbit` function is used for the differentiation in `DiffRules.jl`.

---

<div class="post-metadata">

**Author:** ![shashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shashi/32/1824_2.png) [@shashi](https://discourse.julialang.org/u/shashi)\
**Post date:** [September 1, 2020, 7:28pm UTC](https://discourse.julialang.org/t/differentiation-method-for-element-wise-abs-function-applied-on-operation-types-in-modelingtoolkit-jl/45867/4 "2020-09-01T19:28:00Z")

</div>

This just means ModelingToolkit needs to register `signbit`. `signbit` is the derivative.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [September 2, 2020, 2:22am UTC](https://discourse.julialang.org/t/differentiation-method-for-element-wise-abs-function-applied-on-operation-types-in-modelingtoolkit-jl/45867/5 "2020-09-02T02:22:18Z")

</div>

Let’s continue this here: [https://github.com/SciML/ModelingToolkit.jl/pull/568](https://github.com/SciML/ModelingToolkit.jl/pull/568)
