I'm a bit surprised that I couldn't find an easy when to collapse dimensions of a Numpy array containing identical values. Let me explain.
I might have to multiply to time series implemented as arrays, say a * b. In most cases, this is fine, but sometimes a or b are scalars representing constant signals. This works also smoothly because Numpy knows how to broadcast them, and computes this very quickly (twice as fast as if b were an array full of identical values).
Now sometimes a = 0; and as a result, I get an array full of zeros. I want to collapse it to the scalar 0 because it is a constant signal, but I can't seem to find any easy way to do it -- and I mean, without adding a condition to check whether a or b is 0 and treat it as a special case every singly time I operate on my arrays.
Do you know of any simple ways to achieve this?