sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.33k stars 453 forks source link

Add symplectic structures #30362

Closed tobiasdiez closed 2 years ago

tobiasdiez commented 4 years ago

This ticket implements the basics of symplectic structures, like Poisson brackets and Hamiltonian vector fields.

TODO (as follow-up tickets):

CC: @tscrim @nthiery @mjungmath @egourgoulhon @mkoeppe

Component: manifolds

Author: Tobias Diez

Branch: b78d8a2

Reviewer: Eric Gourgoulhon, Michael Jung, Matthias Koeppe, Travis Scrimshaw

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

mjungmath commented 3 years ago
comment:38

Replying to @tobiasdiez:

Since the PoissonTensor class is relatively short...

Just underlines my argument. ;)

Short code is much easier to review. Besides, since all the symplectic part relies on the Poisson structure, it's good to review that one first.

The sage.all imports are needed due to half-finished module imports otherwise. I plan to address this in forthcoming tickets.

What do you mean?

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 63d3ae6 to d968468

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

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

d968468A bit more code cleanup
mkoeppe commented 3 years ago

Changed dependencies from #31003 to #31003, #30551

mkoeppe commented 3 years ago
comment:40

Added #30551 dependency because of future.annotations

mjungmath commented 3 years ago
comment:41

What's the story behind the ..._test.py files? Is it related to #30738?

mjungmath commented 3 years ago
comment:42

I'd say we should drop these features such as typing, pytest etc. in this ticket for now, otherwise this ticket will stay on hold as long as #30738, #30551, #31003, #29775 etc. and all their follow ups have not been settled.

Taking a look at the discussions, that could take a while, whereas this ticket seems in principle to be ready for review.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from d968468 to 1c2cd6d

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

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

1c2cd6dAdd more examples
tobiasdiez commented 3 years ago
comment:44

Replying to @mjungmath:

The sage.all imports are needed due to half-finished module imports otherwise. I plan to address this in forthcoming tickets.

What do you mean?

Without these imports I get the following error:

ImportError while importing test module '/mnt/d/Programming/sage/src/sage/manifolds/differentiable/examples/symplectic_vector_space_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
sage/manifolds/differentiable/examples/symplectic_vector_space_test.py:2: in <module>
    from sage.manifolds.differentiable.symplectic_form import SymplecticForm
sage/manifolds/differentiable/symplectic_form.py:34: in <module>
    from sage.symbolic.expression import Expression
sage/symbolic/expression.pyx:310: in init sage.symbolic.expression
    from .ring import SR
sage/symbolic/ring.pyx:1: in init sage.symbolic.ring
    # -*- coding: utf-8 -*-
sage/rings/integer.pyx:1: in init sage.rings.integer
    r"""
sage/rings/rational.pyx:95: in init sage.rings.rational
    import sage.rings.real_mpfr
sage/rings/real_mpfr.pyx:1: in init sage.rings.real_mpfr
    r"""
sage/libs/mpmath/utils.pyx:1: in init sage.libs.mpmath.utils
    """
sage/rings/complex_mpfr.pyx:1: in init sage.rings.complex_mpfr
    """
sage/rings/complex_double.pyx:98: in init sage.rings.complex_double
    from . import complex_mpfr
E   ImportError: cannot import name complex_mpfr

I tried to make progress towards solving this in #30741, but I cannot really work on this until #30371 is merged.

tobiasdiez commented 3 years ago
comment:45

Replying to @mjungmath:

What's the story behind the ..._test.py files? Is it related to #30738?

It doesn't really rely on #30738, but it uses pytest as well, yes. I'm not a big fan of doctests (as unit tests) and was told that I can always extract them to test files if I prefer that.

tobiasdiez commented 3 years ago
comment:46

Replying to @mjungmath:

I'd say we should drop these features such as typing, pytest etc. in this ticket for now, otherwise this ticket will stay on hold as long as #30738, #30551, #31003, #29775 etc. and all their follow ups have not been settled.

The added typing doesn't have any dependencies (beyond #30551, which will be merged in 9.4). The pytests rely only on #31003, which is hopefully close to be merged. If you prefer, I can extract the pytests to a new ticket although it feels strange to separate the implementation from its tests.

mjungmath commented 3 years ago
comment:47

Technically, yes. However, I have the feeling that there are still too many open discussions about using pytest and typing and how to pull it on the current framework. I doubt this ticket will get a positive review until everything is clarified in a broader context like #29775 and #30738. See also comment:28 from Travis.

Anyway, I think that symplectic forms are a great and extremely useful addition to Sage! I am looking forward to have that feature acutally in there. Thanks! :)

mkoeppe commented 3 years ago
comment:48

Replying to @tobiasdiez:

I tried to make progress towards solving this in #30741, but I cannot really work on this until #30371 is merged.

It's best to keep workarounds for your experimental setup on your branches and not to push them to tickets. We cannot merge such code and it complicates the discussions in the review. I recommend to use git add -p and git cherry-pick to make clean branches.

tobiasdiez commented 3 years ago
comment:49

Replying to @mjungmath:

I have the feeling that there are still too many open discussions about using pytest and typing and how to pull it on the current framework.

I agree! Since it's also a lot of work to remove the typing information again, I would say we wait until the development cycle of 9.4 is started, which will resolve #30551 and thereby the other typing tickets. When the time comes, we can reevaluate to split of the pytests to a new ticket. I think this is a resonable time plan anyway, since this is my first bigger contribution to sage and I'm expecting quite some comments concerning the actual implementation, beyond pytest and typing.

tobiasdiez commented 3 years ago
comment:50

Replying to @mkoeppe:

It's best to keep workarounds for your experimental setup on your branches and not to push them to tickets. We cannot merge such code and it complicates the discussions in the review. I recommend to use git add -p and git cherry-pick to make clean branches.

Thanks, but the problem does not come from my editable setup. Pytest simply doesn't import sage.all before importing the test file, and without such an import other imports such as from sage.symbolic.expression import Expression do not work (because they implicitly rely on a certain import order that is provided by sage.all). The reference to #30371 was only because I cannot efficiently work on #30741 without recompiling cython files for which I need #30371.

mkoeppe commented 3 years ago
comment:51

Replying to @tobiasdiez:

the problem does not come from my editable setup. Pytest simply doesn't import sage.all before importing the test file, and without such an import other imports such as from sage.symbolic.expression import Expression do not work (because they implicitly rely on a certain import order that is provided by sage.all).

Thanks for the clarification. Would it be enough to do the import in the src/conftest.py file added by #31003?

tobiasdiez commented 3 years ago
comment:52

Replying to @mkoeppe:

Thanks for the clarification. Would it be enough to do the import in the src/conftest.py file added by #31003?

Sadly not. The fixture in conftest.py does load sage.all, but this is only used when the test are run and not on test discovery. Hence, this only prevents these problems on runtime, but not if the import of the test module already fails. Anyway, for the modularization effort is still desired that one can import and use any sage module without using sage.all before.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 1c2cd6d to 2087571

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

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

66c2987build/pkgs/{sage_sws2rst,sage_docbuild}/install-requires.txt: New
7f8ec6cbuild/pkgs/sagelib/src/tox.ini: Add factor sitepackages
0283da5build/make/Makefile.in: Add wheel, setuptools_wheel to PYTHON_TOOLCHAIN to make sure that PEP 517 packages have a complete build system
f720722build/pkgs/sagelib/src/tox.ini: Add factor nobuildisolation
c451b31src/setup.cfg.m4 (install_requires): Add sage_conf
6700223Merge tag '9.3.rc0' into t/30913/sagelib__add_setup_cfg__install_requires_
04da2c6build/pkgs/ipywidgets: Patch out declaring install-requires of nbformat and jupyterlab-widgets
815c944Merge #30913
b06731cRemove Python 3.6 support from metadata and documentation
2087571Add symplectic structures (Trac 30362 squashed)
mkoeppe commented 3 years ago
comment:54

