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
$\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}

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

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.