3 releases (breaking)

0.3.0 May 25, 2022
0.2.0 Feb 17, 2022
0.1.0 Feb 11, 2022

#1575 in Math

46 downloads per month

BSD-2-Clause

33KB
577 lines

nnls

This is a Rust version of nnls (Non-Negative Least Squares). It's a port from a Fortran90 script used by scipy.optimize.nnls and probably several others.

Why?

I decided to port nnls to Rust because:

  • The C version of nnls is not thread-safe! There are several static variables throughout the code which make it unusable in parallel. If you plan to use nnls only in single-thread, you might prefer linking to that version: it has been heavily tested and has no known problem.
  • Both Fortran versions (77 and 90) require some knowledge of Fortran memory model and some compiler tricks to successfuly link against. I wasn't able to do it and I didn't want to waste more hours into this adventure, so I simply ported the script to Rust.

Advantages

  • It has been used several millions of times and it will be used billions of times soon. I work with 3D images and each image I see contains around 1 million voxels. For a specific algorithm, I need to call nnls 2 times for each voxel. Several of those images has been tested and compared with the original Fortran version.
  • There's no unsafe and it is forbidden to use any in this crate.

Problems

  • This is not idiomatic Rust. I tried cleaning and enhancing the code but it's not always possible.
  • I use ndarray. You might prefer something else. I do not plan to change this but you're welcome to discuss it.
  • There are currently 1 panic! in the code because there are 1 code paths in the original code which use goto that I couldn't translate properly to Rust. I planned to handle that case but it turns out that it's never called. As I wrote, nnls has been extensively used and I couldn't find any dataset that triggers that condition. If my programs ever panic because of this, it will be repaired quickly.

Dependencies

~3.5MB
~61K SLoC