-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
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
Make _Py_c_sum(), _Py_c_diff(), etc (elementary operations on Py_complex) - part of the public API #128813
Comments
There is an alternative: the C11
If users want to use Python API for arithmetic, they should use IMO, we should instead add It's a question for the C API WG though, if not for wider discussion. |
Not all compilers support that type. For msvc we have
Maybe rather just one |
On another hand, it seems that all PEP 11 platforms have complex numbers in some form. And Py_complex can be coerced both to So, the plan is:
How this sounds, should this be discussed in the C API WG? CC @serhiy-storchaka We also have API functions to extract real or imaginary components. Maybe we can also hide details of the |
AFAIK, MSVC's
No,
We should add the C complex type. The C API is not just for C and C++ :)
Sounds good to me; do ask the WG though. |
No, but it's memory layout seems compatible with double complex (and Py_complex). However, you can't use usual arithmetic ops with
I'm not saying it's bad. But keeping fields of this structure public - blocks us from using Annex G double complex instead of Py_complex. I'm not sure it's wise to close this door.
|
We should not use the same prefix As for passing the The structure of typedef struct {
double real;
double imag;
} Py_complex; We should also take into account compatibility with C++. |
If the layout is compatible, I'd expect that
Hm, could we switch away from using |
Sure. Naming like But I like @encukou idea to drop arithmetic functions from the public API. People could use native complex types on their platform instead. One minor note, custom functions do make sense if our support for complex arithmetic will go beyond the C standard (or, rather, beyond it's implementations). As in my proposal, for example: https://discuss.python.org/t/77073.
As I said, my preliminary benchmarks show no measurable effect.
No, I meant you can do something like this (on Windows you can use Py_complex z = {.real=1.25, .imag=-0.5}; // say this come from PyComplex_AsCComplex()
double complex *zn = (double complex *)(&z); // in memory layout is same
printf("(%lf%+lf)\n", creal(*zn), cimag(*zn)); // do math with native complex type In principle, this seems to be working in both directions (i.e. you can pass
BTW, PyComplexObject structure fields aren't documented, just as for PyFloatObject. |
No, definitely don't cast between
That doesn't really matter. Per PEP-387, “if something is not documented at all, it is not automatically considered private”. |
No, it's something we can rely on, thanks to the C standard; it says: "Each complex type has the same representation and alignment requirements as an array type containing exactly two elements of the corresponding real type; the first element is equal to the real part, and the second element to the imaginary part, of the complex number." (c) C17 §6.2.5p13. |
Structs are not arrays; they may have padding between the members (§6.7.2.1.15). Unlikely in production, but a future sanitizer might well catch it.
I don't think CPython should not provide and maintain public C API for that. This is not the place to fill holes in libc. |
Thanks, I miss this. Looks not too likely for simple structure with two doubles, but...
Maintenance cost shouldn't be too high. Without this people lose opportunity to add new mathematical functions with C-API. Of course, it's a hypothetical scenario so far. In present shape our low-level function should be equivalent (modulo errors somewhere) to most existing Annex G implementations. I'll open a C-API WG issue, as you suggested. |
The 3.14b1 is coming, so I'm going to remove newly added semi-private functions for doing mixed-mode arithmetic. If we decide to make public the current low-level API - this can be re-added. |
Feature or enhancement
Proposal:
Suggested by @vstinner in #124829 (comment).
I would agree, as these routines could be useful (and it seems, they are used by few projects) to implement mathematical functions just like in the cmath module. No alternatives exist.
It's also suggested to use a different convention for arguments: currently we pass them by value. We could use pointers to Py_complex struct instead. (Though my quick tests shows no measurable difference.)
Also, we should decide on naming. For
_Py_c_sum()
-PyComplex_Add()
was suggested. But this looks misleading, asPyComplex_
is a prefix for functions, operating withPyObject*
arguments. Perhaps, rather we could instead usePy_complex_add()
andPy_complex_add_real()
(in the GNU GSL style). Current semi-private functions should be deprecated.Previous discussions:
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
The text was updated successfully, but these errors were encountered: