File indexing completed on 2026-04-09 07:48:52
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 """
0022
0023 * https://stackoverflow.com/questions/5181320/under-what-circumstances-are-rmul-called
0024
0025
0026 (mul) a * b
0027 (rmul) c * 2
0028 (rmul) d * 6
0029 (rmul) e * 24
0030 120
0031
0032
0033 * https://jameshensman.wordpress.com/2010/06/14/multiple-matrix-multiplication-in-numpy/
0034 * http://www.lfd.uci.edu/~gohlke/code/transformations.py.html
0035
0036
0037
0038
0039
0040 """
0041
0042 from operator import mul
0043 from functools import reduce
0044
0045 class A(object):
0046
0047 @classmethod
0048 def prod(cls, a, b):
0049 return a * b
0050
0051 def __init__(self, name, val):
0052 self.name = name
0053 self.val = val
0054 def __repr__(self):
0055 return self.name
0056
0057 def __mul__(self, other):
0058 print("(mul) %r * %r " % (self, other))
0059 return self.val * other.val
0060
0061 def __rmul__(self, other):
0062 print("(rmul) %r * %r " % (self, other))
0063 return self.val * other
0064
0065
0066
0067
0068
0069 if __name__ == '__main__':
0070
0071
0072 a = A("a",1)
0073 b = A("b",2)
0074 c = A("c",3)
0075 d = A("d",4)
0076 e = A("e",5)
0077
0078
0079 res = reduce(mul, [a,b,c,d,e] )
0080
0081 print(res)
0082
0083
0084