Skip to content

Commit 4ca2a67

Browse files
authored
Merge pull request #139 from rust-math/cauchy
Separation of Scalar trait into another crate
2 parents ff76017 + c870f18 commit 4ca2a67

28 files changed

+152
-465
lines changed

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ serde-1 = ["ndarray/serde-1", "num-complex/serde"]
2323
[dependencies]
2424
lapacke = "0.2"
2525
num-traits = "0.2"
26-
rand = "0.6"
26+
rand = "0.5"
27+
cauchy = "0.2"
2728

2829
[dependencies.num-complex]
29-
version = "0.2"
30+
version = "0.2.1"
3031
default-features = false
32+
features = ["rand"]
3133

3234
[dependencies.ndarray]
3335
version = "0.12"

src/assert.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn close_max<A, S1, S2, D>(
3232
atol: A::Real,
3333
) -> Result<A::Real, A::Real>
3434
where
35-
A: Scalar,
35+
A: Scalar + Lapack,
3636
S1: Data<Elem = A>,
3737
S2: Data<Elem = A>,
3838
D: Dimension,
@@ -52,7 +52,7 @@ pub fn close_l1<A, S1, S2, D>(
5252
rtol: A::Real,
5353
) -> Result<A::Real, A::Real>
5454
where
55-
A: Scalar,
55+
A: Scalar + Lapack,
5656
S1: Data<Elem = A>,
5757
S2: Data<Elem = A>,
5858
D: Dimension,
@@ -72,7 +72,7 @@ pub fn close_l2<A, S1, S2, D>(
7272
rtol: A::Real,
7373
) -> Result<A::Real, A::Real>
7474
where
75-
A: Scalar,
75+
A: Scalar + Lapack,
7676
S1: Data<Elem = A>,
7777
S2: Data<Elem = A>,
7878
D: Dimension,

src/cholesky.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use crate::layout::*;
5252
use crate::triangular::IntoTriangular;
5353
use crate::types::*;
5454

55-
pub use crate::lapack_traits::UPLO;
55+
pub use crate::lapack::UPLO;
5656

