Module array_api_standard

Source
Expand description

§Array API standard Fullfillment

For column status:

  • Y: Implemented in rstsr (may not be fully functional like numpy or Python array API specification, but should be enough);
  • C: Changed feature (breaking using experience from numpy);
  • P: Partial implemented in rstsr (not all features in Python array API is implemented);
  • D: Features that would be dropped in rstsr.
  • blank: Will be implemented in future version of RSTSR.

§Operators

§Arithmetic Operators

statusimplementationPython APIdescription
D__pos__+x
Y-__neg__-x
Y+__add__x1 + x2
Y-__sub__x1 - x2
Y*__mul__x1 * x2
Y/__truediv__x1 / x2
Yfloor_divide__floordiv__x1 // x2
Crt::rem1__mod__x1 % x2
Ypow__pow__x1 ** x2
Y+=__iadd__x1 += x2
Y-=__isub__x1 -= x2
Y*=__imul__x1 *= x2
Y/=__itruediv__x1 /= x2
D__ifloordiv__x1 //= x2
D__ipow__x1 **= x2
Y%=__imod__x1 %= x2

Changed feature

  • __mod__: We do not use remainder function to represent something like 8 % 3 = 2, but instead using notation % to represent matrix multiplication (@ in python/numpy).

Dropped support

  • __pos__: In rust, leading + is not allowed.
  • __ifloordiv__: This is not a priority for implementation.
  • __ipow__: This is not a priority for implementation.

§Array Operators

statusimplementationPython APIdescription
C%, matmul()__matmul__x1 @ x2
D__imatmul__x1 @= x2

Changed feature

  • __matmul__: In rust, there was discussions whether to implement @ as matrix multiplication (or other operator notations, since @ has been used in binary operation for pattern matching). Instead we use notation % to represent matrix multiplication (@ in python/numpy). See __rem__ function for more information.

Dropped support

  • __imatmul__: Inplace matmul is not convenient to be realized.

§Bitwise Operators

statusimplementationPython APIdescription
Y! Not__invert__~x
Y& BitAnd__and__x1 & x2
Y| BitOr__or__x1 | x2
Y^ BitXor__xor__x1 ^ x2
Y<< Shl__lshift__x1 << x2
Y>> Shr__rshift__x1 >> x2
Y&= BitAndAssign__iand__x1 &= x2
Y|= BitOrAssign__ior__x1 |= x2
Y^= BitXorAssign__ixor__x1 ^= x2
Y<<= ShlAssign__ilshift__x1 <<= x2
Y>>= ShrAssign__irshift__x1 >>= x2

§Comparsion Operators

statusimplementationPython APIdescription
Ylt, less__lt__x1 < x2
Yle, less_equal__le__x1 <= x2
Ygt, greater__gt__x1 > x2
Yge, greater_equal__ge__x1 >= x2
Yeq, equal__eq__x1 == x2
Yne, not_equal__ne__x1 != x2

§Array Object Attributes

statusimplementationPython APIdescription
CT of TensorAny<R, T, B, D>dtypeData type of the array elements.
CB of TensorAny<R, T, B, D>deviceHardware device the array data resides on.
YTensorBase::swapaxes(-1, -2)mTTranspose of a matrix (or a stack of matrices).
YTensorBase::ndimndimNumber of array dimensions (axes).
YTensorBase::shapeshapeArray dimensions.
YTensorBase::sizesizeNumber of elements in an array.
Ytranspose, TensorBase::tTTranspose of the array.

§Methods

statusimplementationPython APIdescription
Pabs__abs__Calculates the absolute value for each element of an array instance.
Yasarray__bool__Converts a zero-dimensional array to a Python bool object.
Yasarray__complex__Converts a zero-dimensional array to a Python complex object.
Yasarray__float__Converts a zero-dimensional array to a Python float object.
YIndex__getitem__Returns self[key].
YTensorBase::to_scalar__index__Converts a zero-dimensional integer array to a Python int object.
Yasarray__int__Converts a zero-dimensional array to a Python int object.
YIndexMut__setitem__Sets self[key] to value.
PDeviceChangeAPI::to_deviceto_deviceCopy the array from the device on which it currently resides to the specified device.

Partial Implemented

  • Only ComplexFloat types implemented abs. Integer types has not been implemented for absolute value.

§Constants

statusimplementationPython APIdescription
Ycore::f64::consts::EeIEEE 754 floating-point representation of Euler’s constant.
Yf64::INFINITYinfIEEE 754 floating-point representation of (positive) infinity.
Yf64::NANnanIEEE 754 floating-point representation of Not a Number (NaN).
YIndexer::InsertnewaxisAn alias for None which is useful for indexing arrays.
Ycore::f64::consts::PIpiIEEE 754 floating-point representation of the mathematical constant π.

§Creation Functions

statusimplementationPython APIdescription
YarangearangeReturns evenly spaced values within the half-open interval [start, stop) as a one-dimensional array.
PasarrayasarrayConvert the input to an array.
YemptyemptyReturns an uninitialized array having a specified shape.
Yempty_likeempty_likeReturns an uninitialized array with the same shape as an input array x.
YeyeeyeReturns a two-dimensional array with ones on the kth diagonal and zeros elsewhere.
YfullfullReturns a new array having a specified shape and filled with fill_value.
Yfull_likefull_likeReturns a new array filled with fill_value and having the same shape as an input array x.
YlinspacelinspaceReturns evenly spaced numbers over a specified interval.
meshgridReturns coordinate matrices from coordinate vectors.
YonesonesReturns a new array having a specified shape and filled with ones.
Yones_likeones_likeReturns a new array filled with ones and having the same shape as an input array x.
trilReturns the lower triangular part of a matrix (or a stack of matrices) x.
triuReturns the upper triangular part of a matrix (or a stack of matrices) x.
YzeroszerosReturns a new array having a specified shape and filled with zeros.
Yzeros_likezeros_likeReturns a new array filled with zeros and having the same shape as an input array x.

Partial implementation

  • asarray: This function have different implementations for Vec<T>, [T; N] and Tensor<T, B, D>. Different signatures are utilized for different inputs and purposes.

§Data Type

§Data Type Functions

statusimplementationPython APIdescription
Tnum crateastypeCopies an array to a specified data type irrespective of Type Promotion Rules rules.
Tnum cratecan_castDetermines if one data type can be cast to another data type according Type Promotion Rules rules.
Tnum cratefinfoMachine limits for floating-point data types.
Tnum crateiinfoMachine limits for integer data types.
Tcore::any::type_nameisdtypeReturns a boolean indicating whether a provided dtype is of a specified data type “kind”.
Tnum crateresult_typeReturns the dtype that results from applying the type promotion rules (see Type Promotion Rules) to the arguments.

For this part, we refer to num crate, where data type conversion and promotion are regularized by trait bounds.

§Data Type Categories

rust trait or structdata type categorydtypes
num::NumNumericint8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64, complex64, complex128
Real-valuedint8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64
num::IntegerIntegerint8, int16, int32, int64, uint8, uint16, uint32, uint64
num::complex::ComplexFloatFloating-pointfloat32, float64, complex64, complex128
num::FloatReal-valued floating-pointfloat32, float64
num::ComplexComplex floating-pointcomplex64, complex128
boolBooleanbool

§Element-wise Functions

§Unary Functions

