<div>Hi,</div><div> </div><div>16.06.2020, 21:24, "Pierangelo Masarati" <pierangelo.masarati@polimi.it>:</div><div> </div><div>> One user recently tried something like this:</div><div>></div><div>>      var = v</div><div>></div><div>>      expr = v*abs(v)    # a turbulent viscous law</div><div>></div><div>> the result was</div><div>></div><div>>      dexpr/dvar = abs(v)+1/2*v*(v+conjugate(v))*abs(v)^(-1)</div><div>></div><div>> which fails when v = 0 because of the division by zero.  I tried with</div><div>> "realsymbol" (since our values can only be real); things, however,</div><div>> didn't change much:</div><div>></div><div>>      dexpr/dvar = v^2*abs(v)^(-1)+abs(v)</div><div>></div><div>> I realized that the issue is in the derivative of the abs() function;</div><div> </div><div>Exactly. abs(x) is not differentiable at x = 0. There's no way to "fix" that.</div><div> </div><div>> indeed, the proposed formula is rather general (despite failing when</div><div>> numerically evaluated with 0 as the argument).  When the argument is</div><div>> real, a more robust representation would be "d(abs(v))/dv = sign(v)".</div><div> </div><div>Sorry, but that's just plain wrong. Or rather it's correct for all v != 0.</div><div> </div><div>On the other hand v*abs(v) does have a derivative at v = 0, and it's  zero.</div><div> </div><div>GiNaC needs some help to compute that.</div><div> </div><div>You can replace v^2 -> abs(v)^2 in v^2*abs(v)^(-1), which has a limit when v -> 0</div><div>(and is much nicer for numeric computation)</div><div> </div><div>Hope this helps,</div><div>      Alexey</div>