5757
/// Cholesky decomposition of Hermitian (or real symmetric) positive definite matrix
5858
pub struct CholeskyFactorized<S: Data> {
@@ -66,7 +66,7 @@ pub struct CholeskyFactorized<S: Data> {
6666

6767
impl<A, S> CholeskyFactorized<S>
6868
where
69-
A: Scalar,
69+
A: Scalar + Lapack,
7070
S: DataMut<Elem = A>,
7171
{
7272
/// Returns `L` from the Cholesky decomposition `A = L * L^H`.
@@ -96,10 +96,10 @@ where
9696

9797
impl<A, S> DeterminantC for CholeskyFactorized<S>
9898
where
99-
A: Absolute,
99+
A: Scalar + Lapack,
100100
S: Data<Elem = A>,
101101
{
102-
type Output = <A as AssociatedReal>::Real;
102+
type Output = <A as Scalar>::Real;
103103

104104
fn detc(&self) -> Self::Output {
105105
self.ln_detc().exp()
@@ -109,17 +109,17 @@ where
109109
self.factor
110110
.diag()
111111
.iter()
112-
.map(|elem| elem.abs_sqr().ln())
112+
.map(|elem| elem.square().ln())
113113
.sum::<Self::Output>()
114114
}
115115
}
116116

117117
impl<A, S> DeterminantCInto for CholeskyFactorized<S>
118118
where
119-
A: Absolute,
119+
A: Scalar + Lapack,
120120
S: Data<Elem = A>,
121121
{
122-
type Output = <A as AssociatedReal>::Real;
122+
type Output = <A as Scalar>::Real;
123123

124124
fn detc_into(self) -> Self::Output {
125125
self.detc()
@@ -132,7 +132,7 @@ where
132132

133133
impl<A, S> InverseC for CholeskyFactorized<S>
134134
where
135-
A: Scalar,
135+
A: Scalar + Lapack,
136136
S: Data<Elem = A>,
137137
{
138138
type Output = Array2<A>;
@@ -148,7 +148,7 @@ where
148148

149149
impl<A, S> InverseCInto for CholeskyFactorized<S>
150150
where
151-
A: Scalar,
151+
A: Scalar + Lapack,
152152
S: DataMut<Elem = A>,
153153
{
154154
type Output = ArrayBase<S, Ix2>;
@@ -163,7 +163,7 @@ where
163163

164164
impl<A, S> SolveC<A> for CholeskyFactorized<S>
165165
where
166-
A: Scalar,
166+
A: Scalar + Lapack,
167167
S: Data<Elem = A>,
168168
{
169169
fn solvec_inplace<'a, Sb>(&self, b: &'a mut ArrayBase<Sb, Ix1>) -> Result<&'a mut ArrayBase<Sb, Ix1>>
@@ -226,7 +226,7 @@ pub trait CholeskyInplace {
226226

227227
impl<A, S> Cholesky for ArrayBase<S, Ix2>
228228
where
229-
A: Scalar,
229+
A: Scalar + Lapack,
230230
S: Data<Elem = A>,
231231
{
232232
type Output = Array2<A>;
@@ -239,7 +239,7 @@ where
239239

240240
impl<A, S> CholeskyInto for ArrayBase<S, Ix2>
241241
where
242-
A: Scalar,
242+
A: Scalar + Lapack,
243243
S: DataMut<Elem = A>,
244244
{
245245
type Output = Self;
@@ -252,7 +252,7 @@ where
252252

253253
impl<A, S> CholeskyInplace for ArrayBase<S, Ix2>
254254
where
255-
A: Scalar,
255+
A: Scalar + Lapack,
256256
S: DataMut<Elem = A>,
257257
{
258258
fn cholesky_inplace(&mut self, uplo: UPLO) -> Result<&mut Self> {
@@ -289,7 +289,7 @@ pub trait FactorizeCInto<S: Data> {
289289

290290
impl<A, S> FactorizeCInto<S> for ArrayBase<S, Ix2>
291291
where
292-
A: Scalar,
292+
A: Scalar + Lapack,
293293
S: DataMut<Elem = A>,
294294
{
295295
fn factorizec_into(self, uplo: UPLO) -> Result<CholeskyFactorized<S>> {
@@ -302,7 +302,7 @@ where
302302

303303
impl<A, Si> FactorizeC<OwnedRepr<A>> for ArrayBase<Si, Ix2>
304304
where
305-
A: Scalar,
305+
A: Scalar + Lapack,
306306
Si: Data<Elem = A>,
307307
{
308308
fn factorizec(&self, uplo: UPLO) -> Result<CholeskyFactorized<OwnedRepr<A>>> {
@@ -343,7 +343,7 @@ pub trait SolveC<A: Scalar> {
343343

344344
impl<A, S> SolveC<A> for ArrayBase<S, Ix2>
345345
where
346-
A: Scalar,
346+
A: Scalar + Lapack,
347347
S: Data<Elem = A>,
348348
{
349349
fn solvec_inplace<'a, Sb>(&self, b: &'a mut ArrayBase<Sb, Ix1>) -> Result<&'a mut ArrayBase<Sb, Ix1>>
@@ -372,7 +372,7 @@ pub trait InverseCInto {
372372

373373
impl<A, S> InverseC for ArrayBase<S, Ix2>
374374
where
375-
A: Scalar,
375+
A: Scalar + Lapack,
376376
S: Data<Elem = A>,
377377
{
378378
type Output = Array2<A>;
@@ -384,7 +384,7 @@ where
384384

385385
impl<A, S> InverseCInto for ArrayBase<S, Ix2>
386386
where
387-
A: Scalar,
387+
A: Scalar + Lapack,
388388
S: DataMut<Elem = A>,
389389
{
390390
type Output = Self;
@@ -430,10 +430,10 @@ pub trait DeterminantCInto {
430430

431431
impl<A, S> DeterminantC for ArrayBase<S, Ix2>
432432
where
433-
A: Scalar,
433+
A: Scalar + Lapack,
434434
S: Data<Elem = A>,
435435
{
436-
type Output = Result<<A as AssociatedReal>::Real>;
436+
type Output = Result<<A as Scalar>::Real>;
437437

438438
fn detc(&self) -> Self::Output {
439439
Ok(self.ln_detc()?.exp())
@@ -446,10 +446,10 @@ where
446446

447447
impl<A, S> DeterminantCInto for ArrayBase<S, Ix2>
448448
where
449-
A: Scalar,
449+
A: Scalar + Lapack,
450450
S: DataMut<Elem = A>,
451451
{
452-
type Output = Result<<A as AssociatedReal>::Real>;
452+
type Output = Result<<A as Scalar>::Real>;
453453

454454
fn detc_into(self) -> Self::Output {
455455
Ok(self.ln_detc_into()?.exp())

src/convert.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
use ndarray::*;
44

55
use super::error::*;
6-
use super::lapack_traits::UPLO;
6+
use super::lapack::UPLO;
77
use super::layout::*;
8-
use super::types::Conjugate;
8+
use super::types::*;
99

1010
pub fn into_col<S>(a: ArrayBase<S, Ix1>) -> ArrayBase<S, Ix2>
1111
where
@@ -107,7 +107,7 @@ where
107107
/// ***Panics*** if `a` is not square.
108108
pub(crate) fn triangular_fill_hermitian<A, S>(a: &mut ArrayBase<S, Ix2>, uplo: UPLO)
109109
where
110-
A: Conjugate,
110+
A: Scalar + Lapack,
111111
S: DataMut<Elem = A>,
112112
{
113113
assert!(a.is_square());

src/eigh.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub trait EighInto: Sized {
3030

3131
impl<A, S> EighInto for ArrayBase<S, Ix2>
3232
where
33-
A: Scalar,
33+
A: Scalar + Lapack,
3434
S: DataMut<Elem = A>,
3535
{
3636
type EigVal = Array1<A::Real>;
@@ -43,7 +43,7 @@ where
4343

4444
impl<A, S> Eigh for ArrayBase<S, Ix2>
4545
where
46-
A: Scalar,
46+
A: Scalar + Lapack,
4747
S: Data<Elem = A>,
4848
{
4949
type EigVal = Array1<A::Real>;
@@ -57,7 +57,7 @@ where
5757

5858
impl<A, S> EighInplace for ArrayBase<S, Ix2>
5959
where
60-
A: Scalar,
60+
A: Scalar + Lapack,
6161
S: DataMut<Elem = A>,
6262
{
6363
type EigVal = Array1<A::Real>;
@@ -88,7 +88,7 @@ pub trait EigValshInplace {
8888

8989
impl<A, S> EigValshInto for ArrayBase<S, Ix2>
9090
where
91-
A: Scalar,
91+
A: Scalar + Lapack,
9292
S: DataMut<Elem = A>,
9393
{
9494
type EigVal = Array1<A::Real>;
@@ -100,7 +100,7 @@ where
100100

101101
impl<A, S> EigValsh for ArrayBase<S, Ix2>
102102
where
103-
A: Scalar,
103+
A: Scalar + Lapack,
104104
S: Data<Elem = A>,
105105
{
106106
type EigVal = Array1<A::Real>;
@@ -113,7 +113,7 @@ where
113113

114114
impl<A, S> EigValshInplace for ArrayBase<S, Ix2>
115115
where
116-
A: Scalar,
116+
A: Scalar + Lapack,
117117
S: DataMut<Elem = A>,
118118
{
119119
type EigVal = Array1<A::Real>;
@@ -132,7 +132,7 @@ pub trait SymmetricSqrt {
132132

133133
impl<A, S> SymmetricSqrt for ArrayBase<S, Ix2>
134134
where
135-
A: Scalar,
135+
A: Scalar + Lapack,
136136
S: Data<Elem = A>,
137137
{
138138
type Output = Array2<A>;
@@ -151,14 +151,14 @@ pub trait SymmetricSqrtInto {
151151

152152
impl<A, S> SymmetricSqrtInto for ArrayBase<S, Ix2>
153153
where
154-
A: Scalar,
154+
A: Scalar + Lapack,
155155
S: DataMut<Elem = A> + DataOwned,
156156
{
157157
type Output = Array2<A>;
158158

159159
fn ssqrt_into(self, uplo: UPLO) -> Result<Self::Output> {
160160
let (e, v) = self.eigh_into(uplo)?;
161-
let e_sqrt = Array1::from_iter(e.iter().map(|r| AssociatedReal::inject(r.sqrt())));
161+
let e_sqrt = Array1::from_iter(e.iter().map(|r| Scalar::from_real(r.sqrt())));
162162
let ev = e_sqrt.into_diagonal().op(&v.t());
163163
Ok(v.op(&ev))
164164
}

0 commit comments

Comments
 (0)