MATLAB Answers

define multiplication by zero for Inf

78 views (last 30 days)
Matthew
Matthew on 11 Sep 2014
Commented: Daniel Shub on 14 Sep 2014
It is often a mathematical convention to define 0 * Inf = 0. (For example, Shannon Entropy and the entire field of Information Theory).
However, in Matlab: 0 * Inf = NaN
Is there any way to adjust Matlab multiplication so that 0 * Inf = 0? Is there any way to localize such a modification to just a function scope?
(Note: it is easy to do this manually, but it would be simpler and more reliable to have Matlab do that automatically).

  3 Comments

Matt J
Matt J on 11 Sep 2014
For example, Shannon Entropy and the entire field of Information Theory
I question, though, whether you would ever really be enforcing 0.* Inf = 0 in any other information theory application other than an entropy calculation. If I'm right, it would be more sensible just to write an entropy.m utility file and handle 0.*inf situations within that file specifically.
Roger Stafford
Roger Stafford on 11 Sep 2014
I would strongly advise against such a redefinition of the multiplication operation. There is a perfectly good reason for declaring zero times infinity as indeterminate. The limit of a product as one of two factors approaches zero while the other factor approaches infinity all depends on how rapidly these two respective approaches occur. The product can be made to approach any value from 0 to infinity. It is very important for a user to be made aware of this indeterminate nature by producing a NaN rather than giving a possibly misleading result. The designers of the IEEE 754 standard knew what they were doing when they required a NaN as the answer to 0*inf, inf-inf, 0/0, inf/inf, etc.
Matt J
Matt J on 14 Sep 2014
The limit of a product as one of two factors approaches zero while the other factor approaches infinity all depends on how rapidly these two respective approaches occur.
I agree with Roger. It's not really 0*inf=0 that is the convention in Information Theory, anyway. The convention is really 0*log(0) = 0. All the more reason just to write a specific function file that computes entropy-like expressions p*log(p) with appropriate special handling for p=0.

Sign in to comment.

Answers (3)

the cyclist
the cyclist on 11 Sep 2014
Edited: the cyclist on 11 Sep 2014
"*" is a syntax shorthand for the times() function, and ".*" is syntax shorthand for the mtimes() function. One way to achieve what you want is to write your own multiplication functions that first check the input arguments for this special case, and otherwise call the MATLAB ones.
MATLAB functions can be overloaded for particular classes, but I am not sure that helps you.

  2 Comments

Matt J
Matt J on 11 Sep 2014
Strangely, in R2013b, this no longer works if times() is redefined locally. I know it worked in the past...
function test
q=inf.*0 %gives NaN
function c=times(a,b)
b(isinf(b))=0;
a(isinf(a))=0;
c=builtin('times',a,b);
end
end
Daniel Shub
Daniel Shub on 14 Sep 2014
@matt you need to stick times in an @double folder to overload it for class double.

Sign in to comment.


Joseph Cheng
Joseph Cheng on 11 Sep 2014
create a function called mymult(A,B) or myprod(A,B) that checks for inf and corresponding zero. then make that result index 0. If it is possible (i don't think it is) to create a special character syntax function like how .* is times() function and * is mtimes() (see help * and help .*) then the mymult(A,B) can be used.

  0 Comments

Sign in to comment.


Matt J
Matt J on 11 Sep 2014
Edited: Matt J on 11 Sep 2014
You can create a subclass of MATLAB's double data type using the attached classdef file. It will allow you to do things like this,
>> a=myclass(0); b=myclass(1); c=myclass(inf);
>> s=a+b
s =
1
>> m=a.*c
m =
0
>> entropy=-a.*log(a)
entropy =
0
You have to be a bit careful, though, because any method that you don't overload will often produce the original, non-customized double type,
>> whos s m entropy
Name Size Bytes Class Attributes
entropy 1x1 112 myclass
m 1x1 112 myclass
s 1x1 8 double
So, for example, you need to be sure that you won't be taking entropy of a sum. Or, you could of course overload summation operations to output myclass objects.

  0 Comments

Sign in to comment.

Sign in to answer this question.

Tags