The Python matrix is divided into upper triangular and diagonal three matrices

Source: Internet
Author: User

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.
first, the lower triangular array and the upper triangular array on the diagonal of a square are extracted (if only the upper and lower triangular arrays are needed, the second parameter in the Tril/triu is removed)
on the code (both Tril and Triu are returned in the form of array, and the mat is used to convert back to the matrix):
>>> m = Np.mat ("1,2,3;4,5,6;7,8,9")>>> Mmatrix ([[1, 2, 3],        [4, 5, 6],        [7, 8, 9]])>>> L = Np.tril (m,-1)>>>< c11> Larray ([[0, 0, 0],       [4, 0, 0],       [7, 8, 0]])>>> U = Np.triu (m,1) 
      >>>2, 3],       6],       [0, 0, 0]])

There are two ways to extract the elements on the diagonal as a single matrix:

1, using Np.diag two times, and then use mat conversion back to the matrix:

>>> D = np.diag (Np.diag (m))>>> Darray ([[1, 0, 0],       5, 0],       9]])>>> D = Np.mat (d)>>> Dmatrix ([[1, 0, 0],        5, 0],        9])

2, the use of the lower triangular matrix minus the next triangle matrix (that is, the lower triangular array under the diagonal):

>>> D = Np.tril (m)- L>>> Darray ([[[1, 0, 0]       ,5, 0]       ,9  ]])>>> D = Np.mat (d)>>> Dmatrix ([[1, 0, 0],        5, 0],        /C11>9]])

The Python matrix is divided into upper triangular and diagonal three matrices

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.