zenu_matrix/constructor/
alloc.rs

1use crate::{
2    device::DeviceBase,
3    dim::{default_stride, DimTrait},
4    matrix::{Matrix, Owned, Ptr, Repr},
5    num::Num,
6};
7
8impl<T, S, D> Matrix<Owned<T>, S, D>
9where
10    T: Num,
11    D: DeviceBase,
12    S: DimTrait,
13{
14    #[expect(clippy::missing_panics_doc)]
15    pub fn alloc<I: Into<S>>(shape: I) -> Self {
16        let shape = shape.into();
17        let num_elm = shape.num_elm();
18        let bytes = num_elm * std::mem::size_of::<T>();
19
20        let ptr = Ptr::new(D::alloc(bytes).unwrap().cast(), num_elm, 0);
21
22        let stride = default_stride(shape);
23        Matrix::new(ptr, shape, stride)
24    }
25
26    pub fn alloc_like<R: Repr<Item = T>>(mat: &Matrix<R, S, D>) -> Self {
27        let shape = mat.shape();
28        Self::alloc(shape)
29    }
30}