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

Define round for complex numbers #8291

Merged
merged 5 commits into from
Feb 2, 2015
Merged

Define round for complex numbers #8291

merged 5 commits into from
Feb 2, 2015

Conversation

jiahao
Copy link
Member

@jiahao jiahao commented Sep 9, 2014

No description provided.

@jiahao jiahao changed the title Define round() for complex numbers Define round, ceil, floor, trunc for complex numbers Sep 9, 2014
@ArchRobison
Copy link
Contributor

round made sense to me as "round to nearest Gaussian integer". trunc sort of made sense to me as "round towards origin to a Gaussian integer". floor and ceil seemed more questionable since they cover rounding towards only two of the four corners of the complex plane. They would at least obey the identity floor(-z)=-ceil(z). But relations based on < would seem to be lost.

I'd be inclined to include only complex round for now.

@eschnett
Copy link
Contributor

In addition to round, rint should also be available: round breaks ties away from zero, rint breaks ties towards even.

And there we enter a slippery slope... If we accept round with its notion of "away from zero", then trunc with its notion of "towards zero" should also make sense.

I would not be averse to having floor and ceil as well. You can use complex conjugation to cover the other two "corners". I speculate that these cases are not even necessary most of the time -- if you describe a rectangle in the complex plane, then describing two opposite corners suffices, for which you'd use floor and ceil.

@jiahao
Copy link
Member Author

jiahao commented Feb 1, 2015

@simonbyrne's more recent work on exposing RoundingModes has lent more weight to the idea that only round should be exposed.

The C99 standard actually specifies separate rounding modes for the real and imaginary components, which seems a little odd at first, but I think is the only reasonable thing to do and is also what @eschnett has proposed. trunc, ceil and floor correspond to three different real rounding modes which can be freely applied to real and imaginary components separately; in principle one could define trunc(z) in terms of the (RoundToZero, RoundToZero) rounding mode, but this seems to have limited value.

@jiahao jiahao force-pushed the cjh/round-complex branch 3 times, most recently from a80d2a5 to d3b3b79 Compare February 1, 2015 03:26
@jiahao
Copy link
Member Author

jiahao commented Feb 1, 2015

Following what we do now for real numbers, I've exposed 3 different round methods for complex numbers:

  • The "expert" rounding mode aware round(z, RoundingMode, RoundingMode)
    which takes separate RoundingModes for real and imaginary components
  • The basic round(z) which applies round to the real and imaginary
    components separately using the current global rounding mode
  • round(z, d, b) for rounding the real and imaginary components
    separately to d digits each in base b.

The last method, together with float(z), is sufficient to restore the ability to round complex matrices with round.

@jiahao jiahao added the complex Complex numbers label Feb 1, 2015
@jiahao jiahao changed the title Define round, ceil, floor, trunc for complex numbers Define round for complex numbers Feb 2, 2015
@simonbyrne
Copy link
Contributor

This seems like a reasonable approach.

Returns the nearest integral value of the same type as ``x`` to ``x``,
breaking ties using the specified :obj:`RoundingMode`.

.. function:: round(z, RoundingMode1, RoundingMode2)
Copy link
Member

Choose a reason for hiding this comment

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

I think the signature should rather be

round(z, RoundingModeReal, RoundingModeImaginary)

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

Defines new round methods on Complex numbers:

- The "expert" rounding mode aware round(z, RoundingMode, RoundingMode)
  which takes separate RoundingModes for real and imaginary components
- The basic round(z) which applies round to the real and imaginary
  components separately using the current global rounding mode
- round(z, d, b) for rounding the real and imaginary components
  separately to d digits each in base b.

Also defines float(z).

These methods are sufficient to also allow for round(Array{Complex}) to
work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complex Complex numbers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants