faer 0.20.2

Linear algebra routines
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
use crate::{assert, debug_assert};
use core::{marker::PhantomData, ops::Range};
use faer_entity::*;
use reborrow::*;

/// Wrapper around a group of references.
pub struct RefGroup<'a, E: Entity, T: 'a = <E as Entity>::Unit>(
    GroupCopyFor<E, *const T>,
    PhantomData<&'a ()>,
);
/// Wrapper around a group of mutable references.
pub struct RefGroupMut<'a, E: Entity, T: 'a = <E as Entity>::Unit>(
    GroupFor<E, *mut T>,
    PhantomData<&'a mut ()>,
);

/// Analogous to an immutable reference to a [prim@slice] for groups.
pub struct SliceGroup<'a, E: Entity, T: 'a = <E as Entity>::Unit>(
    GroupCopyFor<E, *const [T]>,
    PhantomData<&'a ()>,
);
/// Analogous to a mutable reference to a [prim@slice] for groups.
pub struct SliceGroupMut<'a, E: Entity, T: 'a = <E as Entity>::Unit>(
    GroupFor<E, *mut [T]>,
    PhantomData<&'a mut ()>,
);

impl<E: Entity, T: core::fmt::Debug> core::fmt::Debug for RefGroup<'_, E, T> {
    #[inline]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        unsafe {
            transmute_unchecked::<GroupFor<E, &T>, GroupDebugFor<E, &T>>(self.into_inner()).fmt(f)
        }
    }
}
impl<E: Entity, T: core::fmt::Debug> core::fmt::Debug for RefGroupMut<'_, E, T> {
    #[inline]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.rb().fmt(f)
    }
}
impl<E: Entity, T: core::fmt::Debug> core::fmt::Debug for SliceGroup<'_, E, T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_list().entries(self.into_ref_iter()).finish()
    }
}
impl<E: Entity, T: core::fmt::Debug> core::fmt::Debug for SliceGroupMut<'_, E, T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.rb().fmt(f)
    }
}

unsafe impl<E: Entity, T: Sync> Send for SliceGroup<'_, E, T> {}
unsafe impl<E: Entity, T: Sync> Sync for SliceGroup<'_, E, T> {}
unsafe impl<E: Entity, T: Send> Send for SliceGroupMut<'_, E, T> {}
unsafe impl<E: Entity, T: Sync> Sync for SliceGroupMut<'_, E, T> {}

impl<E: Entity, T> Copy for SliceGroup<'_, E, T> {}
impl<E: Entity, T> Copy for RefGroup<'_, E, T> {}
impl<E: Entity, T> Clone for SliceGroup<'_, E, T> {
    #[inline]
    fn clone(&self) -> Self {
        *self
    }
}
impl<E: Entity, T> Clone for RefGroup<'_, E, T> {
    #[inline]
    fn clone(&self) -> Self {
        *self
    }
}

impl<'a, E: Entity, T> RefGroup<'a, E, T> {
    /// Create a new [`RefGroup`] from a group of references.
    #[inline(always)]
    pub fn new(reference: GroupFor<E, &'a T>) -> Self {
        Self(
            into_copy::<E, _>(map!(E, reference, |(reference)| { reference as *const T })),
            PhantomData,
        )
    }

    /// Consume `self` to return the internally stored group of references.
    #[inline(always)]
    pub fn into_inner(self) -> GroupFor<E, &'a T> {
        map!(E, from_copy::<E, _>(self.0), |(ptr)| { unsafe { &*ptr } })
    }

    /// Copies and returns the value pointed to by the references.
    #[inline(always)]
    pub fn get(self) -> GroupCopyFor<E, T>
    where
        T: Copy,
    {
        into_copy::<E, _>(E::faer_deref(self.into_inner()))
    }
}

