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

TensorField.__call__ alters zero #30239

Open
mjungmath opened this issue Jul 28, 2020 · 58 comments
Open

TensorField.__call__ alters zero #30239

mjungmath opened this issue Jul 28, 2020 · 58 comments

Comments

@mjungmath
Copy link

sage: M = Manifold(3, 'M', start_index=1)
sage: X.<x,y,z> = M.chart()
sage: omega = M.diff_form(1, 'omega')
sage: omega[:] = [0, 0, 0]
sage: omega(X.frame()[1])
Scalar field omega(d/dx) on the 3-dimensional differentiable manifold M
sage: M.zero_scalar_field()
Scalar field omega(d/dx) on the 3-dimensional differentiable manifold M

Depends on #30266
Depends on #30291

CC: @egourgoulhon @tscrim @mkoeppe

Component: manifolds

Author: Michael Jung

Branch/Commit: u/gh-mjungmath/tensorfield___call___alters_zero @ 9f63351

Reviewer: Eric Gourgoulhon

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

@mjungmath mjungmath added this to the sage-9.2 milestone Jul 28, 2020
@mjungmath
Copy link
Author

@mjungmath
Copy link
Author

Commit: 51ba4e3

@mjungmath
Copy link
Author

New commits:

51ba4e3Trac #30239: comp zero check for __call__

@mkoeppe
Copy link
Member

mkoeppe commented Jul 28, 2020

Author: Michael Jung

@egourgoulhon
Copy link
Member

comment:4

Good catch!

However the proposed fix

            if omega == 0 or vv == 0:
                return self.base_ring().zero()

is not acceptable, because the comparison with 0 can be very slow for complicated expressions.
Also, this does not fix the root of the bug, which results from the += operator acting on the zero element. It should be fixed per se, because it can lead to other errors.
FWIW, this bug was introduced in Sage 9.0 (everything is OK in earlier versions of Sage).

@egourgoulhon
Copy link
Member

Changed author from Michael Jung to none

@egourgoulhon
Copy link
Member

comment:5

Sorry the author name was deleted by mistake.

@egourgoulhon
Copy link
Member

Author: Michael Jung

@egourgoulhon

This comment has been minimized.

@mjungmath
Copy link
Author

comment:7

Replying to @egourgoulhon:

Good catch!

However the proposed fix

            if omega == 0 or vv == 0:
                return self.base_ring().zero()

is not acceptable, because the comparison with 0 can be very slow for complicated expressions.

I was afraid that you say that. :D
I haven't found a fast check for components. Is there any?

Also, this does not fix the root of the bug, which results from the += operator acting on the zero element. It should be fixed per se, because it can lead to other errors.
FWIW, this bug was introduced in Sage 9.0 (everything is OK in earlier versions of Sage).

I suspect the root is #28562. However, the operator += should not be a problem here. When two elements are added from which one is the zero element, the other element is returned. Due to ticket #28562, altering the components of the zero element is not even possible and would result in an error. The only thing happening here is that the zero element is renamed.

@egourgoulhon
Copy link
Member

comment:8

Replying to @mjungmath:

I was afraid that you say that. :D
I haven't found a fast check for components. Is there any?

No, is_trivial_zero is not implemented there.

Also, this does not fix the root of the bug, which results from the += operator acting on the zero element. It should be fixed per se, because it can lead to other errors.
FWIW, this bug was introduced in Sage 9.0 (everything is OK in earlier versions of Sage).

I suspect the root is #28562. However, the operator += should not be a problem here. When two elements are added from which one is the zero element, the other element is returned. Due to ticket #28562, altering the components of the zero element is not even possible and would result in an error. The only thing happening here is that the zero element is renamed.

You are right, += is not the problem here. A way to fix the issue is then to check whether the computed result is the zero of the base ring (I guess by something like resu is self._fmodule.base().zero()), before changing its name at the end of the __call__ method.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 30, 2020

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

78a50ebTrac #30329: is zero check

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 30, 2020

Changed commit from 51ba4e3 to 78a50eb

@mjungmath
Copy link
Author

comment:10

What about this?

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 30, 2020

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

3344505Trac #30239: one check for (0,1) tensors

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 30, 2020

Changed commit from 78a50eb to 3344505

@mkoeppe
Copy link
Member

mkoeppe commented Jul 30, 2020

comment:12

