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