#! /usr/bin/env python3 # def divergence_example ( ): from sympy import cos from sympy import exp from sympy import log from sympy import pprint from sympy import symbols from sympy.vector import CoordSys3D from sympy.vector import curl from sympy.vector import divergence import sympy print ( '' ) print ( 'divergence_example():' ) print ( ' sympy version: ' + sympy.__version__ ) print ( ' We want the divergence and curl of a vector field.' ) print ( ' v(x,y,z) = [ f, g, h ].' ) # # Name a coordinate system. # C = CoordSys3D ( 'C' ) # # Name the components of the coordinate system. # x, y, z = C.x, C.y, C.z # # Define the vector field. # f = x**3 + y**2 + log ( z ) g = x * y**2 * z h = exp ( 2 * z + x ) v = f * C.i + g * C.j + h * C.k # # Compute the divergence. # df/dx + dg/dy + dh/dz # div = divergence ( v ) print ( '' ) print ( ' divergence(v)' ) pprint ( div ) # # Compute the curl. # dh/dy-dg/dz, df/dz-dh/dx, dg/dx-df/dy # print ( '' ) print ( ' curl(v)' ) crl = curl ( v ) pprint ( crl ) return if ( __name__ == "__main__" ): divergence_example ( )