Closed kgryte closed 1 year ago
Would we want a data type function for complex dtypes i.e. cinfo()
? Currently NumPy and PyTorch don't have a dedicate method, but complex dtypes can fallback on finfo()
.
Interestingly there was a little discussion of a torch.cinfo()
at https://github.com/pytorch/pytorch/issues/35954#issuecomment-620223025
- branch cut policy?
I don't know why I missed it... CuPy uses Thrust (and it's likely being used by many other projects that depends on CUDA; IIUC the implementation was ported to libcudacxx) so it's worth mentioning.
Thrust's complex math funcs were based on FreeBSD's. In each FreeBSD man page the branch cut is clearly documented so it's a good starting point. However, as Thrust didn't document it clearly (I asked internally and was told to inspect the source directly, which is challenging given my lack of bandwidth) and it's unclear to me if the Thrust impl was a one-to-one translation of FreeBSD's since I don't have access to the FreeBSD source code, I'd proceed with caution.
Tracking changes to existing APIs within the array API specification:
complex64
and complex128
At this point, PRs have been submitted for all existing APIs for which we currently expect complex number support.
All PRs have been merged - adding complex number support to the API is complete. Thanks a lot to everyone who contributed - and in particular @kgryte for all the spec writing.
Complex Number Support
What follows is a plan for adding complex number support to the 2022 array API specification. This RFC is comprised of the following sections:
Prior Discussions
General Concerns
New APIs
Decisions
initial data types:
complex64
andcomplex128
?complex64
andcomplex128
to be added, thus mirroringfloat32
andfloat64
.rounding complex numbers (component-wise)
round
.ordering (lexicographic?)
argsort
,argmax
,argmin
,min
,max
,sort
)branch cut policy?
casting from complex to real
real
before castingcomplex numbers with components which are infinity and/or NaN
in Python, a complex number can be both infinite and NaN (according to
cmath.isinf
,cmath.isnan
)in NumPy (v1.23.5), behavior is consistent with Python
in C99 (see "complex floating types" section), one infinity model (e.g.,
inf + nan*j
==inf
)update: decision was made to allow implementation-dependent behavior for modeling complex NaNs and infinities. In particular, for complex multiplication and division, handling of complex infinities and NaNs may not be consistent across implementations.
Creation Functions
arange
No changes. No complex number support.
NumPy issue: https://github.com/numpy/numpy/issues/10332
asarray
dtype
status: implemented
empty
empty_like
eye
No changes. Output array data type has not restrictions.
1 + 0j
.status: implemented
from_dlpack
full
fill_value
complex
fill value.dtype
complex
, output array data type must be the default complex floating-point data type.status: implemented
full_like
fill_value
complex
fill value.dtype
dtype
isNone
to includecomplex
.status: implemented
linspace
start
complex
stop
complex
when either
start
orstop
is complex, the result must be complex.status: implemented
meshgrid
update to accept all numeric dtypes.
status: implemented
ones
No changes necessary.
1 + 0j
.status: implemented
ones_like
No changes necessary.
1 + 0j
.status: implemented
tril
triu
zeros
zeros_like
No changes necessary. No restrictions on output array data types.
Data Type Functions
astype
Add note stating that, when casting from a complex data type to a real data type stating, the imaginary component is discarded and casting to integral data types is unspecified and thus implementation-dependent.real
if they want to discard the imaginary component.status: implemented
broadcast_arrays
broadcast_to
can_cast
finfo
iinfo
result_type
No changes necessary. Follows promotion rules for complex numbers.
Data Types
Add
complex64
andcomplex128
following precedent where the numeric suffix specifies the number of bits.status: implemented
Default Data Types
The default complex number data type is dependent on the default floating-point data type. If the latter is
float32
, the default complex data type must becomplex64
.status: implemented
Data Type Categories
Add complex data types to list of numeric data types.
Add "Real Data Types" category which should be equal to the current list of numeric data types.
Add "Complex Data Types" category which only includes the complex number data types.
status: implemented
Element-wise Functions
abs
Add support by returning the complex magnitude.
Add complex number special cases: same as
hypot(creal(z), cimag(z))
.status: implemented
acos
Add complex number support.
Define special cases.
[-inf,-1]
,[1,inf]
status: implemented
acosh
Add complex number support.
Define special cases.
[-inf,1]
status: implemented
add
Add complex number support. Complex number addition is well-defined.
status: implemented
asin
Add complex number support.
Define special cases.
status: implemented
asinh
Add complex number support.
Define special cases.
[-inf*i, -i]
,[i, inf*i]
status: implemented
atan
Add complex number support.
Define special cases.
[-inf*i, -i]
,[i, inf*i]
status: implemented
atan2
atanh
Add complex number support.
Define special cases.
[-inf, -1]
,[1,inf]
status: implemented
bitwise_and
bitwise_left_shift
bitwise_invert
bitwise_or
bitwise_right_shift
bitwise_xor
ceil
Add complex number support. Independently round components (e.g., as in MATLAB).cos
Add complex number support.
Define special cases.
status: implemented
cosh
Add complex number support.
Define special cases.
status: implemented
divide
Add complex number support.
Define special cases.
status: implemented
equal
Add complex number support.
status: implemented
exp
Add complex number support.
Define special cases.
status: implemented
expm1
Add complex number support.
Define special cases.
exp
status: implemented
floor
Add complex number support. Independently round components (e.g., as in MATLAB).floor_divide
greater
No complex number support due to complex numbers lacking a natural ordering.
status: implemented
greater_equal
No complex number support due to complex numbers lacking a natural ordering.
status: implemented
isfinite
Add support for complex numbers.
status: implemented
isinf
Add support for complex numbers.
status: implemented
isnan
Add support for complex numbers.
NaN
, the complex number is considered "NaN"status: implemented
less
No complex number support due to complex numbers lacking a natural ordering.
status: implemented
less_equal
No complex number support due to complex numbers lacking a natural ordering.
status: implemented
log
Add complex number support.
Define special cases.
[-inf, 0]
status: implemented
log1p
Add complex number support.
Define special cases.
log
status: implemented
log2
Add complex number support.
Define special cases.
log
status: implemented
log10
Add complex number support.
Define special cases.
log
status: implemented
logaddexp
logical_and
logical_not
logical_or
logical_xor
multiply
Add complex number support. Complex number multiplication is well-defined.
Define special cases.
status: implemented
negative
Add complex number support. No real changes necessary, as relies on
multiply
.status: implemented
not_equal
Add complex number support.
status: implemented
positive
Add complex number support. No changes necessary, as this is effectively the identity function.
status: implemented
pow
Add complex number support.
Define special cases.
status: implemented
remainder
round
Add complex number support.
status: implemented
sign
Add complex number support.
Define special cases.
NumPy: https://numpy.org/doc/stable/reference/generated/numpy.sign.html
x / sqrt(x*x)
; could also usex/|x|
Update: consensus is to use
x/|x|
.status: implemented
sin
Add complex number support.
Define special cases. No branch cuts.
status: implemented
sinh
Add complex number support.
Define special cases.
status: implemented
square
Add complex number support. Complex number multiplication is well-defined.
abs2
: https://github.com/data-apis/array-api/issues/153#issuecomment-896339259status: implemented
sqrt
Add complex number support.
Define special cases.
[-inf,0)
status: implemented
subtract
Add complex number support. Complex number addition is well-defined.
status: implemented
tan
Add complex number support.
Define special cases.
status: implemented
tanh
Add complex number support.
Define special cases.
status: implemented
trunc
Add complex number support. Independently truncate components (e.g., as in MATLAB).Linear Algebra Functions
matmul
Add complex number support.
status: implemented
matrix_transpose
tensordot
Add complex number support.
status: implemented
vecdot
Implementations should conjugate when computing the complex dot product:
x^H y
status: implemented
Manipulation Functions
concat
expand_dims
flip
permute_dims
reshape
roll
squeeze
stack
No changes necessary.
Searching Functions
argmax
Require that complex numbers be ordered in lexicographic order?
status: implemented
argmin
No complex number support due to complex numbers lacking a natural ordering.
status: implemented
nonzero
No changes necessary. A nonzero complex number is a complex number whose real or imaginary component is nonzero.
status: implemented
where
No changes necessary.
Set Functions
unique_all
Specify complex number value equality.
equal
.status: implemented
unique_counts
Same as
unique_all
.status: implemented
unique_inverse
Same as
unique_all
.status: implemented
unique_values
Same as
unique_all
.status: implemented
Sorting Functions
argsort
No complex number support due to complex numbers lacking a natural ordering.
status: implemented
sort
No complex number support due to complex numbers lacking a natural ordering.
status: implemented
Statistical Functions
max
No complex number support due to complex numbers lacking a natural ordering.
status: implemented
mean
min
No complex number support due to complex numbers lacking a natural ordering.
status: implemented
prod
Add support for complex numbers. Complex number multiplication is well-defined.
dtype
option needs to be updated to accommodate complex numbers.status: implemented
std
sum
Add support for complex numbers. Complex number addition is well-defined.
dtype
option needs to be updated to accommodate complex numbers.status: implemented
var
No complex number support.
Change to only real number data types.
Type Promotion
Rules
Add complex number type promotion table.
Related complex number type promotion to floating-point number type promotion (e.g.,
f8
andc16
)status: implemented
Mixing arrays with Python Scalars
Add
complex
Python scalars.Update note concerning mixed "kind" operations.
status: implemented
Utility Functions
all
No changes necessary. The value
0+0j
should evaluate toFalse
. If a complex number has a non-zero component, the value should evaluate toTrue
.status: implemented
any
Same as
all
.status: implemented
Linear Algebra Extension
cholesky
Add complex number support.
Require that each square matrix must be Hermitian.
status: implemented
cross
Add complex number support.
status: implemented
det (determinant)
Add complex number support.
status: implemented
diagonal
eigh
Add complex number support.
status: implemented
eigvalsh
Add complex number support.
status: implemented
inv
Add complex number support.
status: implemented
matmul
matrix_norm
Add complex number support.
Depends on svd.
status: implemented
matrix_power
Add complex number support.
status: implemented
matrix_rank
Add complex number support.
Depends on svd.
status: implemented
matrix_transpose
outer
Add complex number support.
status: implemented
pinv
Add complex number support.
Depends on svd.
status: implemented
qr
Add complex number support.
status: implemented
slogdet
Add complex number support.
status: implemented
solve
Add complex number support.
status: implemented
svd
Add complex number support.
S
is unconditionally real.status: implemented
svdvals
Add complex number support.
status: implemented
trace
Add complex number support.
status: implemented
vecdot
vector_norm
Add complex number support.
status: implemented