Skip to content

Commit 3712c93

Browse files
authored
Merge pull request #106 from jturner314/remove-signlnoutput
Remove SignLnOutput type from DeterminantH* traits
2 parents 740ea69 + 9fc900d commit 3712c93

File tree

1 file changed

+59
-29
lines changed

1 file changed

+59
-29
lines changed

src/solveh.rs

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,11 @@ where
253253

254254
/// An interface for calculating determinants of Hermitian (or real symmetric) matrix refs.
255255
pub trait DeterminantH {
256-
type Output;
257-
type SignLnOutput;
256+
/// The element type of the matrix.
257+
type Elem: Scalar;
258258

259259
/// Computes the determinant of the Hermitian (or real symmetric) matrix.
260-
fn deth(&self) -> Self::Output;
260+
fn deth(&self) -> Result<<Self::Elem as AssociatedReal>::Real>;
261261

262262
/// Computes the `(sign, natural_log)` of the determinant of the Hermitian
263263
/// (or real symmetric) matrix.
@@ -272,16 +272,23 @@ pub trait DeterminantH {
272272
/// This method is more robust than `.deth()` to very small or very large
273273
/// determinants since it returns the natural logarithm of the determinant
274274
/// rather than the determinant itself.
275-
fn sln_deth(&self) -> Self::SignLnOutput;
275+
fn sln_deth(
276+
&self
277+
) -> Result<
278+
(
279+
<Self::Elem as AssociatedReal>::Real,
280+
<Self::Elem as AssociatedReal>::Real
281+
),
282+
>;
276283
}
277284

278285
/// An interface for calculating determinants of Hermitian (or real symmetric) matrices.
279286
pub trait DeterminantHInto {
280-
type Output;
281-
type SignLnOutput;
287+
/// The element type of the matrix.
288+
type Elem: Scalar;
282289

283290
/// Computes the determinant of the Hermitian (or real symmetric) matrix.
284-
fn deth_into(self) -> Self::Output;
291+
fn deth_into(self) -> Result<<Self::Elem as AssociatedReal>::Real>;
285292

286293
/// Computes the `(sign, natural_log)` of the determinant of the Hermitian
287294
/// (or real symmetric) matrix.
@@ -296,7 +303,14 @@ pub trait DeterminantHInto {
296303
/// This method is more robust than `.deth_into()` to very small or very
297304
/// large determinants since it returns the natural logarithm of the
298305
/// determinant rather than the determinant itself.
299-
fn sln_deth_into(self) -> Self::SignLnOutput;
306+
fn sln_deth_into(
307+
self
308+
) -> Result<
309+
(
310+
<Self::Elem as AssociatedReal>::Real,
311+
<Self::Elem as AssociatedReal>::Real
312+
),
313+
>;
300314
}
301315

302316
/// Returns the sign and natural log of the determinant.
@@ -346,38 +360,56 @@ where
346360
(sign, ln_det)
347361
}
348362

349-
impl<A, S> DeterminantH for BKFactorized<S>
363+
impl<A, S> BKFactorized<S>
350364
where
351365
A: Scalar,
352366
S: Data<Elem = A>,
353367
{
354-
type Output = A::Real;
355-
type SignLnOutput = (A::Real, A::Real);
356-
357-
fn deth(&self) -> A::Real {
368+
/// Computes the determinant of the factorized Hermitian (or real
369+
/// symmetric) matrix.
370+
pub fn deth(&self) -> A::Real {
358371
let (sign, ln_det) = self.sln_deth();
359372
sign * ln_det.exp()
360373
}
361374

362-
fn sln_deth(&self) -> (A::Real, A::Real) {
375+
/// Computes the `(sign, natural_log)` of the determinant of the factorized
376+
/// Hermitian (or real symmetric) matrix.
377+
///
378+
/// The `natural_log` is the natural logarithm of the absolute value of the
379+
/// determinant. If the determinant is zero, `sign` is 0 and `natural_log`
380+
/// is negative infinity.
381+
///
382+
/// To obtain the determinant, you can compute `sign * natural_log.exp()`
383+
/// or just call `.deth()` instead.
384+
///
385+
/// This method is more robust than `.deth()` to very small or very large
386+
/// determinants since it returns the natural logarithm of the determinant
387+
/// rather than the determinant itself.
388+
pub fn sln_deth(&self) -> (A::Real, A::Real) {
363389
bk_sln_det(UPLO::Upper, self.ipiv.iter().cloned(), &self.a)
364390
}
365-
}
366391

367-
impl<A, S> DeterminantHInto for BKFactorized<S>
368-
where
369-
A: Scalar,
370-
S: Data<Elem = A>,
371-
{
372-
type Output = A::Real;
373-
type SignLnOutput = (A::Real, A::Real);
374-
375-
fn deth_into(self) -> A::Real {
392+
/// Computes the determinant of the factorized Hermitian (or real
393+
/// symmetric) matrix.
394+
pub fn deth_into(self) -> A::Real {
376395
let (sign, ln_det) = self.sln_deth_into();
377396
sign * ln_det.exp()
378397
}
379398

380-
fn sln_deth_into(self) -> (A::Real, A::Real) {
399+
/// Computes the `(sign, natural_log)` of the determinant of the factorized
400+
/// Hermitian (or real symmetric) matrix.
401+
///
402+
/// The `natural_log` is the natural logarithm of the absolute value of the
403+
/// determinant. If the determinant is zero, `sign` is 0 and `natural_log`
404+
/// is negative infinity.
405+
///
406+
/// To obtain the determinant, you can compute `sign * natural_log.exp()`
407+
/// or just call `.deth_into()` instead.
408+
///
409+
/// This method is more robust than `.deth_into()` to very small or very
410+
/// large determinants since it returns the natural logarithm of the
411+
/// determinant rather than the determinant itself.
412+
pub fn sln_deth_into(self) -> (A::Real, A::Real) {
381413
bk_sln_det(UPLO::Upper, self.ipiv.into_iter(), &self.a)
382414
}
383415
}
@@ -387,8 +419,7 @@ where
387419
A: Scalar,
388420
S: Data<Elem = A>,
389421
{
390-
type Output = Result<A::Real>;
391-
type SignLnOutput = Result<(A::Real, A::Real)>;
422+
type Elem = A;
392423

393424
fn deth(&self) -> Result<A::Real> {
394425
let (sign, ln_det) = self.sln_deth()?;
@@ -412,8 +443,7 @@ where
412443
A: Scalar,
413444
S: DataMut<Elem = A>,
414445
{
415-
type Output = Result<A::Real>;
416-
type SignLnOutput = Result<(A::Real, A::Real)>;
446+
type Elem = A;
417447

418448
fn deth_into(self) -> Result<A::Real> {
419449
let (sign, ln_det) = self.sln_deth_into()?;

0 commit comments

Comments
 (0)