Skip to content

Update ndarray to version 0.13 #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ num-complex = "0.2.1"
rand = "0.5"

[dependencies.ndarray]
version = "0.12"
version = "0.13"
features = ["blas"]
default-features = false

Expand Down
4 changes: 2 additions & 2 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ where
assert!(a.is_square());
match uplo {
UPLO::Upper => {
for row in 0..a.rows() {
for row in 0..a.nrows() {
for col in 0..row {
a[(row, col)] = a[(col, row)].conj();
}
}
}
UPLO::Lower => {
for col in 0..a.cols() {
for col in 0..a.ncols() {
for row in 0..col {
a[(row, col)] = a[(col, row)].conj();
}
Expand Down
7 changes: 4 additions & 3 deletions src/eigh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::layout::*;
use crate::operator::LinearOperator;
use crate::types::*;
use crate::UPLO;
use std::iter::FromIterator;

/// Eigenvalue decomposition of Hermite matrix reference
pub trait Eigh {
Expand Down Expand Up @@ -70,7 +71,7 @@ where
MatrixLayout::F(_) => {}
}
let s = unsafe { A::eigh(true, self.square_layout()?, uplo, self.as_allocated_mut()?)? };
Ok((ArrayBase::from_vec(s), self))
Ok((ArrayBase::from(s), self))
}
}

Expand Down Expand Up @@ -126,7 +127,7 @@ where

fn eigvalsh_inplace(&mut self, uplo: UPLO) -> Result<Self::EigVal> {
let s = unsafe { A::eigh(true, self.square_layout()?, uplo, self.as_allocated_mut()?)? };
Ok(ArrayBase::from_vec(s))
Ok(ArrayBase::from(s))
}
}

Expand Down Expand Up @@ -164,7 +165,7 @@ where

fn ssqrt_into(self, uplo: UPLO) -> Result<Self::Output> {
let (e, v) = self.eigh_into(uplo)?;
let e_sqrt = Array1::from_iter(e.iter().map(|r| Scalar::from_real(r.sqrt())));
let e_sqrt = Array::from_iter(e.iter().map(|r| Scalar::from_real(r.sqrt())));
let ev = e_sqrt.into_diagonal().apply2(&v.t());
Ok(v.apply2(&ev))
}
Expand Down
4 changes: 2 additions & 2 deletions src/krylov/arnoldi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ where
assert!(ortho.tolerance() < One::one());
// normalize before append because |v| may be smaller than ortho.tolerance()
let norm = v.norm_l2();
azip!(mut v(&mut v) in { *v = v.div_real(norm) });
azip!((v in &mut v) *v = v.div_real(norm));
ortho.append(v.view());
Arnoldi {
a,
Expand Down Expand Up @@ -82,7 +82,7 @@ where
self.a.apply_mut(&mut self.v);
let result = self.ortho.div_append(&mut self.v);
let norm = self.v.norm_l2();
azip!(mut v(&mut self.v) in { *v = v.div_real(norm) });
azip!((v in &mut self.v) *v = v.div_real(norm));
match result {
AppendResult::Added(coef) => {
self.h.push(coef.clone());
Expand Down
6 changes: 3 additions & 3 deletions src/krylov/householder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ where
let alpha = -x[0].mul_real(norm / x[0].abs());
x[0] -= alpha;
let inv_rev_norm = A::Real::one() / x.norm_l2();
azip!(mut a(x) in { *a = a.mul_real(inv_rev_norm)});
azip!((a in x) *a = a.mul_real(inv_rev_norm));
}

/// Take a reflection `P = I - 2ww^T`
Expand Down Expand Up @@ -107,7 +107,7 @@ impl<A: Scalar + Lapack> Householder<A> {
let k = self.len();
let res = a.slice(s![k..]).norm_l2();
let mut c = Array1::zeros(k + 1);
azip!(mut c(c.slice_mut(s![..k])), a(a.slice(s![..k])) in { *c = a });
azip!((c in c.slice_mut(s![..k]), &a in a.slice(s![..k])) *c = a);
if k < a.len() {
let ak = a[k];
c[k] = -ak.mul_real(res / ak.abs());
Expand All @@ -123,7 +123,7 @@ impl<A: Scalar + Lapack> Householder<A> {
S: DataMut<Elem = A>,
{
let k = self.len();
azip!(mut a( a.slice_mut(s![..k])) in { *a = A::zero() });
azip!((a in a.slice_mut(s![..k])) *a = A::zero());
self.backward_reflection(a);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/krylov/mgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<A: Scalar + Lapack> Orthogonalizer for MGS<A> {
for i in 0..self.len() {
let q = &self.q[i];
let c = q.inner(&a);
azip!(mut a (&mut *a), q (q) in { *a = *a - c * q } );
azip!((a in &mut *a, &q in q) *a = *a - c * q);
coef[i] = c;
}
let nrm = a.norm_l2();
Expand Down Expand Up @@ -88,7 +88,7 @@ impl<A: Scalar + Lapack> Orthogonalizer for MGS<A> {
// Linearly dependent
return AppendResult::Dependent(coef);
}
azip!(mut a(&mut *a) in { *a = *a / A::from_real(nrm) });
azip!((a in &mut *a) *a = *a / A::from_real(nrm));
self.q.push(a.to_owned());
AppendResult::Added(coef)
}
Expand Down
8 changes: 4 additions & 4 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ where
let shape = self.shape();
let strides = self.strides();
if shape[0] == strides[1] as usize {
return Ok(MatrixLayout::F((self.cols() as i32, self.rows() as i32)));
return Ok(MatrixLayout::F((self.ncols() as i32, self.nrows() as i32)));
}
if shape[1] == strides[0] as usize {
return Ok(MatrixLayout::C((self.rows() as i32, self.cols() as i32)));
return Ok(MatrixLayout::C((self.nrows() as i32, self.ncols() as i32)));
}
Err(LinalgError::InvalidStride {
s0: strides[0],
Expand All @@ -122,8 +122,8 @@ where
Ok(())
} else {
Err(LinalgError::NotSquare {
rows: self.rows() as i32,
cols: self.cols() as i32,
rows: self.nrows() as i32,
cols: self.ncols() as i32,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub trait LinearOperator {
S: DataMut<Elem = Self::Elem>,
{
let b = self.apply(a);
azip!(mut a(a), b in { *a = b });
azip!((a in a, &b in &b) *a = b);
}

/// Apply operator with move
Expand Down
4 changes: 2 additions & 2 deletions src/qr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ where
type R = Array2<A>;

fn qr_into(mut self) -> Result<(Self::Q, Self::R)> {
let n = self.rows();
let m = self.cols();
let n = self.nrows();
let m = self.ncols();
let k = ::std::cmp::min(n, m);
let l = self.layout()?;
let r = unsafe { A::qr(l, self.as_allocated_mut()?)? };
Expand Down
2 changes: 1 addition & 1 deletion src/solveh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ where
let mut ln_det = A::Real::zero();
let mut ipiv_enum = ipiv_iter.enumerate();
while let Some((k, ipiv_k)) = ipiv_enum.next() {
debug_assert!(k < a.rows() && k < a.cols());
debug_assert!(k < a.nrows() && k < a.ncols());
if ipiv_k > 0 {
// 1x1 block at k, must be real.
let elem = unsafe { a.uget((k, k)) }.re();
Expand Down
2 changes: 1 addition & 1 deletion src/svd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ where
let vt = svd_res
.vt
.map(|vt| into_matrix(l.resized(m, m), vt).expect("Size of VT mismatches"));
let s = ArrayBase::from_vec(svd_res.s);
let s = ArrayBase::from(svd_res.s);
Ok((u, s, vt))
}
}
22 changes: 5 additions & 17 deletions src/svddc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,15 @@ pub trait SVDDCInto {
type U;
type VT;
type Sigma;
fn svddc_into(
self,
uvt_flag: UVTFlag,
) -> Result<(Option<Self::U>, Self::Sigma, Option<Self::VT>)>;
fn svddc_into(self, uvt_flag: UVTFlag) -> Result<(Option<Self::U>, Self::Sigma, Option<Self::VT>)>;
}

/// Singular-value decomposition of matrix reference by divide-and-conquer
pub trait SVDDCInplace {
type U;
type VT;
type Sigma;
fn svddc_inplace(
&mut self,
uvt_flag: UVTFlag,
) -> Result<(Option<Self::U>, Self::Sigma, Option<Self::VT>)>;
fn svddc_inplace(&mut self, uvt_flag: UVTFlag) -> Result<(Option<Self::U>, Self::Sigma, Option<Self::VT>)>;
}

impl<A, S> SVDDC for ArrayBase<S, Ix2>
Expand All @@ -68,10 +62,7 @@ where
type VT = Array2<A>;
type Sigma = Array1<A::Real>;

fn svddc_into(
mut self,
uvt_flag: UVTFlag,
) -> Result<(Option<Self::U>, Self::Sigma, Option<Self::VT>)> {
fn svddc_into(mut self, uvt_flag: UVTFlag) -> Result<(Option<Self::U>, Self::Sigma, Option<Self::VT>)> {
self.svddc_inplace(uvt_flag)
}
}
Expand All @@ -85,10 +76,7 @@ where
type VT = Array2<A>;
type Sigma = Array1<A::Real>;

fn svddc_inplace(
&mut self,
uvt_flag: UVTFlag,
) -> Result<(Option<Self::U>, Self::Sigma, Option<Self::VT>)> {
fn svddc_inplace(&mut self, uvt_flag: UVTFlag) -> Result<(Option<Self::U>, Self::Sigma, Option<Self::VT>)> {
let l = self.layout()?;
let svd_res = unsafe { A::svddc(l, uvt_flag, self.as_allocated_mut()?)? };
let (m, n) = l.size();
Expand All @@ -104,7 +92,7 @@ where
let vt = svd_res
.vt
.map(|vt| into_matrix(l.resized(ldvt, tdvt), vt).expect("Size of VT mismatches"));
let s = ArrayBase::from_vec(svd_res.s);
let s = ArrayBase::from(svd_res.s);
Ok((u, s, vt))
}
}
8 changes: 4 additions & 4 deletions tests/det.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ where
A: Scalar,
S: Data<Elem = A>,
{
let mut select_rows = (0..a.rows()).collect::<Vec<_>>();
let mut select_rows = (0..a.nrows()).collect::<Vec<_>>();
select_rows.remove(row);
let mut select_cols = (0..a.cols()).collect::<Vec<_>>();
let mut select_cols = (0..a.ncols()).collect::<Vec<_>>();
select_cols.remove(col);
a.select(Axis(0), &select_rows).select(Axis(1), &select_cols)
}
Expand All @@ -24,8 +24,8 @@ where
A: Scalar,
S: Data<Elem = A>,
{
assert_eq!(a.rows(), a.cols());
match a.cols() {
assert_eq!(a.nrows(), a.ncols());
match a.ncols() {
0 => A::one(),
1 => a[(0, 0)],
cols => (0..cols)
Expand Down
2 changes: 1 addition & 1 deletion tests/triangular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn test2d<A, Sa, Sb>(uplo: UPLO, a: &ArrayBase<Sa, Ix2>, b: &ArrayBase<Sb, Ix2>,
where
A: Scalar + Lapack,
Sa: Data<Elem = A>,
Sb: DataMut<Elem = A> + DataOwned + DataClone,
Sb: DataMut<Elem = A> + DataOwned + Data + RawDataClone,
{
println!("a = {:?}", a);
println!("b = {:?}", b);
Expand Down