moonvec numerical arrays

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

pub(all) enum BinaryOp { Add Subtract Multiply Divide FloorDivide Remainder Power Maximum Minimum ArcTangent2 Hypotenuse BitAnd BitOr BitXor LeftShift RightShift Equal NotEqual Less LessEqual Greater GreaterEqual LogicalAnd LogicalOr LogicalXor }

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.

pub struct Buffer { data : FixedArray[Byte] dtype : DType len : Int32 }

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.

pub fn Buffer::dtype(Self) -> DType

The element type this buffer stores.

pub fn Buffer::fill(Self, Number) -> Unit raise VecError

Sets every element to value.

nametype
selfSelfthe receiver
—Number
pub fn Buffer::from_bytes(Bytes, dtype~ : DType) -> Self raise VecError

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.

nametype
—Bytes
dtypeDTypelabelled
pub fn Buffer::len(Self) -> Int32

How many elements the buffer holds.

pub fn Buffer::new(dtype~ : DType, len~ : Int32) -> Self

Allocates len elements of dtype, zeroed.

nametype
dtypeDTypelabelled
lenInt32labelled
pub fn Buffer::read(Self, Int32) -> Number raise VecError

The element at index, widened to the class its dtype belongs to.

nametype
selfSelfthe receiver
—Int32
pub fn Buffer::to_bytes(Self) -> Bytes

A copy of the bytes, for handing to another library or writing to a file.

pub fn Buffer::write(Self, Int32, Number) -> Unit raise VecError

Stores value at index, converted the way C converts it to the buffer's dtype.

nametype
selfSelfthe receiver
—Int32
—Number
pub(all) struct Complex { re : Double im : Double }

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
pub fn Complex::abs(Self) -> Double

The modulus, computed as a hypotenuse so that large components do not overflow.

pub fn Complex::arg(Self) -> Double

The angle in radians, in (-pi, pi], the argument atan2 returns.

pub fn Complex::conj(Self) -> Self

The conjugate: the imaginary part negated.

pub fn Complex::cos(Self) -> Self

The cosine, extended to the complex plane.

pub fn Complex::cosh(Self) -> Self

The hyperbolic cosine, extended to the complex plane.

pub fn Complex::exp(Self) -> Self

e raised to this value, by Euler's formula.

pub fn Complex::is_nonzero(Self) -> Bool

True when either part is non-zero.

pub fn Complex::log(Self) -> Self

The principal logarithm: ln|z| + i arg z.

pub fn Complex::neg(Self) -> Self

The additive inverse.

pub fn Complex::new(Double, Double) -> Self

A complex number from its real and imaginary parts.

nametype
—Double
—Double
pub fn Complex::pow(Self, Self) -> Self

self raised to exponent, through exp(exponent * log self).

nametype
selfSelfthe receiver
selfSelfthe receiver
pub fn Complex::sin(Self) -> Self

The sine, extended to the complex plane.

pub fn Complex::sinh(Self) -> Self

The hyperbolic sine, extended to the complex plane.

pub fn Complex::sqrt(Self) -> Self

The principal square root, on the branch where the result's real part is non-negative.

pub fn Complex::tan(Self) -> Self

The tangent, extended to the complex plane.

pub fn Complex::tanh(Self) -> Self

The hyperbolic tangent, extended to the complex plane.

pub struct Cpu { lanes : Int32 }

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.

pub fn Cpu::new() -> Self

The default backend, sized for the vector registers the build target provides.

pub(all) enum DType { Bool I8 I16 I32 I64 U8 U16 U32 U64 F16 F32 F64 C64 C128 }

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
pub fn DType::float_size(Self) -> Int32

The dtype that holds every value of self without loss, the smallest such float being F64.

pub fn DType::is_bool(Self) -> Bool

True for the boolean dtype.

pub fn DType::is_complex(Self) -> Bool

True for the two complex dtypes.

pub fn DType::is_exact(Self) -> Bool

True for the dtypes an operation on integers keeps exact: integers and bool.

pub fn DType::is_float(Self) -> Bool

True for F16, F32 and F64; the complex dtypes are not floats.

pub fn DType::is_integer(Self) -> Bool

True for the signed and unsigned integer dtypes, false for bool.

pub fn DType::is_real(Self) -> Bool

True for anything whose values are stored as a real number: floats and complex.

pub fn DType::is_signed(Self) -> Bool

True for the four signed integer dtypes.

pub fn DType::item_size(Self) -> Int32

The number of bytes one element takes in a buffer.

pub fn DType::name(Self) -> String

The name this type is written as, lower case and without spaces.

pub fn DType::parse(StringView) -> Self?

Reads a dtype back from its name, as written by to_string or a vector file.

nametype
—StringView
pub fn DType::to_real(Self) -> Self

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.

pub(all) enum Index { All At(Int32) Span(Int32, Int32, Int32) NewAxis Take(Array[Int32]) Mask(Array[Bool]) }

One axis selector in a slice, read left to right, one per axis.

