我是靠谱客的博主 能干时光,最近开发中收集的这篇文章主要介绍Python:矩阵加法、减法和数乘,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#矩阵加法、减法和数乘
print("Choose a function: 'addition', 'subtraction' or 'multiplication': ")
function = input()
#选择功能
if function == "addition" or function == "subtraction":
matrix_1 = []
#定义矩阵规模
matrix_2 = []
matrix_3 = []
line = input("Enter the line: ")
row = input("Enter the row: ")
line =int(line)
row = int(row)
number_1 = line * row
number_2 = 1
#输入矩阵1
number_3 = 1
print(f"Enter a {line} * {row} matrix 1: ")
while number_2 <= number_1:
if number_2 % row == 1:
print(f"Enter line {number_3} : ")
number_3 += 1
a = input()
a = int(a)
matrix_1.append(a)
number_2 += 1
print(f"Finished enter matrix 1. The matrix: ")
number_2 = 0
while number_2 < number_1:
print(matrix_1[number_2:number_2 + row])
number_2 += row
number_2 = 1
#输入矩阵2
number_3 = 1
print(f"Enter a {line} * {row} matrix 2: ")
while number_2 <= number_1:
if number_2 % row == 1:
print(f"Enter line {number_3} : ")
number_3 += 1
b = input()
b = int(b)
matrix_2.append(b)
number_2 += 1
print(f"Finished enter matrix 2. The matrix: ")
number_2 = 0
while number_2 < number_1:
print(matrix_2[number_2:number_2 + row])
number_2 += row
number_2 = 0
#计算
if function == "addition":
while number_2 < number_1:
matrix_3.append(matrix_1[number_2] + matrix_2[number_2])
number_2 += 1
if function == "subtraction":
while number_2 < number_1:
matrix_3.append(matrix_1[number_2] - matrix_2[number_2])
number_2 += 1
if function == "addition":
#输出
print("Matrix 1 adds matrix 2 = ")
if function == "subtraction":
print("Matrix 1 subtracts matrix 2 = ")
number_2 = 0
while number_2 < number_1:
print(matrix_3[number_2:number_2 + row])
number_2 += row
if function == "multiplication":
#矩阵数乘
matrix_1 = []
matrix_2 = []
line = input("Enter the line: ")
row = input("Enter the row: ")
k = input("Enter k: ")
line =int(line)
row = int(row)
k = int(k)
number_1 = line * row
number_2 = 1
#输入矩阵
number_3 = 1
print(f"Enter a {line} * {row} matrix: ")
while number_2 <= number_1:
if number_2 % row == 1:
print(f"Enter line {number_3} : ")
number_3 += 1
a = input()
a = int(a)
matrix_1.append(a)
number_2 += 1
print(f"Finished enter matrix. The matrix: ")
number_2 = 0
while number_2 < line * row:
print(matrix_1[number_2:number_2 + row])
number_2 += row
number_2 = 0
#计算
while number_2 < number_1:
matrix_2.append(k * matrix_1[number_2])
number_2 += 1
print("k * matrix = ")
#输出
number_2 = 0
while number_2 < number_1:
print(matrix_2[number_2:number_2 + row])
number_2 += row

有很多代码块重复出现(如输入矩阵、输出矩阵的部分),是因为用现在学的基础知识还不能解决~~~

最后

以上就是能干时光为你收集整理的Python:矩阵加法、减法和数乘的全部内容,希望文章能够帮你解决Python:矩阵加法、减法和数乘所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(32)

评论列表共有 0 条评论

立即
投稿
返回
顶部