numpy.ogrid

numpy.diagflat

# numpy.diag¶

`numpy.``diag`(v, k=0)[source]

Extract a diagonal or construct a diagonal array.

See the more detailed documentation for `numpy.diagonal` if you use this function to extract a diagonal and wish to write to the resulting array; whether it returns a copy or a view depends on what version of numpy you are using.

Parameters
varray_like

If v is a 2-D array, return a copy of its k-th diagonal. If v is a 1-D array, return a 2-D array with v on the k-th diagonal.

kint, optional

Diagonal in question. The default is 0. Use k>0 for diagonals above the main diagonal, and k<0 for diagonals below the main diagonal.

Returns
outndarray

The extracted diagonal or constructed diagonal array.

`diagonal`

Return specified diagonals.

`diagflat`

Create a 2-D array with the flattened input as a diagonal.

`trace`

Sum along diagonals.

`triu`

Upper triangle of an array.

`tril`

Lower triangle of an array.

Examples

```>>> x = np.arange(9).reshape((3,3))
>>> x
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
```
```>>> np.diag(x)
array([0, 4, 8])
>>> np.diag(x, k=1)
array([1, 5])
>>> np.diag(x, k=-1)
array([3, 7])
```
```>>> np.diag(np.diag(x))
array([[0, 0, 0],
[0, 4, 0],
[0, 0, 8]])
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.61878794,"math_prob":0.9792983,"size":968,"snap":"2020-34-2020-40","text_gpt3_token_len":311,"char_repetition_ratio":0.17634855,"word_repetition_ratio":0.0,"special_character_ratio":0.35123968,"punctuation_ratio":0.19915254,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.999203,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-29T14:04:02Z\",\"WARC-Record-ID\":\"<urn:uuid:811f0e5f-b45b-414c-bafb-f58ee41f9640>\",\"Content-Length\":\"11788\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:033ed1f4-7386-4f51-9a0b-498dbda248e2>\",\"WARC-Concurrent-To\":\"<urn:uuid:5de944d9-0195-42a2-9ec8-4d7e63562908>\",\"WARC-IP-Address\":\"172.67.212.79\",\"WARC-Target-URI\":\"https://numpy.org/doc/stable/reference/generated/numpy.diag.html\",\"WARC-Payload-Digest\":\"sha1:26QB3DVZS4RATWWA37VT6TUTUDA2Q3DL\",\"WARC-Block-Digest\":\"sha1:G5QJSK4HQ5DAFIHL6SH6PJOUDLD2LQ2R\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600401643509.96_warc_CC-MAIN-20200929123413-20200929153413-00360.warc.gz\"}"}