如何设置 sympy 以执行标准微分几何任务? [英] How to set up sympy to perform standard differential geometry tasks?

查看:22
本文介绍了如何设置 sympy 以执行标准微分几何任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名工程专业的学生.我需要做的几乎所有数学运算都是在 R2 或 R3 中进行的,并且涉及微分几何.当然,我真的很喜欢 sympy,因为它使我的计算可重用且易于展示.

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.

我发现了什么:

sympy 中的东西与我所知道的功能非常接近,即标量或向量值到标量或向量值的映射,具有名称并连接到表达式似乎是某种形式

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)

或者作为例子

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

我还发现 sympy 有 diffgeom 模块,它定义了 Manifolds、Patches,然后可以在没有表达式或点的情况下对函数执行一些操作.就像将坐标系中的一个点平移到不同的链接坐标系中的同一点.

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.

还没有找到一种方法来对上述函数执行这些操作和转换.或者在 diffgeom 上下文中定义一些类似于 Lambda 函数的内容.

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.

我想做的例子:

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.

  • 这些东西是否存在而我还没有找到它们?
  • 它们是否可以使用 diffgeom 而我不明白?
  • 我是否必须使用 sympy 已经提供的主干自己编写这个?
  • 推荐答案

    sympy 中有一个微分几何模块:

    There is a differential geometry module within sympy:

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

    有关更多示例,您可以查看 http://blog.krastanov.org/pages/diff-geometry-in-python.html

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

    要执行 diffgeom 模块中的建议,只需使用流形的基本坐标定义表达式:

    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.

    最后一点:使用表达式而不是函数和 lambdas 可能会导致更易读和更惯用的 sympy 代码(通常使用 subs 代替符号而不是某种闭包更自然、lambda、函数调用等).

    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).

    这篇关于如何设置 sympy 以执行标准微分几何任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