Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move coercion to Element #20767

Closed
jdemeyer opened this issue Jun 3, 2016 · 80 comments
Closed

Move coercion to Element #20767

jdemeyer opened this issue Jun 3, 2016 · 80 comments

Comments

@jdemeyer
Copy link

jdemeyer commented Jun 3, 2016

This ticket has 2 main changes:

  1. Move all coercion logic from RingElement, ModuleElement and the like to Element.

Because of this, it matters a lot less whether a class inherits from Element or from ModuleElement/RingElement/FieldElement...

One difference remains: the more specialized classes have some default implementations for arithmetic. For example, ModuleElement implements unary negation as multiplication by -1. The base class Element has no such default implementations.

This patch also affects lookup in categories: with this patch, double-underscore methods like __add__ are never taken from the category. The Element classes take precedence over the category, so the default implementations of arithmetic operations will override whatever is in the category (this is existing behaviour, not affected by this patch). For the base class Element, this is not an issue since there are no default implementations.

  1. Change the implementation of double-underscore methods like __add__ to return NotImplemented (rather than raise an error) if one argument is not a Sage Element and coercion fails.

This will cause Python to try the reversed operation (__radd__ or __add__ in Cython). This way, Sage has improved support for operations with non-Sage types.

Depends on #21126
Depends on #20686
Depends on #21139
Depends on #21140
Depends on #21152
Depends on #21153
Depends on #21154
Depends on #21441

CC: @nthiery @videlec @simon-king-jena

Component: coercion

Author: Jeroen Demeyer

Branch/Commit: 25c9d7d

Reviewer: Nicolas M. Thiéry

Issue created by migration from https://trac.sagemath.org/ticket/20767

@jdemeyer jdemeyer added this to the sage-7.3 milestone Jun 3, 2016
@jdemeyer

This comment has been minimized.

@jdemeyer jdemeyer changed the title Move coercion down to Element Move coercion to Element Jun 3, 2016
@jdemeyer
Copy link
Author

Changed dependencies from #269, #20740, #20753, #20761, #20757 to #269, #20740, #20753, #20761, #20757, #20686

@jdemeyer
Copy link
Author

Changed dependencies from #269, #20740, #20753, #20761, #20757, #20686 to #269, #20740, #20753, #20761, #20757, #20686, #20836

@jdemeyer
Copy link
Author

Changed dependencies from #269, #20740, #20753, #20761, #20757, #20686, #20836 to #269, #20740, #20753, #20761, #20757, #20686, #20836, #20852

@jdemeyer
Copy link
Author

Branch: u/jdemeyer/move_coercion_to_element

@jdemeyer
Copy link
Author

New commits:

9a1ff6eImprove getattr_from_other_class
744ffa6Test that static methods work
f5361d9Improve _sage_src_lines_()
8079508Implement `__mod__` in the coercion model
b7856d5Merge commit 'f5361d972a61dcb60a76425264baa9e6d134e54b'; commit 'ccd5fcc16ac1708854a600cfc259c23d6c7568ff'; commit 'c41bc91addb41f593e4509d3fd6e9ce7c5789b39'; commit '80795088d0774413d5cbb94459d95b430e9fe2ad' into t/20767/move_coercion_to_element
dc84d88Remove comments about "private" attributes with two underscores
8a4f48dAbstract away category lookup in cdef method getattr_from_category
20c8675Merge remote-tracking branch 'origin/u/jdemeyer/optimize_method_lookup_from_the_categories_for_instances_of_cython_classes' into t/20767/move_coercion_to_element
ce70b30Move arithmetic using coercion model to Element

@jdemeyer
Copy link
Author

Commit: ce70b30

@jdemeyer
Copy link
Author

Changed dependencies from #269, #20740, #20753, #20761, #20757, #20686, #20836, #20852 to #269, #20686, #15947

@jdemeyer
Copy link
Author

Changed dependencies from #269, #20686, #15947 to #269, #20686

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 26, 2016

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

88d9757Comment that getattr_from_other_class is cached
7140ee3Prefer absolute imports
fc6487cMerge remote-tracking branch 'trac/u/jdemeyer/upgrade_to_cython_0_24_1' into t/20686/optimize_method_lookup_from_the_categories_for_instances_of_cython_classes
16d4743EvaluationMethods should be a new-style class
54ccd82Remove `__dict__` attribute from ElementWrapper
9a71db1Merge commit '54ccd828cb20b1254aeaf55bf7edab4a73f1032a'; tag '7.3.beta9' into t/20767/move_coercion_to_element
04d1ef5Move arithmetic using coercion model to Element

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 26, 2016

Changed commit from ce70b30 to 04d1ef5

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 26, 2016

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

ada2e74Move arithmetic using coercion model to Element

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 26, 2016

Changed commit from 04d1ef5 to ada2e74

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 27, 2016

Changed commit from ada2e74 to b9a3614

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 27, 2016

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

b9a3614Move arithmetic using coercion model to Element

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 29, 2016

Changed commit from b9a3614 to 9c4bdef

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 29, 2016

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

9c4bdefMove arithmetic using coercion model to Element

@jdemeyer
Copy link
Author

Changed dependencies from #269, #20686 to #21126, #20686

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 29, 2016

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

