Python 如何修改算术运算符(+;,-,x)
我目前正在为Python3.x编写一个线性代数模块,其中我处理自定义矩阵对象 有没有办法让基本的算术运算符如+、-、*与我的矩阵对象保持一致?比如说-Python 如何修改算术运算符(+;,-,x),python,Python,我目前正在为Python3.x编写一个线性代数模块,其中我处理自定义矩阵对象 有没有办法让基本的算术运算符如+、-、*与我的矩阵对象保持一致?比如说- >>> A = matrix("1 2;3 4") >>> B = matrix("1 0; 0 1") >>> A + B [2 2] [3 5] >>> A * A [7 10] [15 22] 现在我已经为加法、乘法等编写了单独的函数。但是键入A.multiply(A
>>> A = matrix("1 2;3 4")
>>> B = matrix("1 0; 0 1")
>>> A + B
[2 2]
[3 5]
>>> A * A
[7 10]
[15 22]
现在我已经为加法、乘法等编写了单独的函数。但是键入A.multiply(A)
比简单地键入A*A
要麻烦得多。尤其是在
此外,当您试图实现矩阵和矩阵是容器时,您可能会发现为您的类型定义自定义很有用
更新:以下是使用特殊方法实现算术运算符的自定义对象示例:
class Value(object):
def __init__(self, x):
self.x = x
def __add__(self, other):
if not isinstance(other, Value):
raise TypeError
return Value(self.x + other.x)
def __mul__(self, other):
if not isinstance(other, Value):
raise TypeError
return Value(self.x * other.x)
assert (Value(2) + Value(3)).x == 5
assert (Value(2) * Value(3)).x == 6
你在找我。尤其是在
此外,当您试图实现矩阵和矩阵是容器时,您可能会发现为您的类型定义自定义很有用
更新:以下是使用特殊方法实现算术运算符的自定义对象示例:
class Value(object):
def __init__(self, x):
self.x = x
def __add__(self, other):
if not isinstance(other, Value):
raise TypeError
return Value(self.x + other.x)
def __mul__(self, other):
if not isinstance(other, Value):
raise TypeError
return Value(self.x * other.x)
assert (Value(2) + Value(3)).x == 5
assert (Value(2) * Value(3)).x == 6
如果在类中定义一些特殊方法,Python将调用它们进行算术运算。定义加法、乘法、除法和减法的示例类:
class motar:
def __add__(self, other): return "9999"
def __mul__(self, other): return "8888"
def __sub__(self, other): return "7777"
def __div__(self, other): return "6666"
m = [ motar() for x in range(2) ] # create two instances
# arithmetic operations on class instances will call
# ... the special methods defined above:
print m[0] + m[1]
print m[0] * m[1]
print m[0] - m[1]
print m[0] / m[1]
给出:
如果在类中定义一些特殊方法,Python将调用它们进行算术运算。定义加法、乘法、除法和减法的示例类:
class motar:
def __add__(self, other): return "9999"
def __mul__(self, other): return "8888"
def __sub__(self, other): return "7777"
def __div__(self, other): return "6666"
m = [ motar() for x in range(2) ] # create two instances
# arithmetic operations on class instances will call
# ... the special methods defined above:
print m[0] + m[1]
print m[0] * m[1]
print m[0] - m[1]
print m[0] / m[1]
给出:
除非你是专门为了学习或练习而做的,否则你应该看看数值python,numpy,它是基本线性代数和矩阵的事实上的标准解决方案。它有一个矩阵类,可以实现您想要的功能。除非您专门为了学习或练习而这样做,否则您应该了解数值python,numpy,它是基本线性代数和矩阵的事实上的标准解决方案。它有一个矩阵类,可以完成您要查找的内容。您可以重写 您可能希望包含一些检查(例如添加不同维度的矩阵,或将矩阵添加到非矩阵的对象中,等等),但这只是一个简单的示例。还要注意,我正在使用
list\u to\u matrix()
函数返回一个矩阵
对象-如果这不是所需的功能,您可以很容易地更改它。对于需要实现的所有其他算术函数,您将使用类似的过程
输出:
>>> a = matrix("3 4;1 4")
>>> b = matrix("2 3;0 0")
>>> print(a)
[[3, 4], [1, 4]]
>>> print(b)
[[2, 3], [0, 0]]
>>> print(a + b)
[[5, 7], [1, 4]]
正如在另一个答案中提到的,它可能是用于矩阵运算的一个很好的资源-许多功能已经内置
>>> import numpy as np
>>> a = np.matrix("3 4;1 4")
>>> b = np.matrix("2 3;0 0")
>>> print(a)
[[3 4]
[1 4]]
>>> print(b)
[[2 3]
[0 0]]
>>> print(a + b)
[[5 7]
[1 4]]
您可以覆盖
您可能希望包含一些检查(例如添加不同维度的矩阵,或将矩阵添加到非矩阵的对象中,等等),但这只是一个简单的示例。还要注意,我正在使用list\u to\u matrix()
函数返回一个矩阵
对象-如果这不是所需的功能,您可以很容易地更改它。对于需要实现的所有其他算术函数,您将使用类似的过程
输出:
>>> a = matrix("3 4;1 4")
>>> b = matrix("2 3;0 0")
>>> print(a)
[[3, 4], [1, 4]]
>>> print(b)
[[2, 3], [0, 0]]
>>> print(a + b)
[[5, 7], [1, 4]]
正如在另一个答案中提到的,它可能是用于矩阵运算的一个很好的资源-许多功能已经内置
>>> import numpy as np
>>> a = np.matrix("3 4;1 4")
>>> b = np.matrix("2 3;0 0")
>>> print(a)
[[3 4]
[1 4]]
>>> print(b)
[[2 3]
[0 0]]
>>> print(a + b)
[[5 7]
[1 4]]
我觉得这属于“仅链接”类答案。我不能举起合适的旗子,因为它已经被提升,所以你介意用一个代码示例来改进你的答案吗?@TimCastelijns经过一番思考后,我认为你对示例的看法是完全正确的。添加了一个。我觉得这属于“仅链接”类答案。我不能举起合适的旗子,因为它已经被提升,所以你介意用一个代码示例来改进你的答案吗?@TimCastelijns经过一番思考后,我认为你对示例的看法是完全正确的。增加了一个。虽然晦涩难懂,但有帮助,但向上投票是为了对抗错误的向下投票。尽管如此,一点解释可以通过指出这是如何有用而大大改进这一演示。与其说是有用的,不如说是晦涩难懂的,但向上投票是为了对抗错误的向下投票。尽管如此,一点解释可以通过指出这是如何有用而大大改进此演示。