ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 29 Oct 2021 14:47:57 +0200Differentiate vector field w.r.t a variable not defining the charthttps://ask.sagemath.org/question/59533/differentiate-vector-field-wrt-a-variable-not-defining-the-chart/I define a 3+1-dimensional manifold **M**:
n = 3
M = Manifold(1+n, 'M', structure='Lorentzian')
and a chart **X** with spherical coordinates **t**, **r**, **th** and **ph**.
X.<t,r,th,ph> = M.chart('t r:(0,+oo) th:(0,pi) ph:(0,2*pi)')
I define the following symbolic function **Ne**, which depends on **r** and a new variable, **eps**:
eps = var('eps')
Ne = function('Ne')(r,eps)
Now, I define a vector field, **u**, as:
u = M.vector_field('u')
u[0] = 2*Ne; u[1] = Ne; u[2] = 0; u[3] = 0
At this point, I want to differentiate each term of **u** with respect to **eps**. However, when I write:
diff(u[0],eps)
I get:
> ValueError: tuple.index(x): x not in tuple
However, I would expect to obtain:
> 2*diff(Ne(r, eps), eps)
I guess this is because I am differentiating a vector field from the manifold with respect to a variable (eps) which is not on the chart. In fact, if I differentiate with respect to $r$, then everything goes right. Is there a way I can fix this?
Fri, 29 Oct 2021 14:18:25 +0200https://ask.sagemath.org/question/59533/differentiate-vector-field-wrt-a-variable-not-defining-the-chart/Answer by rburing for <p>I define a 3+1-dimensional manifold <strong>M</strong>:</p>
<pre><code>n = 3
M = Manifold(1+n, 'M', structure='Lorentzian')
</code></pre>
<p>and a chart <strong>X</strong> with spherical coordinates <strong>t</strong>, <strong>r</strong>, <strong>th</strong> and <strong>ph</strong>.</p>
<pre><code>X.<t,r,th,ph> = M.chart('t r:(0,+oo) th:(0,pi) ph:(0,2*pi)')
</code></pre>
<p>I define the following symbolic function <strong>Ne</strong>, which depends on <strong>r</strong> and a new variable, <strong>eps</strong>:</p>
<pre><code>eps = var('eps')
Ne = function('Ne')(r,eps)
</code></pre>
<p>Now, I define a vector field, <strong>u</strong>, as:</p>
<pre><code>u = M.vector_field('u')
u[0] = 2*Ne; u[1] = Ne; u[2] = 0; u[3] = 0
</code></pre>
<p>At this point, I want to differentiate each term of <strong>u</strong> with respect to <strong>eps</strong>. However, when I write:</p>
<pre><code>diff(u[0],eps)
</code></pre>
<p>I get: </p>
<blockquote>
<p>ValueError: tuple.index(x): x not in tuple</p>
</blockquote>
<p>However, I would expect to obtain:</p>
<blockquote>
<p>2*diff(Ne(r, eps), eps)</p>
</blockquote>
<p>I guess this is because I am differentiating a vector field from the manifold with respect to a variable (eps) which is not on the chart. In fact, if I differentiate with respect to $r$, then everything goes right. Is there a way I can fix this?</p>
https://ask.sagemath.org/question/59533/differentiate-vector-field-wrt-a-variable-not-defining-the-chart/?answer=59534#post-id-59534When you write `u[0]` you are getting a [chart function](https://doc.sagemath.org/html/en/reference/manifolds/sage/manifolds/chart_func.html) object. The global `diff` function calls the [derivative](https://doc.sagemath.org/html/en/reference/manifolds/sage/manifolds/chart_func.html#sage.manifolds.chart_func.ChartFunction.derivative) method on the object, and this one is only defined for coordinates of the chart.
What you can do is call the `expr` method on the chart function to get the underlying symbolic expression:
sage: diff(u[0].expr(), eps)
2*diff(Ne(r, eps), eps)Fri, 29 Oct 2021 14:47:57 +0200https://ask.sagemath.org/question/59533/differentiate-vector-field-wrt-a-variable-not-defining-the-chart/?answer=59534#post-id-59534