Add Num trait to Complex #421
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There are a few other fixes that got included here, but the main point is to add the Num trait to Complex.
It is probably worth discussing whether Complex is well suited to the Num trait- I imagine when it was first written, it was thought to be a bad idea. What I want is to be able to write DSP blocks with generics that don't care whether the underlying types are real, complex, or perhaps other objects like vectors or quaternions. What we currently have is a "one size fits all" Num trait, so what I'm proposing now is adding not-quite-right implementations for the things that don't make sense, notably comparison and modulus. Perhaps it would be better to leave them as ???.
I also added a protected function to checkPort to the Bundle class. When calcElements runs, it uses reflection to go through each method and tries to invoke it. Unfortunately, for Complex, some of those methods construct a new Complex, which can lead to a stack overflow. The new function checkPort is supposed to fix this by allowing subclasses of Bundle to filter out elements before invocation. This is related to Albert's old pull request. Adding checkPort is also nice if you want to restrict what types of objects can be in a subclass of Bundle. For the SDR project, I have subclasses of Bundle that throw a ChiselError if any of their objects don't conform to our ready/valid interface.
It may be worth thinking about splitting up Num into a numerical tower that makes sense for DSP applications. This page has a bunch of links, this page discusses some of the problems with Haskell's numeric types (it would be nice to avoid these). Some things that seem essential to DSP are: