Chris Smith (smichr) and I were just talking on IRC, and it turns out
that the Poly class could benefit from this as well.  Currently, Poly
implements __mul__ and __add__, but it causes it to be "non-
commutative", in the sense that order matters for combining.
>>> Poly(x, x)*x
Poly(x**2, x)
>>> x*Poly(x, x)
x*Poly(x, x)
>>> Poly(x, x) + x
Poly(2*x, x)
>>> x + Poly(x, x)
x + Poly(x, x)
>>> Poly(x, x) + Poly(x, x)*x
Poly(x**2 + x, x)
>>> Poly(x, x) + x*Poly(x, x) # It can
x + x*Poly(x, x)

So it definitely isn't just fancy stuff like arbitrary constant or
Order classes that could benefit from this.  Any class that is
abstract container of Basic objects like Poly where you want stuff
multiplied of added from the outside to be incorporated to the inside
non-commutatively could use this.

Also, Poly achieves the above combining by implementing __mul__ and
__add__ (which is why it only works to the right), but I think it
would be better to minimize the use of theses special Python methods
outside of the core classes.

Aaron Meurer

On Jul 11, 1:50 pm, Ronan Lamy <ronan.l...@gmail.com> wrote:
> Le vendredi 10 juillet 2009 à 23:00 -0600, Ondrej Certik a écrit :
>
>
>
>
>
> > Hi,
>
> > Aaron is now with me in Los Alamos and we are trying to figure out how
> > to nicely incorporate the Constant class inside SymPy, it's currently
> > in this branch:
>
> >http://github.com/asmeurer/sympy/tree/constant-Mul
>
> > and it works, however, as you can see, it need modifications in the
> > Add and Mul classes, and that's bad. The same goes to the Order class,
> > which also needs special handling in Mul and Add, and that's bad too.
>
> > I would like to find a solution, so that people can hook up their own
> > classes inside sympy and it would just work, yet the core would not
> > have to be modified. One thing that we figured out is to use a
> >handler, essentially Basic() would have some fasthandler, that would
> > work for symbols and be fast (using dictionaries). But then Constant
> > and Order could override thathandlerand do their own stuff, like
> > O(x)+x -> O(x) and similar things, for which the defaulthandlerwill
> > fail, because O(x) != x, and thus the dictionary approach will fail.
>
> > If anyone has any ideas how to approach this, it'd be awesome --- just
> > throw your idea here, no matter how crazy it is.
>
> > Ondrej
>
> I think the core issue is that Order and Constant are actually sets of
> expressions which we want to handle as ordinary expressions. Therefore,
> the "+" in 1 + O(x) has a different meaning from the "+" in 1 + x: in
> the first case, it's a set operation whose result is an affine
> subspace.
>
> I think that Add should only take care of the "handle as ordinary
> expressions" part, while the code for proper handling of order terms
> should reside in an Order.add method. Additionally, there should be a
> symplify_order function that retrieves the Order terms in an expression
> and calls Order.add (and Order.mul, ...) appropriately.
> So, we'd have:
>         1 + x + O(x)                    ->  1 + x + O(x)
>         O(x).add(1, x)                  ->  1 + O(x)
>         symplify_order(1 + x + O(x))    ->  1 + O(x)
>
> Constant is actually a bit more complex since it should be handled as a
> set only if it appears only once in the expression, but I think my idea
> makes even more sense, precisely because it should be handled as an
> ordinary expression in most cases (e.g. consider expand(C(y)*(x+y)) ).
> Also, there's no existing code to break.
>
> Alternatively, we could implement some generalised abstract nonsense,
> such as classes ExpressionSubspace, ExpressionAffineSpace,
> ExpressionSetPlus with a new base class ExpressionSet, but I'm not sure
> it's worth it.
>
> Ronan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sympy@googlegroups.com
To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to