faer 0.24.0

linear algebra library
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
use super::*;
use crate::utils::bound::{Array, Dim, Partition};
use crate::{ContiguousFwd, Idx, IdxInc};
use equator::{assert, debug_assert};
use faer_traits::Real;
/// see [`super::RowRef`]
pub struct Ref<'a, T, Cols = usize, CStride = isize> {
	pub(crate) trans: ColRef<'a, T, Cols, CStride>,
}
impl<T, Rows: Copy, CStride: Copy> Copy for Ref<'_, T, Rows, CStride> {}
impl<T, Rows: Copy, CStride: Copy> Clone for Ref<'_, T, Rows, CStride> {
	#[inline]
	fn clone(&self) -> Self {
		*self
	}
}
impl<'short, T, Rows: Copy, CStride: Copy> Reborrow<'short>
	for Ref<'_, T, Rows, CStride>
{
	type Target = Ref<'short, T, Rows, CStride>;

	#[inline]
	fn rb(&'short self) -> Self::Target {
		*self
	}
}
impl<'short, T, Rows: Copy, CStride: Copy> ReborrowMut<'short>
	for Ref<'_, T, Rows, CStride>
{
	type Target = Ref<'short, T, Rows, CStride>;

	#[inline]
	fn rb_mut(&'short mut self) -> Self::Target {
		*self
	}
}
impl<'a, T, Rows: Copy, CStride: Copy> IntoConst for Ref<'a, T, Rows, CStride> {
	type Target = Ref<'a, T, Rows, CStride>;

	#[inline]
	fn into_const(self) -> Self::Target {
		self
	}
}
unsafe impl<T: Sync, Rows: Sync, CStride: Sync> Sync
	for Ref<'_, T, Rows, CStride>
{
}
unsafe impl<T: Sync, Rows: Send, CStride: Send> Send
	for Ref<'_, T, Rows, CStride>
{
}
impl<'a, T> RowRef<'a, T> {
	/// creates a row view over the given element
	#[inline]
	pub fn from_ref(value: &'a T) -> Self {
		unsafe { RowRef::from_raw_parts(value as *const T, 1, 1) }
	}

	/// creates a `RowRef` from slice views over the row vector data, the result
	/// has the same number of columns as the length of the input slice
	#[inline]
	pub fn from_slice(slice: &'a [T]) -> Self {
		let len = slice.len();
		unsafe { Self::from_raw_parts(slice.as_ptr(), len, 1) }
	}
}
impl<'a, T, Cols: Shape, CStride: Stride> RowRef<'a, T, Cols, CStride> {
	/// creates a `RowRef` from pointers to the column vector data, number of
	/// rows, and row stride
	///
	/// # safety
	/// this function has the same safety requirements as
	/// [`MatRef::from_raw_parts(ptr, 1, ncols, 0, col_stride)`]
	#[inline(always)]
	#[track_caller]
	pub const unsafe fn from_raw_parts(
		ptr: *const T,
		ncols: Cols,
		col_stride: CStride,
	) -> Self {
		Self {
			0: Ref {
				trans: ColRef::from_raw_parts(ptr, ncols, col_stride),
			},
		}
	}

	/// returns a pointer to the row data
	#[inline(always)]
	pub fn as_ptr(&self) -> *const T {
		self.trans.as_ptr()
	}

	/// returns the number of rows of the row (always 1)
	#[inline(always)]
	pub fn nrows(&self) -> usize {
		1
	}

	/// returns the number of columns of the row
	#[inline(always)]
	pub fn ncols(&self) -> Cols {
		self.trans.nrows()
	}

	/// returns the number of rows and columns of the row
	#[inline(always)]
	pub fn shape(&self) -> (usize, Cols) {
		(self.nrows(), self.ncols())
	}

	/// returns the column stride of the row
	#[inline(always)]
	pub fn col_stride(&self) -> CStride {
		self.trans.row_stride()
	}

	#[inline]
	pub fn map<U>(&self, f: impl FnMut(&T) -> U) -> Row<U, Cols> {
		let mut f = f;
		zip!(self).map(|unzip!(x)| f(x))
	}

