我正在尝试为矢量创建一个类,因此输入的数量取决于矢量的维数。这是我现在的代码:
class vector:
def __init__(self, entries):
self.elements = []
self.dimensionality = len(entries)
for entry in entries:
self.elements.append(entry)
def __str__(self):
buff = "("
for e in self.elements:
buff += str(e)
if self.elements.index(e) < len(self.elements) - 1:
buff += ", "
buff += ")"
return buff
def __mul__(self, otherVector):
if self.dimensionality != otherVector.dimensionality:
raise RuntimeError("Cannot multiply vectors of different dimensions")
else:
product = 0
for e in self.elements:
product += e * otherVector.elements[self.elements.index(e)]
return product
def __eq__(self, otherVariable):
return size(self) == size(otherVariable)
def size(x):
norm = 0
for e in x.elements:
norm += e**2
return norm**(1/2)
正如你现在所看到的,我只是将列表作为输入,所以我不必处理它,但我想接下来做矩阵,这需要一个列表列表,这是一个输入信息非常乏味。任何人都知道一种创建具有灵活参数的类的方法吗?
由于