nalgebra

repository·main·Indexed 26 days ago

https://github.com/dimforge/nalgebra

A comprehensive general-purpose linear algebra library for Rust providing high-performance implementations of vectors, matrices, and geometric transformations. It supports both statically-sized and dynamically-sized matrices and includes the nalgebra-glm package for GLM-style syntax and operations, as well as nalgebra-lapack for LAPACK provider integration.

Tokens
11.5K
Snippets
16
Records
112
Agent score
89%

What's inside nalgebra

  1. Overview of nalgebra

    main
    nalgebra is a linear algebra library for the Rust programming language. It provides tools for working with vectors, matrices, and other geometric structures, suitable for applications in computer graphics, physics, robotics, and general scientific computing.
  2. Configure LAPACK for Ubuntu (Netlib)

    main

    To build nalgebra-lapack against the system installation of Netlib on Ubuntu (tested on 24.04) without LAPACKE or CBLAS, install the required system dependencies and set the corresponding environment variables before building.

    sudo apt-get install gfortran libblas-dev liblapack-dev
    export CARGO_FEATURE_SYSTEM_NETLIB=1
    export CARGO_FEATURE_EXCLUDE_LAPACKE=1
    export CARGO_FEATURE_EXCLUDE_CBLAS=1
    
    export CARGO_FEATURES="--no-default-features --features lapack-netlib"
    cargo build ${CARGO_FEATURES}
  3. Select a LAPACK provider via Cargo features

    main

    The nalgebra-lapack crate uses Cargo features to determine which LAPACK implementation to use. To select a specific provider, you must disable default features and enable the desired lapack-* feature.

    Common patterns include:

    • Using lapack-netlib for system Netlib installations.
    • Using lapack-accelerate for Apple's Accelerate framework on macOS.
  4. Core mathematical and geometric types in nalgebra

    main

    The library provides a wide range of types for linear algebra, computer graphics, and physics:

    • Matrices and Vectors: A single Matrix type for vectors and matrices. Supports compile-time dimensions (statically allocated) or runtime dimensions (heap allocated).
    • Aliases: Convenient aliases like Vector1 to Vector6 and Matrix1x1 to Matrix6x6.
    • Points: Point1 to Point6 (compile-time sizes).
    • Rotations and Transformations:
      • Translation2, Translation3 (Translations)
      • Rotation2, Rotation3 (Rotation matrices)
      • Quaternion, UnitQuaternion (3D rotation)
      • UnitComplex (2D rotation)
      • Isometry2, Isometry3 (Translation $\times$ Rotation)
      • Similarity2, Similarity3 (Translation $\times$ Rotation $\times$ Uniform Scale)
      • Affine2, Affine3 (Affine transformations)
      • Projective2, Projective3 (Projective transformations)
      • Transform2, Transform3 (General transformations)
      • Perspective3, Orthographic3 (3D projections)
    • Factorizations: Cholesky, QR, LU, FullPivLU, SVD, Schur, Hessenberg, SymmetricEigen.
    • Specialized Types: Unit<T> for algebraic entities with a norm of one.
  5. Recommended usage pattern for nalgebra

    main

    While most functionalities are available in the root module nalgebra::, the recommended way to use the library is to import types and traits explicitly and use the na:: prefix for free-functions.

    #[macro_use]
    extern crate approx; // For the macro assert_relative_eq!
    extern crate nalgebra as na;
    use na::{Vector3, Rotation3};
    
    fn main() {
        let axis  = Vector3::x_axis();
        let angle = 1.57;
        let b     = Rotation3::from_axis_angle(&axis, angle);
    
        assert_relative_eq!(b.axis().unwrap(), axis);
        assert_relative_eq!(b.angle(), angle);
    }
    #[macro_use]
    extern crate approx; // For the macro assert_relative_eq!
    extern crate nalgebra as na;
    use na::{Vector3, Rotation3};
    
    fn main() {
        let axis  = Vector3::x_axis();
        let angle = 1.57;
        let b     = Rotation3::from_axis_angle(&axis, angle);
    
    assert_relative_eq!(b.axis().unwrap(), axis);
        assert_relative_eq!(b.angle(), angle);
    }
  6. Install nalgebra via Cargo

    main

    To use nalgebra in your Rust project, add it to your Cargo.toml file. It is recommended to use the latest version.

    [dependencies]
    nalgebra = "*"
    [dependencies]
    // TODO: replace the * by the latest version.
    nalgebra = "*"
  7. Install nalgebra-glm

    main

    To use nalgebra-glm in your Rust project, add it to your Cargo.toml dependencies:

    [dependencies]
    nalgebra-glm = "0.3"

    It is strongly recommended to use a crate alias in your lib.rs or main.rs so you can use the glm:: prefix instead of the more verbose nalgebra_glm:::

    extern crate nalgebra_glm as glm;
    [dependencies]
    nalgebra-glm = "0.3"
  8. Select a LAPACK backend via Cargo features

    main

    nalgebra-lapack uses Cargo features to select a LAPACK provider. By default, it uses lapack-netlib (via lapack-src), which bundles Netlib and requires a FORTRAN compiler.

    Important: You must set default-features = false in your Cargo.toml when selecting a different backend to avoid multiple feature conflicts.

    Available Backends

    • lapack-netlib: Bundled Netlib reference implementation (Default).
    • lapack-openblas: Uses OpenBLAS.
    • lapack-accelerate: Uses Apple's Accelerate framework.
    • lapack-mkl: Alias for lapack-mkl-static-seq (Intel MKL).
    • lapack-mkl-static-seq: Statically link the sequential version of Intel MKL.
    • lapack-mkl-static-par: Statically link the parallel version of Intel MKL.
    • lapack-mkl-dynamic-seq: Dynamically link the sequential version of Intel MKL.
    • lapack-mkl-dynamic-par: Dynamically link the parallel version of Intel MKL.
    • lapack-custom: Use a custom LAPACK backend provided at link time. You must ensure the backend is ABI compatible with the lapack crate.
  9. Construct vectors, matrices, and quaternions in nalgebra-glm

    main

    You can construct algebraic types using several patterns:

    • Lower-case functions: Use functions named after the type in lower-case. For example, glm::vec3(x, y, z) creates a 3D vector.
    • ::new constructor: Use the standard constructor, e.g., Vec3::new(x, y, z).
    • make_ functions: Build types from slices using functions like glm::make_vec3(&[x, y, z]). Note that matrices constructed this way must have components in column-major order.
    • Geometric construction: Use functions like glm::rotation(angle, axis) to build a 4x4 homogeneous rotation matrix.
    • Swizzling: You can use nalgebra's native swizzling on nalgebra-glm vectors (up to dimension 3) to create new vectors from components (e.g., v.yxz() creates a Vec3 from components y, x, and z).