使用“from __future__ import division”在我的程序中,但它没有随我的程序一起加载

发布于 2024-12-18 10:31:32 字数 1128 浏览 6 评论 0原文

我用 Python 2 编写了以下程序,为我的数学问题集进行牛顿法计算,虽然它工作得很好,但出于我不知道的原因,当我最初使用 %run -i NewtonsMethodMultivariate.py< 在 ipython 中加载它时/code>,Python 3 分区未导入。我知道这一点是因为在加载 Python 程序后,输入 x**(3/4) 给出“1”。手动导入新部门后,x**(3/4) 仍为 x**(3/4),如预期。这是为什么呢?

# coding: utf-8
from __future__ import division
from sympy import symbols, Matrix, zeros

x, y = symbols('x y')
X = Matrix([[x],[y]])
tol = 1e-3

def roots(h,a):
  def F(s):
    return h.subs({x: s[0,0], y: s[1,0]})
  def D(s):
    return h.jacobian(X).subs({x: s[0,0], y: s[1,0]})
  if F(a) == zeros((2,1)):
    return a
  else:
    while (F(a)).norm() > tol:
      a = a - ((D(a))**(-1))*F(a)
      print a.evalf(10)

我会使用 Python 3 来避免这个问题,但我的 Linux 发行版只附带了 Python 2 的 SymPy。感谢任何人都可以提供的帮助。

另外,以防万一有人想知道,我还没有将这个脚本推广到 nxn 雅可比行列式,并且只需要处理我的问题集中的 2x2 。 此外,我正在切片 2x2 零矩阵,而不是使用命令 zeros(2,1) 因为安装在我的机器上的 SymPy 0.7.1 抱怨“zeros() 完全需要一个论点”,尽管 wiki 另有建议。也许这个命令仅适用于 git 版本。(感谢 eryksun 纠正我的符号,解决了 Zeros 函数的问题。)

I wrote the following program in Python 2 to do Newton's method computations for my math problem set, and while it works perfectly, for reasons unbeknownst to me, when I initially load it in ipython with %run -i NewtonsMethodMultivariate.py, the Python 3 division is not imported. I know this because after I load my Python program, entering x**(3/4) gives "1". After manually importing the new division, then x**(3/4) remains x**(3/4), as expected. Why is this?

# coding: utf-8
from __future__ import division
from sympy import symbols, Matrix, zeros

x, y = symbols('x y')
X = Matrix([[x],[y]])
tol = 1e-3

def roots(h,a):
  def F(s):
    return h.subs({x: s[0,0], y: s[1,0]})
  def D(s):
    return h.jacobian(X).subs({x: s[0,0], y: s[1,0]})
  if F(a) == zeros((2,1)):
    return a
  else:
    while (F(a)).norm() > tol:
      a = a - ((D(a))**(-1))*F(a)
      print a.evalf(10)

I would use Python 3 to avoid this issue, but my Linux distribution only ships SymPy for Python 2. Thanks to the help anyone can provide.

Also, in case anyone was wondering, I haven't yet generalized this script for nxn Jacobians, and only had to deal with 2x2 in my problem set. Additionally, I'm slicing the 2x2 zero matrix instead of using the command zeros(2,1) because SymPy 0.7.1, installed on my machine, complains that "zeros() takes exactly one argument", though the wiki suggests otherwise. Maybe this command is only for the git version. (Thanks eryksun for correcting my notation, which fixed the issue with the zeros function.)

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

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

发布评论

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

评论(2

深海少女心 2024-12-25 10:31:32

ipython 解释器中的 ipython -i 命令和 run -i 都会忽略 print05 中的 from __future__ import division .py 脚本。

$ cat print05.py 
from __future__ import division
print(1/2)

ipython控制台中:

In [1]: print 1/2
0
In [2]: run -i print05.py
0.5
In [3]: division
Out[3]: _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
In [4]: print 1/2
0
In [5]: from __future__ import division
In [6]: print 1/2
0.5

execfileimport产生相同的结果:

>>> print 1/2
0
>>> execfile('print05.py')
0.5
>>> print 1/2
0
>>> from __future__ import division
>>> print 1/2
0.5

from __future__ import div不应该对来自不同模块的源代码,否则会破坏其他不期望其存在的模块中的代码。

在这里,from __future__ import div 起作用了:

$ python -i print05.py
0.5
>>> print 1/2
0.5
>>> division
_Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)

本例中的模块名称在 print05.py 和提示符中都是 __main__

在这里,第一个 print 1/2print05 模块中执行,第二个在 __main__ 模块中执行,因此它也按预期工作

$ python -im print05
0.5
>>> print 1/2
0

:错误:

$ ipython -i print05.py
0.5
In [1]: division
Out[1]: _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
In [2]: print 1/2
0

__future__ 的文档说:

如果解释器使用 -i 选项启动,则会传递一个脚本
要执行的名称,并且脚本包含未来语句,它将
在脚本执行后启动的交互式会话中生效
已执行。

因此,如果 ipython 的 -i 选项尝试模拟相同的 python 选项,则可能是 ipython 中的一个错误。

Both ipython -i command and run -i in ipython interpreter ignore from __future__ import division in print05.py script.

$ cat print05.py 
from __future__ import division
print(1/2)

In ipython console:

In [1]: print 1/2
0
In [2]: run -i print05.py
0.5
In [3]: division
Out[3]: _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
In [4]: print 1/2
0
In [5]: from __future__ import division
In [6]: print 1/2
0.5

execfile and import produce the same result:

>>> print 1/2
0
>>> execfile('print05.py')
0.5
>>> print 1/2
0
>>> from __future__ import division
>>> print 1/2
0.5

from __future__ import division should not have effect on the source code from different modules, otherwise it would break code in other modules that don't expect its presence.

Here, from __future__ import division has effect:

$ python -i print05.py
0.5
>>> print 1/2
0.5
>>> division
_Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)

The module name in this case is __main__ both inside print05.py and in the prompt.

Here, the first print 1/2 executes in print05 module, the second one in __main__ module so it also works as expected:

$ python -im print05
0.5
>>> print 1/2
0

And here's something wrong:

$ ipython -i print05.py
0.5
In [1]: division
Out[1]: _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
In [2]: print 1/2
0

The docs for __future__ say:

If an interpreter is started with the -i option, is passed a script
name to execute, and the script includes a future statement, it will
be in effect in the interactive session started after the script is
executed.

So It might be a bug in ipython if its -i option tries to emulate the same python option.

冷默言语 2024-12-25 10:31:32

SymPy 还提供了一个脚本——isympy——它是 IPython 的包装器,它执行一些常见的命令,包括从 future 导入除法。它非常方便,在较新的 IPython 版本(0.11+)中,它还允许自动构造符号(这很好,但我似乎总是忘记);使用 -a 参数运行它。

至于Python 3,开发版本已经支持,下一版本将会支持;我不知道发行版什么时候会打包它。

SymPy also provides a script -- isympy -- which is a wrapper for IPython which executes some common commands, including an import of division from future. It's quite handy, and in newer IPython versions (0.11+) it also allows automatic constructions of Symbols (which is nice as I always seem to forget); run it with the -a parameter.

As for Python 3, there is support for it in the development version and the next release will have it; when distributions are going to pack it I don't know.

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