Skip to content

Commit fe679c5

Browse files
committed
Use a trait for EigGeneralized impls
1 parent 61830ef commit fe679c5

File tree

1 file changed

+52
-10
lines changed

1 file changed

+52
-10
lines changed

ndarray-linalg/src/eig.rs

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,27 +125,69 @@ pub trait EigGeneralized {
125125
/// computing the eigenvalues as α/β. If `None`, no approximate comparisons to zero will be
126126
/// made.
127127
fn eig_generalized(
128-
&self,
128+
self,
129129
thresh_opt: Option<Self::Real>,
130130
) -> Result<(Self::EigVal, Self::EigVec)>;
131131
}
132132

133-
impl<A, S> EigGeneralized for (ArrayBase<S, Ix2>, ArrayBase<S, Ix2>)
133+
pub trait MaybeOwnedMatrix {
134+
type Elem;
135+
136+
fn into_owned(self) -> Array2<Self::Elem>;
137+
}
138+
139+
impl<S> MaybeOwnedMatrix for ArrayBase<S, Ix2>
134140
where
135-
A: Scalar + Lapack,
136-
S: Data<Elem = A>,
141+
S: Data,
142+
S::Elem: Clone,
137143
{
138-
type EigVal = Array1<GeneralizedEigenvalue<A::Complex>>;
139-
type EigVec = Array2<A::Complex>;
140-
type Real = A::Real;
144+
type Elem = S::Elem;
145+
146+
fn into_owned(self) -> Array2<S::Elem> {
147+
ArrayBase::into_owned(self)
148+
}
149+
}
150+
151+
impl<S> MaybeOwnedMatrix for &ArrayBase<S, Ix2>
152+
where
153+
S: Data,
154+
S::Elem: Clone,
155+
{
156+
type Elem = S::Elem;
157+
158+
fn into_owned(self) -> Array2<S::Elem> {
159+
self.to_owned()
160+
}
161+
}
162+
163+
impl<A> MaybeOwnedMatrix for &ArrayRef2<A>
164+
where
165+
A: Clone,
166+
{
167+
type Elem = A;
168+
169+
fn into_owned(self) -> Array2<A> {
170+
self.to_owned()
171+
}
172+
}
173+
174+
impl<T1, T2> EigGeneralized for (T1, T2)
175+
where
176+
T1: MaybeOwnedMatrix,
177+
T1::Elem: Lapack + Scalar,
178+
T2: MaybeOwnedMatrix<Elem = T1::Elem>,
179+
{
180+
type EigVal = Array1<GeneralizedEigenvalue<<T1::Elem as Scalar>::Complex>>;
181+
type EigVec = Array2<<T1::Elem as Scalar>::Complex>;
182+
type Real = <T1::Elem as Scalar>::Real;
141183

142184
fn eig_generalized(
143-
&self,
185+
self,
144186
thresh_opt: Option<Self::Real>,
145187
) -> Result<(Self::EigVal, Self::EigVec)> {
146-
let (mut a, mut b) = (self.0.to_owned(), self.1.to_owned());
188+
let (mut a, mut b) = (self.0.into_owned(), self.1.into_owned());
147189
let layout = a.square_layout()?;
148-
let (s, t) = A::eig_generalized(
190+
let (s, t) = T1::Elem::eig_generalized(
149191
true,
150192
layout,
151193
a.as_allocated_mut()?,

0 commit comments

Comments
 (0)