Nimplex
NIM simPLEX: A concise scientific Nim library (with CLI and Python binding) providing samplings, uniform grids, and traversal graphs in compositional (simplex) spaces.
Install / Use
/learn @amkrajewski/NimplexREADME
nimplex
<a href="https://nimble.directory/pkg/nimplex"><img src="https://raw.githubusercontent.com/amkrajewski/nimble-badge/master/badges/nimplex.svg" alt="Nimble Package" height="20"></a>
NIM simPLEX: A concise high-performance scientific Nim library (with CLI and Python binding) providing samplings, uniform grids, traversal graphs, and more in compositional (simplex) spaces, where traditional methods designed for euclidean spaces fail or otherwise become impractical.
Such spaces are considered when an entity can be split into a set of distinct components (a composition), and they play a critical role in many disciplines of science, engineering, and mathematics. For instance, in materials science, chemical composition refers to the way a material (or, more generally, matter) is split into distinct components, such as chemical elements, based on considerations such as fraction of atoms, occupied volume, or contributed mass. And in economics, portfolio composition may refer to how finite capital is split across assets, such as cash, equity instruments, real estate, and commodities, based on their monetary value.
Quick Start
If you have a GitHub account, you can get started with nimplex very quickly by just clicking the button below to launch a CodeSpaces environment with everything installed (per instructions in Reproducible Installation section) and ready to go! From there, you can either use the CLI tool (as explained in CLI section) or import the library in Python (as explained in Usage in Python section) and start using it right away. Of course, it also comes with a full Nim compiler and VSCode IDE extensions for Nim, so you can efortlessely modify/extend the source code and re-compile it if you wish.
Installation
There are several easy ways to quickly get nimplex up and running on your system. The choice depends primarily on your preffered way of interacting with the library (CLI, Nim, or Python) and your system configuration.
Reproducible Installation (recommended)
The recommended way is compiling the library yourself, which may sound scary but is fairly easily and the whole process should not take more than a couple of minutes.
Nim (compiler)
First, you need to install Nim language compiler which on most Unix (Linux/MacOS) systems is very straightforward.
-
On MacOS, assuming you have Homebrew installed, simply:
brew install nim -
Using
conda,miniconda,mamba, ormicromambacross-platform package manager:conda install -c conda-forge nim -
On most Linux distributions, you should also be able to use your built-in package manager like
pacman,apt,yum, orrpm; however, the default channel/repository, especially on enterprise systems, may have an unsupported version (nim<2.0). While we do testnimCSOwith1.6versions too, your experience may be degraded, so you may want to update it or go with another option. -
You can, of course, also build it yourself from
nimsource code! It is relatively straightforward and fast compared to many other languages.
On Windows, you may consider using WSL, i.e., Windows Subsystem for Linux, which is strongly recommended, interplays well with VS Code, and will let you act as if you were on Linux. If you need to use Windows directly, you can follow these installation instructions.
nimplex
Then, you can use the bundled Nimble tool (package manager for Nim, similar to Rust's crate or Python's pip) to install two top-level nim dependencies:
arraymancer, which is a powerful N-dimensional array librarynimpywhich helps with the Python bindings.
It's a single command:
nimble install --depsOnly
or, explicitly:
nimble install -y arraymancer nimpy
Finally, you can clone the repository and compile nimplex with:
git clone https://github.com/amkrajewski/nimplex
cd nimplex
nim c -r -d:release nimplex.nim --benchmark
which will compile the library and run a few benchmarks to make sure everything runs smoothly. You should then see a compiled binary file nimplex in the current directory which exposes the CLI tool.
If you want to use the Python bindings, you can now compile the library with slightly different flags (depending on your system configuration) like so for Linux/MacOS:
nim c --d:release --threads:on --app:lib --out:nimplex.so nimplex
and you should see a compiled library file nimplex.so in the current directory which can be immediately imported and used in Python as explained later. For Windows and other platforms, consult nimpy documentation on what flags and formats should be used.
Pre-Compiled Binaries (quick but not recommended)
If you happen to be on one of the common systems (for which we auto-compile the binaries) and you do not need to modify anything in the source code, there is a good chance you can simply download the latest release from the nimplex GitHub repository and run the executable (nimplex / nimplex.exe) or Python library (nimplex.so / nimplex.pyd) directly just by placing it in your working directory and using it as:
- An interactive command line interface (CLI) tool, which will guide you through how to use it if you run it without any arguments like so (on Linux/MacOS):
or with a concise configuration defining the task type and parameters (explained later in Usage in Nim):./nimplex./nimplex -c IFP 3 10 - An compiled Python library for Unix, which you can import and use in your Python code like so:
and immediately use the functions provided by the library, as described in Usage in Python:import nimplexnimplex.simplex_internal_grid_fractional(dim=3, ndiv=10)
Capabilities
Note: Full technical discussion of methods and motivations is provided in the manuscript. The sections below are meant to provide a concise overview of the library's capabilities.
The library provides a growing number of methods specific to compositional (simplex) spaces:
-
Monte Carlo sampling is the simplest method conceptually, where points are rendomly sampled from a simplex. In low dimensional cases, this can be accomplished by sampling from a uniform distribution in (d-1)-Cartesian space and then rejecting points outside the simplex (left panel below). However, in this approach, the inefficiency growth is factorial with the dimensionality of the simplex space. Instead, some try to sample from a uniform distribution in (d)-Cartesian space and normalize the points to sum to 1, however, this leads to over-sampling in the center of each simplex dimension (middle panel below).
One can, however, fairly easily sample from a special case of Dirichlet distribution, as explained in the manuscript, which leads to uniform sampling in the simplex space (right panel below). Nimplex can sample around 10M points per second in 9-dimensional space on a modern CPU.
<img src="https://raw.githubusercontent.com/amkrajewski/nimplex/main/assets/Fig1.png" alt="Monte Carlo Sampling" width="800" style="display: block; margin-left: auto; margin-right: auto;"/> -
Simplex / Compositional Grids are a more structured approach to sampling, where all possible compositions quantized to a given resolution, like 1% for 100 divisions per dimension, are generated. This is useful for example when one wants to map a function over the simplex space. In total
N_S(d, n_d) = \binom{d-1+n_d}{d-1} = \binom{d-1+n_d}{n_d}are generated, wheredis the dimensionality of the simplex space andn_dis the number of divisions per dimension. Nimplex uses a modified version of NEXCOM algorithm to do that procedurally (see manuscript for details) and can generate around 5M points per second in 9-dimensional space on a modern CPU.
Related Skills
node-connect
344.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
96.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
344.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
344.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
