Stack arrays along a new axis */
| 1265 | |
| 1266 | /** Stack arrays along a new axis */ |
| 1267 | array stack( |
| 1268 | const std::vector<array>& arrays, |
| 1269 | int axis, |
| 1270 | StreamOrDevice s /* = {} */) { |
| 1271 | if (arrays.empty()) { |
| 1272 | throw std::invalid_argument("[stack] No arrays provided for stacking"); |
| 1273 | } |
| 1274 | if (!std::all_of(arrays.begin(), arrays.end(), [&](const auto& a) { |
| 1275 | return arrays[0].shape() == a.shape(); |
| 1276 | })) { |
| 1277 | throw std::invalid_argument("[stack] All arrays must have the same shape"); |
| 1278 | } |
| 1279 | auto normalized_axis = |
| 1280 | normalize_axis_index(axis, arrays[0].ndim() + 1, "[stack] "); |
| 1281 | std::vector<array> new_arrays; |
| 1282 | new_arrays.reserve(arrays.size()); |
| 1283 | for (auto& a : arrays) { |
| 1284 | new_arrays.emplace_back(expand_dims(a, normalized_axis, s)); |
| 1285 | } |
| 1286 | return concatenate(new_arrays, axis, s); |
| 1287 | } |
| 1288 | |
| 1289 | array stack(const std::vector<array>& arrays, StreamOrDevice s /* = {} */) { |
| 1290 | return stack(arrays, 0, s); |