-
-
Notifications
You must be signed in to change notification settings - Fork 526
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
Arithmetic on tensor module elements, manifold objects: Always Return a Copy #30302
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
comment:3
Both The arithmetic operations should either always create an immutable element or always create a new mutable element. It would be very inconvenient if the result of an operation was sometimes immutable, sometimes a new mutable element, depending on the input. (And it would be highly problematic if sometimes it would return an existing mutable element.) |
comment:4
My concern here would be performance. For complicated symbolic expressions, creating a copy can have a significant CPU cost. The main concern is about scalar fields, because tensor fields arithmetics always end up to scalar fields arithmetics, the scalar fields being the tensor components in a given frame. For the time being,
If we change this to return a copy (I understand the arguments put forward by Matthias), then I would advocate for extensive benchmarks, with complicated tensor fields (those in the doctests are too simple), such as in those mentionned here. |
comment:5
Symbolic expressions are immutable and therefore do not need to be copied. The cost of making a copy should be dominated by copying the dictionaries in the Components objects. |
comment:6
If making a copy is so costly, we should try to optimize it. If I remember correctly, until Sage 9 or so, all elements emerged from arithmetics were created from scratch, even the trivial ones. Even if copying would slow the current code down a bit, it should be still faster than versions before Sage 9. |
comment:7
Replying to @mjungmath:
Copying a symbolic expression with hundreds of terms will always remain slower than returning
That's not true: already in Sage 7.4, we have the code snippet shown in comment:4. |
comment:8
Replying to @mjungmath:
My memory was partially wrong. For scalar fields, the Furtermore, I've compared some computation times: Current state:
Returning a copy:
That's a heavy loss of comutational time. |
comment:9
Replying to @egourgoulhon:
Yes sorry, I was mistaken. See above. |
comment:10
Replying to @mjungmath:
... could you share the changes that you made for benchmarking this? |
comment:11
Replying to @mkoeppe:
You are right. Actually they are not copied when copying chart functions (and hence would not be copied when copying scalar fields): the code of
Here if |
Commit: |
comment:14
I've pushed my changes. |
comment:15
Here's the output of
with my pushed changes. |
comment:16
resu = type(self)(self.parent())
- for kk, vv in self._express.items():
- resu._express[kk] = vv
+ resu._express = self._express.copy()
resu._expansion_symbol = self._expansion_symbol is slightly faster btw:
And changing back to
|
comment:17
However, regarding my prun test, I think that resu = type(self)(self.parent()) is the costly line. And that one shouldn't be an issue for more complicated expressions. |
comment:18
Replying to @mkoeppe:
Something like this https://pypi.org/project/cowdict/ ? |
comment:19
I wonder, is copying the chart function even necessary? If new expressions are set/added, a new chart function is created anyway,isn't it? If the user wants the chart function directly, one can create a copy there. Or we state all chart functions belonging to scalar fields automatically as immutable (available since #30310). If the user wants to modify it, he must copy it manually. |
comment:20
Replying to @mjungmath:
Yes, same idea but with a different granularity. Add a flag to |
comment:21
Replying to @mkoeppe:
That sounds like something we should attack. However, the ingredient of scalar fields is Or, alternatively, we apply the very same idea you proposed for Edit:
I just overread this tiny but important part. What about simply making those |
comment:22
Replying to @mjungmath:
I think they're lower-hanging fruit for improving efficiency. For example, #30307 could make copying components faster.
Sorry, I'm not up to speed on chart functions yet. |
comment:24
Watch my edit. Sorry for the chaos. |
comment:25
Another useful thing for benchmarking is |
comment:27
Setting new milestone based on a cursory review of ticket status, priority, and last modification date. |
This question arose from ticket #30239, comment:36.
Should
FiniteRankFreeModule
and manifold objects always return a mutable copy, even for trivial operations? At least, this would be a consistent behavior.As pointed out by Matthias, this already holds true for
FreeModule
:I feel quite torn about this, but slightly tend to the copy-version.
Addendum:
For
FreeModule
, we also have the following behavior:I don't think that a parent should do that, especially when it already has a
zero
method. Should that be changed?CC: @egourgoulhon @tscrim @mkoeppe
Component: misc
Branch/Commit: u/gh-mjungmath/return_copy_always_scalarfields @
b7b0b3f
Issue created by migration from https://trac.sagemath.org/ticket/30302
The text was updated successfully, but these errors were encountered: