numpy:点和

发布于 2024-12-22 05:05:07 字数 621 浏览 4 评论 0原文

>>> from pandac.PandaModules import Vec3
>>> import numpy
>>> l = []
>>> l.append( Vec3(1,1,1) )
>>> l.append( Vec3(1,1,1) )
>>> l.append( Vec3(1,1,1) )
>>> Vec3(1,1,1)+Vec3(1,1,1)
Vec3(2, 2, 2)
>>> sum(l)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'libpanda.Vec3'
>>> numpy.sum(l)
9.0
>>> 

我想要一些快速(快速==不是纯Python中的循环,而是numpy速度)方法来实现:

>>> my_smart_sum(l)
Vec3(3,3,3)
>>> from pandac.PandaModules import Vec3
>>> import numpy
>>> l = []
>>> l.append( Vec3(1,1,1) )
>>> l.append( Vec3(1,1,1) )
>>> l.append( Vec3(1,1,1) )
>>> Vec3(1,1,1)+Vec3(1,1,1)
Vec3(2, 2, 2)
>>> sum(l)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'libpanda.Vec3'
>>> numpy.sum(l)
9.0
>>> 

i want some fast (fast == not for loop in pure python but numpy velocity) method to achive:

>>> my_smart_sum(l)
Vec3(3,3,3)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思念满溢 2024-12-29 05:05:07

试试这个:

sum(l, start=Vec3(0,0,0))

或者,使用 numpy,这个:

numpy.sum(l, axis=0)

速度取决于向量加法的实现。您应该使用 timeit 来确定哪种方法最快。这可能看起来像这样:

python -m timeit "import numpy; foo = [[1,1,1],[1,1,1]]" "numpy.sum(foo, axis=0)"
10000 loops, best of 3: 46.5 usec per loop

您传递的第一个字符串是设置语句 - 这不会包含在计时中。您传递的第二个字符串是您实际想要计时的代码。我对 pandac 一无所知,但有时使用 Cython 可以大大加快数字运算循环的速度

Try this:

sum(l, start=Vec3(0,0,0))

Or, with numpy, this:

numpy.sum(l, axis=0)

The speed depends in the implementation of the vector-addition. You should use timeit to determine which method is fastest. This could look something like this:

python -m timeit "import numpy; foo = [[1,1,1],[1,1,1]]" "numpy.sum(foo, axis=0)"
10000 loops, best of 3: 46.5 usec per loop

The first string you pass is the setup-statement - this will not be included in the timing. The second string you pass is the code you actually want to time. I don't know anything about pandac, but sometimes number-crunching loops can be sped up a lot using Cython.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文