0
$\begingroup$

Good day,

I have the output A and I want to do some operation to A:

A = {0, 1, 2}
B = -Total[(#*Log2[#]& /@ A]

when i run, has error :

Indeterminate expression 0 (-\[Infinity]) encountered. >>

How can I edit the coding, if I want to use

If output A=0, the value of #*Log2[#]=0

Please help me to fix this problem.

Thank you very much

$\endgroup$

1 Answer 1

1
$\begingroup$

Do a check:

If[# == 0, 0, # Log2[#]] & /@ A

Or, you don't have to know the limiting value(s) beforehand:

Limit[x Log2[x], x -> #] & /@ A

Update: holding evaluation

hold = Hold[# Log2[#]] & /@ A

{Hold[0 Log2[0]], Hold[1 Log2[1]], Hold[2 Log2[2]]}

ReleaseHold[hold /. HoldPattern[0 Log2[0]] -> 0]

{0, 0, 2}

$\endgroup$
6
  • $\begingroup$ I want to substitute the value of 0Log2(0) as 0. Thank you, I do it first. $\endgroup$
    – munirah
    Commented Aug 4, 2016 at 5:59
  • 1
    $\begingroup$ @munirah In that case you shouldn't let Log2[0] evaluate. $\endgroup$
    – BoLe
    Commented Aug 4, 2016 at 6:05
  • 1
    $\begingroup$ @munirah, that's what the If[] is for; an alternative would be to use Piecewise[]. $\endgroup$ Commented Aug 4, 2016 at 6:08
  • 1
    $\begingroup$ @munirah Version 10 features inactivated expressions which might be interesting to you. $\endgroup$
    – BoLe
    Commented Aug 4, 2016 at 6:20
  • $\begingroup$ thank you for helping me, I understand it now $\endgroup$
    – munirah
    Commented Aug 4, 2016 at 6:20

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.