Struct Heaped

Source
pub struct Heaped<const N: usize, const M: usize, T>(pub Box<[T; { _ }]>)
where
    [T; { _ }]:;

Tuple Fields§

§0: Box<[T; { _ }]>

Implementations§

Source§

impl<const N: usize, const M: usize, T> Heaped<N, M, T>
where [T; { _ }]:,

Source

pub fn new(boxed_array: Box<[T; { _ }]>) -> Self

Examples found in repository?
examples/main.rs (lines 26-29)
7fn main() {
8    // A + B in stack
9    let left = Matrix::<2, 3, _, i32>::new([
10        1, 2, 3, //
11        4, 5, 6,
12    ]);
13    let right = Matrix::<2, 3, _, i32>::new([
14        1, 2, 3, //
15        4, 5, 6,
16    ]);
17    assert_eq!(
18        left + right,
19        Matrix::<2, 3, _, i32>::new([
20            2, 4, 6, //
21            8, 10, 12
22        ])
23    );
24
25    // A + B in heap
26    let left = Matrix::<2, 3, _, _>::new(Heaped::<2, 3, i32>::new(Box::new([
27        1, 2, 3, //
28        4, 5, 6,
29    ])));
30    let right = Matrix::<2, 3, _, _>::new(Heaped::<2, 3, i32>::new(Box::new([
31        1, 2, 3, //
32        4, 5, 6,
33    ])));
34    assert_eq!(
35        left + right,
36        Matrix::<2, 3, _, i32>::new([
37            2, 4, 6, //
38            8, 10, 12
39        ])
40    );
41
42    // A in heap + B in stack
43    let left = Matrix::<2, 3, _, _>::new(Heaped::<2, 3, i32>::new(Box::new([
44        1, 2, 3, //
45        4, 5, 6,
46    ])));
47    let right = Matrix::<2, 3, _, i32>::new([
48        1, 2, 3, //
49        4, 5, 6,
50    ]);
51    assert_eq!(
52        left + right,
53        Matrix::<2, 3, _, i32>::new([
54            2, 4, 6, //
55            8, 10, 12
56        ])
57    );
58
59    // A * B
60    let left = Matrix::<2, 3, _, u32>::new([
61        3, 7, 2, //
62        2, 4, 3,
63    ]);
64    let right = Matrix::<3, 3, _, u32>::new([
65        2, 1, 4, //
66        9, 2, 7, //
67        8, 3, 2,
68    ]);
69    assert_eq!(
70        left * right,
71        Matrix::<2, 3, _, u32>::new([
72            85, 23, 65, //
73            64, 19, 42
74        ])
75    );
76
77    // LU decomposition
78    let matrix = Matrix::<10, 10, _, f64>::new([
79        3.4, 5.3, 2.4, 4.7, 7.89, 3.2, 3.5, 2.1324, 3.0, 3.4, //
80        1.4, 5.4, 2.4, 4.7, 7.89, 3.2, 4.5, 2.1324, 3.0, 3.4, //
81        2.4, 5.5, 2.4, 4.7, 7.89, 3.2, 2.5, 2.1324, 3.0, 3.4, //
82        3.4, 5.6, 2.4, 4.7, 7.89, 3.2, 4.5, 2.1324, 3.0, 3.4, //
83        3.4, 5.9, 2.4, 4.7, 7.89, 3.2, 5.5, 2.1324, 3.0, 3.4, //
84        5.4, 4.3, 2.4, 4.7, 7.89, 3.2, 4.5, 2.1324, 3.0, 3.4, //
85        6.4, 3.3, 2.4, 4.7, 7.89, 3.2, 7.5, 2.1324, 3.0, 3.4, //
86        7.4, 1.3, 2.4, 4.7, 7.89, 3.2, 9.5, 2.1324, 3.0, 3.4, //
87        8.4, 2.3, 2.4, 4.7, 7.89, 3.2, 4.5, 2.1324, 3.0, 3.4, //
88        9.4, 3.3, 2.4, 4.7, 7.89, 3.2, 1.5, 2.1324, 3.0, 3.4, //
89    ]);
90    let (l, u) = matrix.lu_decomposition();
91    let diff = matrix - l * u;
92    diff.map::<_, _, [(); 10 * 10]>(|e| assert!(e.abs() < 1e-10));
93
94    // Solve Ax = b with Gaussian elimination
95    //
96    // 2a + 2b - 4c + 5d = 16
97    //  a +  b +  c +  d = 10
98    // -a + 2b - 3c -  d = -2
99    //  a + 2b + 3c - 4d = -2
100    //
101    // (a, b, c, d) = (1, 2, 3, 4)
102    let a = Matrix::<4, 4, _, f64>::new([
103        2.0, 3.0, -4.0, 5.0, //
104        1.0, 1.0, 1.0, 1.0, //
105        -1.0, 2.0, -3.0, 1.0, //
106        1.0, 2.0, 3.0, -4.0,
107    ]);
108    let b = Matrix::<4, 1, _, f64>::new([
109        16.0, //
110        10.0, //
111        -2.0, //
112        -2.0,
113    ]);
114    let x = solve_eqn(a, b);
115    assert!((1.0 - x.0[0]).abs() < 1e-10);
116    assert!((2.0 - x.0[1]).abs() < 1e-10);
117    assert!((3.0 - x.0[2]).abs() < 1e-10);
118    assert!((4.0 - x.0[3]).abs() < 1e-10);
119}

Trait Implementations§

Source§

impl<const N: usize, const M: usize, T: Clone> Clone for Heaped<N, M, T>
where [T; { _ }]:,

Source§

fn clone(&self) -> Heaped<N, M, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const N: usize, const M: usize, T: Debug> Debug for Heaped<N, M, T>
where [T; { _ }]:,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: usize, const M: usize, T> From<[T; { _ }]> for Heaped<N, M, T>
where [T; { _ }]:,

Source§

fn from(slice: [T; { _ }]) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize, const M: usize, T> Index<usize> for Heaped<N, M, T>
where [T; { _ }]:,

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<const N: usize, const M: usize, T> Into<[T; { _ }]> for Heaped<N, M, T>
where [T; { _ }]:,

Source§

fn into(self) -> [T; { _ }]

Converts this type into the (usually inferred) input type.

Auto Trait Implementations§

§

impl<const N: usize, const M: usize, T> Freeze for Heaped<N, M, T>

§

impl<const N: usize, const M: usize, T> RefUnwindSafe for Heaped<N, M, T>
where T: RefUnwindSafe,

§

impl<const N: usize, const M: usize, T> Send for Heaped<N, M, T>
where T: Send,

§

impl<const N: usize, const M: usize, T> Sync for Heaped<N, M, T>
where T: Sync,

§

impl<const N: usize, const M: usize, T> Unpin for Heaped<N, M, T>

§

impl<const N: usize, const M: usize, T> UnwindSafe for Heaped<N, M, T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.