1 # For licensing info see the included LICENSE file
3 # Josef Moudrik, <J dot Moudrik at standard google mail ending>, 2012
8 from exc
import IncompatibleUnits
15 OPERATORS_ARITHMETIC
=[]
17 def register_operator(cl
):
21 class GenericOp(object):
22 def __init__(self
, arity
, commutativity
, name
):
24 self
.commutativity
= commutativity
27 def __call__(self
, args
):
28 assert len(args
) == self
.getArity()
29 values
= map(lambda n
: n
.getValue(), args
)
30 sdsqs
= map(lambda n
: n
.getSdSquare(), args
)
31 units
= map(lambda n
: n
.getUnits(), args
)
33 new_value
, new_sdsq
, new_units
= self
.computeNewNumber(values
, sdsqs
, units
)
34 return number
.Number(new_value
, sdsq
=new_sdsq
, string
=self
.name
, units
=new_units
, parents
=args
)
36 def computeNewNumber(self
, values
, sdsqs
, units
):
37 raise NotImplementedError
42 def isCommutative(self
):
43 return self
.commutativity
46 # Arithmetic Operators
50 class Plus(GenericOp
):
52 super(Plus
, self
).__init
__(2, True, "Plus")
54 def computeNewNumber(self
, (n1v
, n2v
), (n1sdsq
, n2sdsq
), (u1
, u2
)):
56 raise IncompatibleUnits
58 new_sdsq
= n1sdsq
+ n2sdsq
59 return new_value
, new_sdsq
, u1
62 class Minus(GenericOp
):
64 super(Minus
, self
).__init
__(2, False, "Minus")
66 def computeNewNumber(self
, (n1v
, n2v
), (n1sdsq
, n2sdsq
), (u1
, u2
)):
68 raise IncompatibleUnits
70 new_sdsq
= n1sdsq
+ n2sdsq
71 return new_value
, new_sdsq
, u1
74 class Mult(GenericOp
):
76 super(Mult
, self
).__init
__(2, True, "Mult")
78 def computeNewNumber(self
, (n1v
, n2v
), (n1sdsq
, n2sdsq
), (u1
, u2
)):
80 new_sdsq
= n1v
**2 * n2sdsq
+ n2v
**2 * n1sdsq
81 return new_value
, new_sdsq
, number
.units_join(u1
, u2
)
86 super(Div
, self
).__init
__(2, False, "Div")
88 def computeNewNumber(self
, (n1v
, n2v
), (n1sdsq
, n2sdsq
), (u1
, u2
)):
90 new_sdsq
= n2v
**(-2) * n1sdsq
+ n1v
**2 * n2v
**(-4) * n2sdsq
91 return new_value
, new_sdsq
, number
.units_diff(u1
, u2
)
96 # e.g. op(number) -> 2 * number
99 class Times(GenericOp
):
100 def __init__(self
, num
):
101 super(Times
, self
).__init
__(1, True, str(num
))
104 def computeNewNumber(self
, (n1v
,), (n1sdsq
, ), (u1
,)):
105 new_value
= self
.num
* n1v
106 new_sdsq
= self
.num
**2 * n1sdsq
107 return new_value
, new_sdsq
, u1
119 OPERATORS_ARITHMETIC
= map(lambda x
: x() , OP_CLASSES
)