chapel-lang / chapel

a Productive Parallel Programming Language
https://chapel-lang.org
Other
1.79k stars 421 forks source link

Math module - should we rename carg? #19006

Closed lydia-duncan closed 1 year ago

lydia-duncan commented 2 years ago

As someone who isn't familiar with this area of math, I have a hard time determining what this function is for based on its name. I believe C stands for complex in this case, but we've typically used it to refer to the C language in other parts of our code, so this overloads the abbreviation. The documentation says it computes the real phase angle of the argument - as a result, I propose renaming this function to phaseAngle, as it is more descriptive of its functionality.

We do inherit this name from C, so programmers familiar with it in that language may expect to use the same name. However, we can make it findable by using that name in our documentation.

damianmoz commented 2 years ago

A complex number x+iy is defined in polar coordinates by its argument (an angle - often called theta) and modulus (or radius often called r). If somebody who is using complex numbers in a program cannot extrapolate the concept of Complex Argument to carg, they need to do more homework. I cannot remember the last time I used the world phase angle.

Functions in languages supporting complex numbers have used the letter c to imply a function pertaining to complex numbers for decades. Untimately the number of C routines called by Chapel will diminish but you will be stuck with complex numbers.

The name carg() was inherited from Fortran. It is at least 44+ years old because it was coded explicitly in L Wayne Fullerton's 1977 version of FNLIB. I think it actually existed before that but I have nobody to ask and have no documentation from before that time to which I can refer.

The same comments as #19010 and #19011 apply here. Sorry, I am going backwards through your list.

I would vote to stick with the original name.

lydia-duncan commented 2 years ago

If somebody who is using complex numbers in a program cannot extrapolate the concept of Complex Argument to carg, they need to do more homework.

But what if they're not using complex numbers? What if they're just looking at options in the Math module?

lydia-duncan commented 2 years ago

Typically, Chapel uses overloading with different argument types rather than including the argument type in the name of a function, because we can handle that where languages like C do not. So you won't often see complex abbreviated at all, Chapel users will be more used to c referring to the language (and c versus c_ seems like a pretty subtle difference to me). Argument is a common word but not in this context (i.e. it is not common in the name of the function, more often being used to refer to arguments to functions), so it made sense to me to provide additional context.

bradcray commented 2 years ago

But what if they're not using complex numbers? What if they're just looking at options in the Math module?

Wouldn't the module documentation tell them what it is/does?

Chapel users will be more used to c referring to the language (and c versus c_ seems like a pretty subtle difference to me).

I hear what you're saying, but am not sure I buy it. For example, I don't think we'd rename cos() simply because of fear that someone would confuse it with a C Operating System call or c_os(). And if the world is accustomed to cos(), I don't think we'd rename it to cosine() simply to make it more self-documenting (though we obviously could if the community sentiment was that it's been an ongoing shame that the world uses sin() and cos()).

My feeling here is that if none of the more modern popular languages since Fortran/C have changed this name, then we shouldn't either.

bradcray commented 2 years ago

Adding one more note on why I don't think we should be concerned about the c_ prefix vs. routines starting with c: All of the c_ symbols are now defined in a library and not used by default (unless we missed something), and I don't think every library should have to worry about potentially causing confusion with every other library in terms of naming.

lydia-duncan commented 2 years ago

It sounds like we're keeping carg

lydia-duncan commented 1 year ago

Based on Damian's comment on the conjg issue (https://github.com/chapel-lang/chapel/issues/19010), we'll take a look at whether to rename this function in the sprint starting August 8th. However, we might run out of time, in which case we may mark it unstable and revisit post 2.0 to determine a better name.

1. What is the API being presented?

  /* Returns the phase (often called `argument`) of complex `x`, an angle (in
     radians).

     In concert with the related :proc:`abs`, the magnitude (a.k.a.
     modulus) of `x`, it can be used to recompute `x`.

     :rtype: ``real(w/2)`` when `x` has a type of ``complex(w)``.
  */

  inline proc carg(x: complex(?w)): real(w/2) { … }

How is it intended to be used?

As stated in the documentation, it can be used to recompute the original complex when combined with the magnitude (obtained via abs). Wikipedia points out that this is used to represent the complex’s polar coordinates in the complex plane (https://en.wikipedia.org/wiki/Complex_number), and that formulas for multiplication, division and exponentiation of complex numbers are simpler using the polar coordinates.

Wikipedia also points out that the argument (phase) can be typically represented by color in domain coloring, while the magnitude is typically represented by brightness.

I’m not an expert on mathematics involving complex numbers, unfortunately, but it seems like there’s utility in determining which complex numbers share a phase angle and thus could be viewed as “in a line” with each other (e.g. the only difference being their magnitude).

The argument (phase) also will change its sign when conjugation is performed on the original complex number.

Complex numbers are used in signal analysis, because they are convenient for describing signals that vary periodically.

How is it being used in Arkouda and CHAMPS?

Not used in Arkouda or CHAMPS, so far as we can tell.

2. What's the history of the feature, if it already exists?


It was added in 2016 along with several other math functions for complex numbers. The PR initially used the name arg for it, but before merging it was renamed to carg to avoid conflicts with arg in other contexts and to match the C name.

Damian helped me improve the documentation for this function when we dealt with abs.

There was some uncertainty about whether we should continue to include it in all programs by default, but upon understanding its role in complex number computations, we decided it was important to continue to include it in all programs.

3. What's the precedent in other languages, if they support it?

a. Python

Python uses the name phase to represent it (https://docs.python.org/3/library/cmath.html#cmath.phase) and refers to “argument” in its definition.

Python also provides a polar function (https://docs.python.org/3/library/cmath.html#cmath.polar) that is equivalent to (abs(x), phase(x)).

It looks like NumPy uses angle instead (https://numpy.org/doc/stable/reference/generated/numpy.angle.html#numpy.angle).

b. C/C++

C/C++ use carg, cargf and cargl for this functionality (where the name varies based on the type of the argument, https://en.cppreference.com/w/c/numeric/complex/carg). We chose our name for this function based on this name.

c. Rust

I’m not seeing an equivalent in Rust.

d. Swift

I’m not seeing an equivalent in Swift.

e. Julia

Julia provides angle (https://docs.julialang.org/en/v1/base/math/#Base.angle)

f. Go

Go doesn’t provide this function in its base support. I see some packages that refer to something along these lines:

4. Are there known Github issues with the feature?

5. Are there features it is related to? What impact would changing or adding this feature have on those other features?

6. How do you propose to solve the problem?


A. Leave as is

B. Rename to cArg

C. Rename to phase

D. Rename to angle

E. Rename to phaseAngle

F. Rename to something else?

G. Mark unstable until we figure out a better name

I think it would be good to open an issue requesting a polar function like Python provides, but that would be a post 2.0 task.

I’d probably rename to phase or phaseAngle, left to my own devices.

lydia-duncan commented 1 year ago

In our discussion today, we decided:

In addition to the options listed in the previous comment, we also considered:

Discussion:

lydia-duncan commented 1 year ago

Opened #23173 for the polar function and #23174 for the magnitude function. Once carg has been renamed, we can close this issue.