1
1
from __future__ import division , absolute_import , print_function
2
2
3
- __all__ = ['atleast_1d' , 'atleast_2d' , 'atleast_3d' , 'vstack' , 'hstack' ,
4
- 'stack' ]
3
+ __all__ = ['atleast_1d' , 'atleast_2d' , 'atleast_3d' , 'block' , 'hstack' ,
4
+ 'stack' , 'vstack' ]
5
+
5
6
6
7
from . import numeric as _nx
7
- from .numeric import asanyarray , newaxis
8
+ from .numeric import array , asanyarray , newaxis
8
9
9
10
def atleast_1d (* arys ):
10
11
"""
@@ -202,6 +203,7 @@ def vstack(tup):
202
203
dstack : Stack arrays in sequence depth wise (along third dimension).
203
204
concatenate : Join a sequence of arrays along an existing axis.
204
205
vsplit : Split array into a list of multiple sub-arrays vertically.
206
+ block : Create block arrays.
205
207
206
208
Notes
207
209
-----
@@ -253,6 +255,7 @@ def hstack(tup):
253
255
dstack : Stack arrays in sequence depth wise (along third axis).
254
256
concatenate : Join a sequence of arrays along an existing axis.
255
257
hsplit : Split array along second axis.
258
+ block : Create block arrays.
256
259
257
260
Notes
258
261
-----
@@ -279,6 +282,7 @@ def hstack(tup):
279
282
else :
280
283
return _nx .concatenate (arrs , 1 )
281
284
285
+
282
286
def stack (arrays , axis = 0 ):
283
287
"""
284
288
Join a sequence of arrays along a new axis.
@@ -305,6 +309,7 @@ def stack(arrays, axis=0):
305
309
--------
306
310
concatenate : Join a sequence of arrays along an existing axis.
307
311
split : Split array into a list of multiple sub-arrays of equal size.
312
+ block : Create block arrays.
308
313
309
314
Examples
310
315
--------
@@ -348,3 +353,83 @@ def stack(arrays, axis=0):
348
353
sl = (slice (None ),) * axis + (_nx .newaxis ,)
349
354
expanded_arrays = [arr [sl ] for arr in arrays ]
350
355
return _nx .concatenate (expanded_arrays , axis = axis )
356
+
357
+
358
+ def block (tup_tup ):
359
+ """
360
+ Create block arrays similar to Matlab's "square bracket stacking":
361
+
362
+ [A A; B B]
363
+
364
+ You can create a block array with the same notation you use for
365
+ `np.array`.
366
+
367
+ Parameters
368
+ ----------
369
+ tup_tup : sequence of sequence of ndarrays
370
+ 1-D arrays are treated as row vectors.
371
+
372
+ Returns
373
+ -------
374
+ stacked : ndarray
375
+ The 2-D array formed by stacking the given arrays.
376
+
377
+ See Also
378
+ --------
379
+ stack : Stack arrays in sequence along a new dimension.
380
+ hstack : Stack arrays in sequence horizontally (column wise).
381
+ vstack : Stack arrays in sequence vertically (row wise).
382
+ dstack : Stack arrays in sequence depth wise (along third dimension).
383
+ concatenate : Join a sequence of arrays together.
384
+ vsplit : Split array into a list of multiple sub-arrays vertically.
385
+
386
+ Examples
387
+ --------
388
+ Stacking in a row:
389
+ >>> A = np.array([[1, 2, 3]])
390
+ >>> B = np.array([[2, 3, 4]])
391
+ >>> block([A, B])
392
+ array([[1, 2, 3, 2, 3, 4]])
393
+
394
+ >>> A = np.array([[1, 1], [1, 1]])
395
+ >>> B = 2 * A
396
+ >>> block([A, B])
397
+ array([[1, 1, 2, 2],
398
+ [1, 1, 2, 2]])
399
+
400
+ >>> # the tuple notation also works
401
+ >>> block((A, B))
402
+ array([[1, 1, 2, 2],
403
+ [1, 1, 2, 2]])
404
+
405
+ >>> # block matrix with arbitrary shaped elements
406
+ >>> One = np.array([[1, 1, 1]])
407
+ >>> Two = np.array([[2, 2, 2]])
408
+ >>> Three = np.array([[3, 3, 3, 3, 3, 3]])
409
+ >>> four = np.array([4, 4, 4, 4, 4, 4])
410
+ >>> five = np.array([5])
411
+ >>> six = np.array([6, 6, 6, 6, 6])
412
+ >>> Zeros = np.zeros((2, 6), dtype=int)
413
+ >>> block([[One, Two],
414
+ ... [Three],
415
+ ... [four],
416
+ ... [five, six],
417
+ ... [Zeros]])
418
+ array([[1, 1, 1, 2, 2, 2],
419
+ [3, 3, 3, 3, 3, 3],
420
+ [4, 4, 4, 4, 4, 4],
421
+ [5, 6, 6, 6, 6, 6],
422
+ [0, 0, 0, 0, 0, 0],
423
+ [0, 0, 0, 0, 0, 0]])
424
+
425
+ >>> # 1-D vectors are treated as row arrays
426
+ >>> a = np.array([1, 1])
427
+ >>> b = np.array([2, 2])
428
+ >>> block([a, b])
429
+ array([[1, 1, 2, 2]])
430
+ """
431
+ if isinstance (tup_tup [0 ], list ) or isinstance (tup_tup [0 ], tuple ):
432
+ result = vstack ([hstack (row ) for row in tup_tup ])
433
+ else :
434
+ result = hstack (tup_tup )
435
+ return atleast_2d (result )
0 commit comments