A
andy2O
Hello comp.lang.py,
Can you help me with ideas for the following (somewhat newbie) OO
design question in Python? Note, I'm using psuedo-code, not actual
Python for the examples!
Background:
-----------
I need to represent a small variety of mathematical constructs
symbolically using Python classes.
1) I have classes, representing numbers, symbolic variables etc.
2) I have classes for groups of these objects, such as a symbolic Sums.
3) To simplify the mathematical constructs, I've provided each of the
classes with a simplify(self) method.
The following psuedo-code gives an example
a = Variable('a')
b = Variable('b')
two = Number(2) #Yes there are good reason not
#to just use Python's int object!![Smile :) :)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
expression1 = Sum([a,b,a,a,two]) #This represents a+b+a+a+2
then calling "expression1.simplify()" would invoke Sum.simplify() and
reduce expression1 to 3*a+b+2
Question:
---------
Now suppose I set "expression2 = Sum([a,-a])" and Sum.simplify()
recognises that the two terms cancel and the Sum has value 0.
Can I make "expression2.simplify()" transform expression2 from an
instance of Sum to an instance of Number(0) **in place**? Is that
possibe, or do I really have to write
expression2 = SimplifyFunction(expression2)
and use the function return to set expression2 to be a Number(0)
object, which is annoying for a variety of reasons! Have I made a
mistake in the design?
Many thanks,
andy.
Can you help me with ideas for the following (somewhat newbie) OO
design question in Python? Note, I'm using psuedo-code, not actual
Python for the examples!
Background:
-----------
I need to represent a small variety of mathematical constructs
symbolically using Python classes.
1) I have classes, representing numbers, symbolic variables etc.
2) I have classes for groups of these objects, such as a symbolic Sums.
3) To simplify the mathematical constructs, I've provided each of the
classes with a simplify(self) method.
The following psuedo-code gives an example
a = Variable('a')
b = Variable('b')
two = Number(2) #Yes there are good reason not
#to just use Python's int object!
expression1 = Sum([a,b,a,a,two]) #This represents a+b+a+a+2
then calling "expression1.simplify()" would invoke Sum.simplify() and
reduce expression1 to 3*a+b+2
Question:
---------
Now suppose I set "expression2 = Sum([a,-a])" and Sum.simplify()
recognises that the two terms cancel and the Sum has value 0.
Can I make "expression2.simplify()" transform expression2 from an
instance of Sum to an instance of Number(0) **in place**? Is that
possibe, or do I really have to write
expression2 = SimplifyFunction(expression2)
and use the function return to set expression2 to be a Number(0)
object, which is annoying for a variety of reasons! Have I made a
mistake in the design?
Many thanks,
andy.