2

I am currently translating a MATLAB program into Python. I successfully ported all the previous vector operations using numpy. However I am stuck in the following bit of code which is a cosine similarity measure.

% W and ind are different sized matrices
dist = full(W * (W(ind2(range),:)' - W(ind1(range),:)' +  W(ind3(range),:)')); 
for i=1:length(range)
    dist(ind1(range(i)),i) = -Inf;
    dist(ind2(range(i)),i) = -Inf;
    dist(ind3(range(i)),i) = -Inf;
end
disp(dist)
[~, mx(range)] = max(dist);

I did not understand the following part.

dist(indx(range(i)),i) = -Inf;

What actuality is happening when you use

= -Inf;

on the right side?

Castilla
  • 85
  • 6

3 Answers3

4

In Matlab (see: Inf):

Inf returns the IEEE® arithmetic representation for positive infinity.

So Inf produces a value that is greater than all other numeric values. -Inf produces a value that is guaranteed to be less than any other numeric value. It's generally used when you want to iteratively find a maximum and need a first value to compare to that's always going to be less than your first comparison.

According to Wikipedia (see: IEEE 754 Inf):

Positive and negative infinity are represented thus:

sign = 0 for positive infinity, 1 for negative infinity.
biased exponent = all 1 bits.
fraction = all 0 bits.

Python has the same concept using '-inf' (see Note 6 here):

float also accepts the strings “nan” and “inf” with an optional prefix “+” or “-” for Not a Number (NaN) and positive or negative infinity.

>>> a=float('-inf')
>>> a
-inf
>>> b=-27983.444
>>> min(a,b)
-inf
beaker
  • 16,331
  • 3
  • 32
  • 49
2

It just assigns a minus infinity value to the left-hand side.

It may appear weird to assign that value, particularly because a distance cannot be negative. But it looks like it's used for effectively removing those entries from the max computation in the last line.

If Python doesn't have "infinity" (I don't know Python) and if dist is really a distance (hence nonnegative) , you could use any negative value instead of -inf to achieve the same effect, namely remove those entries from the max computation.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
2

The -Inf is typically used to initialize a variable such that you later can use it to in a comparison in a loop. For instance if I want to find the the maximum value in a function (and have forgotten the command max). Then I would have made something like:

function maxF = findMax(f,a,b)
maxF = -Inf;
x = a:0.001:b;
for i = 1:length(x)
    if f(x) > maxF
         maxF = f(x);
    end
end

It is a method in matlab to make sure that any other value is larger than the current. So the comparison in Python would be -sys.maxint +1.

See for instance: Maximum and Minimum values for ints

Community
  • 1
  • 1
Nicky Mattsson
  • 3,052
  • 12
  • 28