回答編集履歴
1
テストコード追加
answer
CHANGED
@@ -1,7 +1,25 @@
|
|
1
|
+
ご確認ください。
|
2
|
+
|
1
3
|
```python
|
2
4
|
def mode_n_expansion(x, n):
|
3
5
|
x = np.asarray(x)
|
4
6
|
if n <= 0 or n % 1 != 0 or x.ndim < n:
|
5
7
|
raise ValueError((x.shape, n))
|
6
8
|
return np.moveaxis(x, int(n) - 1, -1).T.reshape(x.shape[int(n) - 1], -1)
|
9
|
+
```
|
10
|
+
|
11
|
+
テストコード:
|
12
|
+
```python
|
13
|
+
def test_mode_n_expand():
|
14
|
+
X = np.random.random(np.random.randint(1, 5, np.random.randint(3, 5)))
|
15
|
+
for n in range(X.ndim):
|
16
|
+
import itertools as it
|
17
|
+
for i in it.product(*map(range, X.shape)):
|
18
|
+
Y = mode_n_expansion(X, n=n + 1)
|
19
|
+
j = int(sum(
|
20
|
+
i_k * np.product([I_m for m, I_m in enumerate(X.shape[:k]) if m != n])
|
21
|
+
for k, i_k in enumerate(i)
|
22
|
+
if k != n
|
23
|
+
))
|
24
|
+
assert X[i] == Y[i[n], j]
|
7
25
|
```
|