Skip to content
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

Merged
merged 2 commits into from
Dec 14, 2022
Merged

Add complex number support for multiplication #551

merged 2 commits into from
Dec 14, 2022

Conversation

kgryte
Copy link
Contributor

@kgryte kgryte commented Dec 5, 2022

This PR

  • adds support for complex number multiplication. For finite components, complex number multiplication is well-defined, where (a + bj) x (c + dj) = (ac - bd) + (bc + ad)j. However, for complex numbers having infinite or NaN 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 is NaN. 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 involving NaNs and infinities is specified as implementation-dependent.
  • Specifies that, in general, the special cases described for real-valued floating-point operands apply to the component operations separately. Meaning, operations involving real-valued components (i.e., a, b, c, d) must abide by real-valued floating-point special cases.
  • updates the input and output array data types to be any numeric data type, not just real-valued data types.

@kgryte kgryte added API change Changes to existing functions or objects in the API. topic: Complex Data Types Complex number data types. labels Dec 5, 2022
@kgryte kgryte added this to the v2022 milestone Dec 5, 2022
Copy link
Member

@rgommers rgommers left a 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 |
Copy link
Member

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.

Copy link
Member

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)

Copy link
Contributor Author

@kgryte kgryte Dec 14, 2022

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.

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kgryte

@rgommers rgommers merged commit 4e82d64 into main Dec 14, 2022
@rgommers rgommers deleted the cmplx-mul branch December 14, 2022 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API change Changes to existing functions or objects in the API. topic: Complex Data Types Complex number data types.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants