The binary operations a backend has to provide. They are named here rather than passed as closures so that a backend that cannot call back into MoonBit — a shader, say — can still run them.
API reference
Everything public, in one place.
Generated from the three package interfaces and the doc comment beside each declaration. Use the filter to narrow by name or by a word in the description.
[0]
moonvec
The array type and everything that works element by element: dtypes, creation, indexing and views, broadcasting, universal functions, reductions, joining, sorting and matrix products.
Types
A flat block of elements of one dtype, which views of an array share. The bytes are little endian on every target, so a buffer written on one machine reads back the same on another and a file of vectors can carry raw values. Storage is a byte array rather than a typed one because one code path has to hold every dtype; the accessors below are the only place that knows the widths.
The element type this buffer stores.
Sets every element to value.
| name | type | |
|---|---|---|
| self | Self | the receiver |
| — | Number |
Wraps bytes that already hold elements; the length comes from the byte count, so a partial element is an error rather than a silently dropped tail.
| name | type | |
|---|---|---|
| — | Bytes | |
| dtype | DType | labelled |
How many elements the buffer holds.
Allocates len elements of dtype, zeroed.
| name | type | |
|---|---|---|
| dtype | DType | labelled |
| len | Int32 | labelled |
The element at index, widened to the class its dtype belongs to.
| name | type | |
|---|---|---|
| self | Self | the receiver |
| — | Int32 |
A copy of the bytes, for handing to another library or writing to a file.
Stores value at index, converted the way C converts it to the buffer's dtype.
| name | type | |
|---|---|---|
| self | Self | the receiver |
| — | Int32 | |
| — | Number |
A complex number, held as a pair of doubles. complex64 arrays store the two parts as single precision floats; the value type always widens to doubles so that arithmetic in a loop does not lose the bits the storage kept.
Operator forms:
- Add for Complex
- Div for Complex
- Eq for Complex
- Mul for Complex
- Neg for Complex
- Show for Complex
- Sub for Complex
- @debug.Debug for Complex
The modulus, computed as a hypotenuse so that large components do not overflow.
The angle in radians, in (-pi, pi], the argument atan2 returns.
The conjugate: the imaginary part negated.
The cosine, extended to the complex plane.
The hyperbolic cosine, extended to the complex plane.
e raised to this value, by Euler's formula.
True when either part is non-zero.
The principal logarithm: ln|z| + i arg z.
The additive inverse.
A complex number from its real and imaginary parts.
| name | type | |
|---|---|---|
| — | Double | |
| — | Double |
self raised to exponent, through exp(exponent * log self).
| name | type | |
|---|---|---|
| self | Self | the receiver |
| self | Self | the receiver |
The sine, extended to the complex plane.
The hyperbolic sine, extended to the complex plane.
The principal square root, on the branch where the result's real part is non-negative.
The tangent, extended to the complex plane.
The hyperbolic tangent, extended to the complex plane.
The only backend there is for now: plain MoonBit, with a vector path where the elements are doubles or floats and the operation is one of the four arithmetic ones.
The default backend, sized for the vector registers the build target provides.
The element type of an array. The set a numerical array needs: one boolean type, the signed and unsigned integers, the half, single and double precision floats, and the two complex widths. There is no platform integer type, because an array's dtype has to mean the same thing on every target.
Operator forms:
- Eq for DType
- Show for DType
- @debug.Debug for DType
The dtype that holds every value of self without loss, the smallest such float being F64.
True for the boolean dtype.
True for the two complex dtypes.
True for the dtypes an operation on integers keeps exact: integers and bool.
True for F16, F32 and F64; the complex dtypes are not floats.
True for the signed and unsigned integer dtypes, false for bool.
True for anything whose values are stored as a real number: floats and complex.
True for the four signed integer dtypes.
The number of bytes one element takes in a buffer.
The name this type is written as, lower case and without spaces.
Reads a dtype back from its name, as written by to_string or a vector file.
| name | type | |
|---|---|---|
| — | StringView |
The float dtype that holds this one at the same width, for operations that leave the integer domain: I8 gives F16, I32 gives F64. Complex dtypes are returned unchanged.
One axis selector in a slice, read left to right, one per axis.
An N-dimensional array: a view over a flat buffer, described by a shape and a stride per axis. The strides are counted in elements, not bytes, and follow C order: the last axis varies fastest. Any axis may have stride 0, which is how a size-one axis broadcasts without copying its data, and any axis may be negative, which is how a reversed view is made. Two arrays made by slicing the same array share one buffer, so a write through one is visible through the other.
Operator forms:
- Add for NDArray
- Div for NDArray
- Eq for NDArray
- Mod for NDArray
- Mul for NDArray
- Neg for NDArray
- Show for NDArray
- Sub for NDArray
- @debug.Debug for NDArray
The element at index, one integer per axis.
| name | type | |
|---|---|---|
| self | Self | the receiver |
| — | Array[Int32] |
The buffer this view reads, shared with every other view of the same data.
A contiguous copy, whatever the layout of this view.
The element type of the buffer.
True when the elements sit next to each other in buffer order, so a backend can take the whole array as one run.
True for a 0-dimensional array, the result of a full reduction.
The one value of a 0-dimensional array.
How many axes the shape has.
Builds a view over buffer; the strides default to the contiguous layout of shape.
| name | type | |
|---|---|---|
| buffer | Buffer | labelled |
| shape | Array[Int32] | labelled |
| strides | Array[Int32] | optional |
| offset | Int32 | optional |
The index in the buffer of the first element, after any slicing.
Writes one element at index.
| name | type | |
|---|---|---|
| self | Self | the receiver |
| — | Array[Int32] | |
| — | Number |
The size of each axis; a copy, so changing it cannot break the view.
How many elements the shape holds, counting a stretched axis once per position.
The element distance between neighbours along each axis; a copy.
Every element as a double, in row-major order. Complex values lose their imaginary part, as any conversion to a real type does.
Every element in row-major order, as a fresh array.
The array laid out one row per line, with the dtype after the values.
One element of an array, widened to the type that holds its whole range. The variants name the class the value belongs to rather than one variant per dtype: reading an int16 gives Signed too, and writing it back checks the width. This keeps a kernel written once per operation instead of once per dtype.
Operator forms:
- Eq for Number
- Show for Number
- @debug.Debug for Number
The same value, stored as dtype would store it: integers wrap to their width, floats round.
| name | type | |
|---|---|---|
| self | Self | the receiver |
| — | DType |
Compares two values by magnitude only; complex pairs compare by real part then imaginary part, which is the order values sort in.
| name | type | |
|---|---|---|
| self | Self | the receiver |
| self | Self | the receiver |
True when the value is not zero; a complex number is true when either part is.
The value as a complex number, with a zero imaginary part.
The value as a double; an integer above 2^53 loses its low bits.
The value truncated toward zero, the rule C uses when a float is stored into an integer.
The value as the array printer writes it.
A loop schedule over a shape: the axes that vary between runs, and the length of the innermost run that stays contiguous for every operand. Adjacent axes are merged when every operand walks them at a constant step, which turns a contiguous pair of arrays into a single tight loop instead of one call per element. Each run hands the loop body the buffer offsets of all operands at the start of the run.
The reductions a backend has to provide, as an associative pair of values.
The unary operations a backend has to provide.
Functions
The magnitude; a complex array answers in its real dtype.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Elementwise sum; the two shapes broadcast.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Adds a scalar to every element, keeping the array's dtype; 1 added to an int8 array stays int8, because a scalar does not widen the array.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | Double | |
| kernel | &Kernel | optional |
True where every element is true; a full reduction over an empty array is true.
| name | type | |
|---|---|---|
| — | NDArray | |
| axis | Int32 | optional |
| keepdims | Bool | optional |
| kernel | &Kernel | optional |
The angle of each complex value in radians; a real array is 0 where positive and pi where negative.
| name | type | |
|---|---|---|
| — | NDArray |
True where any element is true.
| name | type | |
|---|---|---|
| — | NDArray | |
| axis | Int32 | optional |
| keepdims | Bool | optional |
| kernel | &Kernel | optional |
Applies a binary operation to two values already in dtype. The result may have a wider class than dtype — a comparison gives a boolean, absolute value of a complex gives a real — and the caller's output buffer decides what it is stored as.
| name | type | |
|---|---|---|
| — | BinaryOp | |
| — | DType | |
| — | Number | |
| — | Number |
Combines two values in a reduction; the pair must be associative and have the identity the caller started from.
| name | type | |
|---|---|---|
| — | ReduceOp | |
| — | DType | |
| — | Number | |
| — | Number |
Applies a unary operation to a value already cast to dtype.
| name | type | |
|---|---|---|
| — | UnaryOp | |
| — | DType | |
| — | Number |
Evenly spaced values in [start, stop), stepping by step. The count is computed from the span and the step, so arange(0, 1, 0.3) stops at 0.9 rather than at 1.2. A step whose sign does not lead towards stop gives an empty array instead of looping forever.
| name | type | |
|---|---|---|
| start | Double | labelled |
| stop | Double | labelled |
| step | Double | optional |
| dtype | DType | optional |
Inverse cosine of each element, in radians; outside [-1, 1] it is NaN.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Inverse hyperbolic cosine of each element; below one it is NaN.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Inverse sine of each element, in radians; outside [-1, 1] it is NaN.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Inverse hyperbolic sine of each element.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Inverse tangent of each element, in radians.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Elementwise arctangent of a / b, choosing the quadrant from both signs.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Inverse hyperbolic tangent of each element; outside (-1, 1) it is NaN.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
The index of the largest element along the axis; the first one wins a tie.
| name | type | |
|---|---|---|
| — | NDArray | |
| axis | Int32 | optional |
| keepdims | Bool | optional |
The index of the smallest element along the axis; the first one wins a tie.
| name | type | |
|---|---|---|
| — | NDArray | |
| axis | Int32 | optional |
| keepdims | Bool | optional |
The positions that would sort each run along the axis, as int64; stable for equal values.
| name | type | |
|---|---|---|
| — | NDArray | |
| axis | Int32 | optional |
A 1-D array from a list of values.
| name | type | |
|---|---|---|
| — | Array[Double] | |
| dtype | DType | optional |
A copy of the array with every element converted to dtype. The conversion is the one C makes: an integer that does not fit wraps, a float is truncated towards zero when it becomes an integer, and a NaN becomes zero, because there is no integer it could honestly become.
| name | type | |
|---|---|---|
| — | NDArray | |
| dtype | DType | labelled |
The modified Bessel function of the first kind of order order.
| name | type | |
|---|---|---|
| order | Double | labelled |
| — | NDArray |
The Bessel function of the first kind of order order.
| name | type | |
|---|---|---|
| order | Double | labelled |
| — | NDArray |
The modified Bessel function of the second kind of order order.
| name | type | |
|---|---|---|
| order | Double | labelled |
| — | NDArray |
The Bessel function of the second kind of order order.
| name | type | |
|---|---|---|
| order | Double | labelled |
| — | NDArray |
The beta function, elementwise over two broadcast operands.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray |
Elementwise bitwise and; both operands must be integers.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Elementwise bitwise or; both operands are read as integers.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Elementwise bitwise exclusive or; both operands are read as integers.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
The shape an elementwise operation of these two arrays has, without doing the work.
| name | type | |
|---|---|---|
| — | Array[Int32] | |
| — | Array[Int32] |
The shape two arrays take when an elementwise operation meets them. The axes are aligned from the right; an axis missing on one side counts as 1, and an axis of 1 stretches to the other side's length. Any other pair of different lengths is an error, because there is no honest way to pair those elements up.
| name | type | |
|---|---|---|
| — | Array[Int32] | |
| — | Array[Int32] |
The strides an operand of this shape takes when it is broadcast to out_shape: an axis it does not have, or has with length 1, gets stride 0 so every element along that axis reads the same.
| name | type | |
|---|---|---|
| — | Array[Int32] | |
| — | Array[Int32] | |
| — | Array[Int32] |
The same array seen with a larger shape, without copying, by giving stretched axes stride 0. The new shape must broadcast up to shape, so a size-one axis may grow and a leading axis may be added; anything else cannot be expressed as a view and is refused rather than copied silently.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | Array[Int32] |
Elementwise cube root, defined for negative inputs too: cbrt(-8) is -2.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Rounds up.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Clamps every element into [min, max]; either bound may be left out, and the bounds broadcast like any other operand.
| name | type | |
|---|---|---|
| — | NDArray | |
| min | Double | optional |
| max | Double | optional |
| kernel | &Kernel | optional |
Orders two doubles with NaN last, so a sort puts the values that cannot be compared at the end instead of scattering them.
| name | type | |
|---|---|---|
| — | Double | |
| — | Double |
A complex value as a Number, spelling out both types so the variant and the struct do not shadow each other.
| name | type | |
|---|---|---|
| — | Double | |
| — | Double |
Joins arrays along an existing axis; the other axes must agree in length, and the dtypes promote.
| name | type | |
|---|---|---|
| — | Array[NDArray] | |
| axis | Int32 | optional |
Elementwise conjugate; a real array is returned as it is.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Cosine of each element, in radians.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Hyperbolic cosine of each element.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Converts radians to degrees.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
The logarithmic derivative of gamma.
| name | type | |
|---|---|---|
| — | NDArray |
True division: two integer arrays give float64, so 1 / 2 is 0.5 rather than 0.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Divides by a scalar; an integer array gives float64, as with divide.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | Double | |
| kernel | &Kernel | optional |
The dtype the true division of these two dtypes produces: integers turn into float64.
| name | type | |
|---|---|---|
| — | DType | |
| — | DType |
The dot product of two arrays: a's last axis against b's second-to-last, with the axes that are left over kept in order. For 1-D and 2-D operands this is matmul. For higher ranks it differs: a stack times a matrix contracts every row of the stack against the same matrix.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Elementwise a == b, answering with a boolean array.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Elementwise equality that also holds for two NaNs in the same place, which equal does not.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray |
The error function.
| name | type | |
|---|---|---|
| — | NDArray |
The complementary error function, 1 - erf(x), accurate for large arguments.
| name | type | |
|---|---|---|
| — | NDArray |
The inverse of the complementary error function.
| name | type | |
|---|---|---|
| — | NDArray |
The inverse of the error function.
| name | type | |
|---|---|---|
| — | NDArray |
e raised to each element.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Two raised to each element.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
A view with a new axis of length 1 at axis.
| name | type | |
|---|---|---|
| — | NDArray | |
| axis | Int32 | labelled |
exp(a) - 1, accurate near zero.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
A matrix with ones on the k-th diagonal and zeros elsewhere. k counts diagonals from the main one: positive towards the upper right, negative towards the lower left. m defaults to n, so the result is square.
| name | type | |
|---|---|---|
| n | Int32 | labelled |
| m | Int32 | optional |
| k | Int32 | optional |
| dtype | DType | optional |
The magnitude of a real array, as floating point; the same as absolute for floats.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
A 1-D copy, always.
| name | type | |
|---|---|---|
| — | NDArray |
Rounds down.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Division rounded towards negative infinity; both operands keep their integer type.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Visits every element of array in row-major order, passing the buffer offset of each. The offsets are produced by an odometer over the shape rather than by recomputing a dot product per element, so a strided or reversed view costs one addition per axis crossed.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | (Int32) -> Unit raise VecError |
A double as the printer writes it: 1.0 rather than 1, with nan and inf spelled out.
| name | type | |
|---|---|---|
| — | Double |
An array from raw bytes, which must be a whole number of elements of dtype.
| name | type | |
|---|---|---|
| — | Bytes | |
| — | Array[Int32] | |
| dtype | DType | labelled |
An array of shape from doubles in row-major order.
| name | type | |
|---|---|---|
| — | Array[Double] | |
| — | Array[Int32] | |
| dtype | DType | optional |
An array of shape from values in row-major order.
| name | type | |
|---|---|---|
| — | Array[Number] | |
| — | Array[Int32] | |
| dtype | DType | optional |
An array of shape filled with value, which is converted to dtype.
| name | type | |
|---|---|---|
| — | Array[Int32] | |
| — | Number | |
| dtype | DType | optional |
An array with the same shape as prototype, filled with value; the dtype follows prototype unless dtype says otherwise.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | Number | |
| dtype | DType | optional |
The gamma function, from the special-function library.
| name | type | |
|---|---|---|
| — | NDArray |
Elementwise a > b as a boolean array.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Elementwise a >= b as a boolean array.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Joins 1-D arrays end to end, and 2-D arrays along the second axis.
| name | type | |
|---|---|---|
| — | Array[NDArray] |
Elementwise sqrt(a * a + b * b), without the overflow the naive form has.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
The imaginary part; a real array gives zeros.
| name | type | |
|---|---|---|
| — | NDArray |
The inner product: a sum over the last axis of both, so the result has the shape of the operands without their last axis, added together.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Elementwise ~, flipping every bit of an integer array.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
True where the value is neither infinite nor a NaN; integers are always finite.
| name | type | |
|---|---|---|
| — | NDArray |
True where the value is an infinity.
| name | type | |
|---|---|---|
| — | NDArray |
True where the value is not a number; an integer array is never.
| name | type | |
|---|---|---|
| — | NDArray |
Elementwise left shift, with the second array as the count.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Elementwise a < b; complex arrays are not ordered and are refused.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Elementwise a <= b as a boolean array.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
The natural logarithm of the absolute value of gamma, which stays finite where gamma overflows.
| name | type | |
|---|---|---|
| — | NDArray |
num values from start to stop; with endpoint (the default) the last value is exactly stop, so the spacing is (stop - start) / (num - 1). With one value the result is start, not stop.
| name | type | |
|---|---|---|
| start | Double | labelled |
| stop | Double | labelled |
| num | Int32 | optional |
| endpoint | Bool | optional |
| dtype | DType | optional |
Natural logarithm; zero gives minus infinity and a negative input gives NaN.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Base-ten logarithm, with the same treatment of non-positive input as log.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
log(1 + a), accurate near zero.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Base-two logarithm, with the same treatment of non-positive input as log.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Elementwise logical and, reading both operands as truth values.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Elementwise not, answering with a boolean array; zero is false, everything else true.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Elementwise logical or, reading both operands as truth values.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
True where exactly one side is true.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Keeps the positions where the mask is true along one axis; the mask must have the axis's length.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | Array[Bool] | |
| axis | Int32 | optional |
Matrix multiplication, with the usual rules for the shapes that are not both matrices. The last axis of a contracts with the second-to-last of b. A 1-D operand is taken as a row or a column and the axis it borrowed is removed from the result, so a vector times a vector is a 0-dimensional array. Leading axes broadcast against each other, which makes a stack of matrices multiply as a stack.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
A 2-D array from rows that all have the same length.
| name | type | |
|---|---|---|
| — | Array[Array[Double]] | |
| dtype | DType | optional |
The largest element.
| name | type | |
|---|---|---|
| — | NDArray | |
| axis | Int32 | optional |
| keepdims | Bool | optional |
| kernel | &Kernel | optional |
Elementwise larger of the two; a NaN on either side gives NaN.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
The larger of each element and a scalar, elementwise.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | Double | |
| kernel | &Kernel | optional |
The arithmetic mean: an integer array answers in float64, and an empty axis gives NaN rather than an error, since there is a count to divide by, namely zero.
| name | type | |
|---|---|---|
| — | NDArray | |
| axis | Int32 | optional |
| keepdims | Bool | optional |
| kernel | &Kernel | optional |
The smallest element; an empty axis has no smallest element and is refused.
| name | type | |
|---|---|---|
| — | NDArray | |
| axis | Int32 | optional |
| keepdims | Bool | optional |
| kernel | &Kernel | optional |
Elementwise smaller of the two; a NaN on either side gives NaN.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
The smaller of each element and a scalar, elementwise.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | Double | |
| kernel | &Kernel | optional |
The same as remainder, under the name Python uses for the operator.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
A view with one axis moved to another position, the other axes keeping their order.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | Int32 | |
| — | Int32 |
Elementwise product.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Multiplies every element by a scalar, keeping the array's dtype.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | Double | |
| kernel | &Kernel | optional |
Elementwise negation.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Elementwise a != b as a boolean array; NaN is not equal to anything, itself included.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
An array of shape filled with ones.
| name | type | |
|---|---|---|
| — | Array[Int32] | |
| dtype | DType | optional |
An array of ones shaped like prototype.
| name | type | |
|---|---|---|
| — | NDArray | |
| dtype | DType | optional |
The outer product: a as a column, b as a row, their product a matrix. Both are read flat.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
A copy with every value's sign flipped, which for bool gives -1 for true.
| name | type | |
|---|---|---|
| — | NDArray |
Elementwise power; an integer base with a negative integer exponent is refused.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Raises every element to a scalar power, by the same rules as power.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | Double | |
| kernel | &Kernel | optional |
Multiplies the elements.
| name | type | |
|---|---|---|
| — | NDArray | |
| axis | Int32 | optional |
| keepdims | Bool | optional |
| kernel | &Kernel | optional |
The dtype two arrays of these dtypes produce when an arithmetic operation meets them. This is not a widening hierarchy: uint64 with int8 gives float64, because no integer type holds both ranges. The table is symmetric, so the order of the arguments does not matter.
| name | type | |
|---|---|---|
| — | DType | |
| — | DType |
The dtype every one of dtypes promotes to; F64 for an empty list, the neutral element.
| name | type | |
|---|---|---|
| — | ArrayView[DType] |
Converts degrees to radians.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
A 1-D view when the array is contiguous, a 1-D copy when it is not.
| name | type | |
|---|---|---|
| — | NDArray |
The real part; a real array is returned as it is.
| name | type | |
|---|---|---|
| — | NDArray |
Elementwise reciprocal, which for integers gives float64.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
The value a reduction starts from: the identity that leaves every element unchanged. The bounds are written per width rather than by casting one wide value, because a cast would wrap: the largest int8 is 127, not int64's maximum truncated to eight bits.
| name | type | |
|---|---|---|
| — | ReduceOp | |
| — | DType |
The remainder with the sign of the divisor, so -3 % 2 is 1.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
A view with the same elements in a new shape; -1 in one position asks for the size that fits. A contiguous array reshapes as a view. A strided one is copied first, because there is no stride pattern that would make the new shape a view of the old.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | Array[Int32] |
Elementwise right shift, with the second array as the count.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Rounds to the nearest integer with a tie going to the even one, the way IEEE 754 does.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Rounds to the nearest integer, a tie going away from zero; round in Python 3 goes to even, so this is rint's sibling, not a replacement for it.
| name | type | |
|---|---|---|
| — | NDArray |
Chooses elementwise from x where condition holds and from y elsewhere; all three broadcast together. It is named select because where is a keyword in MoonBit.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| — | NDArray |
-1, 0 or 1 by sign; a complex argument gives the value divided by its magnitude.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
True where the value is negative, including -0.0.
| name | type | |
|---|---|---|
| — | NDArray |
Sine of each element, in radians.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Hyperbolic sine of each element.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
A view of array selected axis by axis; axes past the end of indices are taken whole. Slicing a contiguous array stays a view: nothing is copied, so a write through the slice is a write into the original. At removes an axis and NewAxis adds one. Take and Mask cannot be expressed by strides, so they copy.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | Array[Index] |
A sorted copy along one axis, the last by default; NaNs go to the end. The sort is stable, so equal values keep the order they had, which argsort relies on.
| name | type | |
|---|---|---|
| — | NDArray | |
| axis | Int32 | optional |
A run of positions, the common case written without spelling out the step.
| name | type | |
|---|---|---|
| — | Int32 | |
| — | Int32 | |
| step | Int32 | optional |
Splits an axis into equal parts; the length must divide evenly.
| name | type | |
|---|---|---|
| — | NDArray | |
| parts | Int32 | labelled |
| axis | Int32 | optional |
Splits an axis at the given positions, which are the lengths of the parts before them. A position outside the axis is clamped, which lets split_at stand in for split.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | Array[Int32] | |
| axis | Int32 | optional |
Elementwise square root; a negative input gives NaN, as IEEE 754 says, rather than an error.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Elementwise square.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
A view with axes of length 1 removed; with axis, only that axis and only if it has length 1.
| name | type | |
|---|---|---|
| — | NDArray | |
| axis | Int32 | optional |
Joins arrays along a new axis, so the result has one more axis than the inputs.
| name | type | |
|---|---|---|
| — | Array[NDArray] | |
| axis | Int32 | optional |
The standard deviation, the square root of the variance.
| name | type | |
|---|---|---|
| — | NDArray | |
| axis | Int32 | optional |
| ddof | Int32 | optional |
| keepdims | Bool | optional |
| kernel | &Kernel | optional |
Elementwise difference.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | NDArray | |
| kernel | &Kernel | optional |
Subtracts a scalar from every element, keeping the array's dtype.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | Double | |
| kernel | &Kernel | optional |
Sums the elements, either all of them or along one axis. A full reduction returns a 0-dimensional array; keepdims keeps the reduced axis with length 1 so the result still broadcasts against the input.
| name | type | |
|---|---|---|
| — | NDArray | |
| axis | Int32 | optional |
| keepdims | Bool | optional |
| kernel | &Kernel | optional |
A view with two axes exchanged.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | Int32 | |
| — | Int32 |
Picks positions along one axis by an integer array, keeping the number of axes; the result is a copy.
| name | type | |
|---|---|---|
| — | NDArray | |
| — | Array[Int32] | |
| axis | Int32 | optional |
Tangent of each element, in radians.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
Hyperbolic tangent of each element.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
The sum of the diagonal, or of a diagonal chosen by offset; a 2-D only operation.
| name | type | |
|---|---|---|
| — | NDArray | |
| offset | Int32 | optional |
A view with the axes permuted; without axes the order is reversed.
| name | type | |
|---|---|---|
| — | NDArray | |
| axes | Array[Int32] | optional |
Drops the fractional part, towards zero.
| name | type | |
|---|---|---|
| — | NDArray | |
| kernel | &Kernel | optional |
The variance with ddof degrees of freedom removed from the count: the population variance by default, the sample variance with ddof=1.
| name | type | |
|---|---|---|
| — | NDArray | |
| axis | Int32 | optional |
| ddof | Int32 | optional |
| keepdims | Bool | optional |
| kernel | &Kernel | optional |
Rows one after another: a 1-D array becomes a single row, and anything with two or more axes is joined along axis 0.
| name | type | |
|---|---|---|
| — | Array[NDArray] |
An array of shape filled with zeros.
| name | type | |
|---|---|---|
| — | Array[Int32] | |
| dtype | DType | optional |
The common body of the *_like constructors.
| name | type | |
|---|---|---|
| — | NDArray | |
| dtype | DType | optional |
The Riemann zeta function.
| name | type | |
|---|---|---|
| — | NDArray |
Values
The backend every function uses when the caller does not name one.
Errors
What an array operation refuses to do. Shapes and axes are checked before any element is touched, so an error never leaves an array half written. Arithmetic that has no answer — an integer divided by zero, an overflowing cast — also lands here rather than returning a number that looks valid.
Trait implementations:
- @debug.Debug
One line naming what was refused, for a caller that holds only the error.
Traits
What an array backend has to provide: the arithmetic over runs of contiguous elements. Everything else — broadcasting, striding, dtype promotion, allocation — belongs to the array layer above, so a backend only has to be fast on long flat runs and never sees a shape. A run is always within one buffer and one dtype, and the offsets are in elements. Implement this trait to add a backend: a C stub, a compute shader, a vector unit. The array layer takes it as a parameter and does not change.
[1]
moonvec/linalg
Dense linear algebra on the same array: solving, inversion, determinants, Cholesky, QR, SVD, symmetric eigenvalues, least squares, norms, matrix powers and Kronecker products.
Types
How a norm measures a vector or a matrix.
Functions
The Cholesky factorisation a = l lᵀ of a symmetric positive definite matrix, returning the lower triangle with zeros above it.
| name | type | |
|---|---|---|
| — | @moonvec.NDArray |
The determinant, from the product of the pivots and the sign of the row swaps.
| name | type | |
|---|---|---|
| — | @moonvec.NDArray |
The eigenvalues and eigenvectors of a symmetric matrix, by cyclic Jacobi rotations; the columns of the second result are the eigenvectors, in the order of the values.
| name | type | |
|---|---|---|
| — | @moonvec.NDArray |
The inverse, by solving for the identity.
| name | type | |
|---|---|---|
| — | @moonvec.NDArray |
The Kronecker product: every element of a multiplied by the whole of b, tiled to match.
| name | type | |
|---|---|---|
| — | @moonvec.NDArray | |
| — | @moonvec.NDArray |
The least squares solution of a x = b for an overdetermined system, by QR factorisation. A system with fewer rows than columns has infinitely many solutions and is refused, as is a matrix whose columns are linearly dependent, because the answer would not be unique.
| name | type | |
|---|---|---|
| — | @moonvec.NDArray | |
| — | @moonvec.NDArray |
The norm of a matrix read as a whole.
| name | type | |
|---|---|---|
| — | @moonvec.NDArray | |
| order | Norm | optional |
A square matrix raised to an integer power; a negative power inverts first.
| name | type | |
|---|---|---|
| — | @moonvec.NDArray | |
| exponent | Int32 | labelled |
The norm of a vector, or of every vector along one axis. Without axis the whole array is read as one vector and the answer is 0-dimensional; with it, each line along that axis is measured and the axis disappears.
| name | type | |
|---|---|---|
| — | @moonvec.NDArray | |
| order | Norm | optional |
| axis | Int32 | optional |
The thin QR factorisation by Householder reflections: q is m by k with orthonormal columns and r is k by n upper triangular, where k is the smaller of the two sides.
| name | type | |
|---|---|---|
| — | @moonvec.NDArray |
Solves a x = b by LU factorisation with partial pivoting. b may be a vector or a matrix, and its first axis must match a's. A singular matrix is refused rather than answered with an arbitrary solution.
| name | type | |
|---|---|---|
| — | @moonvec.NDArray | |
| — | @moonvec.NDArray |
The singular value decomposition by one-sided Jacobi rotations: a = u s vᵀ, with the singular values in descending order. Each rotation orthogonalises a pair of columns, which converges quickly and keeps the accuracy of small singular values that a normal-equation method would lose.
| name | type | |
|---|---|---|
| — | @moonvec.NDArray |
[2]
moonvec/cute
A layout is a length and a stride, or a tuple of layouts. It holds no data; it names an offset that an array view can be built on, which is what a tiled kernel needs.
Types
A tensor layout: the mapping from a coordinate to an offset in a flat buffer. A layout is either a single mode — a length and the distance between neighbours — or a tuple of layouts, which lets a tile of a tile of a matrix stay one object instead of being flattened into a shape nobody can read back. Nothing here holds data: a layout is the description of where the elements of a tensor sit, which is what a kernel needs and what a view is built from. A stride may be 0, which repeats an element along that axis (how a broadcast is expressed without copying), or negative, which walks an axis backwards.
Operator forms:
- Eq for Layout
- Show for Layout
- @debug.Debug for Layout
Merges adjacent modes that walk the buffer at a constant step, so that a layout with one row per thread becomes one mode instead of many. Two modes merge when the outer one's stride equals the inner one's stride times the inner one's size: then the pair is a single run. A size-one mode disappears, and a stride-zero mode is kept apart because it repeats rather than advances.
One past the largest offset the layout can produce, which is how much space a buffer of this layout needs.
Splits the leading mode into a tile of tile elements and the rest. This is the operation a kernel uses to give each thread a block: the tile part addresses the elements within one block, the rest picks the block. The two multiply back to the original size.
| name | type | |
|---|---|---|
| self | Self | the receiver |
| tile | Int32 | labelled |
The offset of a coordinate, one entry per mode in flattened order.
| name | type | |
|---|---|---|
| self | Self | the receiver |
| — | Array[Int32] |
The offset of the element at index, counting the elements in row-major order as the layout itself orders them.
| name | type | |
|---|---|---|
| self | Self | the receiver |
| — | Int32 |
The shape of every mode, flattened from the outside in.
The number of elements the layout addresses.
The stride of every mode, flattened the same way as shape, so the two arrays line up.
The layout in size:stride form, with modes nested in parentheses.
Functions
A layout of size elements with no gaps, the usual row-major arrangement.
| name | type | |
|---|---|---|
| — | Array[Int32] |
A single mode, the building block of every other layout.
| name | type | |
|---|---|---|
| size | Int32 | labelled |
| stride | Int32 | labelled |
Two layouts side by side, which is how a thread index and a value index combine into one offset.
| name | type | |
|---|---|---|
| — | Layout | |
| — | Layout |
Errors
What a layout operation refuses.
One line naming what the layout algebra refused.
No item matches that filter.