define multiplication by zero for Inf
조회 수: 21 (최근 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
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
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
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
Daniel Shub
2014년 9월 14일
@matt you need to stick times in an @double folder to overload it for class double.
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.
댓글 수: 0
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Descriptive Statistics and Visualization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!