Skip to main content

diffsol_la/
lib.rs

1//! # diffsol-la
2//!
3//! Linear algebra traits and backends for [diffsol](https://github.com/martinjrobins/diffsol).
4//!
5//! This crate provides the vector, matrix, and linear-solver abstractions used by
6//! diffsol, together with concrete implementations backed by
7//! [nalgebra](https://nalgebra.org), [faer](https://github.com/sarah-ek/faer-rs),
8//! suitesparse (KLU), and CUDA.
9
10/// Context objects for managing device resources for vectors and matrices (e.g. device streams, threading pools, etc.).
11///
12/// This module provides context types that encapsulate information about where data is stored and computed
13/// (CPU, GPU, etc.). Different backends like nalgebra and faer may require different context implementations.
14/// The [Context] trait defines the interface that must be implemented.
15pub mod context;
16
17#[cfg(feature = "cuda-oxide")]
18pub(crate) mod cuda_oxide_kernels;
19
20/// Error types and handling.
21///
22/// This module defines the [LaError] enum and specialized error variants for
23/// the linear algebra layer, including matrix, linear-solver, and (optional)
24/// CUDA errors.
25pub mod error;
26
27/// The [LinearOp] trait describing a linear operator `A` for use with [LinearSolver].
28pub mod linear_op;
29
30/// Linear solver implementations and traits.
31///
32/// This module defines the [LinearSolver] trait for solving linear systems and provides implementations:
33/// - Direct solvers: [NalgebraLU], [FaerLU], [FaerSparseLU]
34/// - Optional sparse solvers: `KLU` (requires `suitesparse` feature)
35/// - GPU solvers: `OxideLU` (requires `cuda-oxide` feature), `CudaLU` (deprecated, requires `cuda` feature)
36///
37/// The linear solver is a critical component used internally by nonlinear solvers to solve Newton systems.
38pub mod linear_solver;
39
40/// Matrix types and operations.
41///
42/// This module defines the [Matrix] trait and related traits for matrix operations:
43/// - [DenseMatrix] for dense column-major matrices
44/// - Sparsity detection and handling
45///
46/// Implementations are provided for:
47/// - Dense matrices: [NalgebraMat], [FaerMat]
48/// - Sparse matrices: [FaerSparseMat]
49/// - GPU matrices: `OxideMat` (requires `cuda-oxide` feature), `CudaMat` (deprecated, requires `cuda` feature)
50pub mod matrix;
51
52/// Scalar types and traits.
53///
54/// This module defines the [Scalar] trait that all floating-point types used in diffsol must implement.
55/// It aggregates requirements from nalgebra, faer, and num_traits to ensure compatibility with linear algebra operations.
56///
57/// Implementations are provided for `f32` and `f64`.
58/// GPU scalar types are available via `ScalarCuda` (requires `cuda-oxide` or `cuda` feature).
59pub mod scalar;
60
61/// Vector types and traits.
62///
63/// This module defines the [Vector] trait and related traits for vector operations:
64/// - [VectorView] and [VectorViewMut] for borrowed views
65/// - [VectorIndex] for index collections
66///
67/// Implementations are provided for:
68/// - [NalgebraVec] using nalgebra vectors
69/// - [FaerVec] using faer vectors
70/// - `OxideVec` for GPU computation (requires `cuda-oxide` feature), `CudaVec` (deprecated, requires `cuda` feature)
71pub mod vector;
72
73pub use error::LaError;
74
75pub use context::{faer::FaerContext, nalgebra::NalgebraContext, Context};
76
77pub use linear_op::LinearOp;
78pub use linear_solver::LinearSolver;
79pub use linear_solver::{faer::sparse_lu::FaerSparseLU, FaerLU, NalgebraLU};
80
81#[cfg(feature = "suitesparse")]
82pub use linear_solver::suitesparse::klu::KLU;
83
84pub use matrix::{
85    default_solver::DefaultSolver, dense_faer_serial::FaerMat, dense_nalgebra_serial::NalgebraMat,
86    sparse_faer::FaerSparseMat, DenseMatrix, Matrix, MatrixCommon,
87};
88
89pub(crate) use matrix::extract_block::ColMajBlock;
90pub use matrix::{
91    sparsity::Dense, sparsity::DenseRef, sparsity::MatrixSparsity, sparsity::MatrixSparsityRef,
92    MatrixRef,
93};
94
95pub use scalar::{scale, FaerScalar, IndexType, NalgebraScalar, Scalar, Scale};
96
97pub use vector::DefaultDenseMatrix;
98pub use vector::{
99    faer_serial::{FaerVec, FaerVecIndex, FaerVecMut, FaerVecRef},
100    nalgebra_serial::{NalgebraVec, NalgebraVecMut, NalgebraVecRef},
101    Vector, VectorCommon, VectorIndex, VectorRef, VectorView, VectorViewMut,
102};
103
104#[cfg(feature = "cuda")]
105#[allow(deprecated)]
106pub use context::cuda::CudaContext;
107#[cfg(feature = "cuda")]
108#[allow(deprecated)]
109pub use linear_solver::cuda::lu::CudaLU;
110#[cfg(feature = "cuda")]
111#[allow(deprecated)]
112pub use matrix::cuda::CudaMat;
113#[cfg(any(feature = "cuda", feature = "cuda-oxide"))]
114pub use scalar::cuda::{CudaType, ScalarCuda};
115#[cfg(feature = "cuda")]
116#[allow(deprecated)]
117pub use vector::cuda::{CudaIndex, CudaVec, CudaVecMut, CudaVecRef};
118
119#[cfg(feature = "cuda-oxide")]
120pub use context::cuda_oxide::OxideContext;
121#[cfg(feature = "cuda-oxide")]
122pub use linear_solver::cuda_oxide::lu::OxideLU;
123#[cfg(feature = "cuda-oxide")]
124pub use matrix::cuda_oxide::OxideMat;
125#[cfg(feature = "cuda-oxide")]
126pub use vector::cuda_oxide::{OxideIndex, OxideVec, OxideVecMut, OxideVecRef};