v923z / micropython-ulab

a numpy-like fast vector module for micropython, circuitpython, and their derivatives
https://micropython-ulab.readthedocs.io/en/latest
MIT License
432 stars 117 forks source link

add cross product #43

Closed v923z closed 4 years ago

v923z commented 4 years ago

Add the cross product function https://docs.scipy.org/doc/numpy/reference/generated/numpy.cross.html

rcolistete commented 4 years ago

Thumbs up to have 'ulab.linalg.cross()'.

v923z commented 4 years ago

I haven't touched this, because the cross-product is really nasty. It is a lot of case-checking, and I am not sure the speed gain is worth the trouble: https://numpy.org/doc/stable/reference/generated/numpy.cross.html. I am more inclined to yank this now. If someone need it, it should probably be implemented in python.

rcolistete commented 4 years ago

Perfect is the enemy of good...

My suggestion is to implement only some features of 'numpy.cross', the most useful. For example :

v923z commented 4 years ago

Perfect is the enemy of good...

That's right, but we have limited resources... (in time, RAM, and flash).

My suggestion is to implement only some features of 'numpy.cross', the most useful. For example :

* only vector cross-product;

* only vectors with the same size (2 or 3, as numpy.cross);

* so no 'axisa', 'axisb', 'axisc', 'axis' arguments, accepting only 2 arguments : a, b;

* compatible with 1st, 3rd and 4th examples from [numpy.cross reference](https://docs.scipy.org/doc/numpy/reference/generated/numpy.cross.html) would run.

This could be a reasonable compromise. Do you want to try your hand at it?

rcolistete commented 4 years ago

This could be a reasonable compromise. Do you want to try your hand at it?

Not at the moment. I need to practice writing natice C modules for MicroPython, with simple functions of 1 argument, etc. Then I could contribute with code to ulab.

v923z commented 4 years ago

OK. Thanks for the feedback! You might find hints under https://micropython-usermod.readthedocs.io .

rcolistete commented 4 years ago

I've made a simple version of 'linalg.cross(a, b)'. See the commit 'cross : implemented linalg.cross(a, b)' in the 'cross' branch of my ulab fork. With :

Any suggestion ?

If it is ok, I can make a PR. As it is my first day adding features to ulab in C, I'm asking before sending any PR.

v923z commented 4 years ago

I've made a simple version of 'linalg.cross(a, b)'. See the commit 'cross : implemented linalg.cross(a, b)' in the 'cross' branch of my ulab fork. With :

* only vector cross-product;

* only vectors with the same size/dimension, 3;

* no 'axisa', 'axisb', 'axisc', 'axis' arguments, accepting only 2 arguments : a, b;

* output is always float, with no upcasting.

Any suggestion ?

I would have a couple:

  1. I presume, speed is not an issue here (there only 3 elements in an array), so I would call the iterator function. If you do that, the following will be valid:
a = ulab.array([1, 2, 3])
b = ulab.array([4, 5, 6])
cross(a, b)
cross([1, 2, 3], [4, 5, 6])
  1. I would accept 2-component vectors/iterables, so that

    a = ulab.array([1, 2])
    b = ulab.array([4, 5, 6])
    cross(a, b)

    doesn't throw an error.

  2. When adding new function(s), don't forget to increment the version number in ulab.c, and insert an entry in https://github.com/v923z/micropython-ulab/blob/master/docs/ulab-change-log.md

  3. Add your name in the copyright note in linalg.c.

v923z commented 4 years ago

@rcolistete Do you want to work this out, or should I?

rcolistete commented 4 years ago

I will. I am also spending time building MicroPython firmwares with ulab on Pyboard's (23 variants) and ESP8266.

v923z commented 4 years ago

OK, thanks for the feedback!

v923z commented 4 years ago

@rcolistete cross has been added to the latest version. Closing the issue now.