Skip to content

Commit 769a42b

Browse files
committed
ixdyn: Implement zero-dim indexing for IxDyn
(Enables indexing a zero dimensional dynamic dimension array using array[[]], array[()], array[Ix0()])
1 parent 4941aba commit 769a42b

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

src/dimension/ndindex.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ macro_rules! ndindex_with_array {
132132
}
133133

134134
#[inline]
135-
fn index_unchecked(&self, strides: &$ix_n) -> isize {
135+
fn index_unchecked(&self, _strides: &$ix_n) -> isize {
136136
$(
137-
stride_offset(self[$index], get!(strides, $index)) +
138-
)+
137+
stride_offset(self[$index], get!(_strides, $index)) +
138+
)*
139139
0
140140
}
141141
}
@@ -157,7 +157,7 @@ macro_rules! ndindex_with_array {
157157
self, strides.ndim());
158158
$(
159159
stride_offset(get!(self, $index), get!(strides, $index)) +
160-
)+
160+
)*
161161
0
162162
}
163163
}
@@ -179,7 +179,7 @@ macro_rules! ndindex_with_array {
179179
self, strides.ndim());
180180
$(
181181
stride_offset(self[$index], get!(strides, $index)) +
182-
)+
182+
)*
183183
0
184184
}
185185
}
@@ -188,6 +188,7 @@ macro_rules! ndindex_with_array {
188188
}
189189

190190
ndindex_with_array!{
191+
[0, Ix0]
191192
[1, Ix1 0]
192193
[2, Ix2 0 1]
193194
[3, Ix3 0 1 2]

tests/ixdyn.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ extern crate ndarray;
33

44
use ndarray::Array;
55
use ndarray::Ix3;
6+
use ndarray::IntoDimension;
67
use ndarray::ShapeBuilder;
78

89
#[test]
@@ -98,3 +99,50 @@ fn test_ixdyn_uget() {
9899
}
99100
assert_eq!(sum, 10.);
100101
}
102+
103+
#[test]
104+
fn test_0() {
105+
let mut a = Array::zeros(vec![]);
106+
let z = vec![].into_dimension();
107+
assert_eq!(a[z.clone()], 0.);
108+
a[[]] = 1.;
109+
assert_eq!(a[[]], 1.);
110+
assert_eq!(a.len(), 1);
111+
assert_eq!(a.as_slice().unwrap(), &[1.]);
112+
113+
let mut a = Array::zeros(vec![].f());
114+
assert_eq!(a[[]], 0.);
115+
a[[]] = 1.;
116+
assert_eq!(a[[]], 1.);
117+
assert_eq!(a.len(), 1);
118+
assert_eq!(a.as_slice().unwrap(), &[1.]);
119+
}
120+
121+
#[test]
122+
fn test_0_add() {
123+
let mut a = Array::zeros(vec![]);
124+
a += 1.;
125+
assert_eq!(a[[]], 1.);
126+
a += 2.;
127+
assert_eq!(a[[]], 3.);
128+
}
129+
130+
#[test]
131+
fn test_0_add_add() {
132+
let mut a = Array::zeros(vec![]);
133+
a += 1.;
134+
let mut b = Array::zeros(vec![]);
135+
b += 1.;
136+
a += &b;
137+
assert_eq!(a[[]], 2.);
138+
}
139+
140+
#[test]
141+
fn test_0_add_broad() {
142+
let mut b = Array::from_vec(vec![5., 6.]);
143+
let mut a = Array::zeros(vec![]);
144+
a += 1.;
145+
b += &a;
146+
assert_eq!(b[0], 6.);
147+
assert_eq!(b[1], 7.);
148+
}

0 commit comments

Comments
 (0)