2
$\begingroup$

I have an expression that has the form

f1[a, b, c] Log[ f2[a, b, c] ]

Now if f1[a,b,c] and f2[a,b,c] evaluate to 0, then we have 0 Log[0], which I want to set to 0. I can achieve this by creating the following function:

g[0, 0 | 0.] = 0;
g[x_, y_] := x Log[y];
SetAttributes[g, Listable]

which will evaluate what I want, but set the value to 0 if both f1[a,b,c] and f2[a,b,c] are 0

However, sometimes, f1[a,b,c] and f2[a,b,c] are:

  1. approximately zero (in the order of 10^-8, which can be handled via a Chop command), and
  2. negative (~ - 10^-8).

I am not sure how to handle negative occurrences. I.e. in the instance we have 10^-5 Log[- 10^-8], this should be set to zero. I imagine this can be achieved by an edit to the function g, but how?

$\endgroup$
0

3 Answers 3

2
$\begingroup$

Maybe

ClearAll @ g
SetAttributes[g, Listable]
Off[Infinity::indet]

g[0, 0 | 0.] := 0
g[x_?NumericQ, y_?NumericQ] := Re[x Log @ y]
g[__] := 0

g[10^-5, -10.^-8]

-0.000184207

g[2, 40.]

7.37776

g[0, 0.]

0

g[0 Log @ 0]

0

g[0, Log @ 0]

0

$\endgroup$
1
$\begingroup$

I would define my own Log like e.g.:

ClearAll[myLog]
SetAttributes[myLog, Listable]
myLog[x_] = If[x <= 0, 0, Log[x]];

Now zero arguments are defined:

0 myLog[0]

0

Neither do 10^-8 and -10^-8 pose any problems:

10^-8 myLog[10^-8] 
10^-8 myLog[-10^-8]

![enter image description here

$\endgroup$
2
  • $\begingroup$ the function I am trying to evaluate should be real. The second use example you show has an imaginary output, which is precisely the problem. The outcome needs to therefore be set to zero, which I would like to achieve by changing the definition of myLog $\endgroup$
    – Sid
    Nov 8, 2023 at 13:42
  • $\begingroup$ I changed the definition to ensure that a negative arg of myLog results in zero. $\endgroup$ Nov 8, 2023 at 15:07
1
$\begingroup$

Rather than define your function directly, you could define it based on the result of the log expression.

myLogTerm[coeff_, arg_, chop_] :=
  With[
    {term = coeff Log[arg]},
    If[Norm[term] > chop, term, 0, 0]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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