silx_types/types/nalgebra/
mod.rs1mod hack;
5mod froms;
7mod derefs;
9mod arch_tools;
11
12use crate::f64slx;
13
14use super::{ IntoSlx, SlxInto, SlxFrom, ArchToDerefMut, ArchToDeref, };
15use hashed_type_def::HashedTypeDef;
16use silx_core::utils::{ ArchData, ArchSized, };
17
18use std::{ pin::Pin, fmt::Debug, marker::PhantomData, };
19
20use rkyv::Archived;
21use nalgebra::{ matrix, Const, Dim, DimName, IsContiguous, Matrix, };
22
23#[derive(Debug, Clone, Copy, PartialEq,)]
25#[derive(HashedTypeDef)]
26pub struct ConstSlx<const R: usize>;
27
28pub type U1Slx = ConstSlx<1>;
30
31#[derive(Debug, Clone, Copy, PartialEq,)]
33#[derive(HashedTypeDef)]
34#[repr(transparent)]
35pub struct ArrayStorageSlx<T, const R: usize, const C: usize>(pub [[T; R]; C]);
36
37#[repr(transparent)]
39pub struct ArchRefArrayStorageSlx<'a, T, const R: usize, const C: usize>(&'a Archived<ArrayStorageSlx<T,R,C>>) where T: 'a + rkyv::Archive;
40
41#[repr(transparent)]
43pub struct ArchMutArrayStorageSlx<'a, T, const R: usize, const C: usize>(&'a mut Archived<ArrayStorageSlx<T,R,C>>) where T: 'a + rkyv::Archive;
44
45#[repr(C)]
47#[derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize, Debug, Clone, Copy,)]
48#[derive(HashedTypeDef)]
49pub struct DerefMatrixSlx<T,R,C,S> where R: Dim, C: Dim, {
50 data: S,
51 _phantoms: PhantomData<(T, R, C)>,
52}
53
54pub trait StorageSlx<R: Dim, C: Dim,> {}
56
57unsafe impl<const T: usize> Dim for ConstSlx<T> {
62 fn try_to_usize() -> Option<usize> { Some(T) }
63 fn value(&self) -> usize { T }
64 fn from_usize(dim: usize) -> Self { assert_eq!(dim, T); Self }
65}
66impl<const T: usize> DimName for ConstSlx<T> {
68 const USIZE: usize = T;
69 #[inline] fn name() -> Self { Self }
70 #[inline] fn dim() -> usize { T }
71}
72impl<'a, T, const R: usize, const C: usize> ArchRefArrayStorageSlx<'a, T,R,C> where T: rkyv::Archive {
73 fn new(archive: &'a Archived<ArrayStorageSlx<T,R,C>>) -> Self { Self(archive) }
75}
76impl<'a, T, const R: usize, const C: usize> ArchMutArrayStorageSlx<'a, T,R,C> where T: rkyv::Archive {
77 fn new(archive: &'a mut Archived<ArrayStorageSlx<T,R,C>>) -> Self { Self(archive) }
79}
80
81impl<T: Clone,const R: usize, const C: usize> StorageSlx<Const<R>, Const<C>> for ArrayStorageSlx<T,R,C> {}
82unsafe impl<T: Clone, const R: usize, const C: usize> IsContiguous for ArrayStorageSlx<T, R, C> {}
83
84pub fn exp_silx_matrix() {
86 use hashed_type_def::HashedTypeMethods;
87 let matrix: Matrix<f64slx,_,_,_> = matrix![
89 1f64.slx(), 2f64.slx(), 3f64.slx(), 4f64.slx();
90 5f64.slx(), 6f64.slx(), 7f64.slx(), 8f64.slx();
91 10f64.slx(), 10f64.slx(), 11f64.slx(), 12f64.slx()
92 ];
93 println!("ArrayStorage:");
94 let arst: ArrayStorageSlx<_,3,4> = matrix.slx(); println!("arst -> {:?}",arst);
99 println!("arst::full_id() -> {}", arst.self_type_uuid());
100 let mut arch = arst.arch_sized().expect("failed to serialize");
102 { let rmatrixslx: DerefMatrixSlx<_,_,_,_> = arch.arch_deref().expect("failure");
106 let rmatrix = & * rmatrixslx;
108 println!("rmatrix -> {}", rmatrix);
109 let mtm = rmatrix.transpose() * rmatrix;
111 println!("mtm -> {:?}", mtm);
112 println!();
113 }
114 { let mut rmatrixslx: Pin<DerefMatrixSlx<_,_,_,_>> = Pin::new(&mut arch).arch_deref_mut().expect("failure");
119 let rmatrix: &mut Matrix<f64slx,_,_,_> = &mut * rmatrixslx;
121 rmatrix[(0,0)] = (-1f64).slx();
123 println!("rmatrix -> {}", rmatrix);
124 let mtm = rmatrix.transpose() * &*rmatrix;
126 println!("mtm -> {}", mtm);
127 println!();
128 }
129}