Coming from another library
If you sort with NumPy, PyTorch, the C++ or Rust standard library, Java, JavaScript, Go or .NET, the Kwker call is usually a one-word change. The results are the same; Kwker is faster. In Python you can skip even that change: see No code changes.
NumPy and PyTorch
| You write | With Kwker | Notes |
|---|---|---|
np.sort(a) |
kwker.sorted(a) |
a sorted copy |
a.sort() |
kwker.sort(a) |
in place |
np.argsort(a, kind="stable") |
kwker.argsort(a) |
positions as uint64 |
np.partition(a, k) |
kwker.select(a, k) |
in place; a[k] is the value of rank k |
np.argpartition(a, k) |
kwker.argpartition(a, k) |
|
torch.topk(x, k) |
kwker.top_ |
the values and their positions |
np.searchsorted(s, v) |
kwker.searchsorted(s, v) |
|
np.unique(a) |
kwker.unique(a) |
the same options: index, inverse, counts |
scipy.stats.rankdata(a) |
kwker.rank(a) |
average ranks for ties, as SciPy |
import numpy as np
import kwker
a = np.random.default_rng(0).random(1000)
print(np.array_equal(kwker.sorted(a), np.sort(a)))
print(np.array_equal(kwker.argsort(a), np.argsort(a, kind="stable")))
values, positions = kwker.top_k(a, 3, descending=True)
print(np.array_equal(values, np.sort(a)[::-1][:3]))
Output
True True True
No code changes
Two calls make the libraries you already use run on Kwker, with no other change to your program:
kwker.numpy_ops.install():np.sort,np.argsort,np.partition,np.uniqueand the other NumPy calls Kwker covers. See NumPy drop-in.kwker.torch_ops.install():torch.sort,torch.topk,torch.unique,torch.quantileand the other sorting calls of CPU tensors. See Speed up PyTorch inference.
C++
| You write | With Kwker |
|---|---|
std::sort(v.begin(), v.end()) |
kwker::sort(v) |
std::sort(v.begin(), v.end(), std::greater<>()) |
kwker::sort(v, kwker::Order::descending) |
std::nth_ |
kwker::select(v.data(), v.size(), k) |
std::partial_ |
kwker::partial_ |
an index vector sorted with std::stable_ |
kwker::argsort(v.data(), v.size()) |
kwker::sort takes a std::vector, a std::span or a pointer and a length. See the C++ reference.
Rust
| You write | With Kwker |
|---|---|
v.sort_ |
kwker::sort(&mut v) |
v.sort_ |
kwker::sort(&mut v) - floats sort directly |
v.sort_ |
kwker::sort_ |
v.select_ |
kwker::select_ |
an index vector sorted with sort_ |
let idx: Vec<usize> = kwker::argsort(&v, Order::ASCENDING); |
Java, JavaScript, Go and .NET
| Language | You write | With Kwker |
|---|---|---|
| Java | Arrays.sort(a) |
Kwker.sort(a) |
| Java | the k largest | Kwker.topK(a, k, Kwker.DESCENDING) (their indices) |
| JavaScript | a.sort() on a typed array |
kwk.sort(a) after const kwk = require("kwker") |
| JavaScript | the k largest | kwk.topK(a, k, { descending: true }) |
| Go | slices.Sort(s) |
kwker.Sort(s) |
| Go | the k largest | kwker.TopK(s, k, kwker.Descending, true) (values and positions) |
| .NET | Array.Sort(a) |
Sorter.Sort(a) |
| .NET | the k largest | Sorter.TopK(a, k, Order.Descending) (values and indices) |
Every language has the same calls - sort, select, partial sort, argsort, top-k, search - under its own naming. The API reference has a page per language.
Notes
- Kwker's sort is not stable by default, like
std::sortandsort_unstable; equal numbers are identical, so this only matters when values travel with keys.argsortand the key-value sorts withstablekeep equal keys in their input order. - NaN values sort last, as in NumPy. Behavior has the full order rules.
Related
- Sorting and Top-k and selection: the calls in detail.
- Languages: installing Kwker for each language.