pub struct NDArray { buffer : Buffer shape : Array[Int32] strides : Array[Int32] offset : Int32 }

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
pub fn NDArray::at(Self, Array[Int32]) -> Number raise VecError

The element at index, one integer per axis.

nametype
selfSelfthe receiver
—Array[Int32]
pub fn NDArray::buffer(Self) -> Buffer

The buffer this view reads, shared with every other view of the same data.

pub fn NDArray::copy(Self) -> Self raise VecError

A contiguous copy, whatever the layout of this view.

pub fn NDArray::dtype(Self) -> DType

The element type of the buffer.

pub fn NDArray::is_contiguous(Self) -> Bool

True when the elements sit next to each other in buffer order, so a backend can take the whole array as one run.

pub fn NDArray::is_scalar(Self) -> Bool

True for a 0-dimensional array, the result of a full reduction.

pub fn NDArray::item(Self) -> Number raise VecError

The one value of a 0-dimensional array.

pub fn NDArray::ndim(Self) -> Int32

How many axes the shape has.

pub fn NDArray::new(buffer~ : Buffer, shape~ : Array[Int32], strides? : Array[Int32], offset? : Int32) -> Self raise VecError

Builds a view over buffer; the strides default to the contiguous layout of shape.

nametype
bufferBufferlabelled
shapeArray[Int32]labelled
stridesArray[Int32]optional
offsetInt32optional
pub fn NDArray::offset(Self) -> Int32

The index in the buffer of the first element, after any slicing.

pub fn NDArray::set_at(Self, Array[Int32], Number) -> Unit raise VecError

Writes one element at index.

nametype
selfSelfthe receiver
—Array[Int32]
—Number
pub fn NDArray::shape(Self) -> Array[Int32]

The size of each axis; a copy, so changing it cannot break the view.

pub fn NDArray::size(Self) -> Int32

How many elements the shape holds, counting a stretched axis once per position.

pub fn NDArray::strides(Self) -> Array[Int32]

The element distance between neighbours along each axis; a copy.

pub fn NDArray::to_doubles(Self) -> Array[Double] raise VecError

Every element as a double, in row-major order. Complex values lose their imaginary part, as any conversion to a real type does.

pub fn NDArray::to_numbers(Self) -> Array[Number] raise VecError

Every element in row-major order, as a fresh array.

pub fn NDArray::to_string(Self) -> String raise VecError

The array laid out one row per line, with the dtype after the values.

pub(all) enum Number { Boolean(Bool) Signed(Int64) Unsigned(UInt64) Real(Double) Complex(Complex) }

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
pub fn Number::cast(Self, DType) -> Self

The same value, stored as dtype would store it: integers wrap to their width, floats round.

nametype
selfSelfthe receiver
—DType
pub fn Number::compare_value(Self, Self) -> Int32

Compares two values by magnitude only; complex pairs compare by real part then imaginary part, which is the order values sort in.

nametype
selfSelfthe receiver
selfSelfthe receiver
pub fn Number::to_bool(Self) -> Bool

True when the value is not zero; a complex number is true when either part is.

pub fn Number::to_complex(Self) -> Complex

The value as a complex number, with a zero imaginary part.

pub fn Number::to_double(Self) -> Double

The value as a double; an integer above 2^53 loses its low bits.

pub fn Number::to_int64(Self) -> Int64

The value truncated toward zero, the rule C uses when a float is stored into an integer.

pub fn Number::to_string(Self) -> String

The value as the array printer writes it.

pub struct Plan { outer_sizes : Array[Int32] outer_strides : Array[Array[Int32]] run : Int32 run_strides : Array[Int32] operands : Int32 starts : Array[Int32] }

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.

pub(all) enum ReduceOp { Sum Product Minimum Maximum Any All }

The reductions a backend has to provide, as an associative pair of values.

pub(all) enum UnaryOp { Negate Absolute Square Reciprocal Sign Sqrt Cbrt Exp Exp2 Expm1 Log Log2 Log10 Log1p Sin Cos Tan ArcSin ArcCos ArcTan Sinh Cosh Tanh ArcSinh ArcCosh ArcTanh Floor Ceil Trunc Rint Degrees Radians LogicalNot Conjugate }

The unary operations a backend has to provide.

Functions

pub fn absolute(NDArray, kernel? : &Kernel) -> NDArray raise VecError

The magnitude; a complex array answers in its real dtype.