	#[inline]
	pub fn for_each(&self, f: impl FnMut(&T)) {
		let mut f = f;
		zip!(self).for_each(|unzip!(x)| f(x))
	}

	/// returns a raw pointer to the element at the given index
	#[inline(always)]
	pub fn ptr_at(&self, col: IdxInc<Cols>) -> *const T {
		self.trans.ptr_at(col)
	}

	/// returns a raw pointer to the element at the given index, assuming the
	/// provided index is within the row bounds
	///
	/// # safety
	/// the behavior is undefined if any of the following conditions are
	/// violated:
	/// * `col < self.ncols()`
	#[inline(always)]
	#[track_caller]
	pub unsafe fn ptr_inbounds_at(&self, col: Idx<Cols>) -> *const T {
		debug_assert!(all(col < self.ncols()));
		self.trans.ptr_inbounds_at(col)
	}

	/// splits the row vertically at the given column into two parts and returns
	/// an array of each subrow, in the following order:
	/// * left
	/// * right
	///
	/// # panics
	/// the function panics if the following condition is violated:
	/// * `col <= self.ncols()`
	#[inline]
	#[track_caller]
	pub fn split_at_col(
		self,
		col: IdxInc<Cols>,
	) -> (RowRef<'a, T, usize, CStride>, RowRef<'a, T, usize, CStride>) {
		assert!(all(col <= self.ncols()));
		let rs = self.col_stride();
		let top = self.as_ptr();
		let bot = self.ptr_at(col);
		unsafe {
			(
				RowRef::from_raw_parts(top, col.unbound(), rs),
				RowRef::from_raw_parts(
					bot,
					self.ncols().unbound() - col.unbound(),
					rs,
				),
			)
		}
	}

