本文介绍了sympy.geometry Point 类工作缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个读取非结构化网格的代码.我围绕 sympy.geometry 的几何实体编写了包装器,例如:

I have a code which reads unstructured mesh. I wrote wrappers around geometric entities of sympy.geometry such as:

class Point:
    def __init__(self, x, y, parent_mesh):
        self.shape = sympy.geometry.Point(x,y)
        self.parent_mesh = parent_mesh
        self.parent_cell = list()

一切正常,但 sympy.geometry.Point 的初始化对于每个 Point 需要花费大量时间.实际上,代码没有执行完数千个点.用 C++ 编写的类似代码在几秒钟内完成.没有它,代码足够快(我删除了它并计时).我读到一个可能的原因可能是 sympy.geometry 为了精度将浮点数转换为有理数.有没有办法(标志)来加速 sympy.geometry 因为我不需要精确的精度?

Everything works fine but initialization of sympy.geometry.Point takes a lot of time for each Point. Actually, the code did not finish execution for thousands of points. Similar code written in C++ finished in a few seconds. Without it the code is fast enough (I removed it and timed). I read that a possible reason could be that sympy.geometry converts floating point numbers to rationals for precision. Is there a way (flag) to speed up sympy.geometry as I do not need exact precision?

推荐答案

查看Point 类文档,特别是在第一个示例中:

Take a look at the Point class documentation, specifically, in one of the first examples:

浮点数会自动转换为 Rational,除非评估标志为 False.

因此,您可以在 Point 类的初始化过程中传递一个名为 evaluate 的标志:

So, you could pass a flag named evaluate during initialization of your Point classes:

self.shape = sympy.geometry.Point(x,y, evaluate=False)

这显然表明你在追求什么.

which apparently signals what you're after.

这篇关于sympy.geometry Point 类工作缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:08