Sage: Ticket #29244: Apply a function to all components of a tensor field
https://trac.sagemath.org/ticket/29244
<p>
This ticket adds the method <code>apply_map</code> to the class <code>TensorField</code>, which applies a function to the coordinate expressions of all components of a tensor field in a given vector frame.
This allows operations like factorization, expansion, simplification or substitution to be performed on the components.
</p>
<p>
Such a method has been requested in e.g. <a class="ext-link" href="https://ask.sagemath.org/question/42107/basic-work-on-tensor-components/"><span class="icon"></span>https://ask.sagemath.org/question/42107/basic-work-on-tensor-components/</a> and was discussed again during <a class="ext-link" href="https://wiki.sagemath.org/days107"><span class="icon"></span>Sage Days 107</a>.
</p>
en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/29244
Trac 1.1.6egourgoulhonMon, 24 Feb 2020 11:08:39 GMTcommit, branch set
https://trac.sagemath.org/ticket/29244#comment:1
https://trac.sagemath.org/ticket/29244#comment:1
<ul>
<li><strong>commit</strong>
set to <em>14937a88e33fee39ec4a6873ee55808b2165ea17</em>
</li>
<li><strong>branch</strong>
set to <em>public/manifolds/map_tensor_components-29244</em>
</li>
</ul>
<p>
New commits:
</p>
<table class="wiki">
<tr><td><a class="ext-link" href="https://git.sagemath.org/sage.git/commit?id=14937a88e33fee39ec4a6873ee55808b2165ea17"><span class="icon"></span>14937a8</a></td><td><code>Add method map_components to class TensorField</code>
</td></tr></table>
TicketegourgoulhonMon, 24 Feb 2020 11:09:59 GMTstatus changed; cc set
https://trac.sagemath.org/ticket/29244#comment:2
https://trac.sagemath.org/ticket/29244#comment:2
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>needs_review</em>
</li>
<li><strong>cc</strong>
<em>tscrim</em> added
</li>
</ul>
TicketvdelecroixMon, 24 Feb 2020 12:10:42 GMTstatus changed
https://trac.sagemath.org/ticket/29244#comment:3
https://trac.sagemath.org/ticket/29244#comment:3
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>needs_info</em>
</li>
</ul>
<p>
For vectors and matrices, such method already exists and is called <code>apply_map</code>
</p>
<pre class="wiki">sage: v = vector(range(4))
sage: v.apply_map(lambda x: x^2)
(0, 1, 4, 9)
sage: m = matrix(3,range(9))
sage: m.apply_map(lambda x: x^2)
[ 0 1 4]
[ 9 16 25]
[36 49 64]
</pre><p>
Is the difference in terminology intended?
</p>
TicketegourgoulhonMon, 24 Feb 2020 12:44:42 GMT
https://trac.sagemath.org/ticket/29244#comment:4
https://trac.sagemath.org/ticket/29244#comment:4
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/29244#comment:3" title="Comment 3">vdelecroix</a>:
</p>
<blockquote class="citation">
<p>
For vectors and matrices, such method already exists and is called <code>apply_map</code>
</p>
<p>
Is the difference in terminology intended?
</p>
</blockquote>
<p>
No, not at all. Thanks for pointing out this.
In the context of tensor fields, <code>apply_map</code> may sound a little bit too vague (for instance, it may evoke a pull back operation associated with a map between manifolds). Would <code>apply_map_components</code> be more appropriate?
</p>
TicketvdelecroixMon, 24 Feb 2020 13:09:25 GMT
https://trac.sagemath.org/ticket/29244#comment:5
https://trac.sagemath.org/ticket/29244#comment:5
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/29244#comment:4" title="Comment 4">egourgoulhon</a>:
</p>
<blockquote class="citation">
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/29244#comment:3" title="Comment 3">vdelecroix</a>:
</p>
<blockquote class="citation">
<p>
For vectors and matrices, such method already exists and is called <code>apply_map</code>
</p>
<p>
Is the difference in terminology intended?
</p>
</blockquote>
<p>
No, not at all. Thanks for pointing out this.
In the context of tensor fields, <code>apply_map</code> may sound a little bit too vague (for instance, it may evoke a pull back operation associated with a map between manifolds). Would <code>apply_map_components</code> be more appropriate?
</p>
</blockquote>
<p>
The vagueness also applies to vectors/matrices where map can also refer to a morphism between vector spaces. I am not a big fan of the terminology but to my mind, consistency is the best option for the Sage interface as a whole. This consistency didcatorship includes as an option changing <code>apply_map</code> to something better.
</p>
TicketvdelecroixMon, 24 Feb 2020 13:11:42 GMT
https://trac.sagemath.org/ticket/29244#comment:6
https://trac.sagemath.org/ticket/29244#comment:6
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/29244#comment:5" title="Comment 5">vdelecroix</a>:
</p>
<blockquote class="citation">
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/29244#comment:4" title="Comment 4">egourgoulhon</a>:
</p>
<blockquote class="citation">
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/29244#comment:3" title="Comment 3">vdelecroix</a>:
</p>
<blockquote class="citation">
<p>
For vectors and matrices, such method already exists and is called <code>apply_map</code>
</p>
<p>
Is the difference in terminology intended?
</p>
</blockquote>
<p>
No, not at all. Thanks for pointing out this.
In the context of tensor fields, <code>apply_map</code> may sound a little bit too vague (for instance, it may evoke a pull back operation associated with a map between manifolds). Would <code>apply_map_components</code> be more appropriate?
</p>
</blockquote>
<p>
The vagueness also applies to vectors/matrices where map can also refer to a morphism between vector spaces. I am not a big fan of the terminology but to my mind, consistency is the best option for the Sage interface as a whole. This consistency didcatorship includes as an option changing <code>apply_map</code> to something better.
</p>
</blockquote>
<p>
<code>apply_map</code> is redundant. I would just have called it map because of the already existing Python function
</p>
<pre class="wiki">sage: list(map(lambda x: x^2, range(4)))
[0, 1, 4, 9]
</pre>
TickettscrimMon, 24 Feb 2020 23:07:51 GMT
https://trac.sagemath.org/ticket/29244#comment:7
https://trac.sagemath.org/ticket/29244#comment:7
<p>
I am -1 on calling the method <code>map</code> as I would first think it should return a <code>map</code> either from or based on the object or a map form of the object (such as for a matrix).
</p>
TicketegourgoulhonTue, 25 Feb 2020 16:15:45 GMT
https://trac.sagemath.org/ticket/29244#comment:8
https://trac.sagemath.org/ticket/29244#comment:8
<p>
Thank you Vincent and Travis for the discussion. I am pretty sensitive to the consistency argument put forward in <a class="ticket" href="https://trac.sagemath.org/ticket/29244#comment:5" title="Comment 5">comment:5</a>, so I would lean toward <code>apply_map</code>. Do we all agree?
</p>
TicketvdelecroixTue, 25 Feb 2020 16:29:10 GMT
https://trac.sagemath.org/ticket/29244#comment:9
https://trac.sagemath.org/ticket/29244#comment:9
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/29244#comment:8" title="Comment 8">egourgoulhon</a>:
</p>
<blockquote class="citation">
<p>
Thank you Vincent and Travis for the discussion. I am pretty sensitive to the consistency argument put forward in <a class="ticket" href="https://trac.sagemath.org/ticket/29244#comment:5" title="Comment 5">comment:5</a>, so I would lean toward <code>apply_map</code>. Do we all agree?
</p>
</blockquote>
<p>
Dear Eric, that is of course ok for me!
</p>
Ticketgh-mwageringelTue, 25 Feb 2020 17:18:21 GMT
https://trac.sagemath.org/ticket/29244#comment:10
https://trac.sagemath.org/ticket/29244#comment:10
<p>
I just want to mention that matrices also have a method <code>map_coefficients</code> which is very similar, coming from modules with basis. It is supposed to work with an endofunction on the coefficient ring, however it seems to be broken for matrices:
</p>
<pre class="wiki">sage: matrix.identity(2).map_coefficients(lambda x: x+1)
...
KeyError: 1
</pre><p>
As <code>apply_map</code> does not require an endofunction, it has a more general notion, though.
</p>
Ticketgh-PatrickMassotTue, 25 Feb 2020 17:37:46 GMT
https://trac.sagemath.org/ticket/29244#comment:11
https://trac.sagemath.org/ticket/29244#comment:11
<p>
I'm the one who asked for this function, so I'm very happy to see this patch. But I'm uneasy about the way it can be used to introduce inconsistencies between frames. Was it already easy to abuse <a class="missing wiki">SageManifolds?</a> like this before?
</p>
<p>
At least I would phrase that as a warning in the docstrings, instead of making it sound like a feature. I mean, it can be a feature where the function being mapped is non-destructive like factor, but substituting a parameter in some frame but not the other is clearly evil.
</p>
TicketegourgoulhonTue, 25 Feb 2020 21:05:26 GMT
https://trac.sagemath.org/ticket/29244#comment:12
https://trac.sagemath.org/ticket/29244#comment:12
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/29244#comment:11" title="Comment 11">gh-PatrickMassot</a>:
</p>
<blockquote class="citation">
<p>
I'm the one who asked for this function, so I'm very happy to see this patch. But I'm uneasy about the way it can be used to introduce inconsistencies between frames. Was it already easy to abuse <a class="missing wiki">SageManifolds?</a> like this before?
</p>
<p>
At least I would phrase that as a warning in the docstrings, instead of making it sound like a feature. I mean, it can be a feature where the function being mapped is non-destructive like factor, but substituting a parameter in some frame but not the other is clearly evil.
</p>
</blockquote>
<p>
That's a good point. It should be easy to add a keyword argument such as <code>keep_other_components=False</code> or <code>non-destructive=False</code> to enforce the consistency between the sets of components in various frames. The components in frames different from the requested one will then be deleted. When requested, they can be recomputed via change-of-frame formulas. However, this can be time consuming and, in some cases, may generate a loss of information due to some lack of simplifications.
</p>
TicketgitWed, 26 Feb 2020 21:55:54 GMTcommit changed
https://trac.sagemath.org/ticket/29244#comment:13
https://trac.sagemath.org/ticket/29244#comment:13
<ul>
<li><strong>commit</strong>
changed from <em>14937a88e33fee39ec4a6873ee55808b2165ea17</em> to <em>5cf7fc53f523cb3a1a7c8c75df8b2f0774203df1</em>
</li>
</ul>
<p>
Branch pushed to git repo; I updated commit sha1. New commits:
</p>
<table class="wiki">
<tr><td><a class="ext-link" href="https://git.sagemath.org/sage.git/commit/?id=5cf7fc53f523cb3a1a7c8c75df8b2f0774203df1"><span class="icon"></span>5cf7fc5</a></td><td><code>#29244: rename map_components to apply_map and add keyword argument keep_other_components</code>
</td></tr></table>
TicketegourgoulhonWed, 26 Feb 2020 22:02:09 GMT
https://trac.sagemath.org/ticket/29244#comment:14
https://trac.sagemath.org/ticket/29244#comment:14
<p>
In the above commit, the method has been renamed <code>apply_map</code> (cf. <a class="ticket" href="https://trac.sagemath.org/ticket/29244#comment:9" title="Comment 9">comment:9</a>) and the consistency issue pointed out in <a class="ticket" href="https://trac.sagemath.org/ticket/29244#comment:11" title="Comment 11">comment:11</a> has been delt with by adding the keyword argument <code>keep_other_components</code>. Its default value (<code>False</code>) enforces the consistency among the components in various vector frames.
</p>
TicketegourgoulhonWed, 26 Feb 2020 22:02:22 GMTstatus changed
https://trac.sagemath.org/ticket/29244#comment:15
https://trac.sagemath.org/ticket/29244#comment:15
<ul>
<li><strong>status</strong>
changed from <em>needs_info</em> to <em>needs_review</em>
</li>
</ul>
TicketegourgoulhonWed, 26 Feb 2020 22:04:31 GMTdescription changed
https://trac.sagemath.org/ticket/29244#comment:16
https://trac.sagemath.org/ticket/29244#comment:16
<ul>
<li><strong>description</strong>
modified (<a href="/ticket/29244?action=diff&version=16">diff</a>)
</li>
</ul>
TicketegourgoulhonMon, 09 Mar 2020 10:53:50 GMT
https://trac.sagemath.org/ticket/29244#comment:17
https://trac.sagemath.org/ticket/29244#comment:17
<p>
Anybody to perform the final review? If we want this in 9.1, this could be the good time...
</p>
TickettscrimMon, 09 Mar 2020 12:19:58 GMTstatus changed; reviewer set
https://trac.sagemath.org/ticket/29244#comment:18
https://trac.sagemath.org/ticket/29244#comment:18
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>positive_review</em>
</li>
<li><strong>reviewer</strong>
set to <em>Vincent Delecroix, Travis Scrimshaw</em>
</li>
</ul>
<p>
I will give this the green light. I added Vincent as a reviewer considering the name change was based off his suggestion. If anyone else feels like they want to be a reviewer, please add your name.
</p>
TicketegourgoulhonMon, 09 Mar 2020 13:25:08 GMT
https://trac.sagemath.org/ticket/29244#comment:19
https://trac.sagemath.org/ticket/29244#comment:19
<p>
Thank you Travis!
</p>
TicketvbraunWed, 11 Mar 2020 23:46:20 GMTstatus, branch changed; resolution set
https://trac.sagemath.org/ticket/29244#comment:20
https://trac.sagemath.org/ticket/29244#comment:20
<ul>
<li><strong>status</strong>
changed from <em>positive_review</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>fixed</em>
</li>
<li><strong>branch</strong>
changed from <em>public/manifolds/map_tensor_components-29244</em> to <em>5cf7fc53f523cb3a1a7c8c75df8b2f0774203df1</em>
</li>
</ul>
Ticket