Wednesday, April 6, 2011

Why subtract a value from itself (x - x) in Python?

In NumPy functions, there are often initial lines that do checking of variable types, forcing them to be certain types, etc. Can someone explain the point of these lines in scipy.signal.square? What does subtracting a value from itself do?

t,w = asarray(t), asarray(duty)
w = asarray(w + (t-t))
t = asarray(t + (w-w))

source

From stackoverflow
  • I believe that this will make the final w and t have the same type. For example, if you start with float and int, you will end up with both being float arrays which is better for subsequent operations.

    Dingle : A good observation. I have used the same trick, too.
    iondiode : yes, and it will have the same dimension as well ( in case they started out with different)
    endolith : That's kind of weird considering t is an array and w is a scalar

0 comments:

Post a Comment