Skip to main content
Logo image

Section 3.6 Sage Examples

This section is focused on applying tools provided by Sage to the concepts learned in this chapter. In particular, Sage allows us to avoid tedious algebraic computations in proving general formulas involving the gradient, divergence and curl. The relevant Sage commands are as follows (assuming we are working with fields in \(\RR^3\)):
# declare variables
var('x, y, z')

# define an arbitrary scalar field f(x, y, z)
f = function('f')(x, y, z)

# define components of an arbitrary vector field F(x, y, z)
P = function('P')(x, y, z)
Q = function('Q')(x, y, z)
R = function('R')(x, y, z)

# define an arbitrary vector field
F(x, y, z) = (P, Q, R)

# compute gradient of f
f.gradient()

# compute div and curl of F
F(x, y, z).div([x, y, z])
F(x, y, z).curl([x, y, z])
Listing 3.6.1. Sage commands for scalar and vector fields in \(\RR^3\text{.}\)
You can also make use of the pretty_print() command to provide more readable output.

Example 3.6.2. Proving the Product Rule for Gradients.

Let \(f(x,y,z)\) and \(g(x, y, z)\) be differentiable scalar fields in \(\RR^3\text{.}\) Prove that \(\del(fg) = f(\del g) + (\del f) g\text{.}\)
Solution.
To prove this using Sage, you can complete the code cell below by filling in the variables lhs and rhs to represent the left hand and right hand sides of the equation
\begin{equation*} \del(fg) = f(\del g) + (\del f) g\text{.} \end{equation*}
The final line of code, lhs == rhs, then checks if the two quantities are equal by returning True or False.

Example 3.6.3. Computing a Laplacian.

The Laplacian of a scalar field \(f(x, y, z)\) is the scalar field \(\del^2 f\) where \(\del^2 = \div\del\text{.}\) In other words,
\begin{equation*} \del^2 f = \divt(\del f)\text{.} \end{equation*}
Use the Sage cell below to find formulas for both \(\del^2 f\) and \(\del^2 (fg)\text{.}\)

Example 3.6.4. Checking if a Vector Field is Conservative.

Recall that a differentiable vector field \(\FF\) is conservative if \(\curl\FF = \vb{0}\text{.}\) With this in mind, determine if the vector field given by
\begin{equation*} \FF = xyz\mqty[xy \amp yz \amp zx] \end{equation*}
is conservative using the Sage cell below.

Example 3.6.5. Computing a Vector Laplacian.

The vector Laplacian of a vector field \(\FF\) in \(\RR^3\) is the vector field \(\del^2\FF\) defined by
\begin{equation*} \del^2\FF = \del(\div\FF) - \curl(\curl\FF)\text{.} \end{equation*}
Let \(\FF = (x^2 + y^2 + z^2)^{-\frac{1}{2}}\mqty[x \amp y\amp z]\text{.}\) Find \(\del^2\FF\) using the Sage cell below.
Solution.
The vector Laplacian of \(\FF\) is
\begin{equation*} \del^2\FF = -2(x^2+y^2+z^2)^{-\frac{3}{2}}\mqty[x \amp y \amp z]\text{,} \end{equation*}
after some simplifying.