-
Notifications
You must be signed in to change notification settings - Fork 127
Complex2 #456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@wired8 Could you, please, check, whether you still have the error that you mentioned in #366 (comment) ? |
fix ndarray_shape for arrays of zero length
…plex2 pulling in remote
Looks good, no more kernal panic. Doing some testing with the following code:
Can we add support for iterables over complex types? |
The problem is that the exponential function is not defined for complexes at the moment. I could implement that, but the question really is, what would be useful in this regard. There are some 20 universal functions, and I am not sure, whether implementing everything for complex types makes sense. The exponential function should definitely be included. What else? Here is a rudimentary list.
EDIT: actually, I think that the error that you encountered comes from here
or something similar. |
The list you provided is a great start. Implementing binary, exp would give us partial butterworth filter support. |
|
I think this is OK that way, at least, intentional. If you need the exponential of a complex list, you can cast it first as
The reason for this is that, when applying a function to an object, first you have to figure out, Floats are simpler in this regard, because applying a mathematical function to a number always |
Yes
The following is the guts of the zpk2tf method, so if we can implement the aforementioned we should be able to support this function which is the last part of the iirfilter. if issubclass(b.dtype.type, numpy.complexfloating):
# if complex roots are all complex conjugates, the roots are real.
roots = numpy.asarray(z, complex)
pos_roots = numpy.compress(roots.imag > 0, roots)
neg_roots = numpy.conjugate(numpy.compress(roots.imag < 0, roots))
if len(pos_roots) == len(neg_roots):
if numpy.all(numpy.sort_complex(neg_roots) ==
numpy.sort_complex(pos_roots)):
b = b.real.copy() |
Should
That also copies the content automatically. |
Keeps it to spec with numpy, so why not! |
Well, because if you want to have But to address your comment, |
@v923z awesome work, I am now able to get the correct output for a butter filter with one small caveat that the all function doesn't support complex yet. I do have a work around for now. https://micropython-ulab.readthedocs.io/en/latest/numpy-functions.html#all |
Possible bug with np.all p = np.array([1j,2j,3j,4j], dtype=np.complex)
q = np.array([1j,2j,3j,4j], dtype=np.complex)
print(np.all(p==q))
>>> False Expect True |
Possible bug with sorting and conjugate roots = np.array([0.98-0.01j,0.99+0.01j,0.98+0.01j,0.99-0.01j], dtype=np.complex)
p = np.sort_complex(roots)
q = np.sort_complex(np.conjugate(roots))
print(p)
print(q)
>>> array([0.9800000000000001-0.01j, 0.9800000000000001+0.01j, 0.99-0.01j, 0.99+0.01j], dtype=complex)
>>> array([0.9800000000000001+0.01j, 0.9800000000000001-0.01j, 0.99+0.01j, 0.99-0.01j], dtype=complex) Expect: [0.98-0.01j 0.98+0.01j 0.99-0.01j 0.99+0.01j] |
I think the problem is that I should probably also raise an exception for |
Thanks for reporting the issue! I'll sort it out. (Pun intended.) |
Hey @v923z your fixes work and I now get the same expected output from a butter filter in ulab as I do in numpy/scipy. Thank you so much for all you hard work. I want to contribute my partial implementation of these libraries back into ulab, I'm guessing I add them as snippets? I was thinking of adding a few libraries under snippets/scipy, snippets/numpy and some tests to boot. Please advise on how to proceed. |
Not at all. I am glad that it works now. There were a couple of smaller glitches that I discovered after you had reported the problem with
If the implementation is in One argument against the C implementation (beyond the obvious) is that it requires flash space, even if you don't use the function. So, snippets are somewhat more flexible.
You have just described what to do. I would like to merge this branch, and release 4.0 very soon, but that doesn't really interfere with your stuff. You can PR against this, or the master branch, whenever you are ready. I am quite certain that contributions of this kind would be very useful for the wider community. Actually, if you have any comments on the release draft, please, let me know! Also, I think #465 is obsolete now. Can we close that? |
This is the re-based version of #366, which will be closed now.