MCPcopy Index your code
hub / github.com/numpy/numpy / diagflat

Function diagflat

numpy/lib/_twodim_base_impl.py:334–388  ·  view source on GitHub ↗

Create a two-dimensional array with the flattened input as a diagonal. Parameters ---------- v : array_like Input data, which is flattened and set as the `k`-th diagonal of the output. k : int, optional Diagonal to set; 0, the default, corresponds to the

(v, k=0)

Source from the content-addressed store, hash-verified

332
333@array_function_dispatch(_diag_dispatcher)
334def diagflat(v, k=0):
335 """
336 Create a two-dimensional array with the flattened input as a diagonal.
337
338 Parameters
339 ----------
340 v : array_like
341 Input data, which is flattened and set as the `k`-th
342 diagonal of the output.
343 k : int, optional
344 Diagonal to set; 0, the default, corresponds to the "main" diagonal,
345 a positive (negative) `k` giving the number of the diagonal above
346 (below) the main.
347
348 Returns
349 -------
350 out : ndarray
351 The 2-D output array.
352
353 See Also
354 --------
355 diag : MATLAB work-alike for 1-D and 2-D arrays.
356 diagonal : Return specified diagonals.
357 trace : Sum along diagonals.
358
359 Examples
360 --------
361 >>> import numpy as np
362 >>> np.diagflat([[1,2], [3,4]])
363 array([[1, 0, 0, 0],
364 [0, 2, 0, 0],
365 [0, 0, 3, 0],
366 [0, 0, 0, 4]])
367
368 >>> np.diagflat([1,2], 1)
369 array([[0, 1, 0],
370 [0, 0, 2],
371 [0, 0, 0]])
372
373 """
374 conv = _array_converter(v)
375 v, = conv.as_arrays(subok=False)
376 v = v.ravel()
377 s = len(v)
378 n = s + abs(k)
379 res = zeros((n, n), v.dtype)
380 if (k >= 0):
381 i = arange(0, n - k, dtype=intp)
382 fi = i + k + i * n
383 else:
384 i = arange(0, n + k, dtype=intp)
385 fi = i + (i - k) * n
386 res.flat[fi] = v
387
388 return conv.wrap(res)
389
390@finalize_array_function_like
391@set_module('numpy')

Callers 1

test_shape_scalarMethod · 0.85

Calls 3

zerosFunction · 0.85
wrapMethod · 0.80
ravelMethod · 0.45

Tested by 1

test_shape_scalarMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…