Skip to main content

diffsol_c/
matrix_type.rs

1// Matrix type Python enum
2
3use diffsol::{FaerScalar, Matrix, NalgebraScalar};
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6
7/// Enumerates the possible matrix types for diffsol
8///
9/// :attr nalgebra_dense: dense matrix using nalgebra crate (https://nalgebra.rs/)
10/// :attr faer_dense: dense matrix using faer crate (https://faer.veganb.tw/)
11/// :attr faer_sparse: sparse matrix using faer crate (https://faer.veganb.tw/)
12#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
13#[serde(rename_all = "snake_case")]
14pub enum MatrixType {
15    NalgebraDense,
16    FaerDense,
17    FaerSparse,
18}
19
20// Internal trait to determine runtime MatrixType from a compile-time diffsol matrix type
21pub(crate) trait MatrixKind {
22    const MATRIX_TYPE: MatrixType;
23}
24
25impl<T: NalgebraScalar> MatrixKind for diffsol::NalgebraMat<T> {
26    const MATRIX_TYPE: MatrixType = MatrixType::NalgebraDense;
27}
28
29impl<T: FaerScalar> MatrixKind for diffsol::FaerMat<T> {
30    const MATRIX_TYPE: MatrixType = MatrixType::FaerDense;
31}
32
33impl<T: FaerScalar> MatrixKind for diffsol::FaerSparseMat<T> {
34    const MATRIX_TYPE: MatrixType = MatrixType::FaerSparse;
35}
36
37impl MatrixType {
38    pub(crate) fn get_name(&self) -> &str {
39        match self {
40            MatrixType::NalgebraDense => "nalgebra_dense",
41            MatrixType::FaerDense => "faer_dense",
42            MatrixType::FaerSparse => "faer_sparse",
43        }
44    }
45
46    // Determine runtime matrix type compiled diffsol matrix type
47    pub(crate) fn from_diffsol<M: Matrix + MatrixKind>() -> Self {
48        M::MATRIX_TYPE
49    }
50}