1
$\begingroup$

With reference to this post, can one define 0 Log[0] = 0 in Mathemtica?

$\endgroup$
  • 2
    $\begingroup$ This might be an XY problem. What are you trying to do? $\endgroup$ – J. M. is away Mar 8 at 11:08
  • 1
    $\begingroup$ That being said, I would use Function[r, Piecewise[{{Log[r^r], 0 < r < 1}, {r Log[r], 1 < r}}, 0], Listable]. $\endgroup$ – J. M. is away Mar 8 at 11:08
  • $\begingroup$ The problem I'm facing is that in my code I have terms like x Log[x]; when x becomes very small, Mathematica shows indeterminate when it should show 0. $\endgroup$ – H. Kenan Mar 8 at 11:13
  • 1
    $\begingroup$ IF the coefficient of Log[x] always vanishes with an order higher than some x^r, r > 0, as x -> 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$ – Michael E2 Mar 8 at 13:39
  • 1
    $\begingroup$ A similar approach could be used with @J.M.'s function but defining xlogx[x] and replacing expr /. Log -> (xlogx[#]/# &) // Simplify $\endgroup$ – Michael E2 Mar 8 at 13:43
1
$\begingroup$

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.

$\endgroup$
  • 1
    $\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$ – Michael E2 Mar 8 at 16:09
0
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Thanks, @Roman. How can one put further restrictions like s[x]=0 for say x<10^(-15). $\endgroup$ – H. Kenan Mar 8 at 16:31
  • $\begingroup$ @TobiasFritzn take a look at the Picewise function. $\endgroup$ – gothicVI Mar 9 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$ – Roman Mar 9 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.