avatarMathcube

Summary

The web content provides a detailed guide on performing vector analysis, specifically calculating gradient, curl, and divergence using SymPy's sympy.vector subpackage and custom operators, with a focus on solving problems in computer algebra systems.

Abstract

The article titled "Vector Analysis with Sympy: Gradient, Curl, and Divergence" serves as a tutorial for readers looking to master computer algebra systems, particularly SymPy. It presents two methods for vector analysis: using the sympy.vector subpackage, which is convenient for 3D problems, and writing custom vector operators for problems in dimensions other than three. The article demonstrates how to define coordinate systems, symbols, and fields, and then applies the gradient, divergence, and curl operations to a given scalar and vector field. It also touches on the use of Levi-Civita symbols for simplifying the calculation of the curl. The author encourages readers to practice these techniques and suggests that mastering computer algebra systems requires regular engagement with such problems.

Opinions

  • The author suggests that using sympy.vector is the easiest way for 3D vector analysis but notes its limitation to three dimensions.
  • Writing custom vector operators is presented as a straightforward alternative when dealing with dimensions outside of 3D.
  • The article implies a preference for the custom operator approach, with the author stating they would personally choose this method.
  • The author emphasizes the importance of practice in becoming proficient with computer algebra systems like SymPy, Sage, or Mathematica.
  • A recommendation is made for readers to consider becoming Medium members to support the author and gain access to a broader range of content.

Vector Analysis with Sympy: Gradient, Curl, and Divergence

Your Daily Dose of Computer Algebra

Photo by Dan Cristian Pădureț on Unsplash

About this series: Learning to use computer algebra systems with ease requires a lot of practice. To help you on your journey to mastery, follow me in this series and solve common and not-so-common problems using systems like SymPy, Sage, or Mathematica.

Today’s Problem

Suppose we have the following scalar and vector fields

where 𝑟=|𝑟⃗|. Calculate the gradient for the scalar fields and divergence and curl for the vector field.

Solution with SymPy

Basically, there are two different ways how one can treat the problem in SymPy. The first way is to use the sympy.vector subpackage, which is convenient, because it already provides functions for the usual operators in vector analysis. So this is the easiest way to go. However, the vectorsubpackage is restricted to 3 dimensions and does not generalize to more or fewer dimensions. So if your problem is 3D, sympy.vector is fine, otherwise the second way is for you: write your own vector analysis operators, which is much easier than it may sound. We will go both ways here.

Using sympy.vector

First, we have to create our cartesian coordinate system:

The base_scalars method are special symbols for the coordinate variables that have knowledge about which coordinate system they belong to. We don't need that special knowledge here, it's just necessary if you work with several coordinate systems and want to transform between them.

Now define some more symbols and the vector 𝑟⃗:

Now define the field

Then take the ready-to-use functions from sympy.vector. For the gradient, we have

We can also take the Laplacian (divergence of the gradient):

Finally, the curl of 𝐴⃗ should be just zero, because it’s a gradient field:

Using custom operators

Writing the vector operators from scratch is very easy. For the gradient, just define your own Python function that returns a column vector:

The divergence is similarly easy. You just build the summation yourself using the sympy.Add class:

The curl needs a little bit more bookkeeping. It’s easiest to remember this trick to derive vector identities so that you can write the cross product in terms of Levi-Civita symbols.

Then you can immediately write down:

Let’s try it out. Define the symbols and field:

Take the gradient

Now the divergence:

And finally, the curl:

Judge for yourself which way you prefer. Personally, I would go for the second one.

If you found this article useful, you may want to consider becoming a Medium member to get unlimited access to all Medium articles. By registering using this link you can even support me as a writer.

Mathematics
Mathematics Education
Python
Programming
Science
Recommended from ReadMedium