Python输出格式问题

2024-06-26 00:16:19 发布

您现在位置:Python中文网/ 问答频道 /正文

不确定我正在接收的输出的语法。任何帮助都将不胜感激。 这是我的密码:

import numpy

def g(): #generate random complex values
    return numpy.random.random(1) + numpy.random.random(1) *1j

p = numpy.poly1d(numpy.squeeze([g(),g(),g()]))  # test function p
pprime = numpy.polyder(p) #derivative of p

print 'Our p(x) is {} '. format(p)
print('\n') # new line
print'Our pprime(x) is {} '. format(pprime)  #apply newtons method to p
print('\n') # new line

#apply newtons method to p
def root_newton ( f, df, tolerance = 1.0e-6):
    dx = 2 * tolerance
    x=0
    while dx > tolerance:
        x1 = x - f(x)/df(x)
        dx = abs (x - x1)
        x = x1
    return x
print('Our first root is at {}'.format(root_newton(p,pprime)))
print('\n') # new line

输出如下:

Our p(x) is                    2
(0.6957 + 0.683j) x + (0.3198 + 0.5655j) x + (0.9578 + 0.1899j) 


Our pprime(x) is  
(1.391 + 1.366j) x + (0.3198 + 0.5655j) 


Our first root is at (0.00925817978737+0.830966156841j)


The correct roots are [-0.64968928-1.01513333j  0.00925818+0.83096616j]

我输出的第一行中第二个分量上面的2是什么意思?我在网上找不到与我的问题类似的东西。我猜这可能意味着x分量是平方的,但我不确定?顺便说一下,这是python3。你知道吗


Tags: numpyformatnewreturnisdeflineour
1条回答
网友
1楼 · 发布于 2024-06-26 00:16:19

2是第一个x上的指数,未对齐,因为您将文本放在同一行的前面。你知道吗

如果我们取你的输出:

Our p(x) is                    2
(0.6957 + 0.683j) x + (0.3198 + 0.5655j) x + (0.9578 + 0.1899j)

并删除前面的文字:

                   2
(0.6957 + 0.683j) x + (0.3198 + 0.5655j) x + (0.9578 + 0.1899j)

2的意思变得更清楚了。你知道吗

相关问题 更多 >