impl<'a, E: Entity, T, const N: usize> RefGroup<'a, E, [T; N]> {
    /// Convert a reference to an array to an array of references.
    #[inline(always)]
    pub fn unzip(self) -> [RefGroup<'a, E, T>; N] {
        unsafe {
            let mut out = transmute_unchecked::<
                core::mem::MaybeUninit<[RefGroup<'a, E, T>; N]>,
                [core::mem::MaybeUninit<RefGroup<'a, E, T>>; N],
            >(core::mem::MaybeUninit::<[RefGroup<'a, E, T>; N]>::uninit());
            for (out, inp) in core::iter::zip(out.iter_mut(), E::faer_into_iter(self.into_inner()))
            {
                out.write(RefGroup::new(inp));
            }
            transmute_unchecked::<
                [core::mem::MaybeUninit<RefGroup<'a, E, T>>; N],
                [RefGroup<'a, E, T>; N],
            >(out)
        }
    }
}

impl<'a, E: Entity, T, const N: usize> RefGroupMut<'a, E, [T; N]> {
    /// Convert a mutable reference to an array to an array of mutable references.
    #[inline(always)]
    pub fn unzip(self) -> [RefGroupMut<'a, E, T>; N] {
        unsafe {
            let mut out =
                transmute_unchecked::<
                    core::mem::MaybeUninit<[RefGroupMut<'a, E, T>; N]>,
                    [core::mem::MaybeUninit<RefGroupMut<'a, E, T>>; N],
                >(core::mem::MaybeUninit::<[RefGroupMut<'a, E, T>; N]>::uninit());
            for (out, inp) in core::iter::zip(out.iter_mut(), E::faer_into_iter(self.into_inner()))
            {
                out.write(RefGroupMut::new(inp));
            }
            transmute_unchecked::<
                [core::mem::MaybeUninit<RefGroupMut<'a, E, T>>; N],
                [RefGroupMut<'a, E, T>; N],
            >(out)
        }
    }
}

impl<'a, E: Entity, T> RefGroupMut<'a, E, T> {
    /// Create a new [`RefGroupMut`] from a group of mutable references.
    #[inline(always)]
    pub fn new(reference: GroupFor<E, &'a mut T>) -> Self {
        Self(
            map!(E, reference, |(reference)| { reference as *mut T }),
            PhantomData,
        )
    }

    /// Consume `self` to return the internally stored group of references.
    #[inline(always)]
    pub fn into_inner(self) -> GroupFor<E, &'a mut T> {
        map!(E, self.0, |(ptr)| { unsafe { &mut *ptr } })
    }

    /// Copies and returns the value pointed to by the references.
    #[inline(always)]
    pub fn get(&self) -> GroupCopyFor<E, T>
    where
        T: Copy,
    {
        self.rb().get()
    }

    /// Writes `value` to the location pointed to by the references.
    #[inline(always)]
    pub fn set(&mut self, value: GroupCopyFor<E, T>)
    where
        T: Copy,
    {
        map!(
            E,
            E::faer_zip(self.rb_mut().into_inner(), from_copy::<E, _>(value)),
            |((r, value))| { *r = value },
        );
    }

    /// Writes `value` to the location pointed to by the references.
    #[inline(always)]
    pub fn set_(&mut self, value: GroupFor<E, T>) {
        map!(
            E,
            E::faer_zip(self.rb_mut().into_inner(), value),
            |((r, value))| { *r = value },
        );
    }
}

impl<'a, E: Entity, T> IntoConst for SliceGroup<'a, E, T> {
    type Target = SliceGroup<'a, E, T>;

    #[inline(always)]
    fn into_const(self) -> Self::Target {
        self
    }
}
impl<'a, E: Entity, T> IntoConst for SliceGroupMut<'a, E, T> {
    type Target = SliceGroup<'a, E, T>;

    #[inline(always)]
    fn into_const(self) -> Self::Target {
        SliceGroup::new(map!(E, self.into_inner(), |(slice)| { &*slice }))
    }
}

impl<'a, E: Entity, T> IntoConst for RefGroup<'a, E, T> {
    type Target = RefGroup<'a, E, T>;

    #[inline(always)]
    fn into_const(self) -> Self::Target {
        self
    }
}
impl<'a, E: Entity, T> IntoConst for RefGroupMut<'a, E, T> {
    type Target = RefGroup<'a, E, T>;

    #[inline(always)]
    fn into_const(self) -> Self::Target {
        RefGroup::new(map!(E, self.into_inner(), |(slice)| { &*slice }))
    }
}

impl<'short, 'a, E: Entity, T> ReborrowMut<'short> for RefGroup<'a, E, T> {
    type Target = RefGroup<'short, E, T>;

    #[inline(always)]
    fn rb_mut(&'short mut self) -> Self::Target {
        *self
    }
}

impl<'short, 'a, E: Entity, T> Reborrow<'short> for RefGroup<'a, E, T> {
    type Target = RefGroup<'short, E, T>;

    #[inline(always)]
    fn rb(&'short self) -> Self::Target {
        *self
    }
}

impl<'short, 'a, E: Entity, T> ReborrowMut<'short> for RefGroupMut<'a, E, T> {
    type Target = RefGroupMut<'short, E, T>;

    #[inline(always)]
    fn rb_mut(&'short mut self) -> Self::Target {
        RefGroupMut::new(map!(E, E::faer_as_mut(&mut self.0), |(this)| {
            unsafe { &mut **this }
        }))
    }
}

impl<'short, 'a, E: Entity, T> Reborrow<'short> for RefGroupMut<'a, E, T> {
    type Target = RefGroup<'short, E, T>;

    #[inline(always)]
    fn rb(&'short self) -> Self::Target {
        RefGroup::new(map!(E, E::faer_as_ref(&self.0), |(this)| {
            unsafe { &**this }
        },))
    }
}

impl<'a, E: Entity, T> SliceGroup<'a, E, T> {
    /// Create a new [`SliceGroup`] from a group of slice references.
    #[inline(always)]
    pub fn new(slice: GroupFor<E, &'a [T]>) -> Self {
        Self(
            into_copy::<E, _>(map!(E, slice, |(slice)| { slice as *const [T] })),
            PhantomData,
        )
    }

    /// Consume `self` to return the internally stored group of slice references.
    #[inline(always)]
    pub fn into_inner(self) -> GroupFor<E, &'a [T]> {
        unsafe { map!(E, from_copy::<E, _>(self.0), |(ptr)| { &*ptr }) }
    }

    /// Decompose `self` into a slice of arrays of size `N`, and a remainder part with length
    /// `< N`.
    #[inline(always)]
    pub fn as_arrays<const N: usize>(self) -> (SliceGroup<'a, E, [T; N]>, SliceGroup<'a, E, T>) {
        let (head, tail) = E::faer_as_arrays::<N, _>(self.into_inner());
        (SliceGroup::new(head), SliceGroup::new(tail))
    }
}

impl<'a, E: Entity, T> SliceGroupMut<'a, E, T> {
    /// Create a new [`SliceGroup`] from a group of mutable slice references.
    #[inline(always)]
    pub fn new(slice: GroupFor<E, &'a mut [T]>) -> Self {
        Self(map!(E, slice, |(slice)| { slice as *mut [T] }), PhantomData)
    }

    /// Consume `self` to return the internally stored group of mutable slice references.
    #[inline(always)]
    pub fn into_inner(self) -> GroupFor<E, &'a mut [T]> {
        unsafe { map!(E, self.0, |(ptr)| { &mut *ptr }) }
    }

    /// Decompose `self` into a mutable slice of arrays of size `N`, and a remainder part with
    /// length `< N`.
    #[inline(always)]
    pub fn as_arrays_mut<const N: usize>(
        self,
    ) -> (SliceGroupMut<'a, E, [T; N]>, SliceGroupMut<'a, E, T>) {
        let (head, tail) = E::faer_as_arrays_mut::<N, _>(self.into_inner());
        (SliceGroupMut::new(head), SliceGroupMut::new(tail))
    }
}

impl<'short, 'a, E: Entity, T> ReborrowMut<'short> for SliceGroup<'a, E, T> {
    type Target = SliceGroup<'short, E, T>;

    #[inline(always)]
    fn rb_mut(&'short mut self) -> Self::Target {
        *self
    }
}

impl<'short, 'a, E: Entity, T> Reborrow<'short> for SliceGroup<'a, E, T> {
    type Target = SliceGroup<'short, E, T>;

    #[inline(always)]
    fn rb(&'short self) -> Self::Target {
        *self
    }
}

impl<'short, 'a, E: Entity, T> ReborrowMut<'short> for SliceGroupMut<'a, E, T> {
    type Target = SliceGroupMut<'short, E, T>;

    #[inline(always)]
    fn rb_mut(&'short mut self) -> Self::Target {
        SliceGroupMut::new(map!(E, E::faer_as_mut(&mut self.0), |(this)| {
            unsafe { &mut **this }
        },))
    }
}

impl<'short, 'a, E: Entity, T> Reborrow<'short> for SliceGroupMut<'a, E, T> {
    type Target = SliceGroup<'short, E, T>;

    #[inline(always)]
    fn rb(&'short self) -> Self::Target {
        SliceGroup::new(map!(E, E::faer_as_ref(&self.0), |(this)| {
            unsafe { &**this }
        },))
    }
}

impl<'a, E: Entity> RefGroup<'a, E> {
    /// Read the element pointed to by the references.
    #[inline(always)]
    pub fn read(&self) -> E {
        E::faer_from_units(E::faer_deref(self.into_inner()))
    }
}

impl<'a, E: Entity> RefGroupMut<'a, E> {
    /// Read the element pointed to by the references.
    #[inline(always)]
    pub fn read(&self) -> E {
        self.rb().read()
    }

    /// Write `value` to the location pointed to by the references.
    #[inline(always)]
    pub fn write(&mut self, value: E) {
        map!(
            E,
            E::faer_zip(self.rb_mut().into_inner(), value.faer_into_units()),
            |((r, value))| { *r = value },
        );
    }
}

impl<'a, E: Entity> SliceGroup<'a, E> {
    /// Read the element at position `idx`.
    #[inline(always)]
    #[track_caller]
    pub fn read(&self, idx: usize) -> E {
        assert!(idx < self.len());
        unsafe { self.read_unchecked(idx) }
    }

    /// Read the element at position `idx`, without bound checks.
    ///
    /// # Safety
    /// The behavior is undefined if `idx >= self.len()`.
    #[inline(always)]
    #[track_caller]
    pub unsafe fn read_unchecked(&self, idx: usize) -> E {
        debug_assert!(idx < self.len());
        E::faer_from_units(map!(E, self.into_inner(), |(slice)| {
            *slice.get_unchecked(idx)
        }))
    }
}
impl<'a, E: Entity, T> SliceGroup<'a, E, T> {
    /// Get a [`RefGroup`] pointing to the element at position `idx`.
    #[inline(always)]
    #[track_caller]
    pub fn get(self, idx: usize) -> RefGroup<'a, E, T> {
        assert!(idx < self.len());
        unsafe { self.get_unchecked(idx) }
    }

    /// Get a [`RefGroup`] pointing to the element at position `idx`, without bound checks.
    ///
    /// # Safety
    /// The behavior is undefined if `idx >= self.len()`.
    #[inline(always)]
    #[track_caller]
    pub unsafe fn get_unchecked(self, idx: usize) -> RefGroup<'a, E, T> {
        debug_assert!(idx < self.len());
        RefGroup::new(map!(E, self.into_inner(), |(slice)| {
            slice.get_unchecked(idx)
        }))
    }

    /// Checks whether the slice is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the length of the slice.
    #[inline]
    pub fn len(&self) -> usize {
        let mut len = E::faer_first(self.into_inner()).len();
        map!(E, self.into_inner(), |(slice)| {
            len = Ord::min(len, slice.len())
        });
        len
    }

    /// Returns the subslice of `self` from the start to the end of the provided range.
    #[inline(always)]
    #[track_caller]
    pub fn subslice(self, range: Range<usize>) -> Self {
        assert!(all(range.start <= range.end, range.end <= self.len()));
        unsafe { self.subslice_unchecked(range) }
    }

    /// Split `self` at the midpoint `idx`, and return the two parts.
    #[inline(always)]
    #[track_caller]
    pub fn split_at(self, idx: usize) -> (Self, Self) {
        assert!(idx <= self.len());
        let (head, tail) = E::faer_unzip(map!(E, self.into_inner(), |(slice)| {
            slice.split_at(idx)
        }));
        (Self::new(head), Self::new(tail))
    }

    /// Returns the subslice of `self` from the start to the end of the provided range, without
    /// bound checks.
    ///
    /// # Safety
    /// The behavior is undefined if `range.start > range.end` or `range.end > self.len()`.
    #[inline(always)]
    #[track_caller]
    pub unsafe fn subslice_unchecked(self, range: Range<usize>) -> Self {
        debug_assert!(all(range.start <= range.end, range.end <= self.len()));
        Self::new(map!(E, self.into_inner(), |(slice)| {
            slice.get_unchecked(range.start..range.end)
        }))
    }

    /// Returns an iterator of [`RefGroup`] over the elements of the slice.
    #[inline(always)]
    pub fn into_ref_iter(self) -> impl Iterator<Item = RefGroup<'a, E, T>> {
        E::faer_into_iter(self.into_inner()).map(RefGroup::new)
    }

    /// Returns an iterator of slices over chunks of size `chunk_size`, and the remainder of
    /// the slice.
    #[inline(always)]
    pub fn into_chunks_exact(
        self,
        chunk_size: usize,
    ) -> (impl Iterator<Item = SliceGroup<'a, E, T>>, Self) {
        let len = self.len();
        let mid = len / chunk_size * chunk_size;
        let (head, tail) = E::faer_unzip(map!(E, self.into_inner(), |(slice)| {
            slice.split_at(mid)
        }));
        let head = map!(E, head, |(head)| { head.chunks_exact(chunk_size) });
        (
            E::faer_into_iter(head).map(SliceGroup::new),
            SliceGroup::new(tail),
        )
    }
}

impl<'a, E: Entity> SliceGroupMut<'a, E> {
    /// Read the element at position `idx`.
    #[inline(always)]
    #[track_caller]
    pub fn read(&self, idx: usize) -> E {
        self.rb().read(idx)
    }

    /// Read the element at position `idx`, without bound checks.
    ///
    /// # Safety
    /// The behavior is undefined if `idx >= self.len()`.
    #[inline(always)]
    #[track_caller]
    pub unsafe fn read_unchecked(&self, idx: usize) -> E {
        self.rb().read_unchecked(idx)
    }

    /// Write `value` to the location at position `idx`.
    #[inline(always)]
    #[track_caller]
    pub fn write(&mut self, idx: usize, value: E) {
        assert!(idx < self.len());
        unsafe { self.write_unchecked(idx, value) }
    }

    /// Write `value` to the location at position `idx`, without bound checks.
    ///
    /// # Safety
    /// The behavior is undefined if `idx >= self.len()`.
    #[inline(always)]
    #[track_caller]
    pub unsafe fn write_unchecked(&mut self, idx: usize, value: E) {
        debug_assert!(idx < self.len());
        map!(
            E,
            E::faer_zip(self.rb_mut().into_inner(), value.faer_into_units()),
            |((slice, value))| { *slice.get_unchecked_mut(idx) = value },
        );
    }

    /// Fill the slice with zeros.
    #[inline]
    pub fn fill_zero(&mut self) {
        map!(E, self.rb_mut().into_inner(), |(slice)| {
            unsafe {
                let len = slice.len();
                core::ptr::write_bytes(slice.as_mut_ptr(), 0u8, len);
            }
        });
    }
}

impl<'a, E: Entity, T> SliceGroupMut<'a, E, T> {
    /// Get a [`RefGroupMut`] pointing to the element at position `idx`.
    #[inline(always)]
    #[track_caller]
    pub fn get_mut(self, idx: usize) -> RefGroupMut<'a, E, T> {
        assert!(idx < self.len());
        unsafe { self.get_unchecked_mut(idx) }
    }

    /// Get a [`RefGroupMut`] pointing to the element at position `idx`.
    ///
    /// # Safety
    /// The behavior is undefined if `idx >= self.len()`.
    #[inline(always)]
    #[track_caller]
    pub unsafe fn get_unchecked_mut(self, idx: usize) -> RefGroupMut<'a, E, T> {
        debug_assert!(idx < self.len());
        RefGroupMut::new(map!(E, self.into_inner(), |(slice)| {
            slice.get_unchecked_mut(idx)
        },))
    }

    /// Get a [`RefGroup`] pointing to the element at position `idx`.
    #[inline(always)]
    #[track_caller]
    pub fn get(self, idx: usize) -> RefGroup<'a, E, T> {
        self.into_const().get(idx)
    }

    /// Get a [`RefGroup`] pointing to the element at position `idx`, without bound checks.
    ///
    /// # Safety
    /// The behavior is undefined if `idx >= self.len()`.
    #[inline(always)]
    #[track_caller]
    pub unsafe fn get_unchecked(self, idx: usize) -> RefGroup<'a, E, T> {
        self.into_const().get_unchecked(idx)
    }

    /// Checks whether the slice is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.rb().is_empty()
    }

    /// Returns the length of the slice.
    #[inline]
    pub fn len(&self) -> usize {
        self.rb().len()
    }

    /// Returns the subslice of `self` from the start to the end of the provided range.
    #[inline(always)]
    #[track_caller]
    pub fn subslice(self, range: Range<usize>) -> Self {
        assert!(all(range.start <= range.end, range.end <= self.len()));
        unsafe { self.subslice_unchecked(range) }
    }

    /// Returns the subslice of `self` from the start to the end of the provided range, without
    /// bound checks.
    ///
    /// # Safety
    /// The behavior is undefined if `range.start > range.end` or `range.end > self.len()`.
    #[inline(always)]
    #[track_caller]
    pub unsafe fn subslice_unchecked(self, range: Range<usize>) -> Self {
        debug_assert!(all(range.start <= range.end, range.end <= self.len()));
        Self::new(map!(E, self.into_inner(), |(slice)| {
            slice.get_unchecked_mut(range.start..range.end)
        },))
    }

    /// Returns an iterator of [`RefGroupMut`] over the elements of the slice.
    #[inline(always)]
    pub fn into_mut_iter(self) -> impl Iterator<Item = RefGroupMut<'a, E, T>> {
        E::faer_into_iter(self.into_inner()).map(RefGroupMut::new)
    }

    /// Split `self` at the midpoint `idx`, and return the two parts.
    #[inline(always)]
    #[track_caller]
    pub fn split_at(self, idx: usize) -> (Self, Self) {
        assert!(idx <= self.len());
        let (head, tail) = E::faer_unzip(map!(E, self.into_inner(), |(slice)| {
            slice.split_at_mut(idx)
        },));
        (Self::new(head), Self::new(tail))
    }

    /// Returns an iterator of slices over chunks of size `chunk_size`, and the remainder of
    /// the slice.
    #[inline(always)]
    pub fn into_chunks_exact(
        self,
        chunk_size: usize,
    ) -> (impl Iterator<Item = SliceGroupMut<'a, E, T>>, Self) {
        let len = self.len();
        let mid = len % chunk_size * chunk_size;
        let (head, tail) = E::faer_unzip(map!(E, self.into_inner(), |(slice)| {
            slice.split_at_mut(mid)
        },));
        let head = map!(E, head, |(head)| { head.chunks_exact_mut(chunk_size) },);
        (
            E::faer_into_iter(head).map(SliceGroupMut::new),
            SliceGroupMut::new(tail),
        )
    }
}

impl<E: Entity, T: Copy + core::fmt::Debug> pulp::Read for RefGroupMut<'_, E, T> {
    type Output = GroupCopyFor<E, T>;
    #[inline(always)]
    fn read_or(&self, _or: Self::Output) -> Self::Output {
        self.get()
    }
}
impl<E: Entity, T: Copy + core::fmt::Debug> pulp::Write for RefGroupMut<'_, E, T> {
    #[inline(always)]
    fn write(&mut self, values: Self::Output) {
        self.set(values)
    }
}
impl<E: Entity, T: Copy + core::fmt::Debug> pulp::Read for RefGroup<'_, E, T> {
    type Output = GroupCopyFor<E, T>;
    #[inline(always)]
    fn read_or(&self, _or: Self::Output) -> Self::Output {
        self.get()
    }
}