N-dimensional arrays with dtypes, broadcasting, universal functions and linear algebra.
moon add moonbitstack/moonveclet a = @moonvec.from_doubles([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [2, 3])
let b = @moonvec.transpose(a)
println(@moonvec.matmul(a, b).to_string())array([[14.0, 32.0],
[32.0, 77.0]], dtype=float64)let zeros = @moonvec.zeros([2, 3])
let ones = @moonvec.ones([2, 3], dtype=@moonvec.I32)
let filled = @moonvec.full([2, 2], @moonvec.Real(0.5), dtype=@moonvec.F32)
let stepped = @moonvec.arange(start=0.0, stop=1.0, step=0.25)
let spaced = @moonvec.linspace(start=0.0, stop=1.0, num=5)
let identity = @moonvec.eye(n=3, dtype=@moonvec.F64)
println(stepped.to_string())
println(spaced.to_string())array([0.0, 0.25, 0.5, 0.75], dtype=float64)
array([0.0, 0.25, 0.5, 0.75, 1.0], dtype=float64)let a = @moonvec.from_doubles([1.0, 2.0, 3.0, 4.0], [2, 2])
a.set_at([1, 0], @moonvec.Real(9.0))
println(a.to_string())
println(a.at([1, 0]).to_string())array([[1.0, 2.0],
[9.0, 4.0]], dtype=float64)
9.0let a = @moonvec.from_doubles([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [2, 3])
let row = @moonvec.slice(a, [@moonvec.At(1)])
let column = @moonvec.slice(a, [@moonvec.All, @moonvec.At(2)])
let window = @moonvec.slice(a, [@moonvec.All, @moonvec.span(1, 3)])
println(row.to_string())
println(column.to_string())
println(window.is_contiguous())array([4.0, 5.0, 6.0], dtype=float64)
array([3.0, 6.0], dtype=float64)
falselet a = @moonvec.arange(start=0.0, stop=6.0)
let picked = @moonvec.take(a, [4, 0, 2])
let kept = @moonvec.mask(a, [true, false, true, false, false, true])
println(picked.to_string())
println(kept.to_string())array([4.0, 0.0, 2.0], dtype=float64)
array([0.0, 2.0, 5.0], dtype=float64)let column = @moonvec.from_doubles([1.0, 2.0, 3.0], [3, 1])
let row = @moonvec.from_doubles([10.0, 20.0, 30.0], [1, 3])
println((column + row).to_string())
let counts = @moonvec.full([3], @moonvec.Signed(7L), dtype=@moonvec.I32)
println(@moonvec.divide(counts, counts).to_string())array([[11.0, 21.0, 31.0],
[12.0, 22.0, 32.0],
[13.0, 23.0, 33.0]], dtype=float64)
array([1.0, 1.0, 1.0], dtype=float64)let angles = @moonvec.linspace(start=0.0, stop=3.141592653589793, num=3)
println(@moonvec.sin(angles).to_string())
println(@moonvec.erf(@moonvec.from_doubles([0.0, 1.0, 2.0], [3])).to_string())array([0.0, 1.0, 1.2246467991473532e-16], dtype=float64)
array([0.0, 0.8427007929497147, 0.9953222650189527], dtype=float64)let a = @moonvec.from_doubles([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [2, 3])
println(@moonvec.sum(a, axis=0).to_string())
println(@moonvec.max(a, axis=1, keepdims=true).to_string())
println(@moonvec.argmax(a).to_string())
println(@moonvec.mean(a).to_string())array([5.0, 7.0, 9.0], dtype=float64)
array([[3.0],
[6.0]], dtype=float64)
array(5, dtype=int64)
array(3.5, dtype=float64)let a = @moonvec.arange(start=0.0, stop=6.0)
println(@moonvec.reshape(a, [2, 3]).to_string())
println(@moonvec.reshape(a, [-1, 2]).shape().map(fn(n) { n.to_string() }).join(" by "))
let rows = @moonvec.stack([a, a], axis=0)
println(rows.shape().map(fn(n) { n.to_string() }).join(" by "))
for part in @moonvec.split(rows, parts=2, axis=1) {
println(part.shape().map(fn(n) { n.to_string() }).join(" by "))
}array([[0.0, 1.0, 2.0],
[3.0, 4.0, 5.0]], dtype=float64)
3 by 2
2 by 6
2 by 3
2 by 3let a = @moonvec.from_doubles([3.0, 1.0, 2.0, 5.0, 4.0], [5])
println(@moonvec.sort(a).to_string())
println(@moonvec.argsort(a).to_string())
let values = @moonvec.from_doubles([-2.0, 0.5, 3.0], [3])
let zeros = @moonvec.zeros([3])
println(@moonvec.select(@moonvec.greater(values, zeros), values, zeros).to_string())
println(@moonvec.clip(values, min=0.0, max=1.0).to_string())array([1.0, 2.0, 3.0, 4.0, 5.0], dtype=float64)
array([1, 2, 0, 4, 3], dtype=int64)
array([0.0, 0.5, 3.0], dtype=float64)
array([0.0, 0.5, 1.0], dtype=float64)let a = @moonvec.matrix([[3.0, 1.0], [1.0, 2.0]])
let b = @moonvec.from_doubles([9.0, 8.0], [2])
println(@linalg.solve(a, b).to_string())
println(@moonvec.format_real(@linalg.det(a)))array([2.0, 3.0], dtype=float64)
5.0let layout = @cute.compact([2, 3])
println(layout.to_string())
println(layout.offset([1, 2]))
let (tile, tiles) = @cute.compact([6]).divide(tile=3)
println(tile.to_string())
println(tiles.to_string())(_2:_3, _3:_1)
5
(_3:_1)
(_2:_3)| Area | Interface |
|---|---|
| dtype | DType with Bool, I8, I16, I32, I64, U8, U16, U32, U64, F16, F32, F64, C64, C128; item_size, name, parse, is_bool, is_integer, is_signed, is_float, is_complex, is_real, is_exact, to_real, float_size |
| promotion | promote, promote_all, division_dtype |
| values | Number (Boolean, Signed, Unsigned, Real, Complex), Complex, format_real |
| buffer | Buffer (new, from_bytes, dtype, len, read, write, fill, to_bytes) |
| array | NDArray (new, shape, strides, offset, ndim, size, dtype, buffer, is_contiguous, is_scalar, at, set_at, item, to_numbers, to_doubles, copy, to_string) |
| creation | zeros, ones, full, zeros_like, ones_like, full_like, arange, linspace, eye, from_doubles, from_numbers, from_bytes, array, matrix |
| casting | astype |
| indexing | Index (All, At, Span, NewAxis, Take, Mask), span, slice, take, mask |
| broadcasting | broadcast, broadcast_to, broadcast_shape, broadcast_strides |
| arithmetic | add, subtract, multiply, divide, floor_divide, remainder, mod, power, maximum, minimum, arctan2, hypot, bitwise_and, bitwise_or, bitwise_xor, left_shift, right_shift, invert, negative, positive, absolute, fabs, sign, square, reciprocal, clip, select, and the scalar forms add_scalar, subtract_scalar, multiply_scalar, divide_scalar, power_scalar, maximum_scalar, minimum_scalar |
| comparisons | equal, not_equal, less, less_equal, greater, greater_equal, equal_nan, logical_and, logical_or, logical_xor, logical_not, isnan, isinf, isfinite, signbit |
| mathematics | 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, round, degrees, radians, real, imag, angle, conj |
| special functions | gamma, lgamma, digamma, erf, erfc, erfinv, erfcinv, beta, zeta, bessel_j, bessel_y, bessel_i, bessel_k |
| reductions | sum, prod, min, max, mean, variance, std, any, all, argmin, argmax |
| shape | reshape, transpose, swap_axes, move_axis, ravel, flatten, squeeze, expand_dims |
| joining | concatenate, stack, hstack, vstack, split, split_at |
| ordering | sort, argsort |
| products | matmul, dot, inner, outer, trace |
| backend | Kernel (name, binary, unary, reduce, matmul), Cpu, cpu, BinaryOp, UnaryOp, ReduceOp |
| errors | VecError (ByteCountMismatch, IndexOutOfBounds, ShapeMismatch, AxisOutOfRange, CannotReshape, EmptyAxis, Domain, NotSolvable), message |
| Area | Interface |
|---|---|
| solving | solve, inv, det, cholesky |
| factoring | qr, lstsq, svd, eigh, matrix_power, kron |
| measuring | Norm (Frobenius, One, Two, Infinity, NegInfinity, P), norm, matrix_norm |
| Area | Interface |
|---|---|
| layout | Layout (Mode, Tuple), mode, compact, product |
| measuring | size, cosize, shape, strides, offset, offset_of_index |
| algebra | coalesce, divide |
| errors | LayoutError (RankMismatch, IndexOutOfRange, NotDivisible, EmptyLayout), message |
| Group | Cases | Passing |
|---|---|---|
| dtype promotion, every pair of the fourteen dtypes | 196 | 196 |
| arithmetic, comparison and logical operations over 24 dtype pairs and 10 shape pairs | 1 267 | 1 267 |
| mathematics over float and complex values, including the edges | 106 | 106 |
| reductions with every axis and keepdims, over five dtypes and two ranks | 880 | 880 |
| products, shape, joining, slicing, sorting, choice and casting | 110 | 110 |
| linear algebra: solving, determinants, Cholesky, least squares, norms, powers, products | 29 | 29 |
| total | 2 588 | 2 588 |
pub(open) trait Kernel {
fn name(Self) -> String
fn binary(Self, op : BinaryOp, dtype : DType, a : Buffer, a_at : Int, a_step : Int, b : Buffer, b_at : Int, b_step : Int, out : Buffer, out_at : Int, out_step : Int, len : Int) -> Unit raise VecError
fn unary(Self, op : UnaryOp, dtype : DType, a : Buffer, a_at : Int, a_step : Int, out : Buffer, out_at : Int, out_step : Int, len : Int) -> Unit raise VecError
fn matmul(Self, dtype : DType, a : Buffer, a_at : Int, b : Buffer, b_at : Int, out : Buffer, out_at : Int, m : Int, k : Int, n : Int) -> Unit raise VecError
fn reduce(Self, op : ReduceOp, dtype : DType, a : Buffer, a_at : Int, a_step : Int, len : Int, into : Number) -> Number raise VecError
}pub(all) suberror VecError {
ByteCountMismatch(total~ : Int, item_size~ : Int)
IndexOutOfBounds(index~ : Int, len~ : Int)
ShapeMismatch(want~ : Array[Int], got~ : Array[Int])
AxisOutOfRange(axis~ : Int, ndim~ : Int)
CannotReshape(size~ : Int, shape~ : Array[Int])
EmptyAxis(axis~ : Int)
Domain(message~ : String)
NotSolvable(message~ : String)
}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
}pub(all) struct Complex {
re : Double
im : Double
}pub struct Cpu {
lanes : Int
}pub(all) enum DType {
Bool
I8
I16
I32
I64
U8
U16
U32
U64
F16
F32
F64
C64
C128
}pub(all) enum ReduceOp {
Sum
Product
Minimum
Maximum
Any
All
}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
}fn compare_real(left : Double, right : Double) -> Intfn format_real(value : Double) -> StringInstall
Download zipN-dimensional arrays with dtypes, broadcasting, universal functions and linear algebra.