statusimplementationPython APIdescription
AbsAPIabsabsCalculates the absolute value for each element x_i of the input array x.
ComplexFloatacosacosCalculates an implementation-dependent approximation of the principal value of the inverse cosine for each element x_i of the input array x.
ComplexFloatacoshacoshCalculates an implementation-dependent approximation to the inverse hyperbolic cosine for each element x_i of the input array x.
ComplexFloatasinasinCalculates an implementation-dependent approximation of the principal value of the inverse sine for each element x_i of the input array x.
ComplexFloatasinhasinhCalculates an implementation-dependent approximation to the inverse hyperbolic sine for each element x_i in the input array x.
ComplexFloatatanatanCalculates an implementation-dependent approximation of the principal value of the inverse tangent for each element x_i of the input array x.
ComplexFloatatanhatanhCalculates an implementation-dependent approximation to the inverse hyperbolic tangent for each element x_i of the input array x.
Not!, notbitwise_invertInverts (flips) each bit for each element x_i of the input array x.
FloatceilceilRounds each element x_i of the input array x to the smallest (i.e., closest to -infinity) integer-valued number that is not less than x_i.
ComplexFloatconjconjReturns the complex conjugate for each element x_i of the input array x.
ComplexFloatcoscosCalculates an implementation-dependent approximation to the cosine for each element x_i of the input array x.
ComplexFloatcoshcoshCalculates an implementation-dependent approximation to the hyperbolic cosine for each element x_i in the input array x.
ComplexFloatexpexpCalculates an implementation-dependent approximation to the exponential function for each element x_i of the input array x (e raised to the power of x_i, where e is the base of the natural logarithm).
Float (partial)expm1expm1Calculates an implementation-dependent approximation to exp(x)-1 for each element x_i of the input array x.
FloatfloorfloorRounds each element x_i of the input array x to the greatest (i.e., closest to +infinity) integer-valued number that is not greater than x_i.
ReImAPIimagimagReturns the imaginary component of a complex number for each element x_i of the input array x.
ComplexFloatis_finiteisfiniteTests each element x_i of the input array x to determine if finite.
ComplexFloatis_infisinfTests each element x_i of the input array x to determine if equal to positive or negative infinity.
ComplexFloatis_nanisnanTests each element x_i of the input array x to determine whether the element is NaN.
ComplexFloatloglogCalculates an implementation-dependent approximation to the natural (base e) logarithm for each element x_i of the input array x.
Not implementedlog1pCalculates an implementation-dependent approximation to log(1+x), where log refers to the natural (base e) logarithm, for each element x_i of the input array x.
ComplexFloatlog2log2Calculates an implementation-dependent approximation to the base 2 logarithm for each element x_i of the input array x.
ComplexFloatlog10log10Calculates an implementation-dependent approximation to the base 10 logarithm for each element x_i of the input array x.
Notnot insteadlogical_notComputes the logical NOT for each element x_i of the input array x.
Neg-, negnegativeComputes the numerical negative of each element x_i (i.e., y_i = -x_i) of the input array x.
DroppedpositiveComputes the numerical positive of each element x_i (i.e., y_i = +x_i) of the input array x.
ReImAPIrealrealReturns the real component of a complex number for each element x_i of the input array x.
FloatroundroundRounds each element x_i of the input array x to the nearest integer-valued number.
ComplexFloatsignsignReturns an indication of the sign of a number for each element x_i of the input array x.
SignedsignbitsignbitDetermines whether the sign bit is set for each element x_i of the input array x.
ComplexFloatsinsinCalculates an implementation-dependent approximation to the sine for each element x_i of the input array x.
ComplexFloatsinhsinhCalculates an implementation-dependent approximation to the hyperbolic sine for each element x_i of the input array x.
NumsquaresquareSquares each element x_i of the input array x.
ComplexFloatsqrtsqrtCalculates the principal square root for each element x_i of the input array x.
ComplexFloattantanCalculates an implementation-dependent approximation to the tangent for each element x_i of the input array x.
ComplexFloattanhtanhCalculates an implementation-dependent approximation to the hyperbolic tangent for each element x_i of the input array x.
FloattrunctruncRounds each element x_i of the input array x to the nearest integer-valued number that is closer to zero than x_i.

§Binary Functions

