python 下三角矩阵_Python | 矩阵的下三角

python 下三角矩阵

A matrix can be seen in different ways and one of them is the lower triangular matrix part. Some problems in linear algebra are concerned with the lower triangular part of the matrix.

可以用不同的方式看到矩阵,其中之一是下部三角形矩阵部分。 线性代数中的一些问题与矩阵下三角部分有关

For this purpose, we have a predefined function numpy.tril(a) in the NumPy library package which automatically stores the lower triangular elements in a separate matrix. In this article, we are going to print the lower triangular elements of a matrix using inbuilt function numpy.tril(a).

为此,我们在NumPy库包中提供了预定义的函数numpy.tril(a) ,该函数自动将下部三角形元素存储在单独的矩阵中。 在本文中,我们将使用内置函数numpy.tril(a)打印矩阵的下三角元素

Python代码查找矩阵的下三角 (Python code to find lower triangle of a matrix)

# Linear Algebra Learning Sequence
# Lower Triangle of matrix

import numpy as np

print('lower Triangle of an 3x3 identity matrix : ', np.tril(np.eye(3)))

a = np.arange(9).reshape((3,3))

print('\n\nMatrix a : ', a)
print('lower Triangle of Matrix a : ', np.tril(a))

b = np.tril(np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]))
print('\n\nMatrix b : ', np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]))
print('lower Triangle of Matrix b : ', b)

Output:

输出:

lower Triangle of an 3x3 identity matrix :  [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]


Matrix a :  [[0 1 2]
 [3 4 5]
 [6 7 8]]
lower Triangle of Matrix a :  [[0 0 0]
 [3 4 0]
 [6 7 8]]


Matrix b :  [[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
lower Triangle of Matrix b :  [[ 1  0  0]
 [ 4  5  0]
 [ 7  8  9]
 [10 11 12]]


翻译自: https://www.includehelp.com/python/lower-triangle-of-a-matrix.aspx

python 下三角矩阵

你可能感兴趣的:(python,numpy,机器学习,深度学习,人工智能)