Point class repeatedly returns a hardcoded Point class

23 views
Skip to first unread message

Duane Nykamp

unread,
Dec 6, 2014, 5:17:15 PM12/6/14
to sy...@googlegroups.com
The Point class, for numerous functions, such as .evalf, returns a hard coded Point class.  This means that for a derived class, these functions end up returning the original Point class not the derived class.  I assume there must be a way to code it differently so these functions return the derived class.  For now, I'm just overriding the functions, like .evalf, that I need to use so that they return my derived class.

Should the calls to Point be changed to self.__class__ or is there a different way one is supposed to accomplish this in Python?  If it is as simple as that, I could make the change and submit a pull request.

(I'm creating a derived class to get rid of the call to nsimplify, as functions like doit() and bottom up get rid of the evaluate=False flag.  nsimplify is quite slow.  Each call to nsimplify for something like Point(3.23423545235,6.345436534630) takes on the order of 100 ms.  My webserver slowed to a crawl with 140 students repeatedly hitting "Submit" multiple times to check answers with a problem that had less than 10 Points in it.  Even with no load, the server takes over a second to grade the question and spit back the results.)

Thanks,
Duane

Aaron Meurer

unread,
Dec 6, 2014, 5:38:24 PM12/6/14
to sy...@googlegroups.com
This is an issue in a lot of places in SymPy (see
https://github.com/sympy/sympy/issues/6751). The solution is to always
reference the class indirectly via self, and make use of super(),
isinstance(), and issubclass() (the exception is that the class does
have to be named explicitly in the arguments to super()). You can use
self.__class__ or type(self). I'm not sure if one is better than the
other. If you see issues with this, please do submit pull requests
fixing them. We do want SymPy to be usable as a library, and an
important part of that is making classes subclassable.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/fbc166f3-0dd7-4111-9bd3-bbe9826460ce%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Duane Nykamp

unread,
Dec 6, 2014, 6:40:17 PM12/6/14
to sy...@googlegroups.com
Well, upon closer examination, I find many uses of Point in point.py that I don't know how to fix.  I think my Python knowledge isn't quite there yet.

It appears many of the functions are intended be called off the class rather than an instance, such as Point.is_collinear(p1, p2, p3, p4)

    def is_collinear(*points):
        .....
        points = [Point(a) for a in points]

and, I'm not sure what to replace Point with in these cases.

Duane

Aaron Meurer

unread,
Dec 6, 2014, 9:54:52 PM12/6/14
to sy...@googlegroups.com
This really should be a class method:

@classmethod
def is_collinear(cls, *points)
points = [cls(a) for a in points]

Note that this breaks backwards compatibility with the method being
called on an instance. Previously it would work, because the instance
would be the first element of points (what is usually the argument
self).

I think to maintain this behavior, or even raise a deprecation
warning, you'd need a custom descriptor. Or override it in __init__.
See http://stackoverflow.com/a/861109/161801.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/d1a69ef6-a64a-415c-a4c2-ae262491c621%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages