Kwker

Kwker for MATLAB and GNU Octave

The +kwker package: Kwker's sorts and selections with the semantics of MATLAB's own functions, through one MEX gateway (kwker_mex.c) over the C ABI. Real, full numeric arrays of every class (double, single, int8 ... uint64) run through Kwker; anything else (logical, char, complex, sparse, ...) falls back to the built-in.

Function What it returns
B = kwker.sort(A), [B, I] = kwker.sort(A, dim, 'descend', 'MissingPlacement', p, 'Threads', t) sort's result: along the first non-singleton dimension (or dim), stable indices I
[B, I] = kwker.maxk(A, k, dim, 'Threads', t) / kwker.mink(...) the k largest / smallest along a dimension, in order, by selection (NaNs after every number)
[B, I] = kwker.kth(A, k, dim, 'descend') the k-th element of the sorted order along a dimension (what S(k, :) of S = sort(A) holds)
kwker.version() the library version and the engine it runs (AVX-512, AVX2, NEON, portable)

Ties follow the built-in sort: -0 equals +0 and every NaN equals every other NaN whatever its bit pattern - such elements keep their input order, and the results hold the input's own values. NaN placement follows MATLAB: last ascending, first descending ('MissingPlacement' changes it); maxk / mink put NaNs last both ways. GNU Octave warns once that +kwker/sort.m shadows the built-in sort (it does not: package functions are called by their qualified name); warning('off', 'Octave:shadowed-function') silences it. The folder holds the package, the MEX file and three internal helpers (kwker_args, kwker_front, kwker_topk: Octave has no private folders in packages).

Build

From a Kwker C package (ss release tarball or ss capi --install <prefix>), in MATLAB or Octave:

MATLABPut the +kwker folder on the MATLAB or Octave path.
cd bindings/matlab
build_mex('/path/to/kwker-prefix')   % links the static library: the MEX file needs nothing beside it
addpath(pwd)
[B, I] = kwker.sort(randn(1e6, 1));

In this repository ss native --lang octave builds the MEX file with mkoctfile --mex against target/release/libkwker_c.a and runs test/test_kwker.m in GNU Octave per engine (--isa): the built-in sort as the reference over every class, shape and direction, the index outputs, NaN placement, the multithreaded path, the fallbacks, then 1M-key timings.