I think #30181 (Immutable elements of FreeModuleTensor) could provide a systematic solution

@egourgoulhon
Copy link
Member

Reviewer: Eric Gourgoulhon

@egourgoulhon
Copy link
Member

comment:13

Thanks for covering the case of one() as well. Seems OK to me. Waiting for the patchbot green light...

@egourgoulhon
Copy link
Member

comment:14

There is a doctest error in src/sage/manifolds/differentiable/chart.py and some pycodestyle error in src/sage/tensor/modules/free_module_tensor.py.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 5, 2020

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

abda8ecMerge branch 'develop' into t/30239/tensorfield___call___alters_zero
0638207Trac 30291: simple checks for trivial cases in `_mul_`, `_add_` and _div_

@mjungmath
Copy link
Author

Changed dependencies from #30266 to #30266, #30291

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 5, 2020

Changed commit from 0638207 to 5c14b39

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 5, 2020

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

2446f7eTrac 30291: simple checks for trivial cases in `_mul_`, `_add_` and _div_
4bd82e4Trac #30291: patchbot doctest fixed
b2ba1e8Merge branch 't/30291/scalar_field_arithmetics__trivial_cases' into t/30239/tensorfield___call___alters_zero
5c14b39Trac #30239: doctest adapted

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 5, 2020

Changed commit from 5c14b39 to 9f63351

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 5, 2020

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

2416d87ModuleElementWithMutability.is_immutable, is_mutable: Change def to cpdef
4f07938Trac #30266: make scalarfields immutable
3faa75aTrac #30266: hash function condition + treatment of restrictions
f812586Trac #30266: minor doctest improvement
02726e2Trac #30266: check by name
81e67cfTrac #30266: ValueError replaced by AssertionError
cf4fce5Trac #30239: use try-except block for immutable elements
4f3b69fTrac 30291: simple checks for trivial cases in `_mul_`, `_add_` and _div_
9d8720dMerge branch 't/30291/scalar_field_arithmetics__trivial_cases' into t/30239/tensorfield___call___alters_zero
9f63351Trac #30239: doctest outputs adapted

@mjungmath
Copy link
Author

comment:36

Changing the doctest outputs, especially in src/doc/en/thematic_tutorials/vector_calculus/vector_calc_advanced.rst left my aesthetic devil inside of me rebelling. A uniform output would be indeed nice.

If I remember correctly, our original argument for this behavior was that elements of modules/rings are usually treated immutable and that we should recover this behavior as far as possible. But thanks to Matthias with #30181, and the resulting changes in #30261, our elements are now undoubtfully viewed as mutable elements until they are stated immutable.

So, what about keeping the checks and always returning copies after each operation instead? This has also the great advantage that the results are again mutable (i.e. consistent behavior). Since we use fast checks that don't depend on particular elements now, this should also not affect the performance and would fit into the framework.

If we decide for the "return-copy-behavior", this ticket would become obsolete. Thus, I hope for a quick response so that I can work further on either one.

@mkoeppe
Copy link
Member

mkoeppe commented Aug 5, 2020

comment:37

FWIW, FreeModule returns a mutable copy even on trivial operations such as w = v + 0, so implementing this behavior would be compatible.

@mjungmath
Copy link
Author

comment:38

Replying to @mkoeppe:

FWIW, FreeModule returns a mutable copy even on trivial operations such as w = v + 0, so implementing this behavior would be compatible.

That's good to know. Thanks. Do you also have an opinion which behavior is more suitable?

@mjungmath
Copy link
Author

comment:39

I've opened another ticket #30302 for this discussion.

@mkoeppe mkoeppe modified the milestones: sage-9.2, sage-9.3 Oct 24, 2020
@mkoeppe
Copy link
Member

mkoeppe commented Apr 2, 2021

comment:41

Moving this ticket to 9.4, as it seems unlikely that it will be merged in 9.3, which is in the release candidate stage

@mkoeppe mkoeppe modified the milestones: sage-9.3, sage-9.4 Apr 2, 2021
@mkoeppe
Copy link
Member

mkoeppe commented Jul 19, 2021

comment:42

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

@mkoeppe mkoeppe modified the milestones: sage-9.4, sage-9.5 Jul 19, 2021
@mkoeppe
Copy link
Member

mkoeppe commented Dec 18, 2021

comment:43

Stalled in needs_review or needs_info; likely won't make it into Sage 9.5.

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