Skip to main content

faer/diag/
diagmut.rs

1use super::*;
2use crate::internal_prelude::DiagRef;
3/// see [`super::DiagMut`]
4pub struct Mut<'a, T, Dim = usize, Stride = isize> {
5	pub(crate) inner: ColMut<'a, T, Dim, Stride>,
6}
7impl<T: core::fmt::Debug, Dim: Shape, S: Stride> core::fmt::Debug
8	for Mut<'_, T, Dim, S>
9{
10	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
11		self.inner.fmt(f)
12	}
13}
14impl<'a, T> DiagMut<'a, T> {
15	/// creates a diagonal matrix view over the given element
16	#[inline]
17	pub fn from_mut(value: &'a mut T) -> Self {
18		unsafe { DiagMut::from_raw_parts_mut(value as *mut T, 1, 1) }
19	}
20
21	/// creates a `DiagMut` from slice views over the diagonal data, the result
22	/// has the same dimension as the length of the input slice
23	#[inline]
24	pub fn from_slice_mut(slice: &'a mut [T]) -> Self {
25		let len = slice.len();
26		unsafe { Self::from_raw_parts_mut(slice.as_mut_ptr(), len, 1) }
27	}
28}
29impl<'a, T, Dim: Shape, Stride: crate::Stride> DiagMut<'a, T, Dim, Stride> {
30	/// creates a `DiagMut` from pointers to the diagonal data, dimension, and
31	/// stride
32	///
33	/// # safety
34	/// this function has the same safety requirements as
35	/// [`MatMut::from_raw_parts_mut(ptr, dim, 1, stride, 0)`]
36	#[inline(always)]
37	#[track_caller]
38	pub const unsafe fn from_raw_parts_mut(
39		ptr: *mut T,
40		dim: Dim,
41		stride: Stride,
42	) -> Self {
43		Self {
44			0: Mut {
45				inner: ColMut::from_raw_parts_mut(ptr, dim, stride),
46			},
47		}
48	}
49
50	/// returns the stride of the diagonal, specified in number of elements, not
51	/// in bytes
52	#[inline(always)]
53	pub fn stride(&self) -> Stride {
54		self.rb().stride()
55	}
56
57	#[inline]
58	pub fn map<U>(&self, f: impl Fn(&T) -> U) -> Diag<U, Dim> {
59		self.rb().map(f)
60	}
61
62	#[inline]
63	pub fn for_each(&self, f: impl Fn(&T)) {
64		self.rb().for_each(f)
65	}
66
67	#[inline]
68	pub fn map_mut<U>(&mut self, f: impl FnMut(&mut T) -> U) -> Diag<U, Dim> {
69		let mut f = f;
70		zip!(self).map(|unzip!(x)| f(x))
71	}
72
73	#[inline]
74	pub fn for_each_mut(&mut self, f: impl FnMut(&mut T)) {
75		let mut f = f;
76		zip!(self).for_each(|unzip!(x)| f(x))
77	}
78
79	/// returns the diagonal as a column vector view
80	#[inline(always)]
81	pub fn column_vector(self) -> ColRef<'a, T, Dim, Stride> {
82		self.into_const().column_vector()
83	}
84
85	/// returns the diagonal as a mutable column vector view
86	#[inline(always)]
87	pub fn column_vector_mut(self) -> ColMut<'a, T, Dim, Stride> {
88		self.0.inner
89	}
90
91	/// returns a view over `self`
92	#[inline]
93	pub fn as_ref(&self) -> DiagRef<'_, T, Dim, Stride> {
94		self.rb()
95	}
96
97	/// returns a view over `self`
98	#[inline]
99	pub fn as_mut(&mut self) -> DiagMut<'_, T, Dim, Stride> {
100		self.rb_mut()
101	}
102
103	/// fills all the elements of `self` with `value`
104	#[inline]
105	pub fn fill(&mut self, value: T)
106	where
107		T: Clone,
108	{
109		self.0.inner.fill(value)
110	}
111
112	#[inline]
113	#[track_caller]
114	/// see [`DiagRef::as_shape`]
115	pub fn as_shape<D: Shape>(self, len: D) -> DiagRef<'a, T, D, Stride> {
116		DiagRef {
117			0: Ref {
118				inner: self.0.inner.as_row_shape(len),
119			},
120		}
121	}
122
123	#[inline]
124	/// see [`DiagRef::as_dyn`]
125	pub fn as_dyn(self) -> DiagRef<'a, T, usize, Stride> {
126		DiagRef {
127			0: Ref {
128				inner: self.0.inner.as_dyn_rows(),
129			},
130		}
131	}
132
133	#[inline]
134	/// see [`DiagRef::as_dyn_stride`]
135	pub fn as_dyn_stride(self) -> DiagRef<'a, T, Dim> {
136		DiagRef {
137			0: Ref {
138				inner: self.0.inner.as_dyn_stride(),
139			},
140		}
141	}
142
143	#[inline]
144	/// see [`DiagRef::conjugate`]
145	pub fn conjugate(self) -> DiagRef<'a, T::Conj, Dim, Stride>
146	where
147		T: Conjugate,
148	{
149		DiagRef {
150			0: Ref {
151				inner: self.0.inner.conjugate(),
152			},
153		}
154	}
155
156	#[inline]
157	/// see [`DiagRef::canonical`]
158	pub fn canonical(self) -> DiagRef<'a, T::Canonical, Dim, Stride>
159	where
160		T: Conjugate,
161	{
162		DiagRef {
163			0: Ref {
164				inner: self.0.inner.canonical(),
165			},
166		}
167	}
168
169	#[inline]
170	#[track_caller]
171	/// see [`DiagRef::as_shape`]
172	pub fn as_shape_mut<D: Shape>(self, len: D) -> DiagMut<'a, T, D, Stride> {
173		DiagMut {
174			0: Mut {
175				inner: self.0.inner.as_row_shape_mut(len),
176			},
177		}
178	}
179
180	#[inline]
181	/// see [`DiagRef::as_dyn`]
182	pub fn as_dyn_mut(self) -> DiagMut<'a, T, usize, Stride> {
183		DiagMut {
184			0: Mut {
185				inner: self.0.inner.as_dyn_rows_mut(),
186			},
187		}
188	}
189
190	#[inline]
191	/// see [`DiagRef::as_dyn_stride`]
192	pub fn as_dyn_stride_mut(self) -> DiagMut<'a, T, Dim> {
193		DiagMut {
194			0: Mut {
195				inner: self.0.inner.as_dyn_stride_mut(),
196			},
197		}
198	}
199
200	#[inline]
201	/// see [`DiagRef::conjugate`]
202	pub fn conjugate_mut(self) -> DiagMut<'a, T::Conj, Dim, Stride>
203	where
204		T: Conjugate,
205	{
206		DiagMut {
207			0: Mut {
208				inner: self.0.inner.conjugate_mut(),
209			},
210		}
211	}
212
213	#[inline]
214	/// see [`DiagRef::canonical`]
215	pub fn canonical_mut(self) -> DiagMut<'a, T::Canonical, Dim, Stride>
216	where
217		T: Conjugate,
218	{
219		DiagMut {
220			0: Mut {
221				inner: self.0.inner.canonical_mut(),
222			},
223		}
224	}
225
226	/// returns the dimension of `self`
227	#[inline]
228	pub fn dim(&self) -> Dim {
229		self.0.inner.nrows()
230	}
231
232	/// copies `other` into `self`
233	#[inline]
234	#[track_caller]
235	pub fn copy_from<RhsT: Conjugate<Canonical = T>>(
236		&mut self,
237		rhs: impl AsDiagRef<T = RhsT, Dim = Dim>,
238	) where
239		T: ComplexField,
240	{
241		self.0.inner.copy_from(rhs.as_diag_ref().inner)
242	}
243}
244impl<'short, T, N: Copy, Stride: Copy> Reborrow<'short>
245	for Mut<'_, T, N, Stride>
246{
247	type Target = Ref<'short, T, N, Stride>;
248
249	#[inline]
250	fn rb(&'short self) -> Self::Target {
251		Ref {
252			inner: self.inner.rb(),
253		}
254	}
255}
256impl<'short, T, N: Copy, Stride: Copy> ReborrowMut<'short>
257	for Mut<'_, T, N, Stride>
258{
259	type Target = Mut<'short, T, N, Stride>;
260
261	#[inline]
262	fn rb_mut(&'short mut self) -> Self::Target {
263		Mut {
264			inner: self.inner.rb_mut(),
265		}
266	}
267}
268impl<'a, T, N: Copy, Stride: Copy> IntoConst for Mut<'a, T, N, Stride> {
269	type Target = Ref<'a, T, N, Stride>;
270
271	#[inline]
272	fn into_const(self) -> Self::Target {
273		Ref {
274			inner: self.inner.into_const(),
275		}
276	}
277}