statusimplementationPython APIdescription
Add+, addaddCalculates the sum for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
Floatatan2atan2Calculates an implementation-dependent approximation of the inverse tangent of the quotient x1/x2, having domain [-infinity, +infinity] x [-infinity, +infinity] (where the x notation denotes the set of ordered pairs of elements (x1_i, x2_i)) and codomain [-π, +π], for each pair of elements (x1_i, x2_i) of the input arrays x1 and x2, respectively.
BitAnd&, bitandbitwise_andComputes the bitwise AND of the underlying binary representation of each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
Shl<<, shlbitwise_left_shiftShifts the bits of each element x1_i of the input array x1 to the left by appending x2_i (i.e., the respective element in the input array x2) zeros to the right of x1_i.
BitOr|, bitorbitwise_orComputes the bitwise OR of the underlying binary representation of each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
Shr>>, shrbitwise_right_shiftShifts the bits of each element x1_i of the input array x1 to the right according to the respective element x2_i of the input array x2.
BitXor^, bitxorbitwise_xorComputes the bitwise XOR of the underlying binary representation of each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
FloatcopysigncopysignComposes a floating-point value with the magnitude of x1_i and the sign of x2_i for each element of the input array x1.
Div/, divdivideCalculates the division of each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
PartialEqeq, equalequalComputes the truth value of x1_i == x2_i for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
FloorDivideAPIfloor_dividefloor_divideRounds the result of dividing each element x1_i of the input array x1 by the respective element x2_i of the input array x2 to the greatest (i.e., closest to +infinity) integer-value number that is not greater than the division result.
PartialOrdgt, greatergreaterComputes the truth value of x1_i > x2_i for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
PartialOrdge, greater_equalgreater_equalComputes the truth value of x1_i >= x2_i for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
FloathypothypotComputes the square root of the sum of squares for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
PartialOrdlt, lesslessComputes the truth value of x1_i < x2_i for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
PartialOrdle, less_equalless_equalComputes the truth value of x1_i <= x2_i for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
ComplexFloatlog_add_explogaddexpCalculates the logarithm of the sum of exponentiations log(exp(x1) + exp(x2)) for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
bitand insteadlogical_andComputes the logical AND for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
bitor insteadlogical_orComputes the logical OR for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
bitxor insteadlogical_xorComputes the logical XOR for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
MinMaxAPImaximummaximumComputes the maximum value for each element x1_i of the input array x1 relative to the respective element x2_i of the input array x2.
MinMaxAPIminimumminimumComputes the minimum value for each element x1_i of the input array x1 relative to the respective element x2_i of the input array x2.
MulmulmultiplyCalculates the product for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
PartialEqne, not_equalnot_equalComputes the truth value of x1_i != x2_i for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.
PowpowpowCalculates an implementation-dependent approximation of exponentiation by raising each element x1_i (the base) of the input array x1 to the power of x2_i (the exponent), where x2_i is the corresponding element of the input array x2.
Remrt::rem1remainderReturns the remainder of division for each element x1_i of the input array x1 and the respective element x2_i of the input array x2.
Sub-, subsubtractCalculates the difference for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.

§Other functions

statusimplementationPython APIdescription
clipClamps each element x_i of the input array x to the range [min, max].

§Indexing Functions

statusimplementationPython APIdescription
takeReturns elements of an array along an axis.

§Inspection

statusimplementationPython APIdescription
Drequires reflectioncapabilitiesReturns a dictionary of array library capabilities.
FixedDeviceCpudefault_deviceReturns the default device.
Dcontroled by rustdefault_dtypesReturns a dictionary containing default data types.
YTensorBase::devicedevicesReturns a list of supported devices which are available at runtime.
Ycore::any::type_name_of_valdtypesReturns a dictionary of supported Array API data types.

§Linear Algebra Functions

statusimplementationPython APIdescription
Ymatmul()matmulComputes the matrix product.
Yswapaxes(-1, -2)matrix_transposeTransposes a matrix (or a stack of matrices) x.
tensordotReturns a tensor contraction of x1 and x2 over specific axes.
vecdotComputes the (vector) dot product of two arrays.

