Skip to content

Remove usize as dimension pattern #623

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

Closed
Closed
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
34 changes: 17 additions & 17 deletions benches/bench1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use test::black_box;
#[bench]
fn iter_sum_1d_regular(bench: &mut test::Bencher)
{
let a = Array::<i32, _>::zeros(64 * 64);
let a = Array::<i32, _>::zeros((64 * 64,));
let a = black_box(a);
bench.iter(|| {
let mut sum = 0;
Expand All @@ -35,7 +35,7 @@ fn iter_sum_1d_regular(bench: &mut test::Bencher)
fn iter_sum_1d_raw(bench: &mut test::Bencher)
{
// this is autovectorized to death (= great performance)
let a = Array::<i32, _>::zeros(64 * 64);
let a = Array::<i32, _>::zeros((64 * 64,));
let a = black_box(a);
bench.iter(|| {
let mut sum = 0;
Expand Down Expand Up @@ -404,7 +404,7 @@ fn add_2d_cutouts_by_32(bench: &mut test::Bencher)
fn add_2d_broadcast_1_to_2(bench: &mut test::Bencher)
{
let mut a = Array2::<i32>::zeros((ADD2DSZ, ADD2DSZ));
let b = Array1::<i32>::zeros(ADD2DSZ);
let b = Array1::<i32>::zeros((ADD2DSZ,));
let bv = b.view();
bench.iter(|| {
a += &bv;
Expand Down Expand Up @@ -614,7 +614,7 @@ const ADD1D_SIZE: usize = 64 * 64;
#[bench]
fn add_1d_regular(bench: &mut test::Bencher)
{
let mut a = Array::<f32, _>::zeros(ADD1D_SIZE);
let mut a = Array::<f32, _>::zeros((ADD1D_SIZE,));
let b = Array::<f32, _>::zeros(a.dim());
bench.iter(|| {
a += &b;
Expand All @@ -624,7 +624,7 @@ fn add_1d_regular(bench: &mut test::Bencher)
#[bench]
fn add_1d_strided(bench: &mut test::Bencher)
{
let mut a = Array::<f32, _>::zeros(ADD1D_SIZE * 2);
let mut a = Array::<f32, _>::zeros((ADD1D_SIZE * 2,));
let mut av = a.slice_mut(s![..;2]);
let b = Array::<f32, _>::zeros(av.dim());
bench.iter(|| {
Expand Down Expand Up @@ -880,40 +880,40 @@ fn equality_f32_mixorder(bench: &mut test::Bencher)
#[bench]
fn dot_f32_16(bench: &mut test::Bencher)
{
let a = Array::<f32, _>::zeros(16);
let b = Array::<f32, _>::zeros(16);
let a = Array::<f32, _>::zeros((16,));
let b = Array::<f32, _>::zeros((16,));
bench.iter(|| a.dot(&b));
}

#[bench]
fn dot_f32_20(bench: &mut test::Bencher)
{
let a = Array::<f32, _>::zeros(20);
let b = Array::<f32, _>::zeros(20);
let a = Array::<f32, _>::zeros((20,));
let b = Array::<f32, _>::zeros((20,));
bench.iter(|| a.dot(&b));
}

#[bench]
fn dot_f32_32(bench: &mut test::Bencher)
{
let a = Array::<f32, _>::zeros(32);
let b = Array::<f32, _>::zeros(32);
let a = Array::<f32, _>::zeros((32,));
let b = Array::<f32, _>::zeros((32,));
bench.iter(|| a.dot(&b));
}

#[bench]
fn dot_f32_256(bench: &mut test::Bencher)
{
let a = Array::<f32, _>::zeros(256);
let b = Array::<f32, _>::zeros(256);
let a = Array::<f32, _>::zeros((256,));
let b = Array::<f32, _>::zeros((256,));
bench.iter(|| a.dot(&b));
}

#[bench]
fn dot_f32_1024(bench: &mut test::Bencher)
{
let av = Array::<f32, _>::zeros(1024);
let bv = Array::<f32, _>::zeros(1024);
let av = Array::<f32, _>::zeros((1024,));
let bv = Array::<f32, _>::zeros((1024,));
bench.iter(|| {
av.dot(&bv)
});
Expand All @@ -923,8 +923,8 @@ fn dot_f32_1024(bench: &mut test::Bencher)
fn dot_f32_10e6(bench: &mut test::Bencher)
{
let n = 1_000_000;
let av = Array::<f32, _>::zeros(n);
let bv = Array::<f32, _>::zeros(n);
let av = Array::<f32, _>::zeros((n,));
let bv = Array::<f32, _>::zeros((n,));
bench.iter(|| {
av.dot(&bv)
});
Expand Down
12 changes: 6 additions & 6 deletions benches/gemv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use ndarray::linalg::general_mat_vec_mul;
fn gemv_64_64c(bench: &mut Bencher) {
let a = Array::zeros((64, 64));
let (m, n) = a.dim();
let x = Array::zeros(n);
let mut y = Array::zeros(m);
let x = Array::zeros((n,));
let mut y = Array::zeros((m,));
bench.iter(|| {
general_mat_vec_mul(1.0, &a, &x, 1.0, &mut y);
});
Expand All @@ -23,8 +23,8 @@ fn gemv_64_64c(bench: &mut Bencher) {
fn gemv_64_64f(bench: &mut Bencher) {
let a = Array::zeros((64, 64).f());
let (m, n) = a.dim();
let x = Array::zeros(n);
let mut y = Array::zeros(m);
let x = Array::zeros((n,));
let mut y = Array::zeros((m,));
bench.iter(|| {
general_mat_vec_mul(1.0, &a, &x, 1.0, &mut y);
});
Expand All @@ -34,8 +34,8 @@ fn gemv_64_64f(bench: &mut Bencher) {
fn gemv_64_32(bench: &mut Bencher) {
let a = Array::zeros((64, 32));
let (m, n) = a.dim();
let x = Array::zeros(n);
let mut y = Array::zeros(m);
let x = Array::zeros((n,));
let mut y = Array::zeros((m,));
bench.iter(|| {
general_mat_vec_mul(1.0, &a, &x, 1.0, &mut y);
});
Expand Down
12 changes: 6 additions & 6 deletions benches/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ const I2DSZ: usize = 64;

#[bench]
fn indexed_iter_1d_ix1(bench: &mut Bencher) {
let mut a = Array::<f64, _>::zeros(I2DSZ * I2DSZ);
for (i, elt) in a.indexed_iter_mut() {
let mut a = Array::<f64, _>::zeros((I2DSZ * I2DSZ,));
for ((i,), elt) in a.indexed_iter_mut() {
*elt = i as _;
}

Expand All @@ -250,8 +250,8 @@ fn indexed_iter_1d_ix1(bench: &mut Bencher) {

#[bench]
fn indexed_zip_1d_ix1(bench: &mut Bencher) {
let mut a = Array::<f64, _>::zeros(I2DSZ * I2DSZ);
for (i, elt) in a.indexed_iter_mut() {
let mut a = Array::<f64, _>::zeros((I2DSZ * I2DSZ,));
for ((i,), elt) in a.indexed_iter_mut() {
*elt = i as _;
}

Expand Down Expand Up @@ -346,7 +346,7 @@ fn indexed_iter_3d_dyn(bench: &mut Bencher) {
#[bench]
fn iter_sum_1d_strided_fold(bench: &mut Bencher)
{
let mut a = Array::<u64, _>::ones(10240);
let mut a = Array::<u64, _>::ones((10240,));
a.slice_axis_inplace(Axis(0), Slice::new(0, None, 2));
bench.iter(|| {
a.iter().fold(0, |acc, &x| acc + x)
Expand All @@ -356,7 +356,7 @@ fn iter_sum_1d_strided_fold(bench: &mut Bencher)
#[bench]
fn iter_sum_1d_strided_rfold(bench: &mut Bencher)
{
let mut a = Array::<u64, _>::ones(10240);
let mut a = Array::<u64, _>::ones((10240,));
a.slice_axis_inplace(Axis(0), Slice::new(0, None, 2));
bench.iter(|| {
a.iter().rfold(0, |acc, &x| acc + x)
Expand Down
10 changes: 5 additions & 5 deletions blas-tests/tests/oper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ fn reference_mat_vec_mul<A, S, S2>(lhs: &ArrayBase<S, Ix2>, rhs: &ArrayBase<S2,
S: Data<Elem=A>,
S2: Data<Elem=A>,
{
let ((m, _), k) = (lhs.dim(), rhs.dim());
let ((m, _), (k,)) = (lhs.dim(), rhs.dim());
reference_mat_mul(lhs, &rhs.to_owned().into_shape((k, 1)).unwrap())
.into_shape(m).unwrap()
.into_shape((m,)).unwrap()
}

// simple, slow, correct (hopefully) mat mul
Expand All @@ -195,9 +195,9 @@ fn reference_vec_mat_mul<A, S, S2>(lhs: &ArrayBase<S, Ix1>, rhs: &ArrayBase<S2,
S: Data<Elem=A>,
S2: Data<Elem=A>,
{
let (m, (_, n)) = (lhs.dim(), rhs.dim());
let ((m,), (_, n)) = (lhs.dim(), rhs.dim());
reference_mat_mul(&lhs.to_owned().into_shape((1, m)).unwrap(), rhs)
.into_shape(n).unwrap()
.into_shape((n,)).unwrap()
}

#[test]
Expand Down Expand Up @@ -309,7 +309,7 @@ fn mat_mul_broadcast() {
let x1 = 1.;
let x = Array::from_vec(vec![x1]);
let b0 = x.broadcast((n, k)).unwrap();
let b1 = Array::from_elem(n, x1);
let b1 = Array::from_elem((n,), x1);
let b1 = b1.broadcast((n, k)).unwrap();
let b2 = Array::from_elem((n, k), x1);

Expand Down
2 changes: 1 addition & 1 deletion examples/axis_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn main() {
*elt = i;
}
regularize(&mut b).ok();
let mut b = b.into_shape(a.len()).unwrap();
let mut b = b.into_shape((a.len(),)).unwrap();
regularize(&mut b).ok();
b.invert_axis(Axis(0));
regularize(&mut b).ok();
Expand Down
2 changes: 1 addition & 1 deletion examples/zip_many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main() {

// sum of each row
let ax = Axis(0);
let mut sums = Array::zeros(a.len_of(ax));
let mut sums = Array::zeros((a.len_of(ax),));
azip!(mut sums, ref a (a.axis_iter(ax)) in { *sums = a.sum() });

// sum of each chunk
Expand Down
4 changes: 2 additions & 2 deletions src/arraytraits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl<'a, A, Slice: ?Sized> From<&'a Slice> for ArrayView<'a, A, Ix1>
);
}
unsafe {
Self::from_shape_ptr(xs.len(), xs.as_ptr())
Self::from_shape_ptr((xs.len(),), xs.as_ptr())
}
}
}
Expand Down Expand Up @@ -262,7 +262,7 @@ impl<'a, A, Slice: ?Sized> From<&'a mut Slice> for ArrayViewMut<'a, A, Ix1>
);
}
unsafe {
Self::from_shape_ptr(xs.len(), xs.as_mut_ptr())
Self::from_shape_ptr((xs.len(),), xs.as_mut_ptr())
}
}
}
Expand Down
8 changes: 1 addition & 7 deletions src/dimension/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use std::ops::{Index, IndexMut};
use num_traits::Zero;

use crate::{Ix, Ix1, IxDyn, Dimension, Dim, IxDynImpl};
use crate::{Ix, IxDyn, Dimension, Dim, IxDynImpl};

/// $m: macro callback
/// $m is called with $arg and then the indices corresponding to the size argument
Expand Down Expand Up @@ -43,12 +43,6 @@ pub trait IntoDimension {
fn into_dimension(self) -> Self::Dim;
}

impl IntoDimension for Ix {
type Dim = Ix1;
#[inline(always)]
fn into_dimension(self) -> Ix1 { Ix1(self) }
}

impl<D> IntoDimension for D where D: Dimension {
type Dim = D;
#[inline(always)]
Expand Down
6 changes: 3 additions & 3 deletions src/dimension/dimension_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub trait Dimension : Clone + Eq + Debug + Send + Sync + Default +
type SliceArg: ?Sized + AsRef<[SliceOrIndex]>;
/// Pattern matching friendly form of the dimension value.
///
/// - For `Ix1`: `usize`,
/// - For `Ix1`: `(usize)`,
/// - For `Ix2`: `(usize, usize)`
/// - and so on..
/// - For `IxDyn`: `IxDyn`
Expand Down Expand Up @@ -402,7 +402,7 @@ impl Dimension for Dim<[Ix; 0]> {
impl Dimension for Dim<[Ix; 1]> {
const NDIM: Option<usize> = Some(1);
type SliceArg = [SliceOrIndex; 1];
type Pattern = Ix;
type Pattern = (Ix,);
type Smaller = Ix0;
type Larger = Ix2;
#[inline]
Expand All @@ -413,7 +413,7 @@ impl Dimension for Dim<[Ix; 1]> {
fn slice_mut(&mut self) -> &mut [Ix] { self.ixm() }
#[inline]
fn into_pattern(self) -> Self::Pattern {
get!(&self, 0)
self.ix().convert()
}
#[inline]
fn zeros(ndim: usize) -> Self {
Expand Down
10 changes: 10 additions & 0 deletions src/dimension/ndindex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ unsafe impl NdIndex<Ix0> for () {
}
}

unsafe impl NdIndex<Ix1> for (Ix,) {
#[inline]
fn index_checked(&self, dim: &Ix1, strides: &Ix1) -> Option<isize> {
dim.stride_offset_checked(strides, &Ix1(self.0))
}
#[inline(always)]
fn index_unchecked(&self, strides: &Ix1) -> isize {
stride_offset(self.0, get!(strides, 0))
}
}
unsafe impl NdIndex<Ix2> for (Ix, Ix) {
#[inline]
fn index_checked(&self, dim: &Ix2, strides: &Ix2) -> Option<isize> {
Expand Down
2 changes: 1 addition & 1 deletion src/doc/ndarray_for_numpy_users/simple_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
//! let odd_sum = a.slice(s![.., 1..;2]).sum();
//!
//! // Create a 1-D array of exp(index).
//! let b = Array::from_shape_fn(4, |i| (i as f64).exp());
//! let b = Array::from_shape_fn((4,), |(i,)| (i as f64).exp());
//!
//! // Add b to a (broadcasting to rows).
//! let c = a + &b;
Expand Down
2 changes: 1 addition & 1 deletion src/impl_constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl<S, A> ArrayBase<S, Ix1>
"Length must fit in `isize`.",
);
}
unsafe { Self::from_shape_vec_unchecked(v.len() as Ix, v) }
unsafe { Self::from_shape_vec_unchecked((v.len() as Ix,), v) }
}

/// Create a one-dimensional array from an iterable.
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ pub type Ixs = isize;
///
/// // 2. Use Zip to pair each row in 2D `a` with elements in 1D `b`
/// use ndarray::Zip;
/// let mut b = Array::zeros(a.rows());
/// let mut b = Array::zeros((a.rows(),));
///
/// Zip::from(a.genrows())
/// .and(&mut b)
Expand Down
10 changes: 5 additions & 5 deletions src/linalg/impl_linalg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<A, S> ArrayBase<S, Ix1>
let mut sum = A::zero();
for i in 0..self.len() {
unsafe {
sum = sum.clone() + self.uget(i).clone() * rhs.uget(i).clone();
sum = sum.clone() + self.uget((i,)).clone() * rhs.uget((i,)).clone();
}
}
sum
Expand Down Expand Up @@ -312,14 +312,14 @@ impl<A, S, S2> Dot<ArrayBase<S2, Ix1>> for ArrayBase<S, Ix2>
type Output = Array<A, Ix1>;
fn dot(&self, rhs: &ArrayBase<S2, Ix1>) -> Array<A, Ix1>
{
let ((m, a), n) = (self.dim(), rhs.dim());
let ((m, a), (n,)) = (self.dim(), rhs.dim());
if a != n {
dot_shape_error(m, a, n, 1);
}

// Avoid initializing the memory in vec -- set it during iteration
unsafe {
let mut c = Array::uninitialized(m);
let mut c = Array::uninitialized((m,));
general_mat_vec_mul(A::one(), self, rhs, A::zero(), &mut c);
c
}
Expand Down Expand Up @@ -574,8 +574,8 @@ pub fn general_mat_vec_mul<A, S1, S2, S3>(alpha: A,
S3: DataMut<Elem=A>,
A: LinalgScalar,
{
let ((m, k), k2) = (a.dim(), x.dim());
let m2 = y.dim();
let ((m, k), (k2,)) = (a.dim(), x.dim());
let (m2,) = y.dim();
if k != k2 || m != m2 {
general_dot_shape_error(m, k, k2, 1, m2, 1);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/zip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ impl<'a, A, D: Dimension> NdProducer for ArrayViewMut<'a, A, D> {
///
/// use ndarray::{Array1, Axis};
///
/// let mut totals = Array1::zeros(a.rows());
/// let mut totals = Array1::zeros((a.rows(),));
///
/// Zip::from(&mut totals)
/// .and(a.genrows())
Expand Down
2 changes: 1 addition & 1 deletion src/zip/zipmacro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
/// //
/// // The row is an array view; use the 'ref' rule on the row, to avoid the
/// // default which is to dereference the produced item.
/// let mut totals = Array1::zeros(a.rows());
/// let mut totals = Array1::zeros((a.rows(),));
///
/// azip!(mut totals, ref row (a.genrows()) in {
/// *totals = row.sum();
Expand Down
Loading