faer/diag/
diagmut.rs

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