nametype
—NDArray
kernel&Kerneloptional
pub fn add(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise sum; the two shapes broadcast.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn add_scalar(NDArray, Double, kernel? : &Kernel) -> NDArray raise VecError

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.

nametype
—NDArray
—Double
kernel&Kerneloptional
pub fn all(NDArray, axis? : Int32, keepdims? : Bool, kernel? : &Kernel) -> NDArray raise VecError

True where every element is true; a full reduction over an empty array is true.

nametype
—NDArray
axisInt32optional
keepdimsBooloptional
kernel&Kerneloptional
pub fn angle(NDArray) -> NDArray raise VecError

The angle of each complex value in radians; a real array is 0 where positive and pi where negative.

nametype
—NDArray
pub fn any(NDArray, axis? : Int32, keepdims? : Bool, kernel? : &Kernel) -> NDArray raise VecError

True where any element is true.

nametype
—NDArray
axisInt32optional
keepdimsBooloptional
kernel&Kerneloptional
pub fn apply_binary(BinaryOp, DType, Number, Number) -> Number raise VecError

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.

nametype
—BinaryOp
—DType
—Number
—Number
pub fn apply_reduce(ReduceOp, DType, Number, Number) -> Number raise VecError

Combines two values in a reduction; the pair must be associative and have the identity the caller started from.

nametype
—ReduceOp
—DType
—Number
—Number
pub fn apply_unary(UnaryOp, DType, Number) -> Number raise VecError

Applies a unary operation to a value already cast to dtype.

nametype
—UnaryOp
—DType
—Number
pub fn arange(start~ : Double, stop~ : Double, step? : Double, dtype? : DType) -> NDArray raise VecError

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.

nametype
startDoublelabelled
stopDoublelabelled
stepDoubleoptional
dtypeDTypeoptional
pub fn arccos(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Inverse cosine of each element, in radians; outside [-1, 1] it is NaN.

nametype
—NDArray
kernel&Kerneloptional
pub fn arccosh(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Inverse hyperbolic cosine of each element; below one it is NaN.

nametype
—NDArray
kernel&Kerneloptional
pub fn arcsin(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Inverse sine of each element, in radians; outside [-1, 1] it is NaN.

nametype
—NDArray
kernel&Kerneloptional
pub fn arcsinh(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Inverse hyperbolic sine of each element.

nametype
—NDArray
kernel&Kerneloptional
pub fn arctan(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Inverse tangent of each element, in radians.

nametype
—NDArray
kernel&Kerneloptional
pub fn arctan2(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise arctangent of a / b, choosing the quadrant from both signs.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn arctanh(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Inverse hyperbolic tangent of each element; outside (-1, 1) it is NaN.

nametype
—NDArray
kernel&Kerneloptional
pub fn argmax(NDArray, axis? : Int32, keepdims? : Bool) -> NDArray raise VecError

The index of the largest element along the axis; the first one wins a tie.

nametype
—NDArray
axisInt32optional
keepdimsBooloptional
pub fn argmin(NDArray, axis? : Int32, keepdims? : Bool) -> NDArray raise VecError

The index of the smallest element along the axis; the first one wins a tie.

nametype
—NDArray
axisInt32optional
keepdimsBooloptional
pub fn argsort(NDArray, axis? : Int32) -> NDArray raise VecError

The positions that would sort each run along the axis, as int64; stable for equal values.

nametype
—NDArray
axisInt32optional
pub fn array(Array[Double], dtype? : DType) -> NDArray raise VecError

A 1-D array from a list of values.

nametype
—Array[Double]
dtypeDTypeoptional
pub fn astype(NDArray, dtype~ : DType) -> NDArray raise VecError

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.

nametype
—NDArray
dtypeDTypelabelled
pub fn bessel_i(order~ : Double, NDArray) -> NDArray raise VecError

The modified Bessel function of the first kind of order order.

nametype
orderDoublelabelled
—NDArray
pub fn bessel_j(order~ : Double, NDArray) -> NDArray raise VecError

The Bessel function of the first kind of order order.

nametype
orderDoublelabelled
—NDArray
pub fn bessel_k(order~ : Double, NDArray) -> NDArray raise VecError

The modified Bessel function of the second kind of order order.

nametype
orderDoublelabelled
—NDArray
pub fn bessel_y(order~ : Double, NDArray) -> NDArray raise VecError

The Bessel function of the second kind of order order.

nametype
orderDoublelabelled
—NDArray
pub fn beta(NDArray, NDArray) -> NDArray raise VecError

The beta function, elementwise over two broadcast operands.

nametype
—NDArray
—NDArray
pub fn bitwise_and(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise bitwise and; both operands must be integers.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn bitwise_or(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise bitwise or; both operands are read as integers.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn bitwise_xor(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise bitwise exclusive or; both operands are read as integers.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn broadcast(Array[Int32], Array[Int32]) -> Array[Int32] raise VecError

The shape an elementwise operation of these two arrays has, without doing the work.

nametype
—Array[Int32]
—Array[Int32]
pub fn broadcast_shape(Array[Int32], Array[Int32]) -> Array[Int32] raise VecError

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.

nametype
—Array[Int32]
—Array[Int32]
pub fn broadcast_strides(Array[Int32], Array[Int32], 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.

nametype
—Array[Int32]
—Array[Int32]
—Array[Int32]
pub fn broadcast_to(NDArray, Array[Int32]) -> NDArray raise VecError

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.

nametype
—NDArray
—Array[Int32]
pub fn cbrt(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise cube root, defined for negative inputs too: cbrt(-8) is -2.

nametype
—NDArray
kernel&Kerneloptional
pub fn ceil(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Rounds up.

nametype
—NDArray
kernel&Kerneloptional
pub fn clip(NDArray, min? : Double, max? : Double, kernel? : &Kernel) -> NDArray raise VecError

Clamps every element into [min, max]; either bound may be left out, and the bounds broadcast like any other operand.

nametype
—NDArray
minDoubleoptional
maxDoubleoptional
kernel&Kerneloptional
pub fn compare_real(Double, Double) -> Int32

Orders two doubles with NaN last, so a sort puts the values that cannot be compared at the end instead of scattering them.

nametype
—Double
—Double
pub fn complex_number(Double, Double) -> Number

A complex value as a Number, spelling out both types so the variant and the struct do not shadow each other.

nametype
—Double
—Double
pub fn concatenate(Array[NDArray], axis? : Int32) -> NDArray raise VecError

Joins arrays along an existing axis; the other axes must agree in length, and the dtypes promote.

nametype
—Array[NDArray]
axisInt32optional
pub fn conj(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise conjugate; a real array is returned as it is.

nametype
—NDArray
kernel&Kerneloptional
pub fn cos(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Cosine of each element, in radians.

nametype
—NDArray
kernel&Kerneloptional
pub fn cosh(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Hyperbolic cosine of each element.

nametype
—NDArray
kernel&Kerneloptional
pub fn degrees(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Converts radians to degrees.

nametype
—NDArray
kernel&Kerneloptional
pub fn digamma(NDArray) -> NDArray raise VecError

The logarithmic derivative of gamma.

nametype
—NDArray
pub fn divide(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

True division: two integer arrays give float64, so 1 / 2 is 0.5 rather than 0.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn divide_scalar(NDArray, Double, kernel? : &Kernel) -> NDArray raise VecError

Divides by a scalar; an integer array gives float64, as with divide.

nametype
—NDArray
—Double
kernel&Kerneloptional
pub fn division_dtype(DType, DType) -> DType

The dtype the true division of these two dtypes produces: integers turn into float64.

nametype
—DType
—DType
pub fn dot(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

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.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn equal(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise a == b, answering with a boolean array.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn equal_nan(NDArray, NDArray) -> NDArray raise VecError

Elementwise equality that also holds for two NaNs in the same place, which equal does not.

nametype
—NDArray
—NDArray
pub fn erf(NDArray) -> NDArray raise VecError

The error function.

nametype
—NDArray
pub fn erfc(NDArray) -> NDArray raise VecError

The complementary error function, 1 - erf(x), accurate for large arguments.

nametype
—NDArray
pub fn erfcinv(NDArray) -> NDArray raise VecError

The inverse of the complementary error function.

nametype
—NDArray
pub fn erfinv(NDArray) -> NDArray raise VecError

The inverse of the error function.

nametype
—NDArray
pub fn exp(NDArray, kernel? : &Kernel) -> NDArray raise VecError

e raised to each element.

nametype
—NDArray
kernel&Kerneloptional
pub fn exp2(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Two raised to each element.

nametype
—NDArray
kernel&Kerneloptional
pub fn expand_dims(NDArray, axis~ : Int32) -> NDArray raise VecError

A view with a new axis of length 1 at axis.

nametype
—NDArray
axisInt32labelled
pub fn expm1(NDArray, kernel? : &Kernel) -> NDArray raise VecError

exp(a) - 1, accurate near zero.

nametype
—NDArray
kernel&Kerneloptional
pub fn eye(n~ : Int32, m? : Int32, k? : Int32, dtype? : DType) -> NDArray raise VecError

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.

nametype
nInt32labelled
mInt32optional
kInt32optional
dtypeDTypeoptional
pub fn fabs(NDArray, kernel? : &Kernel) -> NDArray raise VecError

The magnitude of a real array, as floating point; the same as absolute for floats.

nametype
—NDArray
kernel&Kerneloptional
pub fn flatten(NDArray) -> NDArray raise VecError

A 1-D copy, always.

nametype
—NDArray
pub fn floor(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Rounds down.

nametype
—NDArray
kernel&Kerneloptional
pub fn floor_divide(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Division rounded towards negative infinity; both operands keep their integer type.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn for_each_offset(NDArray, (Int32) -> Unit raise VecError) -> Unit raise VecError

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.

nametype
—NDArray
—(Int32) -> Unit raise VecError
pub fn format_real(Double) -> String

A double as the printer writes it: 1.0 rather than 1, with nan and inf spelled out.

nametype
—Double
pub fn from_bytes(Bytes, Array[Int32], dtype~ : DType) -> NDArray raise VecError

An array from raw bytes, which must be a whole number of elements of dtype.

nametype
—Bytes
—Array[Int32]
dtypeDTypelabelled
pub fn from_doubles(Array[Double], Array[Int32], dtype? : DType) -> NDArray raise VecError

An array of shape from doubles in row-major order.

nametype
—Array[Double]
—Array[Int32]
dtypeDTypeoptional
pub fn from_numbers(Array[Number], Array[Int32], dtype? : DType) -> NDArray raise VecError

An array of shape from values in row-major order.

nametype
—Array[Number]
—Array[Int32]
dtypeDTypeoptional
pub fn full(Array[Int32], Number, dtype? : DType) -> NDArray raise VecError

An array of shape filled with value, which is converted to dtype.

nametype
—Array[Int32]
—Number
dtypeDTypeoptional
pub fn full_like(NDArray, Number, dtype? : DType) -> NDArray raise VecError

An array with the same shape as prototype, filled with value; the dtype follows prototype unless dtype says otherwise.

nametype
—NDArray
—Number
dtypeDTypeoptional
pub fn gamma(NDArray) -> NDArray raise VecError

The gamma function, from the special-function library.

nametype
—NDArray
pub fn greater(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise a > b as a boolean array.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn greater_equal(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise a >= b as a boolean array.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn hstack(Array[NDArray]) -> NDArray raise VecError

Joins 1-D arrays end to end, and 2-D arrays along the second axis.

nametype
—Array[NDArray]
pub fn hypot(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise sqrt(a * a + b * b), without the overflow the naive form has.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn imag(NDArray) -> NDArray raise VecError

The imaginary part; a real array gives zeros.

nametype
—NDArray
pub fn inner(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

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.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn invert(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise ~, flipping every bit of an integer array.

nametype
—NDArray
kernel&Kerneloptional
pub fn isfinite(NDArray) -> NDArray raise VecError

True where the value is neither infinite nor a NaN; integers are always finite.

nametype
—NDArray
pub fn isinf(NDArray) -> NDArray raise VecError

True where the value is an infinity.

nametype
—NDArray
pub fn isnan(NDArray) -> NDArray raise VecError

True where the value is not a number; an integer array is never.

nametype
—NDArray
pub fn left_shift(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise left shift, with the second array as the count.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn less(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise a < b; complex arrays are not ordered and are refused.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn less_equal(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise a <= b as a boolean array.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn lgamma(NDArray) -> NDArray raise VecError

The natural logarithm of the absolute value of gamma, which stays finite where gamma overflows.

nametype
—NDArray
pub fn linspace(start~ : Double, stop~ : Double, num? : Int32, endpoint? : Bool, dtype? : DType) -> NDArray raise VecError

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.

nametype
startDoublelabelled
stopDoublelabelled
numInt32optional
endpointBooloptional
dtypeDTypeoptional
pub fn log(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Natural logarithm; zero gives minus infinity and a negative input gives NaN.

nametype
—NDArray
kernel&Kerneloptional
pub fn log10(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Base-ten logarithm, with the same treatment of non-positive input as log.

nametype
—NDArray
kernel&Kerneloptional
pub fn log1p(NDArray, kernel? : &Kernel) -> NDArray raise VecError

log(1 + a), accurate near zero.

nametype
—NDArray
kernel&Kerneloptional
pub fn log2(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Base-two logarithm, with the same treatment of non-positive input as log.

nametype
—NDArray
kernel&Kerneloptional
pub fn logical_and(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise logical and, reading both operands as truth values.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn logical_not(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise not, answering with a boolean array; zero is false, everything else true.

nametype
—NDArray
kernel&Kerneloptional
pub fn logical_or(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise logical or, reading both operands as truth values.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn logical_xor(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

True where exactly one side is true.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn mask(NDArray, Array[Bool], axis? : Int32) -> NDArray raise VecError

Keeps the positions where the mask is true along one axis; the mask must have the axis's length.

nametype
—NDArray
—Array[Bool]
axisInt32optional
pub fn matmul(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

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.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn matrix(Array[Array[Double]], dtype? : DType) -> NDArray raise VecError

A 2-D array from rows that all have the same length.

nametype
—Array[Array[Double]]
dtypeDTypeoptional
pub fn max(NDArray, axis? : Int32, keepdims? : Bool, kernel? : &Kernel) -> NDArray raise VecError

The largest element.

nametype
—NDArray
axisInt32optional
keepdimsBooloptional
kernel&Kerneloptional
pub fn maximum(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise larger of the two; a NaN on either side gives NaN.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn maximum_scalar(NDArray, Double, kernel? : &Kernel) -> NDArray raise VecError

The larger of each element and a scalar, elementwise.

nametype
—NDArray
—Double
kernel&Kerneloptional
pub fn mean(NDArray, axis? : Int32, keepdims? : Bool, kernel? : &Kernel) -> NDArray raise VecError

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.

nametype
—NDArray
axisInt32optional
keepdimsBooloptional
kernel&Kerneloptional
pub fn min(NDArray, axis? : Int32, keepdims? : Bool, kernel? : &Kernel) -> NDArray raise VecError

The smallest element; an empty axis has no smallest element and is refused.

nametype
—NDArray
axisInt32optional
keepdimsBooloptional
kernel&Kerneloptional
pub fn minimum(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise smaller of the two; a NaN on either side gives NaN.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn minimum_scalar(NDArray, Double, kernel? : &Kernel) -> NDArray raise VecError

The smaller of each element and a scalar, elementwise.

nametype
—NDArray
—Double
kernel&Kerneloptional
pub fn mod(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

The same as remainder, under the name Python uses for the operator.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn move_axis(NDArray, Int32, Int32) -> NDArray raise VecError

A view with one axis moved to another position, the other axes keeping their order.

nametype
—NDArray
—Int32
—Int32
pub fn multiply(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise product.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn multiply_scalar(NDArray, Double, kernel? : &Kernel) -> NDArray raise VecError

Multiplies every element by a scalar, keeping the array's dtype.

nametype
—NDArray
—Double
kernel&Kerneloptional
pub fn negative(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise negation.

nametype
—NDArray
kernel&Kerneloptional
pub fn not_equal(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise a != b as a boolean array; NaN is not equal to anything, itself included.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn ones(Array[Int32], dtype? : DType) -> NDArray raise VecError

An array of shape filled with ones.

nametype
—Array[Int32]
dtypeDTypeoptional
pub fn ones_like(NDArray, dtype? : DType) -> NDArray raise VecError

An array of ones shaped like prototype.

nametype
—NDArray
dtypeDTypeoptional
pub fn outer(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

The outer product: a as a column, b as a row, their product a matrix. Both are read flat.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn positive(NDArray) -> NDArray raise VecError

A copy with every value's sign flipped, which for bool gives -1 for true.

nametype
—NDArray
pub fn power(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise power; an integer base with a negative integer exponent is refused.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn power_scalar(NDArray, Double, kernel? : &Kernel) -> NDArray raise VecError

Raises every element to a scalar power, by the same rules as power.

nametype
—NDArray
—Double
kernel&Kerneloptional
pub fn prod(NDArray, axis? : Int32, keepdims? : Bool, kernel? : &Kernel) -> NDArray raise VecError

Multiplies the elements.

nametype
—NDArray
axisInt32optional
keepdimsBooloptional
kernel&Kerneloptional
pub fn promote(DType, DType) -> DType

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.

nametype
—DType
—DType
pub fn promote_all(ArrayView[DType]) -> DType

The dtype every one of dtypes promotes to; F64 for an empty list, the neutral element.

nametype
—ArrayView[DType]
pub fn radians(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Converts degrees to radians.

nametype
—NDArray
kernel&Kerneloptional
pub fn ravel(NDArray) -> NDArray raise VecError

A 1-D view when the array is contiguous, a 1-D copy when it is not.

nametype
—NDArray
pub fn real(NDArray) -> NDArray raise VecError

The real part; a real array is returned as it is.

nametype
—NDArray
pub fn reciprocal(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise reciprocal, which for integers gives float64.

nametype
—NDArray
kernel&Kerneloptional
pub fn reduce_identity(ReduceOp, DType) -> Number

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.

nametype
—ReduceOp
—DType
pub fn remainder(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

The remainder with the sign of the divisor, so -3 % 2 is 1.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn reshape(NDArray, Array[Int32]) -> NDArray raise VecError

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.

nametype
—NDArray
—Array[Int32]
pub fn right_shift(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise right shift, with the second array as the count.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn rint(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Rounds to the nearest integer with a tie going to the even one, the way IEEE 754 does.

nametype
—NDArray
kernel&Kerneloptional
pub fn round(NDArray) -> NDArray raise VecError

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.

nametype
—NDArray
pub fn select(NDArray, NDArray, NDArray) -> NDArray raise VecError

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.

nametype
—NDArray
—NDArray
—NDArray
pub fn sign(NDArray, kernel? : &Kernel) -> NDArray raise VecError

-1, 0 or 1 by sign; a complex argument gives the value divided by its magnitude.

nametype
—NDArray
kernel&Kerneloptional
pub fn signbit(NDArray) -> NDArray raise VecError

True where the value is negative, including -0.0.

nametype
—NDArray
pub fn sin(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Sine of each element, in radians.

nametype
—NDArray
kernel&Kerneloptional
pub fn sinh(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Hyperbolic sine of each element.

nametype
—NDArray
kernel&Kerneloptional
pub fn slice(NDArray, Array[Index]) -> NDArray raise VecError

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.

nametype
—NDArray
—Array[Index]
pub fn sort(NDArray, axis? : Int32) -> NDArray raise VecError

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.

nametype
—NDArray
axisInt32optional
pub fn span(Int32, Int32, step? : Int32) -> Index

A run of positions, the common case written without spelling out the step.

nametype
—Int32
—Int32
stepInt32optional
pub fn split(NDArray, parts~ : Int32, axis? : Int32) -> Array[NDArray] raise VecError

Splits an axis into equal parts; the length must divide evenly.

nametype
—NDArray
partsInt32labelled
axisInt32optional
pub fn split_at(NDArray, Array[Int32], axis? : Int32) -> Array[NDArray] raise VecError

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.

nametype
—NDArray
—Array[Int32]
axisInt32optional
pub fn sqrt(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise square root; a negative input gives NaN, as IEEE 754 says, rather than an error.

nametype
—NDArray
kernel&Kerneloptional
pub fn square(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise square.

nametype
—NDArray
kernel&Kerneloptional
pub fn squeeze(NDArray, axis? : Int32) -> NDArray raise VecError

A view with axes of length 1 removed; with axis, only that axis and only if it has length 1.

nametype
—NDArray
axisInt32optional
pub fn stack(Array[NDArray], axis? : Int32) -> NDArray raise VecError

Joins arrays along a new axis, so the result has one more axis than the inputs.

nametype
—Array[NDArray]
axisInt32optional
pub fn std(NDArray, axis? : Int32, ddof? : Int32, keepdims? : Bool, kernel? : &Kernel) -> NDArray raise VecError

The standard deviation, the square root of the variance.

nametype
—NDArray
axisInt32optional
ddofInt32optional
keepdimsBooloptional
kernel&Kerneloptional
pub fn subtract(NDArray, NDArray, kernel? : &Kernel) -> NDArray raise VecError

Elementwise difference.

nametype
—NDArray
—NDArray
kernel&Kerneloptional
pub fn subtract_scalar(NDArray, Double, kernel? : &Kernel) -> NDArray raise VecError

Subtracts a scalar from every element, keeping the array's dtype.

nametype
—NDArray
—Double
kernel&Kerneloptional
pub fn sum(NDArray, axis? : Int32, keepdims? : Bool, kernel? : &Kernel) -> NDArray raise VecError

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.

nametype
—NDArray
axisInt32optional
keepdimsBooloptional
kernel&Kerneloptional
pub fn swap_axes(NDArray, Int32, Int32) -> NDArray raise VecError

A view with two axes exchanged.

nametype
—NDArray
—Int32
—Int32
pub fn take(NDArray, Array[Int32], axis? : Int32) -> NDArray raise VecError

Picks positions along one axis by an integer array, keeping the number of axes; the result is a copy.

nametype
—NDArray
—Array[Int32]
axisInt32optional
pub fn tan(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Tangent of each element, in radians.

nametype
—NDArray
kernel&Kerneloptional
pub fn tanh(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Hyperbolic tangent of each element.

nametype
—NDArray
kernel&Kerneloptional
pub fn trace(NDArray, offset? : Int32) -> NDArray raise VecError

The sum of the diagonal, or of a diagonal chosen by offset; a 2-D only operation.

nametype
—NDArray
offsetInt32optional
pub fn transpose(NDArray, axes? : Array[Int32]) -> NDArray raise VecError

A view with the axes permuted; without axes the order is reversed.

nametype
—NDArray
axesArray[Int32]optional
pub fn trunc(NDArray, kernel? : &Kernel) -> NDArray raise VecError

Drops the fractional part, towards zero.

nametype
—NDArray
kernel&Kerneloptional
pub fn variance(NDArray, axis? : Int32, ddof? : Int32, keepdims? : Bool, kernel? : &Kernel) -> NDArray raise VecError

The variance with ddof degrees of freedom removed from the count: the population variance by default, the sample variance with ddof=1.

nametype
—NDArray
axisInt32optional
ddofInt32optional
keepdimsBooloptional
kernel&Kerneloptional
pub fn vstack(Array[NDArray]) -> NDArray raise VecError

Rows one after another: a 1-D array becomes a single row, and anything with two or more axes is joined along axis 0.

nametype
—Array[NDArray]
pub fn zeros(Array[Int32], dtype? : DType) -> NDArray raise VecError

An array of shape filled with zeros.

nametype
—Array[Int32]
dtypeDTypeoptional
pub fn zeros_like(NDArray, dtype? : DType) -> NDArray raise VecError

The common body of the *_like constructors.

nametype
—NDArray
dtypeDTypeoptional
pub fn zeta(NDArray) -> NDArray raise VecError

The Riemann zeta function.

nametype
—NDArray

Values

pub let cpu : Cpu

The backend every function uses when the caller does not name one.

Errors

pub(all) suberror VecError { ByteCountMismatch(total~ : Int32, item_size~ : Int32) IndexOutOfBounds(index~ : Int32, len~ : Int32) ShapeMismatch(want~ : Array[Int32], got~ : Array[Int32]) AxisOutOfRange(axis~ : Int32, ndim~ : Int32) CannotReshape(size~ : Int32, shape~ : Array[Int32]) EmptyAxis(axis~ : Int32) Domain(message~ : String) NotSolvable(message~ : String) }

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
pub fn VecError::message(Self) -> String

One line naming what was refused, for a caller that holds only the error.

Traits

pub(open) trait Kernel { fn name(Self) -> String fn binary(Self, BinaryOp, DType, Buffer, Int32, Int32, Buffer, Int32, Int32, Buffer, Int32, Int32, Int32) -> Unit raise VecError fn unary(Self, UnaryOp, DType, Buffer, Int32, Int32, Buffer, Int32, Int32, Int32) -> Unit raise VecError fn matmul(Self, DType, Buffer, Int32, Buffer, Int32, Buffer, Int32, Int32, Int32, Int32) -> Unit raise VecError fn reduce(Self, ReduceOp, DType, Buffer, Int32, Int32, Int32, Number) -> Number raise VecError }

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

pub(all) enum Norm { Frobenius One Two Infinity NegInfinity P(Double) }

How a norm measures a vector or a matrix.

Functions

pub fn cholesky(@moonvec.NDArray) -> @moonvec.NDArray raise @moonvec.VecError

The Cholesky factorisation a = l lᵀ of a symmetric positive definite matrix, returning the lower triangle with zeros above it.

nametype
—@moonvec.NDArray
pub fn det(@moonvec.NDArray) -> Double raise @moonvec.VecError

The determinant, from the product of the pivots and the sign of the row swaps.

nametype
—@moonvec.NDArray
pub fn eigh(@moonvec.NDArray) -> (@moonvec.NDArray, @moonvec.NDArray) raise @moonvec.VecError

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.

nametype
—@moonvec.NDArray
pub fn inv(@moonvec.NDArray) -> @moonvec.NDArray raise @moonvec.VecError

The inverse, by solving for the identity.

nametype
—@moonvec.NDArray
pub fn kron(@moonvec.NDArray, @moonvec.NDArray) -> @moonvec.NDArray raise @moonvec.VecError

The Kronecker product: every element of a multiplied by the whole of b, tiled to match.

nametype
—@moonvec.NDArray
—@moonvec.NDArray
pub fn lstsq(@moonvec.NDArray, @moonvec.NDArray) -> @moonvec.NDArray raise @moonvec.VecError

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.

nametype
—@moonvec.NDArray
—@moonvec.NDArray
pub fn matrix_norm(@moonvec.NDArray, order? : Norm) -> Double raise @moonvec.VecError

The norm of a matrix read as a whole.

nametype
—@moonvec.NDArray
orderNormoptional
pub fn matrix_power(@moonvec.NDArray, exponent~ : Int32) -> @moonvec.NDArray raise @moonvec.VecError

A square matrix raised to an integer power; a negative power inverts first.

nametype
—@moonvec.NDArray
exponentInt32labelled
pub fn norm(@moonvec.NDArray, order? : Norm, axis? : Int32) -> @moonvec.NDArray raise @moonvec.VecError

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.

nametype
—@moonvec.NDArray
orderNormoptional
axisInt32optional
pub fn qr(@moonvec.NDArray) -> (@moonvec.NDArray, @moonvec.NDArray) raise @moonvec.VecError

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.

nametype
—@moonvec.NDArray
pub fn solve(@moonvec.NDArray, @moonvec.NDArray) -> @moonvec.NDArray raise @moonvec.VecError

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.

nametype
—@moonvec.NDArray
—@moonvec.NDArray
pub fn svd(@moonvec.NDArray) -> (@moonvec.NDArray, @moonvec.NDArray, @moonvec.NDArray) raise @moonvec.VecError

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.

nametype
—@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

pub(all) enum Layout { Mode(Int32, Int32) Tuple(Array[Layout]) }

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
pub fn Layout::coalesce(Self) -> Self

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.

pub fn Layout::cosize(Self) -> Int32

One past the largest offset the layout can produce, which is how much space a buffer of this layout needs.

pub fn Layout::divide(Self, tile~ : Int32) -> (Self, Self) raise LayoutError

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.

nametype
selfSelfthe receiver
tileInt32labelled
pub fn Layout::offset(Self, Array[Int32]) -> Int32 raise LayoutError

The offset of a coordinate, one entry per mode in flattened order.

nametype
selfSelfthe receiver
—Array[Int32]
pub fn Layout::offset_of_index(Self, Int32) -> Int32 raise LayoutError

The offset of the element at index, counting the elements in row-major order as the layout itself orders them.

nametype
selfSelfthe receiver
—Int32
pub fn Layout::shape(Self) -> Array[Int32]

The shape of every mode, flattened from the outside in.

pub fn Layout::size(Self) -> Int32

The number of elements the layout addresses.

pub fn Layout::strides(Self) -> Array[Int32]

The stride of every mode, flattened the same way as shape, so the two arrays line up.

pub fn Layout::to_string(Self) -> String

The layout in size:stride form, with modes nested in parentheses.

Functions

pub fn compact(Array[Int32]) -> Layout

A layout of size elements with no gaps, the usual row-major arrangement.

nametype
—Array[Int32]
pub fn mode(size~ : Int32, stride~ : Int32) -> Layout

A single mode, the building block of every other layout.

nametype
sizeInt32labelled
strideInt32labelled
pub fn product(Layout, Layout) -> Layout

Two layouts side by side, which is how a thread index and a value index combine into one offset.

nametype
—Layout
—Layout

Errors

pub(all) suberror LayoutError { RankMismatch(want~ : Int32, got~ : Int32) IndexOutOfRange(index~ : Int32, size~ : Int32) NotDivisible(tile~ : Int32, size~ : Int32) EmptyLayout }

What a layout operation refuses.

pub fn LayoutError::message(Self) -> String

One line naming what the layout algebra refused.

No item matches that filter.