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

Enhancement request 13022: add std.complex.abs2 #2297

Merged
merged 1 commit into from Jul 17, 2014
Merged

Enhancement request 13022: add std.complex.abs2 #2297

merged 1 commit into from Jul 17, 2014

Conversation

JLTastet
Copy link
Contributor

@JLTastet JLTastet commented Jul 2, 2014

Add std.complex.abs2, which returns the squared modulus of a
Complex or real number.

Rationale:

  • It is common to need the squared modulus of a complex number,
    and using abs(z)*abs(z) would be inefficient due to the
    computation of the square root. Moreover, z*conj(z) returns a
    complex number, usually with nonzero imaginary part due to
    rounding, and cannot be used in a generic algorithm that can work
    with real numbers as well.
  • The name abs2 has been chosen for consistency with abs, and
    for being mathematically more correct than norm, used by C++ STL
    for the equivalent function.
  • For genericity, the function abs2 is also declared for real
    floating point numbers, in which case it simply returns its
    square.

UPDATE: use sqAbs instead of abs2.

https://issues.dlang.org/show_bug.cgi?id=13022

@JLTastet
Copy link
Contributor Author

JLTastet commented Jul 2, 2014

A trivial improvement, but it was missing.
I would like to hear your opinion about the name of the squared modulus function, since we won't be able to change it once it is released. As said in the commit message, I particularly dislike C++'s norm, but we could use sqAbs, sqMod, sqNorm as well.
I also think that some algorithms may benefit from having abs2 declared for real numbers.

@Safety0ff
Copy link
Contributor

I would prefer either prefix or postfix sq over 2 to denote squaring.
Even prefix squared would be preferable to me, though I don't know how cumbersome it is.
2 suggests it is a different version of the function, e.g. atan versus atan2

@JLTastet
Copy link
Contributor Author

JLTastet commented Jul 2, 2014

The use of 2 to denote another version was actually what was worrying me. sqAbs seems perfectly fine to me. Ideally the name should be kept short, like most other mathematical functions, so I would avoid squaredAbs.
I would like to hear opinions from more people before changing it (especially from those who use Complex a lot !).

Add `std.complex.abs2`, which returns the squared modulus of a
`Complex` or real number.

Rationale:
* It is common to need the squared modulus of a complex number,
and using `abs(z)*abs(z)` would be inefficient due to the
computation of the square root.
* The name `abs2` has been chosen for consistency with `abs`, and
for being mathematically more correct than `norm`, used by C++ STL
for the equivalent function.
* For genericity, the function `abs2` is also declared for real
floating point numbers, in which case it simply returns its
square.

UPDATE: use `sqAbs` instead of `abs2`.
@JLTastet
Copy link
Contributor Author

JLTastet commented Jul 3, 2014

Rebased and changed the name for sqAbs.

@quickfur
Copy link
Member

LGTM.

@JLTastet
Copy link
Contributor Author

I just searched how this function is called in other languages.
Julia calls it abs2, while SciPy and MATLAB don't have it, and there is no such intrinsic function in Fortran or R (at least in package base where complex is defined).

@mihails-strasuns
Copy link

LGTM. sqAbs is much more clear than abs2

@mihails-strasuns
Copy link

Auto-merge toggled on

mihails-strasuns pushed a commit that referenced this pull request Jul 17, 2014
Enhancement request 13022: add `std.complex.abs2`
@mihails-strasuns mihails-strasuns merged commit 82dc45e into dlang:master Jul 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants