0

I need to add 2 dim arrays of variable sizes to each other. There are many methods to do this!

Typical sizes are a few thousand by a few hundred (as this may impact scaling!). Need to carry out hundreds of thousands of these additions. The first dimension is guaranteed to be the same in my case, but subarrays are variable length.

working smaller example:

a = np.ones(shape=(20,20))
b = np.ones(shape=(20,18))
c = a+b # Expected error

b.resize(a.shape)
c = b+c # This works!

Are there faster ways of doing this? I am interested in other pythonic solutions like above, but also what might be truly fastest regardless of complexity (it is that kind of project where speed is mostly king, but not so much as to write it in C).

RScotia
  • 3
  • 1
  • I don't think that the resize is doing what you think it is – Mad Physicist Mar 12 '21 at 18:59
  • You need to specify very carefully what you actually mean by adding arrays of different sizes. Otherwise, you may as well just add random numbers together. – Mad Physicist Mar 12 '21 at 19:00
  • The `resize` method is in-place and thus relatively fast, but it has very restricted usage. But pay close attention to how it pads the array. – hpaulj Mar 12 '21 at 19:30

1 Answers1

1

If you don't mind changing a, you could do

a[:, :b.shape[1]] += b

I suspect this will be faster than padding b and then adding, but things like this need to be tested with realistic data.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Very slick! I think your approach is much cleaner and prob faster bc you don't need to call an external function. I'm removing mine lol. – v0rtex20k Mar 12 '21 at 20:13
  • This is a very good answer, and I am using this in a few places. The note about changing a is important. In my case, desired. Thanks! – RScotia Feb 01 '22 at 14:14