from math import *
class Matrix:
def __init__(self,mat):
self.mat=None
self.array=None
self.value=None
if isinstance(mat, list) and all(isinstance(row, list) for row in mat):
self.mat = mat
self.drow = len(mat)
self.dcol = len(mat[0])
elif isinstance(mat, list):
self.array = mat
self.len = len(mat)
elif isinstance(mat,(int, float)):
self.value=mat
else:
raise ValueError("Invalid matrix or value")
def __add__(self, other):
if self.mat and other.mat and self.drow == other.drow and self.dcol == other.dcol:
return list(map(lambda x, y: [i + j for i, j in zip(x, y)], self.mat, other.mat))
elif self.array and other.array and self.len==other.len:
return [i+j for i,j in zip(self.array,other.array)]
else:
return None
def __sub__(self, other):
python 矩阵的基本运算(加、减、乘、除)
最新推荐文章于 2025-05-05 17:49:45 发布