	/// returns a view over the transpose of `self`
	#[inline(always)]
	pub fn transpose(self) -> ColRef<'a, T, Cols, CStride> {
		self.trans
	}

	/// returns a view over the conjugate of `self`
	#[inline(always)]
	pub fn conjugate(self) -> RowRef<'a, T::Conj, Cols, CStride>
	where
		T: Conjugate,
	{
		RowRef {
			0: Ref {
				trans: self.trans.conjugate(),
			},
		}
	}

	/// returns an unconjugated view over `self`
	#[inline(always)]
	pub fn canonical(self) -> RowRef<'a, T::Canonical, Cols, CStride>
	where
		T: Conjugate,
	{
		RowRef {
			0: Ref {
				trans: self.trans.canonical(),
			},
		}
	}

	/// returns a view over the conjugate transpose of `self`
	#[inline(always)]
	pub fn adjoint(self) -> ColRef<'a, T::Conj, Cols, CStride>
	where
		T: Conjugate,
	{
		self.conjugate().transpose()
	}

	#[inline(always)]
	#[track_caller]
	pub(crate) fn at(self, col: Idx<Cols>) -> &'a T {
		assert!(all(col < self.ncols()));
		unsafe { self.at_unchecked(col) }
	}

	#[inline(always)]
	#[track_caller]
	pub(crate) unsafe fn at_unchecked(self, col: Idx<Cols>) -> &'a T {
		&*self.ptr_inbounds_at(col)
	}

	/// returns a reference to the element at the given index, or a subrow if
	/// `col` is a range, with bound checks
	///
	/// # panics
	/// the function panics if any of the following conditions are violated:
	/// * `col` must be contained in `[0, self.ncols())`
	#[track_caller]
	#[inline(always)]
	pub fn get<ColRange>(
		self,
		col: ColRange,
	) -> <RowRef<'a, T, Cols, CStride> as RowIndex<ColRange>>::Target
	where
		RowRef<'a, T, Cols, CStride>: RowIndex<ColRange>,
	{
		<RowRef<'a, T, Cols, CStride> as RowIndex<ColRange>>::get(self, col)
	}

	/// returns a reference to the element at the given index, or a subrow if
	/// `col` is a range, without bound checks
	///
	/// # panics
	/// the behavior is undefined if any of the following conditions are
	/// violated:
	/// * `col` must be contained in `[0, self.ncols())`
	#[track_caller]
	#[inline(always)]
	pub unsafe fn get_unchecked<ColRange>(
		self,
		col: ColRange,
	) -> <RowRef<'a, T, Cols, CStride> as RowIndex<ColRange>>::Target
	where
		RowRef<'a, T, Cols, CStride>: RowIndex<ColRange>,
	{
		unsafe {
			<RowRef<'a, T, Cols, CStride> as RowIndex<ColRange>>::get_unchecked(
				self, col,
			)
		}
	}

	/// returns a view over the `self`, with the columns in reversed order
	#[inline]
	pub fn reverse_cols(self) -> RowRef<'a, T, Cols, CStride::Rev> {
		RowRef {
			0: Ref {
				trans: self.trans.reverse_rows(),
			},
		}
	}

	/// returns a view over the subrow starting at column `col_start`, and with
	/// number of columns `ncols`
	///
	/// # panics
	/// the function panics if any of the following conditions are violated:
	/// * `col_start <= self.ncols()`
	/// * `ncols <= self.ncols() - col_start`
	#[inline]
	pub fn subcols<V: Shape>(
		self,
		col_start: IdxInc<Cols>,
		ncols: V,
	) -> RowRef<'a, T, V, CStride> {
		assert!(all(col_start <= self.ncols()));
		{
			let ncols = ncols.unbound();
			let full_ncols = self.ncols().unbound();
			let col_start = col_start.unbound();
			assert!(all(ncols <= full_ncols - col_start));
		}
		let cs = self.col_stride();
		unsafe { RowRef::from_raw_parts(self.ptr_at(col_start), ncols, cs) }
	}

	/// returns the input row with the given column shape after checking that it
	/// matches the current column shape
	#[inline]
	#[track_caller]
	pub fn as_col_shape<V: Shape>(self, ncols: V) -> RowRef<'a, T, V, CStride> {
		assert!(all(self.ncols().unbound() == ncols.unbound()));
		unsafe {
			RowRef::from_raw_parts(self.as_ptr(), ncols, self.col_stride())
		}
	}

	/// returns the input row with dynamic column shape
	#[inline]
	pub fn as_dyn_cols(self) -> RowRef<'a, T, usize, CStride> {
		unsafe {
			RowRef::from_raw_parts(
				self.as_ptr(),
				self.ncols().unbound(),
				self.col_stride(),
			)
		}
	}

	/// returns the input row with dynamic stride
	#[inline]
	pub fn as_dyn_stride(self) -> RowRef<'a, T, Cols, isize> {
		unsafe {
			RowRef::from_raw_parts(
				self.as_ptr(),
				self.ncols(),
				self.col_stride().element_stride(),
			)
		}
	}

	/// returns an iterator over the elements of the row
	#[inline]
	pub fn iter(
		self,
	) -> impl 'a + ExactSizeIterator + DoubleEndedIterator<Item = &'a T>
	where
		Cols: 'a,
	{
		self.trans.iter()
	}

	/// returns a parallel iterator over the elements of the row
	#[inline]
	#[cfg(feature = "rayon")]
	pub fn par_iter(
		self,
	) -> impl 'a + rayon::iter::IndexedParallelIterator<Item = &'a T>
	where
		T: Sync,
		Cols: 'a,
	{
		self.trans.par_iter()
	}

	/// returns a parallel iterator that provides exactly `count` successive
	/// chunks of the elements of this row
	///
	/// only available with the `rayon` feature
	#[inline]
	#[track_caller]
	#[cfg(feature = "rayon")]
	pub fn par_partition(
		self,
		count: usize,
	) -> impl 'a
	+ rayon::iter::IndexedParallelIterator<
		Item = RowRef<'a, T, usize, CStride>,
	>
	where
		T: Sync,
		Cols: 'a,
	{
		use rayon::prelude::*;
		self.transpose().par_partition(count).map(ColRef::transpose)
	}

	/// returns a view over the row with a static column stride equal to `+1`,
	/// or `None` otherwise
	#[inline]
	pub fn try_as_row_major(
		self,
	) -> Option<RowRef<'a, T, Cols, ContiguousFwd>> {
		if self.col_stride().element_stride() == 1 {
			Some(unsafe {
				RowRef::from_raw_parts(
					self.as_ptr(),
					self.ncols(),
					ContiguousFwd,
				)
			})
		} else {
			None
		}
	}

	#[inline(always)]
	#[doc(hidden)]
	pub unsafe fn const_cast(self) -> RowMut<'a, T, Cols, CStride> {
		RowMut {
			0: Mut {
				trans: self.trans.const_cast(),
			},
		}
	}

	/// returns a matrix view over `self`
	#[inline]
	pub fn as_mat(self) -> MatRef<'a, T, usize, Cols, isize, CStride> {
		self.transpose().as_mat().transpose()
	}

	/// interprets the row as a diagonal matrix
	#[inline]
	pub fn as_diagonal(self) -> DiagRef<'a, T, Cols, CStride> {
		DiagRef {
			0: crate::diag::Ref { inner: self.trans },
		}
	}

	#[inline]
	pub(crate) fn __at(self, i: Idx<Cols>) -> &'a T {
		self.at(i)
	}
}
impl<
	T,
	Cols: Shape,
	CStride: Stride,
	Inner: for<'short> Reborrow<'short, Target = Ref<'short, T, Cols, CStride>>,
