define multiplication by zero for Inf

15 次查看(过去 30 天)
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 个评论
Roger Stafford
Roger Stafford 2014-9-11
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 2014-9-14
编辑:Matt J 2014-9-14
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.

请先登录,再进行评论。

回答(3 个)

the cyclist
the cyclist 2014-9-11
编辑:the cyclist 2014-9-11
"*" 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 个评论
Matt J
Matt J 2014-9-11
编辑:Matt J 2014-9-11
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 2014-9-14
@matt you need to stick times in an @double folder to overload it for class double.

请先登录,再进行评论。


Joseph Cheng
Joseph Cheng 2014-9-11
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.

Matt J
Matt J 2014-9-11
编辑:Matt J 2014-9-11
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.

类别

Help CenterFile Exchange 中查找有关 Descriptive Statistics and Visualization 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by