Skip to main content

faer/diag/
diagown.rs

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