Page MenuHome

Implement vec/vec element wise division
AbandonedPublic

Authored by Ruben De Smet (rubdos) on Jan 20 2016, 1:56 PM.

Details

Reviewers
None
Summary

We're currently doing element-wise division in Python. Profiling learned me it was one of the bottlenecks of the plugin. By implementing this addition, I had a performance improvement of about 6%.

C&C welcome, I'll happily refactor whatever is wrong with the patch :)

Diff Detail

Repository
rB Blender

Event Timeline

Ruben De Smet (rubdos) retitled this revision from to Implement vec/vec element wise division.
Ruben De Smet (rubdos) updated this object.
Ruben De Smet (rubdos) set the repository for this revision to rB Blender.

Something awkward here is that a * b for 2 vectors currently returns the dot product.

Am not too comfortable to make a * b have very different meaning to a / b.

Probably best to use element wise multiplication too, and dot product can just use the a.dot(b) form (this is what numpy does).

To avoid confusion for this, we would be best to deprecate vec * vec with a warning for at least a release.
Then change the behavior of multiply and divide.


As for this patch, it seems ok. though we would want idiv to be implemented as well.

Something awkward here is that a * b for 2 vectors currently returns the dot product.

Am not too comfortable to make a * b have very different meaning to a / b.

Probably best to use element wise multiplication too, and dot product can just use the a.dot(b) form (this is what numpy does).

To avoid confusion for this, we would be best to deprecate vec * vec with a warning for at least a release.
Then change the behavior of multiply and divide.

In MATLAB it happens as follows:

a * b

is the dot product

a .* b

is the element wise product. Same for division (a./b works for vectors, a/b only works for vector/scalar).

I know it's impossible for python, but perhaps it adds something to the conversation.

I'd say that a/b doesn't really make sense in any other aspect than element wise division (or division by a scalar). I also think that there's more use of dot products than of element wise products, so I'd vote to add an element wise method (a.times(b), http://nl.mathworks.com/help/matlab/ref/times.html) instead of throwing all backwards compatibility over board.

As for this patch, it seems ok. though we would want idiv to be implemented as well.

Do you want me to implement that, or shall we just merge this already?

I agree with @Campbell Barton (campbellbarton) here - we cannot have different behaviors/meanings for * and /, those two ops are universally expected to be “symmetric” I think?

However, it is also true that dot product is maybe the most common vec operation, and that removing that will also break probably quite a bit of code.

So we could also decide to not use / op for vector by vector division, and instead add v.divide(v)?

Side note: if we search for references, think NumPY is also nice to look at - afaict, they only have a named func for dot products? * and / operations over arrays just perform element-wise multiplication/division.

So I guess it’s also a matter of deciding whether we want to follow some convention or another?

I agree with @Campbell Barton (campbellbarton) here - we cannot have different behaviors/meanings for * and /, those two ops are universally expected to be “symmetric” I think?

However, it is also true that dot product is maybe the most common vec operation, and that removing that will also break probably quite a bit of code.

So we could also decide to not use / op for vector by vector division, and instead add v.divide(v)?

Side note: if we search for references, think NumPY is also nice to look at - afaict, they only have a named func for dot products? * and / operations over arrays just perform element-wise multiplication/division.

Yes, numpy is a better reference than MATLAB. I'm sorry about that, I got a lot of courses in MATLAB and none in numpy ^^

So I guess it’s also a matter of deciding whether we want to follow some convention or another?

Ultimately, it's up to you to choose, just make sure to let me know, so I know what to use in my Python code.

If you decide on a.divide(b), I'll be happy to change this diff.

So, ... Any update on this? Would be nice to see it in one of the next releases ^^

I'd be happy to make any update.