define multiplication by zero for Inf
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Roger Stafford
le 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
le 14 Sep 2014
Modifié(e) : Matt J
le 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.
Réponses (3)
the cyclist
le 11 Sep 2014
Modifié(e) : the cyclist
le 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 commentaires
Daniel Shub
le 14 Sep 2014
@matt you need to stick times in an @double folder to overload it for class double.
Joseph Cheng
le 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 commentaires
Matt J
le 11 Sep 2014
Modifié(e) : Matt J
le 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 commentaires
Voir également
Catégories
En savoir plus sur Descriptive Statistics and Visualization dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!