回答編集履歴
1
テストコード追加
test
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
ご確認ください。
|
2
|
+
|
3
|
+
|
4
|
+
|
1
5
|
```python
|
2
6
|
|
3
7
|
def mode_n_expansion(x, n):
|
@@ -11,3 +15,35 @@
|
|
11
15
|
return np.moveaxis(x, int(n) - 1, -1).T.reshape(x.shape[int(n) - 1], -1)
|
12
16
|
|
13
17
|
```
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
テストコード:
|
22
|
+
|
23
|
+
```python
|
24
|
+
|
25
|
+
def test_mode_n_expand():
|
26
|
+
|
27
|
+
X = np.random.random(np.random.randint(1, 5, np.random.randint(3, 5)))
|
28
|
+
|
29
|
+
for n in range(X.ndim):
|
30
|
+
|
31
|
+
import itertools as it
|
32
|
+
|
33
|
+
for i in it.product(*map(range, X.shape)):
|
34
|
+
|
35
|
+
Y = mode_n_expansion(X, n=n + 1)
|
36
|
+
|
37
|
+
j = int(sum(
|
38
|
+
|
39
|
+
i_k * np.product([I_m for m, I_m in enumerate(X.shape[:k]) if m != n])
|
40
|
+
|
41
|
+
for k, i_k in enumerate(i)
|
42
|
+
|
43
|
+
if k != n
|
44
|
+
|
45
|
+
))
|
46
|
+
|
47
|
+
assert X[i] == Y[i[n], j]
|
48
|
+
|
49
|
+
```
|