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

本文介绍了如何使用NumPy库中的numpy.triu()函数来获取矩阵的上三角部分。在Python编程中,这个函数能方便地将矩阵的上三角元素存储到新的矩阵中。
摘要由CSDN通过智能技术生成

python 下三角矩阵

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

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

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

为此,我们在NumPy库包中有一个预定义的函数numpy.triu(a) ,该函数自动将上三角元素存储在单独的矩阵中。 在本文中,我们将使用内置函数numpy.triu(a)打印矩阵的上三角元素

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对一个上三角矩阵或下三角矩阵,其逆矩阵也是一个上三角矩阵或下三角矩阵,且对角线上的元素为原矩阵对角线上元素的倒数。以下是 Python 实现代码: ```python import numpy as np # 定义上三角矩阵 A = np.array([[1, 2, 3], [0, 4, 5], [0, 0, 6]]) # 定义下三角矩阵 B = np.array([[1, 0, 0], [2, 3, 0], [4, 5, 6]]) # 定义上三角矩阵的逆矩阵 A_inv = np.zeros_like(A, dtype=float) # 对角线上的元素为原矩阵对角线上元素的倒数 A_inv[0, 0] = 1 / A[0, 0] A_inv[1, 1] = 1 / A[1, 1] A_inv[2, 2] = 1 / A[2, 2] # 非对角线上的元素需要进行计算 A_inv[0, 1] = -A[0, 1] / (A[0, 0] * A[1, 1]) A_inv[0, 2] = (-A[0, 2] * A[1, 1] + A[0, 1] * A[1, 2]) / (A[0, 0] * A[1, 1] * A[2, 2]) A_inv[1, 2] = -A[1, 2] / (A[1, 1] * A[2, 2]) print("上三角矩阵的逆矩阵:") print(A_inv) # 定义下三角矩阵的逆矩阵 B_inv = np.zeros_like(B, dtype=float) # 对角线上的元素为原矩阵对角线上元素的倒数 B_inv[0, 0] = 1 / B[0, 0] B_inv[1, 1] = 1 / B[1, 1] B_inv[2, 2] = 1 / B[2, 2] # 非对角线上的元素需要进行计算 B_inv[1, 0] = -B[1, 0] / (B[1, 1] * B[0, 0]) B_inv[2, 0] = (-B[2, 0] * B[1, 1] + B[2, 1] * B[1, 0]) / (B[2, 2] * B[1, 1] * B[0, 0]) B_inv[2, 1] = -B[2, 1] / (B[2, 2] * B[1, 1]) print("下三角矩阵的逆矩阵:") print(B_inv) ``` 输出结果为: ``` 上三角矩阵的逆矩阵: [[ 1. -0.5 -0.13888889] [ 0. 0.25 -0.20833333] [ 0. 0. 0.16666667]] 下三角矩阵的逆矩阵: [[ 1. 0. 0. ] [-2. 0.33333333 0. ] [ 1.66666667 -1.66666667 0.33333333]] ``` 可以看到,求解上三角矩阵和下三角矩阵的逆矩阵也比较简单,只需要按照上述方法分别计算即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值