With reference to this post, can one define 0 Log[0] = 0
in Mathemtica?
2 Answers
You can use Inactive
here.
expr = (1 - x) Log[1 - x];
iexpr = expr /. Log -> Inactive[Log];
Now, insert a number, activate after numeric evaluation of everything but Log.
iexpr /. x -> -2. // Activate
(* 3.29584 *)
Because you've controlled the order of evaluation, the troublesome case becomes zero.
iexpr /. x -> 1. // Activate
(* 0. *)
Activate
not needed in this special case, but harmless.
This works for exact numbers and machine numbers. For controlled precision, you'll need to do more.
-
2$\begingroup$ This is similar to the approach in my comment, and probably should have the same priviso, "IF the coefficient of Log[x] always vanishes with an order higher than some x^r, r > 0, as x -> 0,..." $\endgroup$ Commented Mar 8, 2019 at 16:09
You can define
s[0|0.] = 0;
s[x_] = -x*Log[x];
SetAttributes[s, Listable]
and then use the function s
instead of x Log[x]
. If you also have values of $x<0$ then other definitions should be added.
-
$\begingroup$ Thanks, @Roman. How can one put further restrictions like s[x]=0 for say x<10^(-15). $\endgroup$– H. KenanCommented Mar 8, 2019 at 16:31
-
$\begingroup$ @TobiasFritzn take a look at the
Picewise
function. $\endgroup$– gothicVICommented Mar 9, 2019 at 0:09 -
$\begingroup$ I don't think you need to make a separate definition for, say, x<10^(-15), as the function x*log(x) is well-behaved around x=0. There's only really a need for a special definition at the very point x=0. $\endgroup$– RomanCommented Mar 9, 2019 at 1:23
Function[r, Piecewise[{{Log[r^r], 0 < r < 1}, {r Log[r], 1 < r}}, 0], Listable]
. $\endgroup$Log[x]
always vanishes with an order higher than somex^r
,r > 0
, asx -> 0
, then the following should work:log[x_?NumericQ /; x != 0] := Log[x]; expr /. Log -> log /. Thread[{x, y, z} -> 0.] /. log -> Log
. Some tweaking may be needed depending on the application. $\endgroup$xlogx[x]
and replacingexpr /. Log -> (xlogx[#]/# &) // Simplify
$\endgroup$