[python高性能编程-学习笔记]章节2.3计算完整的Julia集合

Python高性能编程2014版中2.3采用了Julia集合作为范例来作为性能分析的范例,下面是作为一个python初阶使用者在学习示例中遇到的一些问题,我在这里把他们记录下来,作为备忘。同时希望能够帮助到后续学习该书的同学解决一些疑点。

首先是对Julia集合的介绍。以下关于Julia集合的定义摘自百度百科。

               

图1-Julia集合的定义

本章需要做的事情是采用代码去生成这个集合,并且在其中加上对于代码的部分性能分析,代码不展示ru生成图形的部分。下面

是生成Julia集合的代码。

def calc_pure_python(desired_width, max_iterations):
    """Create a list of complex coordinates (zs) and complex
    parameters (cs), build Julia set, and display"""
    x_step = (float(x2 - x1) / float(desired_width))
    y_step = (float(y1 - y2) / float(desired_width))
    x = []
    y = []
    ycoord = y2
    while ycoord > y1:
        y.append(ycoord)
        ycoord += y_step
    xcoord = x1
    while xcoord < x2:
        x.append(xcoord)
        xcoord += x_step
    # Build a list of coordinates and the initial condition for each cell.
    # Note that our initial condition is a constant and could easily be removed;
    # we use it to simulate a real-world scenario with several inputs to
    # our function.
    zs = []
    cs = []
    for ycoord in y:
        for xcoord in x:
            zs.append(complex(xcoord, ycoord))
            cs.append(complex(c_real, c_imag))

    print "Length of x:", len(x)
    print "Total elements:", len(zs)
    start_time = time.time()
    output = calculate_z_serial_purepython(max_iterations, zs, cs)
    end_time = time.time()
    secs = end_time - start_time
    print calculate_z_serial_purepython.func_name + " took", secs, "seconds"

    # This sum is expected for a 1000^2 grid with 300 iterations.
    # It catches minor errors we might introduce when we're
    # working on a fixed set of inputs.
    assert sum(output) == 33219980

作为一个初阶python使用者,我在阅读这段代码的时候,碰到的问题有如下两个。

第一个问题,函数调用了一个名为complex()的函数,该函数是python标准内置函数库中的一个函数,这个函数的作用是生成复数。

具体的使用方法如下:

示例:

#complex()
print(complex(1))
print(complex('2+1j'))
print(complex(2, 5))

l = [1, 3, 4, 5]
for i in l:
 print(complex(i, 5))
结果:

(1+0j)
(2+1j)
(2+5j)

(1+5j)

(3+5j)

(4+5j)

(5+5j)
当直接使用complex(n)时,会返回一个(n+0j)的复数,默认虚部为0。

而当指定了虚部时,例如调用complex(x,j),则会返回一个形如(x+yj)的复数。

需要注意的是,complex还可以通过complex('2+1j')这种形式直接生成一个形如(2+1j)的复数。


第二个问题出现在一个自定义的函数calculate_z_serial_purepython中,这个函数代码如下。

def calculate_z_serial_purepython(maxiter, zs, cs):
    """Calculate output list using Julia update rule"""
    output = [0] * len(zs)
    for i in range(len(zs)):
        n = 0
        z = zs[i]
        c = cs[i]
        while abs(z) < 2 and n < maxiter:
            z = z * z + c
            n += 1
        output[i] = n
    return output

这个函数中有一个针对列表output的初始化定义,使用的是[0]*len(zs),这是为了将list初始化为长度为zs的长度,元素全部为0的一种标准赋值方法。


引用:

1High Performance Python byMicha Gorelick and Ian Ozvald(O'Rilly).Copyright 2014 Micha Gorelick and Ian Ozsvald,

978-1-449-36159-4

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值