§Manipulation Functions

statusimplementationPython APIdescription
Ybroadcast_arraysbroadcast_arraysBroadcasts one or more arrays against one another.
Yto_broadcastbroadcast_toBroadcasts an array to a specified shape.
concatJoins a sequence of arrays along an existing axis.
Yexpand_dimsexpand_dimsExpands the shape of an array by inserting a new axis (dimension) of size one at the position specified by axis.
YflipflipReverses the order of elements in an array along the given axis.
moveaxisMoves array axes (dimensions) to new positions, while leaving other axes in their original positions.
Ytranspose, permute_dimspermute_dimsPermutes the axes (dimensions) of an array x.
repeatRepeats each element of an array a specified number of times on a per-element basis.
Preshape, into_shape_assume_contigreshapeReshapes an array without changing its data.
rollRolls array elements along a specified axis.
PsqueezesqueezeRemoves singleton dimensions (axes) from x.
stackJoins a sequence of arrays along a new axis.
tileConstructs an array by tiling an input array.
unstackSplits an array into a sequence of arrays along the given axis.

Partial implementation

  • squeeze accepts one axis as input, instead of accepting multiple axes. This is mostly because output of smaller dimension tensor can be fixed-dimension array ([DimSmallerOneAPI::SmallerOne]) when only one axis is passed as argument.
  • reshape: Currently reshape is work-in-progress. It does not copy array when c-contiguous. For numpy, much more cases may not invoke explicit copy when reshape.

§Searching Functions

statusimplementationPython APIdescription
argmaxReturns the indices of the maximum values along a specified axis.
argminReturns the indices of the minimum values along a specified axis.
nonzeroReturns the indices of the array elements which are non-zero.
searchsortedFinds the indices into x1 such that, if the corresponding elements in x2 were inserted before the indices, the order of x1, when sorted in ascending order, would be preserved.
whereReturns elements chosen from x1 or x2 depending on condition.

§Set Functions

statusimplementationPython APIdescription
unique_allReturns the unique elements of an input array x, the first occurring indices for each unique element in x, the indices from the set of unique elements that reconstruct x, and the corresponding counts for each unique element in x.
unique_countsReturns the unique elements of an input array x and the corresponding counts for each unique element in x.
unique_inverseReturns the unique elements of an input array x and the indices from the set of unique elements that reconstruct x.
unique_valuesReturns the unique elements of an input array x.

§Sorting Functions

statusimplementationPython APIdescription
argsortReturns the indices that sort an array x along a specified axis.
sortReturns a sorted copy of an input array x.

§Statistical Functions

statusimplementationPython APIdescription
cumulative_sumCalculates the cumulative sum of elements in the input array x.
YmaxmaxCalculates the maximum value of the input array x.
YmeanmeanCalculates the arithmetic mean of the input array x.
YminminCalculates the minimum value of the input array x.
YprodprodCalculates the product of input array x elements.
YstdstdCalculates the standard deviation of the input array x.
YsumsumCalculates the sum of the input array x.
YvarvarCalculates the variance of the input array x.

§Utility Functions

statusimplementationPython APIdescription
allTests whether all input array elements evaluate to True along a specified axis.
anyTests whether any input array element evaluates to True along a specified axis.

§Other Dropped Specifications

We decide to drop some supports in Python Array API:

  • Reflected (swapped) operands. A typical function in python is __radd__. Reflected operands are not easy to be implemented in rust. I believe that in python, this is realized by checking dynamic object type; this is not friendly to language that requires compilation.
  • Functions related to Python Array API namespace and dlpack. These routines are mostly for forcing other python packages to be compatible to Python Array API. This is not possible for another language currently.

  1. To use remainder (modular) function correctly, one may use rt::rem (as function), instead of using x1 % x2 (as operator) or x1.rem(x2) (as associated method). In this crate, the latter two will call matmul()