093395eAdd a Cython fix for cpdef methods
972533aMerge commit '54ccd828cb20b1254aeaf55bf7edab4a73f1032a' into t/20767/move_coercion_to_element
e4919e0Move arithmetic using coercion model to Element

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 29, 2016

Changed commit from 9c4bdef to e4919e0

@jdemeyer
Copy link
Author

jdemeyer commented Aug 1, 2016

Changed dependencies from #21126, #20686 to #21126, #20686, #21139

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 1, 2016

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

b72af0cImplement negation for modular forms
1570a11Move arithmetic using coercion model to Element

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 1, 2016

Changed commit from e4919e0 to 1570a11

@jdemeyer
Copy link
Author

jdemeyer commented Aug 1, 2016

Changed dependencies from #21126, #20686, #21139 to #21126, #20686, #21139, #21140

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 1, 2016

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

a0d5ec6Implement negation for modular forms
fbcda3bMerge branch 't/21139/implement_negation_for_modular_forms' into t/20767/move_coercion_to_element
69d18c7Remove redundant `_lmul_` and `_rmul_` methods
2f8e1c9Merge commit '69d18c75875c8a9f770e9ef493d6e721854f4448' into t/20767/move_coercion_to_element
950d4abMove arithmetic using coercion model to Element

@nthiery
Copy link
Contributor

nthiery commented Sep 7, 2016

@nthiery
Copy link
Contributor

nthiery commented Sep 7, 2016

Changed commit from 59a04e4 to 2d40948

@nthiery
Copy link
Contributor

nthiery commented Sep 7, 2016

comment:49

I read your new documentation; that's a very nice addition to the Sage documentation. I have done some changes here and there, and added a draft of explanation of the cdef _add_ trick. Please proofread. The _add_long and _mul_long protocols need to be documented and tested in element.pyx; see the TODO there.

Other than this, I believe this is good to go. Thanks a lot Jeroen!


New commits:

2d4094820767: Proofreading doc + doc of cdef `_add_` trick + TODO about _add_long / _mul_long

@jdemeyer
Copy link
Author

jdemeyer commented Sep 8, 2016

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 8, 2016

Changed commit from 2d40948 to 25c9d7d

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 8, 2016

Branch pushed to git repo; I updated commit sha1. New commits:

25c9d7dAdd tests for _add_long and _mul_long

@jdemeyer
Copy link
Author

jdemeyer commented Sep 8, 2016

comment:52

Done, needs review.

@nthiery
Copy link
Contributor

nthiery commented Sep 8, 2016

comment:53

Thanks; I checked your commits and am happy with them.

Two small questions:

  • The documentation currently seems to suggest that it's only possible to implement _add_long and _mul_long within a Cython class. Is that right? Of course, in a Python class it's probably rarely relevant to implement _add_long, but if this works there is no reason to hide it in the doc.

  • Concrete implementations (of `_add_`) in subclasses should be cpdefordef methods: do we need to be specific? cdef would actually work too, right? Or is it because with cdef, a Python level call to x._add_(y) would fail?

Once you have made up your mind on the above, you can set a positive review on my behalf (assuming of course all tests keep passing).

Thanks!

Cheers,
Nicolas

@jdemeyer
Copy link
Author

jdemeyer commented Sep 8, 2016

Reviewer: Nicolas M. Thiéry

@jdemeyer
Copy link
Author

jdemeyer commented Sep 8, 2016

comment:54

Replying to @nthiery:

  • The documentation currently seems to suggest that it's only possible to implement _add_long and _mul_long within a Cython class. Is that right?

Yes, it's a cdef method, so it's Cython-only. Since it is only an optimization, it makes little sense to allow it to be a Python method. If you care about performance, you should be using Cython anyway.

  • Concrete implementations (of `_add_`) in subclasses should be cpdefordef methods: do we need to be specific? cdef would actually work too, right?
    Or is it because with cdef, a Python level call to x._add_(y) would fail?

It would work for x + y yes. But I think it is expected that x._add_(y) also works. Anyway, that is how things were historically.

Maybe one could argue that x._add_(y) should not be required to work from Python. But let's not change that in this ticket.

@jdemeyer
Copy link
Author

jdemeyer commented Sep 9, 2016

Changed dependencies from #21126, #20686, #21139, #21140, #21152, #21153, #21154 to #21126, #20686, #21139, #21140, #21152, #21153, #21154, #21441

@jdemeyer
Copy link
Author

jdemeyer commented Sep 9, 2016

comment:55

This ticket is suffering also from #21441 (see patchbot)

@jdemeyer
Copy link
Author

jdemeyer commented Sep 9, 2016

comment:56

Replying to @nthiery:

  • Concrete implementations (of _add_) in subclasses should be cpdef or def methods``: do we need to be specific? cdef would actually work too, right? Or is it because with cdef, a Python level call to x._add_(y) would fail?

A more complete answer: yes, they really should be cpdef or def methods because you want to allow Python subclasses to override them. A cdef method is Cython-only and cannot be overridden by a Python method. Of course, Element._add_ is cdef but it "manually" checks for a Python _add_ method.

@nthiery
Copy link
Contributor

nthiery commented Sep 9, 2016

comment:57

Ah, yes, That's a good point indeed. Thanks!

@vbraun
Copy link
Member

vbraun commented Oct 3, 2016

Changed branch from u/jdemeyer/move_coercion_to_element to 25c9d7d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants