def block_diag(*arrs):
 if (arrs == ()):
  arrs = ([],)
 arrs = [np.atleast_2d(a) for a in arrs]
 bad_args = [k for k in range(len(arrs)) if (arrs[k].ndim > 2)]
 if bad_args:
  raise ValueError(('arguments in the following positions have dimension greater than 2: %s' % bad_args))
 shapes = np.array([a.shape for a in arrs])
 out_dtype = np.find_common_type([arr.dtype for arr in arrs], [])
 out = np.zeros(np.sum(shapes, axis=0), dtype=out_dtype)
 (r, c) = (0, 0)
 for (i, (rr, cc)) in enumerate(shapes):
  out[r:(r + rr), c:(c + cc)] = arrs[i]
  r += rr
  c += cc
 return out

def block_diag ( * arrs ) : if ( arrs == ( ) ) : arrs = ( [ ] , ) arrs = [ np . atleast_2d ( a ) for a in arrs ] bad_args = [ k for k in range ( len ( arrs ) ) if ( arrs [ k ] . ndim > 2 ) ] if bad_args : raise ValueError ( ( 'arguments in the following positions have dimension greater than 2: %s' % bad_args ) ) shapes = np . array ( [ a . shape for a in arrs ] ) out_dtype = np . find_common_type ( [ arr . dtype for arr in arrs ] , [ ] ) out = np . zeros ( np . sum ( shapes , axis = 0 ) , dtype = out_dtype ) ( r , c ) = ( 0 , 0 ) for ( i , ( rr , cc ) ) in enumerate ( shapes ) : out [ r : ( r + rr ) , c : ( c + cc ) ] = arrs [ i ] r += rr c += cc return out
build a block diagonal sparse matrix from provided matrices .