回答編集履歴

2

追記

2019/12/07 06:01

投稿

LouiS0616
LouiS0616

スコア35668

test CHANGED
@@ -93,3 +93,71 @@
93
93
 
94
94
 
95
95
  分かり易いと思う方を使えば良いのではないでしょうか。
96
+
97
+
98
+
99
+ なぜ reshape(3, -3) でも動作するか?
100
+
101
+ ---
102
+
103
+ 実装で dimensions[i] == -1 ではなく dimensions[i] < 0 が判定に用いられているから。
104
+
105
+
106
+
107
+ > ```C
108
+
109
+ > for (i = 0; i < n; i++) {
110
+
111
+ if (dimensions[i] < 0) {
112
+
113
+ if (i_unknown == -1) {
114
+
115
+ i_unknown = i;
116
+
117
+ }
118
+
119
+ else {
120
+
121
+ PyErr_SetString(PyExc_ValueError,
122
+
123
+ "can only specify one unknown dimension");
124
+
125
+ return -1;
126
+
127
+ }
128
+
129
+ }
130
+
131
+ else if (npy_mul_with_overflow_intp(&s_known, s_known,
132
+
133
+ dimensions[i])) {
134
+
135
+ raise_reshape_size_mismatch(newshape, arr);
136
+
137
+ return -1;
138
+
139
+ }
140
+
141
+ }
142
+
143
+
144
+
145
+ > if (i_unknown >= 0) {
146
+
147
+ if (s_known == 0 || s_original % s_known != 0) {
148
+
149
+ raise_reshape_size_mismatch(newshape, arr);
150
+
151
+ return -1;
152
+
153
+ }
154
+
155
+ dimensions[i_unknown] = s_original / s_known;
156
+
157
+ }
158
+
159
+ > ```
160
+
161
+
162
+
163
+ [numpy/shape.c at v1.17.4 · numpy/numpy · GitHub](https://github.com/numpy/numpy/blob/v1.17.4/numpy/core/src/multiarray/shape.c#L472)

1

修正

2019/12/07 06:01

投稿

LouiS0616
LouiS0616

スコア35668

test CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
 
4
4
 
5
- > **newshape : int or tuple of ints**
5
+ > #####**newshape : int or tuple of ints**
6
6
 
7
- The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. **One shape dimension can be -1**.
7
+ The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length.
8
8
 
9
- In this case, the value is inferred from the length of the array and remaining dimensions.
9
+ **One shape dimension can be -1**. In this case, the value is inferred from the length of the array and remaining dimensions.
10
10
 
11
11
 
12
12