Rust Logonalgebra

nalgebra is a generic and high-performance linear algebra library for the Rust programming language. It is designed to be highly ergonomic and efficient, making it a popular choice for applications requiring mathematical operations on vectors, matrices, and other geometric entities, such as computer graphics, physics simulations, game development, robotics, and scientific computing.

Key features and characteristics of nalgebra include:

* Genericity: nalgebra is generic over the scalar type, meaning you can use it with `f32`, `f64`, or even custom scalar types that implement the necessary traits. It also supports both fixed-size and dynamically-sized vectors and matrices.
* Compile-time Dimension Checking: For fixed-size vectors and matrices, nalgebra leverages Rust's type system to perform dimension checking at compile time. This prevents many common linear algebra errors (e.g., trying to add matrices of incompatible dimensions) from even compiling, leading to more robust code.
* Performance: The library is optimized for performance, often leveraging SIMD (Single Instruction, Multiple Data) instructions through optional integration with crates like `simd-rs`. It aims for zero-cost abstractions wherever possible.
* Comprehensive Functionality: It provides a rich set of functionalities for:
* Vectors: Creation, addition, subtraction, scalar multiplication, dot product, cross product, normalization, magnitude, component-wise operations.
* Matrices: Creation, addition, subtraction, multiplication (matrix-matrix, matrix-vector), transpose, inverse, determinant, transformations (scaling, rotation, translation).
* Rotations and Transformations: Support for `Rotation`, `UnitQuaternion`, `Isometry`, and `Transform` types, essential for 3D graphics and physics.
* Points: Specific types for representing points in space.
* Ergonomics: nalgebra aims for an API that feels natural and intuitive for Rust developers, using operator overloading for common mathematical operations (e.g., `+`, `-`, `*`).
* Modularity: While `nalgebra` is the main crate, it’s built upon `alga` (Abstract Linear Geometric Algebra) which defines traits, making it extensible.

In essence, nalgebra brings the power and expressiveness of linear algebra tools (like NumPy for Python or Eigen for C++) to Rust, but with Rust's strong type system, memory safety guarantees, and emphasis on performance.

Example Code

```rust
use nalgebra::{Vector3, Matrix4, UnitQuaternion, Point3};
use std::f32::consts::PI;

fn main() {
    // --- Dependencies in Cargo.toml ---
    // [dependencies]
    // nalgebra = "0.32" // Use the latest stable version

    println!("--- nalgebra Example ---");

    // --- Vectors ---
    println!("\n--- Vectors ---");
    // Fixed-size 3D vector of f32
    let v1 = Vector3::new(1.0, 2.0, 3.0);
    let v2 = Vector3::new(4.0, 5.0, 6.0);
    println!("v1: {}", v1);
    println!("v2: {}", v2);

    // Vector addition
    let v_sum = v1 + v2;
    println!("v1 + v2: {}", v_sum);

    // Scalar multiplication
    let v_scaled = v1 * 2.5;
    println!("v1 * 2.5: {}", v_scaled);

    // Dot product
    let dot_product = v1.dot(&v2);
    println!("Dot product of v1 and v2: {}", dot_product);

    // Cross product (for 3D vectors)
    let cross_product = v1.cross(&v2);
    println!("Cross product of v1 and v2: {}", cross_product);

    // Magnitude (length) of a vector
    let magnitude = v1.norm();
    println!("Magnitude of v1: {}", magnitude);

    // Normalized vector
    let v1_normalized = v1.normalize();
    println!("Normalized v1: {}", v1_normalized);

    // --- Matrices ---
    println!("\n--- Matrices ---");
    // Fixed-size 4x4 matrix of f32 (often used for transformations)
    let m1 = Matrix4::new(
        1.0, 2.0, 3.0, 4.0,
        5.0, 6.0, 7.0, 8.0,
        9.0, 10.0, 11.0, 12.0,
        13.0, 14.0, 15.0, 16.0,
    );
    println!("m1:\n{}", m1);

    // Identity matrix
    let identity = Matrix4::identity();
    println!("Identity matrix:\n{}", identity);

    // Matrix multiplication
    let m2 = Matrix4::new(
        16.0, 15.0, 14.0, 13.0,
        12.0, 11.0, 10.0, 9.0,
        8.0, 7.0, 6.0, 5.0,
        4.0, 3.0, 2.0, 1.0,
    );
    let m_product = m1 * m2;
    println!("m1 * m2:\n{}", m_product);

    // Inverse of a matrix
    if let Some(m1_inverse) = m1.try_inverse() {
        println!("Inverse of m1:\n{}", m1_inverse);
        // To verify: m1 * m1_inverse should approximate the identity matrix
        // println!("m1 * m1_inverse:\n{}", m1 * m1_inverse);
    } else {
        println!("m1 is singular and cannot be inverted.");
    }

    // --- Transformations (Rotation, Translation) ---
    println!("\n--- Transformations ---");
    let point = Point3::new(1.0, 0.0, 0.0);
    println!("Original point: {}", point);

    // Create a rotation around the Y-axis by 90 degrees (PI/2 radians)
    let rotation = UnitQuaternion::from_axis_angle(&Vector3::y_axis(), PI / 2.0);
    let rotated_point = rotation * point;
    println!("Point rotated 90 deg around Y-axis: {}", rotated_point);

    // Create a translation vector
    let translation_vector = Vector3::new(2.0, 3.0, 4.0);
    let translated_point = point + translation_vector;
    println!("Point translated by (2,3,4): {}", translated_point);

    // Combine rotation and translation (Isometry)
    // Isometry represents a rigid transformation (rotation + translation)
    let transform = nalgebra::Isometry3::new(translation_vector, Vector3::y_axis() * (PI / 2.0));
    let transformed_point = transform * point;
    println!("Point transformed by rotation (90 deg Y) and translation (2,3,4): {}", transformed_point);
}
```