使用“from __future__ import division”在我的程序中,但它没有随我的程序一起加载
我用 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 零矩阵,而不是使用命令 (感谢 eryksun 纠正我的符号,解决了 Zeros 函数的问题。)zeros(2,1)
因为安装在我的机器上的 SymPy 0.7.1 抱怨“zeros() 完全需要一个论点”,尽管 wiki 另有建议。也许这个命令仅适用于 git 版本。
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 (Thanks eryksun for correcting my notation, which fixed the issue with the zeros function.)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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ipython
解释器中的ipython -i
命令和run -i
都会忽略print05 中的
脚本。from __future__ import division
.py在
ipython
控制台中:execfile
和import
产生相同的结果:from __future__ import div
不应该对来自不同模块的源代码,否则会破坏其他不期望其存在的模块中的代码。在这里,
from __future__ import div
起作用了:本例中的模块名称在
print05.py
和提示符中都是__main__
。在这里,第一个
print 1/2
在print05
模块中执行,第二个在__main__
模块中执行,因此它也按预期工作:错误:
__future__
的文档说:因此,如果 ipython 的 -i 选项尝试模拟相同的 python 选项,则可能是 ipython 中的一个错误。
Both
ipython -i
command andrun -i
inipython
interpreter ignorefrom __future__ import division
inprint05.py
script.In
ipython
console:execfile
andimport
produce the same result: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:The module name in this case is
__main__
both insideprint05.py
and in the prompt.Here, the first
print 1/2
executes inprint05
module, the second one in__main__
module so it also works as expected:And here's something wrong:
The docs for
__future__
say:So It might be a bug in
ipython
if its-i
option tries to emulate the same python option.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.