ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 20 Sep 2018 06:17:38 -0500div, grad and curl once againhttp://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/ HI, and sorry to badger people who are all working to give us a terrific maths tool for no cost, but there's a big need for div, grad and curl in many applications, such as electromagnetics, quantum theory, fluid flow, etc.
Specifically, my wish list would be, if s is a scalar field, and v a vector one,
grad (s) in cartesians, polars, cylindricals and sphericals
div (v) over the same coordinate systems
curl (v) over the same coordinate systems
and
grad(grad(s)) over these four systems, the spherical one being quite tricky anyway
Is there any cance of some kind person implementing (and documenting) these?
Fri, 26 Jan 2018 15:24:04 -0600http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/Answer by eric_g for <p>HI, and sorry to badger people who are all working to give us a terrific maths tool for no cost, but there's a big need for div, grad and curl in many applications, such as electromagnetics, quantum theory, fluid flow, etc.</p>
<p>Specifically, my wish list would be, if s is a scalar field, and v a vector one,</p>
<p>grad (s) in cartesians, polars, cylindricals and sphericals</p>
<p>div (v) over the same coordinate systems</p>
<p>curl (v) over the same coordinate systems</p>
<p>and</p>
<p>grad(grad(s)) over these four systems, the spherical one being quite tricky anyway</p>
<p>Is there any cance of some kind person implementing (and documenting) these? </p>
http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?answer=40803#post-id-40803As suggested in @tmonteil 's answer, all necessary material is in the manifolds part, but it needs to be made explicit for the end user. For instance, there should be a function `EuclideanSpace` so that
sage: E = EuclideanSpace(3)
creates the 3-dimensional Euclidean space as a smooth manifold endowed with a Riemannian flat metric. Then one could do things like
sage: v = E.vector_field([-x*z, x+z, y^2])
sage: c = v.curl()
sage: d = v.div()
sage: spher = E.spherical_frame() # orthonormal frame associated with spherical coordinates
sage: c.display(spher)
...
This was on my todo list for a while... @quantum_leopard 's question acts as an efficient reminder: I'm on it ;-)
I will report here (with a Trac ticket number) when the code is ready.
**EDIT (18 March 2018):** the code implementing vector calculus on Euclidean space is ready for review at Trac [#24623](https://trac.sagemath.org/ticket/24623).
**EDIT (10 May 2018):** the ticket [#24623](https://trac.sagemath.org/ticket/24623) has received a positive review and has been merged in SageMath 8.3.beta0. Alas, it was too late to be integrated in SageMath 8.2, which has been released 5 days ago. However, among the new features of SageMath 8.2, there are the grad, div, curl, etc. operators on a generic pseudo-Riemannian manifold (implemented through Trac [#24622](https://trac.sagemath.org/ticket/24622)). So if you upgrade to SageMath 8.2, you can declare an Euclidean space as a Riemannian manifold with a flat metric. It suffices to initialize the metric to the identity matrix in Cartesian coordinates:
sage: E = Manifold(3, 'E', structure='Riemannian')
sage: cartesian.<x,y,z> = E.chart()
sage: E.metric()[:] = identity_matrix(3)
Then, you can compute the curl of a vector field as follows:
sage: v = E.vector_field(name='v')
sage: v[:] = [-x*z, x+z, y^2]
sage: cv = v.curl()
sage: cv.display()
curl(v) = (2*y - 1) d/dx - x d/dy + d/dz
sage: cv[:]
[2*y - 1, -x, 1]
the divergence:
sage: dv = v.div()
sage: dv.display()
div(v): E --> R
(x, y, z) |--> -z
sage: dv.expr()
-z
the gradient:
sage: gdv = dv.gradient()
sage: gdv.display()
grad(div(v)) = -d/dz
sage: gdv[:]
[0, 0, -1]
the Laplacian:
sage: lv = v.laplacian()
sage: lv.display()
Delta(v) = 2 d/dz
sage: lv[:]
[0, 0, 2]
Instead of writing `v.div()`, you may use the notation `div(v)`:
sage: from sage.manifolds.operators import *
sage: div(v) == v.div()
True
Let us check a famous identity:
sage: curl(curl(v)) == grad(div(v)) - laplacian(v)
True
**EDIT (14 August 2018):** SageMath 8.3 is out and it contains all the tools for standard vector calculus, with various predefined coordinate systems (Cartesian, spherical, cylindrical). See these [example notebooks](https://sagemanifolds.obspm.fr/vector_calculus.html).
For instance, the first 3 lines of the "EDIT (10 May 2018)" block above can now be replaced by a single line:
sage: E.<x,y,z> = EuclideanSpace()Sat, 27 Jan 2018 07:19:40 -0600http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?answer=40803#post-id-40803Comment by mforets for <p>As suggested in <a href="/users/1305/tmonteil/">@tmonteil</a> 's answer, all necessary material is in the manifolds part, but it needs to be made explicit for the end user. For instance, there should be a function <code>EuclideanSpace</code> so that </p>
<pre><code>sage: E = EuclideanSpace(3)
</code></pre>
<p>creates the 3-dimensional Euclidean space as a smooth manifold endowed with a Riemannian flat metric. Then one could do things like</p>
<pre><code>sage: v = E.vector_field([-x*z, x+z, y^2])
sage: c = v.curl()
sage: d = v.div()
sage: spher = E.spherical_frame() # orthonormal frame associated with spherical coordinates
sage: c.display(spher)
...
</code></pre>
<p>This was on my todo list for a while... <a href="/users/24450/quantum_leopard/">@quantum_leopard</a> 's question acts as an efficient reminder: I'm on it ;-)
I will report here (with a Trac ticket number) when the code is ready. </p>
<p><strong>EDIT (18 March 2018):</strong> the code implementing vector calculus on Euclidean space is ready for review at Trac <a href="https://trac.sagemath.org/ticket/24623">#24623</a>.</p>
<p><strong>EDIT (10 May 2018):</strong> the ticket <a href="https://trac.sagemath.org/ticket/24623">#24623</a> has received a positive review and has been merged in SageMath 8.3.beta0. Alas, it was too late to be integrated in SageMath 8.2, which has been released 5 days ago. However, among the new features of SageMath 8.2, there are the grad, div, curl, etc. operators on a generic pseudo-Riemannian manifold (implemented through Trac <a href="https://trac.sagemath.org/ticket/24622">#24622</a>). So if you upgrade to SageMath 8.2, you can declare an Euclidean space as a Riemannian manifold with a flat metric. It suffices to initialize the metric to the identity matrix in Cartesian coordinates: </p>
<pre><code>sage: E = Manifold(3, 'E', structure='Riemannian')
sage: cartesian.<x,y,z> = E.chart()
sage: E.metric()[:] = identity_matrix(3)
</code></pre>
<p>Then, you can compute the curl of a vector field as follows:</p>
<pre><code>sage: v = E.vector_field(name='v')
sage: v[:] = [-x*z, x+z, y^2]
sage: cv = v.curl()
sage: cv.display()
curl(v) = (2*y - 1) d/dx - x d/dy + d/dz
sage: cv[:]
[2*y - 1, -x, 1]
</code></pre>
<p>the divergence:</p>
<pre><code>sage: dv = v.div()
sage: dv.display()
div(v): E --> R
(x, y, z) |--> -z
sage: dv.expr()
-z
</code></pre>
<p>the gradient:</p>
<pre><code>sage: gdv = dv.gradient()
sage: gdv.display()
grad(div(v)) = -d/dz
sage: gdv[:]
[0, 0, -1]
</code></pre>
<p>the Laplacian:</p>
<pre><code>sage: lv = v.laplacian()
sage: lv.display()
Delta(v) = 2 d/dz
sage: lv[:]
[0, 0, 2]
</code></pre>
<p>Instead of writing <code>v.div()</code>, you may use the notation <code>div(v)</code>:</p>
<pre><code>sage: from sage.manifolds.operators import *
sage: div(v) == v.div()
True
</code></pre>
<p>Let us check a famous identity:</p>
<pre><code>sage: curl(curl(v)) == grad(div(v)) - laplacian(v)
True
</code></pre>
<p><strong>EDIT (14 August 2018):</strong> SageMath 8.3 is out and it contains all the tools for standard vector calculus, with various predefined coordinate systems (Cartesian, spherical, cylindrical). See these <a href="https://sagemanifolds.obspm.fr/vector_calculus.html">example notebooks</a>.
For instance, the first 3 lines of the "EDIT (10 May 2018)" block above can now be replaced by a single line:</p>
<pre><code>sage: E.<x,y,z> = EuclideanSpace()
</code></pre>
http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?comment=43711#post-id-43711I couldn't resist to comment that the vector calculus example notebooks are delightful!Thu, 20 Sep 2018 06:17:38 -0500http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?comment=43711#post-id-43711Comment by eric_g for <p>As suggested in <a href="/users/1305/tmonteil/">@tmonteil</a> 's answer, all necessary material is in the manifolds part, but it needs to be made explicit for the end user. For instance, there should be a function <code>EuclideanSpace</code> so that </p>
<pre><code>sage: E = EuclideanSpace(3)
</code></pre>
<p>creates the 3-dimensional Euclidean space as a smooth manifold endowed with a Riemannian flat metric. Then one could do things like</p>
<pre><code>sage: v = E.vector_field([-x*z, x+z, y^2])
sage: c = v.curl()
sage: d = v.div()
sage: spher = E.spherical_frame() # orthonormal frame associated with spherical coordinates
sage: c.display(spher)
...
</code></pre>
<p>This was on my todo list for a while... <a href="/users/24450/quantum_leopard/">@quantum_leopard</a> 's question acts as an efficient reminder: I'm on it ;-)
I will report here (with a Trac ticket number) when the code is ready. </p>
<p><strong>EDIT (18 March 2018):</strong> the code implementing vector calculus on Euclidean space is ready for review at Trac <a href="https://trac.sagemath.org/ticket/24623">#24623</a>.</p>
<p><strong>EDIT (10 May 2018):</strong> the ticket <a href="https://trac.sagemath.org/ticket/24623">#24623</a> has received a positive review and has been merged in SageMath 8.3.beta0. Alas, it was too late to be integrated in SageMath 8.2, which has been released 5 days ago. However, among the new features of SageMath 8.2, there are the grad, div, curl, etc. operators on a generic pseudo-Riemannian manifold (implemented through Trac <a href="https://trac.sagemath.org/ticket/24622">#24622</a>). So if you upgrade to SageMath 8.2, you can declare an Euclidean space as a Riemannian manifold with a flat metric. It suffices to initialize the metric to the identity matrix in Cartesian coordinates: </p>
<pre><code>sage: E = Manifold(3, 'E', structure='Riemannian')
sage: cartesian.<x,y,z> = E.chart()
sage: E.metric()[:] = identity_matrix(3)
</code></pre>
<p>Then, you can compute the curl of a vector field as follows:</p>
<pre><code>sage: v = E.vector_field(name='v')
sage: v[:] = [-x*z, x+z, y^2]
sage: cv = v.curl()
sage: cv.display()
curl(v) = (2*y - 1) d/dx - x d/dy + d/dz
sage: cv[:]
[2*y - 1, -x, 1]
</code></pre>
<p>the divergence:</p>
<pre><code>sage: dv = v.div()
sage: dv.display()
div(v): E --> R
(x, y, z) |--> -z
sage: dv.expr()
-z
</code></pre>
<p>the gradient:</p>
<pre><code>sage: gdv = dv.gradient()
sage: gdv.display()
grad(div(v)) = -d/dz
sage: gdv[:]
[0, 0, -1]
</code></pre>
<p>the Laplacian:</p>
<pre><code>sage: lv = v.laplacian()
sage: lv.display()
Delta(v) = 2 d/dz
sage: lv[:]
[0, 0, 2]
</code></pre>
<p>Instead of writing <code>v.div()</code>, you may use the notation <code>div(v)</code>:</p>
<pre><code>sage: from sage.manifolds.operators import *
sage: div(v) == v.div()
True
</code></pre>
<p>Let us check a famous identity:</p>
<pre><code>sage: curl(curl(v)) == grad(div(v)) - laplacian(v)
True
</code></pre>
<p><strong>EDIT (14 August 2018):</strong> SageMath 8.3 is out and it contains all the tools for standard vector calculus, with various predefined coordinate systems (Cartesian, spherical, cylindrical). See these <a href="https://sagemanifolds.obspm.fr/vector_calculus.html">example notebooks</a>.
For instance, the first 3 lines of the "EDIT (10 May 2018)" block above can now be replaced by a single line:</p>
<pre><code>sage: E.<x,y,z> = EuclideanSpace()
</code></pre>
http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?comment=40893#post-id-40893This is now tickets [#24622](https://trac.sagemath.org/ticket/24622) and [#24623](https://trac.sagemath.org/ticket/24623). Things are split in two tickets for the ease of the reviewer(s). Note that the code is not ready yet (it should be within a few days).Wed, 31 Jan 2018 04:28:25 -0600http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?comment=40893#post-id-40893Comment by danielvolinski for <p>As suggested in <a href="/users/1305/tmonteil/">@tmonteil</a> 's answer, all necessary material is in the manifolds part, but it needs to be made explicit for the end user. For instance, there should be a function <code>EuclideanSpace</code> so that </p>
<pre><code>sage: E = EuclideanSpace(3)
</code></pre>
<p>creates the 3-dimensional Euclidean space as a smooth manifold endowed with a Riemannian flat metric. Then one could do things like</p>
<pre><code>sage: v = E.vector_field([-x*z, x+z, y^2])
sage: c = v.curl()
sage: d = v.div()
sage: spher = E.spherical_frame() # orthonormal frame associated with spherical coordinates
sage: c.display(spher)
...
</code></pre>
<p>This was on my todo list for a while... <a href="/users/24450/quantum_leopard/">@quantum_leopard</a> 's question acts as an efficient reminder: I'm on it ;-)
I will report here (with a Trac ticket number) when the code is ready. </p>
<p><strong>EDIT (18 March 2018):</strong> the code implementing vector calculus on Euclidean space is ready for review at Trac <a href="https://trac.sagemath.org/ticket/24623">#24623</a>.</p>
<p><strong>EDIT (10 May 2018):</strong> the ticket <a href="https://trac.sagemath.org/ticket/24623">#24623</a> has received a positive review and has been merged in SageMath 8.3.beta0. Alas, it was too late to be integrated in SageMath 8.2, which has been released 5 days ago. However, among the new features of SageMath 8.2, there are the grad, div, curl, etc. operators on a generic pseudo-Riemannian manifold (implemented through Trac <a href="https://trac.sagemath.org/ticket/24622">#24622</a>). So if you upgrade to SageMath 8.2, you can declare an Euclidean space as a Riemannian manifold with a flat metric. It suffices to initialize the metric to the identity matrix in Cartesian coordinates: </p>
<pre><code>sage: E = Manifold(3, 'E', structure='Riemannian')
sage: cartesian.<x,y,z> = E.chart()
sage: E.metric()[:] = identity_matrix(3)
</code></pre>
<p>Then, you can compute the curl of a vector field as follows:</p>
<pre><code>sage: v = E.vector_field(name='v')
sage: v[:] = [-x*z, x+z, y^2]
sage: cv = v.curl()
sage: cv.display()
curl(v) = (2*y - 1) d/dx - x d/dy + d/dz
sage: cv[:]
[2*y - 1, -x, 1]
</code></pre>
<p>the divergence:</p>
<pre><code>sage: dv = v.div()
sage: dv.display()
div(v): E --> R
(x, y, z) |--> -z
sage: dv.expr()
-z
</code></pre>
<p>the gradient:</p>
<pre><code>sage: gdv = dv.gradient()
sage: gdv.display()
grad(div(v)) = -d/dz
sage: gdv[:]
[0, 0, -1]
</code></pre>
<p>the Laplacian:</p>
<pre><code>sage: lv = v.laplacian()
sage: lv.display()
Delta(v) = 2 d/dz
sage: lv[:]
[0, 0, 2]
</code></pre>
<p>Instead of writing <code>v.div()</code>, you may use the notation <code>div(v)</code>:</p>
<pre><code>sage: from sage.manifolds.operators import *
sage: div(v) == v.div()
True
</code></pre>
<p>Let us check a famous identity:</p>
<pre><code>sage: curl(curl(v)) == grad(div(v)) - laplacian(v)
True
</code></pre>
<p><strong>EDIT (14 August 2018):</strong> SageMath 8.3 is out and it contains all the tools for standard vector calculus, with various predefined coordinate systems (Cartesian, spherical, cylindrical). See these <a href="https://sagemanifolds.obspm.fr/vector_calculus.html">example notebooks</a>.
For instance, the first 3 lines of the "EDIT (10 May 2018)" block above can now be replaced by a single line:</p>
<pre><code>sage: E.<x,y,z> = EuclideanSpace()
</code></pre>
http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?comment=41233#post-id-41233I would also suggest to add the command potential for vector fields, like v.potential() with an error if the vector is not conservative.
DanielThu, 22 Feb 2018 07:48:26 -0600http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?comment=41233#post-id-41233Comment by eric_g for <p>As suggested in <a href="/users/1305/tmonteil/">@tmonteil</a> 's answer, all necessary material is in the manifolds part, but it needs to be made explicit for the end user. For instance, there should be a function <code>EuclideanSpace</code> so that </p>
<pre><code>sage: E = EuclideanSpace(3)
</code></pre>
<p>creates the 3-dimensional Euclidean space as a smooth manifold endowed with a Riemannian flat metric. Then one could do things like</p>
<pre><code>sage: v = E.vector_field([-x*z, x+z, y^2])
sage: c = v.curl()
sage: d = v.div()
sage: spher = E.spherical_frame() # orthonormal frame associated with spherical coordinates
sage: c.display(spher)
...
</code></pre>
<p>This was on my todo list for a while... <a href="/users/24450/quantum_leopard/">@quantum_leopard</a> 's question acts as an efficient reminder: I'm on it ;-)
I will report here (with a Trac ticket number) when the code is ready. </p>
<p><strong>EDIT (18 March 2018):</strong> the code implementing vector calculus on Euclidean space is ready for review at Trac <a href="https://trac.sagemath.org/ticket/24623">#24623</a>.</p>
<p><strong>EDIT (10 May 2018):</strong> the ticket <a href="https://trac.sagemath.org/ticket/24623">#24623</a> has received a positive review and has been merged in SageMath 8.3.beta0. Alas, it was too late to be integrated in SageMath 8.2, which has been released 5 days ago. However, among the new features of SageMath 8.2, there are the grad, div, curl, etc. operators on a generic pseudo-Riemannian manifold (implemented through Trac <a href="https://trac.sagemath.org/ticket/24622">#24622</a>). So if you upgrade to SageMath 8.2, you can declare an Euclidean space as a Riemannian manifold with a flat metric. It suffices to initialize the metric to the identity matrix in Cartesian coordinates: </p>
<pre><code>sage: E = Manifold(3, 'E', structure='Riemannian')
sage: cartesian.<x,y,z> = E.chart()
sage: E.metric()[:] = identity_matrix(3)
</code></pre>
<p>Then, you can compute the curl of a vector field as follows:</p>
<pre><code>sage: v = E.vector_field(name='v')
sage: v[:] = [-x*z, x+z, y^2]
sage: cv = v.curl()
sage: cv.display()
curl(v) = (2*y - 1) d/dx - x d/dy + d/dz
sage: cv[:]
[2*y - 1, -x, 1]
</code></pre>
<p>the divergence:</p>
<pre><code>sage: dv = v.div()
sage: dv.display()
div(v): E --> R
(x, y, z) |--> -z
sage: dv.expr()
-z
</code></pre>
<p>the gradient:</p>
<pre><code>sage: gdv = dv.gradient()
sage: gdv.display()
grad(div(v)) = -d/dz
sage: gdv[:]
[0, 0, -1]
</code></pre>
<p>the Laplacian:</p>
<pre><code>sage: lv = v.laplacian()
sage: lv.display()
Delta(v) = 2 d/dz
sage: lv[:]
[0, 0, 2]
</code></pre>
<p>Instead of writing <code>v.div()</code>, you may use the notation <code>div(v)</code>:</p>
<pre><code>sage: from sage.manifolds.operators import *
sage: div(v) == v.div()
True
</code></pre>
<p>Let us check a famous identity:</p>
<pre><code>sage: curl(curl(v)) == grad(div(v)) - laplacian(v)
True
</code></pre>
<p><strong>EDIT (14 August 2018):</strong> SageMath 8.3 is out and it contains all the tools for standard vector calculus, with various predefined coordinate systems (Cartesian, spherical, cylindrical). See these <a href="https://sagemanifolds.obspm.fr/vector_calculus.html">example notebooks</a>.
For instance, the first 3 lines of the "EDIT (10 May 2018)" block above can now be replaced by a single line:</p>
<pre><code>sage: E.<x,y,z> = EuclideanSpace()
</code></pre>
http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?comment=41063#post-id-41063Here is some update: the first ticket, [#24622](https://trac.sagemath.org/ticket/24622), is ready for review. It already implements the operators `grad`, `div`, `curl`, `laplacian` and `dalembertian`, as you can see on this [demo Jupyter notebook](http://nbviewer.jupyter.org/github/egourgoulhon/SageMathTest/blob/master/Worksheets/vector_calculus.ipynb).Sun, 11 Feb 2018 11:15:02 -0600http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?comment=41063#post-id-41063Comment by quantum_leopard for <p>As suggested in <a href="/users/1305/tmonteil/">@tmonteil</a> 's answer, all necessary material is in the manifolds part, but it needs to be made explicit for the end user. For instance, there should be a function <code>EuclideanSpace</code> so that </p>
<pre><code>sage: E = EuclideanSpace(3)
</code></pre>
<p>creates the 3-dimensional Euclidean space as a smooth manifold endowed with a Riemannian flat metric. Then one could do things like</p>
<pre><code>sage: v = E.vector_field([-x*z, x+z, y^2])
sage: c = v.curl()
sage: d = v.div()
sage: spher = E.spherical_frame() # orthonormal frame associated with spherical coordinates
sage: c.display(spher)
...
</code></pre>
<p>This was on my todo list for a while... <a href="/users/24450/quantum_leopard/">@quantum_leopard</a> 's question acts as an efficient reminder: I'm on it ;-)
I will report here (with a Trac ticket number) when the code is ready. </p>
<p><strong>EDIT (18 March 2018):</strong> the code implementing vector calculus on Euclidean space is ready for review at Trac <a href="https://trac.sagemath.org/ticket/24623">#24623</a>.</p>
<p><strong>EDIT (10 May 2018):</strong> the ticket <a href="https://trac.sagemath.org/ticket/24623">#24623</a> has received a positive review and has been merged in SageMath 8.3.beta0. Alas, it was too late to be integrated in SageMath 8.2, which has been released 5 days ago. However, among the new features of SageMath 8.2, there are the grad, div, curl, etc. operators on a generic pseudo-Riemannian manifold (implemented through Trac <a href="https://trac.sagemath.org/ticket/24622">#24622</a>). So if you upgrade to SageMath 8.2, you can declare an Euclidean space as a Riemannian manifold with a flat metric. It suffices to initialize the metric to the identity matrix in Cartesian coordinates: </p>
<pre><code>sage: E = Manifold(3, 'E', structure='Riemannian')
sage: cartesian.<x,y,z> = E.chart()
sage: E.metric()[:] = identity_matrix(3)
</code></pre>
<p>Then, you can compute the curl of a vector field as follows:</p>
<pre><code>sage: v = E.vector_field(name='v')
sage: v[:] = [-x*z, x+z, y^2]
sage: cv = v.curl()
sage: cv.display()
curl(v) = (2*y - 1) d/dx - x d/dy + d/dz
sage: cv[:]
[2*y - 1, -x, 1]
</code></pre>
<p>the divergence:</p>
<pre><code>sage: dv = v.div()
sage: dv.display()
div(v): E --> R
(x, y, z) |--> -z
sage: dv.expr()
-z
</code></pre>
<p>the gradient:</p>
<pre><code>sage: gdv = dv.gradient()
sage: gdv.display()
grad(div(v)) = -d/dz
sage: gdv[:]
[0, 0, -1]
</code></pre>
<p>the Laplacian:</p>
<pre><code>sage: lv = v.laplacian()
sage: lv.display()
Delta(v) = 2 d/dz
sage: lv[:]
[0, 0, 2]
</code></pre>
<p>Instead of writing <code>v.div()</code>, you may use the notation <code>div(v)</code>:</p>
<pre><code>sage: from sage.manifolds.operators import *
sage: div(v) == v.div()
True
</code></pre>
<p>Let us check a famous identity:</p>
<pre><code>sage: curl(curl(v)) == grad(div(v)) - laplacian(v)
True
</code></pre>
<p><strong>EDIT (14 August 2018):</strong> SageMath 8.3 is out and it contains all the tools for standard vector calculus, with various predefined coordinate systems (Cartesian, spherical, cylindrical). See these <a href="https://sagemanifolds.obspm.fr/vector_calculus.html">example notebooks</a>.
For instance, the first 3 lines of the "EDIT (10 May 2018)" block above can now be replaced by a single line:</p>
<pre><code>sage: E.<x,y,z> = EuclideanSpace()
</code></pre>
http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?comment=40889#post-id-40889That would be great. Thank you!Tue, 30 Jan 2018 15:18:02 -0600http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?comment=40889#post-id-40889Comment by tmonteil for <p>As suggested in <a href="/users/1305/tmonteil/">@tmonteil</a> 's answer, all necessary material is in the manifolds part, but it needs to be made explicit for the end user. For instance, there should be a function <code>EuclideanSpace</code> so that </p>
<pre><code>sage: E = EuclideanSpace(3)
</code></pre>
<p>creates the 3-dimensional Euclidean space as a smooth manifold endowed with a Riemannian flat metric. Then one could do things like</p>
<pre><code>sage: v = E.vector_field([-x*z, x+z, y^2])
sage: c = v.curl()
sage: d = v.div()
sage: spher = E.spherical_frame() # orthonormal frame associated with spherical coordinates
sage: c.display(spher)
...
</code></pre>
<p>This was on my todo list for a while... <a href="/users/24450/quantum_leopard/">@quantum_leopard</a> 's question acts as an efficient reminder: I'm on it ;-)
I will report here (with a Trac ticket number) when the code is ready. </p>
<p><strong>EDIT (18 March 2018):</strong> the code implementing vector calculus on Euclidean space is ready for review at Trac <a href="https://trac.sagemath.org/ticket/24623">#24623</a>.</p>
<p><strong>EDIT (10 May 2018):</strong> the ticket <a href="https://trac.sagemath.org/ticket/24623">#24623</a> has received a positive review and has been merged in SageMath 8.3.beta0. Alas, it was too late to be integrated in SageMath 8.2, which has been released 5 days ago. However, among the new features of SageMath 8.2, there are the grad, div, curl, etc. operators on a generic pseudo-Riemannian manifold (implemented through Trac <a href="https://trac.sagemath.org/ticket/24622">#24622</a>). So if you upgrade to SageMath 8.2, you can declare an Euclidean space as a Riemannian manifold with a flat metric. It suffices to initialize the metric to the identity matrix in Cartesian coordinates: </p>
<pre><code>sage: E = Manifold(3, 'E', structure='Riemannian')
sage: cartesian.<x,y,z> = E.chart()
sage: E.metric()[:] = identity_matrix(3)
</code></pre>
<p>Then, you can compute the curl of a vector field as follows:</p>
<pre><code>sage: v = E.vector_field(name='v')
sage: v[:] = [-x*z, x+z, y^2]
sage: cv = v.curl()
sage: cv.display()
curl(v) = (2*y - 1) d/dx - x d/dy + d/dz
sage: cv[:]
[2*y - 1, -x, 1]
</code></pre>
<p>the divergence:</p>
<pre><code>sage: dv = v.div()
sage: dv.display()
div(v): E --> R
(x, y, z) |--> -z
sage: dv.expr()
-z
</code></pre>
<p>the gradient:</p>
<pre><code>sage: gdv = dv.gradient()
sage: gdv.display()
grad(div(v)) = -d/dz
sage: gdv[:]
[0, 0, -1]
</code></pre>
<p>the Laplacian:</p>
<pre><code>sage: lv = v.laplacian()
sage: lv.display()
Delta(v) = 2 d/dz
sage: lv[:]
[0, 0, 2]
</code></pre>
<p>Instead of writing <code>v.div()</code>, you may use the notation <code>div(v)</code>:</p>
<pre><code>sage: from sage.manifolds.operators import *
sage: div(v) == v.div()
True
</code></pre>
<p>Let us check a famous identity:</p>
<pre><code>sage: curl(curl(v)) == grad(div(v)) - laplacian(v)
True
</code></pre>
<p><strong>EDIT (14 August 2018):</strong> SageMath 8.3 is out and it contains all the tools for standard vector calculus, with various predefined coordinate systems (Cartesian, spherical, cylindrical). See these <a href="https://sagemanifolds.obspm.fr/vector_calculus.html">example notebooks</a>.
For instance, the first 3 lines of the "EDIT (10 May 2018)" block above can now be replaced by a single line:</p>
<pre><code>sage: E.<x,y,z> = EuclideanSpace()
</code></pre>
http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?comment=40830#post-id-40830Let me add a `confirmed_bug` tag to add some motivation in fixing that :PSun, 28 Jan 2018 15:28:00 -0600http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?comment=40830#post-id-40830Comment by tmonteil for <p>As suggested in <a href="/users/1305/tmonteil/">@tmonteil</a> 's answer, all necessary material is in the manifolds part, but it needs to be made explicit for the end user. For instance, there should be a function <code>EuclideanSpace</code> so that </p>
<pre><code>sage: E = EuclideanSpace(3)
</code></pre>
<p>creates the 3-dimensional Euclidean space as a smooth manifold endowed with a Riemannian flat metric. Then one could do things like</p>
<pre><code>sage: v = E.vector_field([-x*z, x+z, y^2])
sage: c = v.curl()
sage: d = v.div()
sage: spher = E.spherical_frame() # orthonormal frame associated with spherical coordinates
sage: c.display(spher)
...
</code></pre>
<p>This was on my todo list for a while... <a href="/users/24450/quantum_leopard/">@quantum_leopard</a> 's question acts as an efficient reminder: I'm on it ;-)
I will report here (with a Trac ticket number) when the code is ready. </p>
<p><strong>EDIT (18 March 2018):</strong> the code implementing vector calculus on Euclidean space is ready for review at Trac <a href="https://trac.sagemath.org/ticket/24623">#24623</a>.</p>
<p><strong>EDIT (10 May 2018):</strong> the ticket <a href="https://trac.sagemath.org/ticket/24623">#24623</a> has received a positive review and has been merged in SageMath 8.3.beta0. Alas, it was too late to be integrated in SageMath 8.2, which has been released 5 days ago. However, among the new features of SageMath 8.2, there are the grad, div, curl, etc. operators on a generic pseudo-Riemannian manifold (implemented through Trac <a href="https://trac.sagemath.org/ticket/24622">#24622</a>). So if you upgrade to SageMath 8.2, you can declare an Euclidean space as a Riemannian manifold with a flat metric. It suffices to initialize the metric to the identity matrix in Cartesian coordinates: </p>
<pre><code>sage: E = Manifold(3, 'E', structure='Riemannian')
sage: cartesian.<x,y,z> = E.chart()
sage: E.metric()[:] = identity_matrix(3)
</code></pre>
<p>Then, you can compute the curl of a vector field as follows:</p>
<pre><code>sage: v = E.vector_field(name='v')
sage: v[:] = [-x*z, x+z, y^2]
sage: cv = v.curl()
sage: cv.display()
curl(v) = (2*y - 1) d/dx - x d/dy + d/dz
sage: cv[:]
[2*y - 1, -x, 1]
</code></pre>
<p>the divergence:</p>
<pre><code>sage: dv = v.div()
sage: dv.display()
div(v): E --> R
(x, y, z) |--> -z
sage: dv.expr()
-z
</code></pre>
<p>the gradient:</p>
<pre><code>sage: gdv = dv.gradient()
sage: gdv.display()
grad(div(v)) = -d/dz
sage: gdv[:]
[0, 0, -1]
</code></pre>
<p>the Laplacian:</p>
<pre><code>sage: lv = v.laplacian()
sage: lv.display()
Delta(v) = 2 d/dz
sage: lv[:]
[0, 0, 2]
</code></pre>
<p>Instead of writing <code>v.div()</code>, you may use the notation <code>div(v)</code>:</p>
<pre><code>sage: from sage.manifolds.operators import *
sage: div(v) == v.div()
True
</code></pre>
<p>Let us check a famous identity:</p>
<pre><code>sage: curl(curl(v)) == grad(div(v)) - laplacian(v)
True
</code></pre>
<p><strong>EDIT (14 August 2018):</strong> SageMath 8.3 is out and it contains all the tools for standard vector calculus, with various predefined coordinate systems (Cartesian, spherical, cylindrical). See these <a href="https://sagemanifolds.obspm.fr/vector_calculus.html">example notebooks</a>.
For instance, the first 3 lines of the "EDIT (10 May 2018)" block above can now be replaced by a single line:</p>
<pre><code>sage: E.<x,y,z> = EuclideanSpace()
</code></pre>
http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?comment=40805#post-id-40805That would be awesome !Sat, 27 Jan 2018 08:22:11 -0600http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?comment=40805#post-id-40805Answer by kcrisman for <p>HI, and sorry to badger people who are all working to give us a terrific maths tool for no cost, but there's a big need for div, grad and curl in many applications, such as electromagnetics, quantum theory, fluid flow, etc.</p>
<p>Specifically, my wish list would be, if s is a scalar field, and v a vector one,</p>
<p>grad (s) in cartesians, polars, cylindricals and sphericals</p>
<p>div (v) over the same coordinate systems</p>
<p>curl (v) over the same coordinate systems</p>
<p>and</p>
<p>grad(grad(s)) over these four systems, the spherical one being quite tricky anyway</p>
<p>Is there any cance of some kind person implementing (and documenting) these? </p>
http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?answer=41232#post-id-41232See also [this ask.sagemath question](https://ask.sagemath.org/question/10104/gradient-divergence-curl-and-vector-products/) and [Trac 3021](https://trac.sagemath.org/ticket/3021), where this was implemented to some degree. I don't know whether the current implementation (e.g. [curl](http://doc.sagemath.org/html/en/reference/modules/sage/modules/free_module_element.html#sage.modules.free_module_element.FreeModuleElement.curl)) is what you need or not.Thu, 22 Feb 2018 06:55:48 -0600http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?answer=41232#post-id-41232Answer by tmonteil for <p>HI, and sorry to badger people who are all working to give us a terrific maths tool for no cost, but there's a big need for div, grad and curl in many applications, such as electromagnetics, quantum theory, fluid flow, etc.</p>
<p>Specifically, my wish list would be, if s is a scalar field, and v a vector one,</p>
<p>grad (s) in cartesians, polars, cylindricals and sphericals</p>
<p>div (v) over the same coordinate systems</p>
<p>curl (v) over the same coordinate systems</p>
<p>and</p>
<p>grad(grad(s)) over these four systems, the spherical one being quite tricky anyway</p>
<p>Is there any cance of some kind person implementing (and documenting) these? </p>
http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?answer=40797#post-id-40797I think all the material for this is available in the [manifolds](http://doc.sagemath.org/html/en/reference/manifolds/) module, in particular changes of coordinates, though the operators are not explicitely written. This is a very good idea to implement this, and include it in Sage. Do not hesitate to try that module and report how far you can go with that. If you want to turn a chance into a reality, to you might consider becoming the kind person implementing these ;)Sat, 27 Jan 2018 04:51:03 -0600http://ask.sagemath.org/question/40792/div-grad-and-curl-once-again/?answer=40797#post-id-40797