> generic::Row<Inner>
{
	/// returns a view over `self`
	#[inline]
	pub fn as_ref(&self) -> RowRef<'_, T, Cols, CStride> {
		self.rb()
	}

	/// returns the maximum norm of `self`
	#[inline]
	pub fn norm_max(&self) -> Real<T>
	where
		T: Conjugate,
	{
		self.rb().as_mat().norm_max()
	}

	/// returns the l2 norm of `self`
	#[inline]
	pub fn norm_l2(&self) -> Real<T>
	where
		T: Conjugate,
	{
		self.rb().as_mat().norm_l2()
	}

	/// returns the squared l2 norm of `self`
	#[inline]
	pub fn squared_norm_l2(&self) -> Real<T>
	where
		T: Conjugate,
	{
		self.rb().as_mat().squared_norm_l2()
	}

	/// returns the l1 norm of `self`
	#[inline]
	pub fn norm_l1(&self) -> Real<T>
	where
		T: Conjugate,
	{
		self.rb().as_mat().norm_l1()
	}

	/// returns the sum of the elements of `self`
	#[inline]
	pub fn sum(&self) -> T::Canonical
	where
		T: Conjugate,
	{
		self.rb().as_mat().sum()
	}

	/// see [`Mat::kron`]
	#[inline]
	pub fn kron(
		&self,
		rhs: impl AsMatRef<T: Conjugate<Canonical = T::Canonical>>,
	) -> Mat<T::Canonical>
	where
		T: Conjugate,
	{
		fn imp<T: ComplexField>(
			lhs: MatRef<impl Conjugate<Canonical = T>>,
			rhs: MatRef<impl Conjugate<Canonical = T>>,
		) -> Mat<T> {
			let mut out = Mat::zeros(
				lhs.nrows() * rhs.nrows(),
				lhs.ncols() * rhs.ncols(),
			);
			linalg::kron::kron(out.rb_mut(), lhs, rhs);
			out
		}
		imp(
			self.rb().as_mat().as_dyn().as_dyn_stride(),
			rhs.as_mat_ref().as_dyn().as_dyn_stride(),
		)
	}

	/// returns `true` if all of the elements of `self` are finite.
	/// otherwise returns `false`.
	#[inline]
	pub fn is_all_finite(&self) -> bool
	where
		T: Conjugate,
	{
		self.rb().transpose().is_all_finite()
	}

	/// returns `true` if any of the elements of `self` is `NaN`.
	/// otherwise returns `false`.
	#[inline]
	pub fn has_nan(&self) -> bool
	where
		T: Conjugate,
	{
		self.rb().transpose().has_nan()
	}

	/// returns a newly allocated row holding the cloned values of `self`
	#[inline]
	pub fn cloned(&self) -> Row<T, Cols>
	where
		T: Clone,
	{
		self.rb().transpose().cloned().into_transpose()
	}

	/// returns a newly allocated row holding the (possibly conjugated) values
	/// of `self`
	#[inline]
	pub fn to_owned(&self) -> Row<T::Canonical, Cols>
	where
		T: Conjugate,
	{
		self.rb().transpose().to_owned().into_transpose()
	}
}
impl<'a, T, Rows: Shape> RowRef<'a, T, Rows, ContiguousFwd> {
	/// returns a reference over the elements as a slice
	#[inline]
	pub fn as_slice(self) -> &'a [T] {
		self.transpose().as_slice()
	}
}
impl<'a, 'ROWS, T> RowRef<'a, T, Dim<'ROWS>, ContiguousFwd> {
	/// returns a reference over the elements as a lifetime-bound slice
	#[inline]
	pub fn as_array(self) -> &'a Array<'ROWS, T> {
		self.transpose().as_array()
	}
}
impl<'COLS, 'a, T, CStride: Stride> RowRef<'a, T, Dim<'COLS>, CStride> {
	#[doc(hidden)]
	#[inline]
	pub fn split_cols_with<'LEFT, 'RIGHT>(
		self,
		col: Partition<'LEFT, 'RIGHT, 'COLS>,
	) -> (
		RowRef<'a, T, Dim<'LEFT>, CStride>,
		RowRef<'a, T, Dim<'RIGHT>, CStride>,
	) {
		let (a, b) = self.split_at_col(col.midpoint());
		(a.as_col_shape(col.head), b.as_col_shape(col.tail))
	}
}
impl<T: core::fmt::Debug, Cols: Shape, CStride: Stride> core::fmt::Debug
	for Ref<'_, T, Cols, CStride>
{
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		fn imp<T: core::fmt::Debug>(
			f: &mut core::fmt::Formatter<'_>,
			this: RowRef<'_, T, Dim<'_>>,
		) -> core::fmt::Result {
			f.debug_list()
				.entries(
					this.ncols()
						.indices()
						.map(|j| crate::hacks::hijack_debug(this.at(j))),
				)
				.finish()
		}
		let this = generic::Row::from_inner_ref(self);
		with_dim!(N, this.ncols().unbound());
		imp(f, this.as_col_shape(N).as_dyn_stride())
	}
}
impl<'a, T> RowRef<'a, T, usize, isize>
where
	T: RealField,
{
	/// Returns the maximum element in the row, or `None` if the row is empty
	pub(crate) fn internal_max(self) -> Option<T> {
		if self.nrows().unbound() == 0 || self.ncols() == 0 {
			return None;
		}
		let mut max_val = self.get(0);
		self.iter().for_each(|val| {
			if val > max_val {
				max_val = val;
			}
		});
		Some((*max_val).clone())
	}

	/// Returns the minimum element in the row, or `None` if the row is empty
	pub(crate) fn internal_min(self) -> Option<T> {
		if self.nrows().unbound() == 0 || self.ncols() == 0 {
			return None;
		}
		let mut min_val = self.get(0);
		self.iter().for_each(|val| {
			if val < min_val {
				min_val = val;
			}
		});
		Some((*min_val).clone())
	}
}
impl<'a, T, Cols: Shape, CStride: Stride> RowRef<'a, T, Cols, CStride>
where
	T: RealField,
{
	/// Returns the maximum element in the row, or `None` if the row is empty
	pub fn max(&self) -> Option<T> {
		self.as_dyn_cols().as_dyn_stride().internal_max()
	}

	/// Returns the minimum element in the row, or `None` if the row is empty
	pub fn min(&self) -> Option<T> {
		self.as_dyn_cols().as_dyn_stride().internal_min()
	}
}
#[cfg(test)]
mod tests {
	use crate::Row;
	#[test]
	fn test_row_min() {
		let row: Row<f64> = Row::from_fn(5, |x| (x + 1) as f64);
		let rowref = row.as_ref();
		assert_eq!(rowref.min(), Some(1.0));
		let empty: Row<f64> = Row::from_fn(0, |_| 0.0);
		let emptyref = empty.as_ref();
		assert_eq!(emptyref.min(), None);
	}
	#[test]
	fn test_row_max() {
		let row: Row<f64> = Row::from_fn(5, |x| (x + 1) as f64);
		let rowref = row.as_ref();
		assert_eq!(rowref.max(), Some(5.0));
		let empty: Row<f64> = Row::from_fn(0, |_| 0.0);
		let emptyref = empty.as_ref();
		assert_eq!(emptyref.max(), None);
	}
}