-
Notifications
You must be signed in to change notification settings - Fork 46
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
Add complex number support for multiplication #551
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @kgryte. I have one comment on the table here that would be good to address.
+------------+----------------+-----------------+--------------------------+ | ||
| **bj** | (b*c)j | -(b*d) | -(b*d) + (b*c)j | | ||
+------------+----------------+-----------------+--------------------------+ | ||
| **a + bj** | (a*c) + (b*c)j | -(b*d) + (a*d)j | implementation-dependent | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation-dependent
here is confusing, and inconsistent with the PR description. Why not add (ac - bd) + (bc + ad)j
here? The "if a component is nan/inf" is already handled by special-case text below, now it looks like the whole operation isn't well-defined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same comment applies to division (gh-554)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the entry from implementation-dependent
to special rules
. The main purpose of the table is to describe when the normal rules for real-valued floating-point case special cases must apply. As the normal rules don't apply for multiplication of two complex numbers, IMO adding the textbook formula for cmplx-cmplx would also be misleading given the content which immediately follows the table.
My concern is that if the table shows only the textbook formula without indication that cmplx-cmplx may vary between implementations, readers won't continue reading on to find out that special case handling doesn't always apply.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrt complex division, I should note that the guidance following the table is that, for finite components, division "should" (not "must") be computed as if by the textbook formula, as there are algorithms, similar to hypot
, for avoiding underflow/overflow. And for complex multiplication, one can use Karatsuba's algorithm to reduce the number of multiplications. In short, I am hesitant to be too prescriptive in what is allowed/disallowed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"special rules" seems better, thanks. After that section is moved to notes, so it's clearer that the whole thing is special case handling related, it will be even better. Let's roll with this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @kgryte
This PR
(a + bj) x (c + dj) = (ac - bd) + (bc + ad)j
. However, for complex numbers having infinite orNaN
components, results may diverge depending on how one chooses to model complex infinities. In C99 with its one-infinity model, a complex value is infinite, even if the other component isNaN
. In order to maintain a single point at infinity, C99 suspends the normal rules of arithmetic floating-point operations and requires branching logic within implementations in order to accurately handle special cases. Such branching logic is undesirable when performance is paramount (e.g., in certain accelerator libraries), and, thus, special cases involvingNaNs
and infinities is specified as implementation-dependent.a
,b
,c
,d
) must abide by real-valued floating-point special cases.