Question

I'm an engineering student. Pretty much all math I have to do is something in R2 or R3 and concerns differential geometry. Naturally I really like sympy because it makes my calculations reusable and presentable.

What I found:

The thing in sympy that comes closeset to what I know functions as, which is as mapping of scalar or vector values to scalar or vector values, with a name and connected to an expressions seems to be something of the form

functionname=sympy.Lambda(Variables in tuple, Expression)

or as an example

f=sympy.Lambda((x),x+1)

I also found that sympy has the diffgeom module that defines Manifolds, Patches and can then perform some operations on functions without expressions or points. Like translating a point in a coordinate system to the same point in a different, linked coordinate system.

I haven't found a way to perform those operations and transformations on functions like those above. Or to define something in the diffgeom context that performs like the Lambda function.

Examples of what I'd like to do:

scalarfield f (x,y,z) = expression

grad (f) = ( d expression / dx , d expression / dy , d expression / dz)^T

vectorfield v (x,y,z) = ( expression 1 , expression 2 , expression 3 )^T

I'd then like to be able to integrate the vectorfield over bodies or curves.
  • Do these things exist and I haven't found them?
  • Are they doable with diffgeom and I didn't understand it?
  • Would I have to write this myself with the backbones that sympy already provides?
Was it helpful?

Solution

There is a differential geometry module within sympy:

http://docs.sympy.org/latest/modules/diffgeom.html

For more examples you can see http://blog.krastanov.org/pages/diff-geometry-in-python.html

To do the suggested in the diffgeom module, just define your expression using the base coordinates of your manifold:

from diffgeom.rn import R2
scalar = (R2.r**2 - R2.x**2 - R2.y**2) # you can mix coordinate systems
gradient = (R2.e_x + R2.e_y).rcall(scalar)

There are various functions for change of coordinates, etc. Probably many things are missing, but it would take usage and bug reports (and help) for all this to get implemented.

You can see some other examples in the test files:

However for doing what is suggested in your question, doing it through differential geometry (while possible) would be an overkill. You can just use the matrices module:

def gradient(expr, vars):
    return Matrix([expr.diff(v) for v in vars])

More fancy things like matrix jacobians and more are implemented.

A final remark: using expressions instead of functions and lambdas will probably result in more readable and idiomatic sympy code (often it is more natural to use subs to substitute a symbols instead of some kind of closure, lambda, function call, etc).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top