Rebased (squashed) on top of updated #30551

mkoeppe commented 3 years ago

Changed dependencies from #31003, #30551 to #30551

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

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

14619d1Add symplectic structures (Trac 30362 squashed)
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 2087571 to 14619d1

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

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

daf5af8Add symplectic structures (Trac 30362 squashed)
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 14619d1 to daf5af8

mkoeppe commented 3 years ago
comment:57

Rebased, no longer on top of #30551

tobiasdiez commented 3 years ago

Changed dependencies from #30551 to none

tobiasdiez commented 3 years ago
comment:58

Thanks! From my side this is also ready for review!

mkoeppe commented 3 years ago
comment:59

The ticket still has to depend on #30551, just testing it you don't need the branch to be on top of it.

mkoeppe commented 3 years ago

Dependencies: #30551

mjungmath commented 3 years ago
comment:60

There are a lot of doctests missing. For example symplectic_form and poisson_tensor in differentiable/manifold.py.

tobiasdiez commented 3 years ago
comment:61

Thanks for the input. That was one point I was struggling with. The tests are there, just in a different file (e.g. differentiable/poisson_tensor.py) and the methods in manifold.py is just a convenient helper method. So I was wondering where to add tests, without just copying them all over the place.

mjungmath commented 3 years ago
comment:62

Well, it is not only about testing but also about documenting. Keep in mind that EXAMPLE blocks occur in the official reference manual and therefore provide a guideline for the user how to use that method adequately aside from testing.

Having the same examples in different places in the documentation isn't necessarily a bad thing. What I usually do is to change some values, dimensions or expressions instead of blindly copy.

mjungmath commented 3 years ago
comment:63

It is also a good idea to add a SEEALSO block in the poisson_tensor method linking to differentiable/poisson_tensor.py.

egourgoulhon commented 3 years ago
comment:64

Now that Sage 9.4 beta cycle has started, it would be nice to have this ticket make its way into Sage 9.4.

mkoeppe commented 3 years ago
comment:65

Setting a new milestone for this ticket based on a cursory review.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Changed commit from daf5af8 to c471d06

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

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

193bb88Merge branch 'develop' of https://github.com/sagemath/sage into public/manifolds/symplectic
c471d06Cleanup and add missing doctests
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Changed commit from c471d06 to 0903929

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

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

0903929Final cleanup
tobiasdiez commented 2 years ago
comment:68

Please excuse that it took me so long, but I've finally found the time to cleanup the code. The tests should be complete and pass now, except for the pytests for S2 which fail because of #32953. Other than this, this is ready for review.

tobiasdiez commented 2 years ago

Changed dependencies from #30551 to #32953

egourgoulhon commented 2 years ago
comment:69

Replying to @tobiasdiez:

Please excuse that it took me so long, but I've finally found the time to cleanup the code. The tests should be complete and pass now, except for the pytests for S2 which fail because of #32953. Other than this, this is ready for review.

Happy to see that symplectic structures make their way to Sage! I'll have a look in the coming days. Meanwhile, the patchbot reveals that the doctest coverage is not complete. You can check it by yourself by running sage -coverage src/sage/manifolds.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

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

16a08afAdd and fix doctests
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Changed commit from 0903929 to 16a08af

tobiasdiez commented 2 years ago
comment:71

Thanks, I've overlooked that some doctests were missing. Now everything should be in order, except for a few 'false-positves' related to the usage of pytest. For this, I've opened #32975.

tobiasdiez commented 2 years ago

Changed dependencies from #32953 to #32953, #32975

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Changed commit from 16a08af to fbc1af8

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

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

fbc1af8Fix S2 tests