Crate russell_lab

source ·
Expand description

Russell - Rust Scientific Library

russell_lab: Scientific laboratory for linear algebra and numerical mathematics

Important: This crate depends on external libraries (non-Rust). Thus, please check the Installation Instructions on the GitHub Repository.

§Introduction

This crate implements specialized mathematical functions (e.g., Bessel, Chebyshev, Erf, Gamma, Heaviside, Logistic, …) and functions to perform linear algebra computations (e.g., Matrix, Vector, Matrix-Vector, Eigen-decomposition, SVD, Inverse, …). This crate also implements a set of helpful function for comparing floating-point numbers, measuring computer time, reading table-formatted data, and more.

The code shall be implemented in native Rust code as much as possible. However, thin interfaces (“wrappers”) are implemented for some of the best tools available in numerical mathematics, including OpenBLAS and Intel MKL.

The code is organized in modules:

  • algo – Structs and algorithms that roughly depend on the other modules
  • base – “Base” functionality to help other modules
  • check – Functions to assist in unit and integration testing
  • math (not re-exported) – Mathematical “special” functions and constants
  • matrix – Matrix struct and associated functions
  • matvec – Functions operating on matrices and vectors
  • vector – Vector struct and associated functions

§Linear algebra

For linear algebra, the main structures are NumVector and NumMatrix, that are generic Vector and Matrix structures. The Matrix data is stored as column-major. The Vector and Matrix are f64 and Complex64 aliases of NumVector and NumMatrix, respectively.

The linear algebra functions currently handle only (f64, i32) pairs, i.e., accessing the (double, int) C functions. We also consider (Complex64, i32) pairs.

There are many functions for linear algebra, such as (for Real and Complex types):

§Complex numbers

For convenience, this library re-exports:

  • num_complex::Complex64 – Needed for ComplexMatrix, ComplexVector and complex functions
  • num_complex::ComplexFloat – Needed for the intrinsic Complex64 operators such as abs() an others

When using the crate::cpx! macro, the Complex64 type must be imported as well. For example:

use russell_lab::{cpx, Complex64};

println!("{}", cpx!(1.0, 2.0));

§Examples

§(matrix) Eigen-decomposition of a small matrix

use russell_lab::{mat_eigen, Matrix, Vector};
use russell_lab::StrError;

fn main() -> Result<(), StrError> {
    let data = [
        [2.0, 0.0, 0.0],
        [0.0, 3.0, 4.0],
        [0.0, 4.0, 9.0],
    ];
    let mut a = Matrix::from(&data);
    let m = a.nrow();
    let mut l_real = Vector::new(m);
    let mut l_imag = Vector::new(m);
    let mut v_real = Matrix::new(m, m);
    let mut v_imag = Matrix::new(m, m);
    mat_eigen(&mut l_real, &mut l_imag, &mut v_real, &mut v_imag, &mut a)?;
    println!("eigenvalues =\n{}", l_real);
    println!("eigenvectors =\n{}", v_real);
    Ok(())
}

§(math) Bessel functions

use russell_lab::math;
use russell_lab::{Vector, StrError};

fn main() -> Result<(), StrError> {
    let xx = Vector::linspace(0.0, 15.0, 101)?;
    let j0 = xx.get_mapped(|x| math::bessel_j0(x));
    let j1 = xx.get_mapped(|x| math::bessel_j1(x));
    let j2 = xx.get_mapped(|x| math::bessel_jn(2, x));
    Ok(())
}

Re-exports§

Modules§

  • This module implements algorithms built from base, math, and vector-matrix routines
  • This module implements a “base” functionality to help other modules
  • This module contains functions to compare float numbers and arrays for unit testing
  • This module implements mathematical (specialized) functions and constants
  • This module contains functions for calculations with matrices
  • This module contains functions for calculations with matrices and vectors
  • This module contains functions for calculations with vectors

Macros§

  • Allocates a new Complex64 number

Traits§

  • Generic trait for floating point complex numbers.

Type Aliases§

  • Defines complex numbers with f64-real and f64-imaginary parts
  • Defines the error output as a static string