Python Scripting for Computational Science 阅读笔记之矩阵

本文介绍了如何使用Python和Numpy库进行矩阵的基本操作,包括创建、复制矩阵、生成序列矩阵、坐标定位、循环遍历及矩阵计算等内容。

《Python Scripting for Computational Science, Third Edition》由 Hans Petter Langtangen等编写,找到这本书纯粹是为了学怎么用python输入矩阵,比较初级,但是有效。

 

取自第四章 数字计算。

要像输入矩阵类型的数据必须先导入库,在这里Python的扩展称为数值python,Numpy,所以

from numpy import *

这样我们就可以使用python的数值方法,与matlab类似。

所有的代码段在该文件中

src/py/intro/NumPy_basics.py

1,建立矩阵

例如

>>> from numpy import * 

>>> n = 4 

>>> a = zeros(n) # one-dim. array of length n 

>>> print a # str(a) 

[ 0. 0. 0. 0.] 

>>> a # repr(a) 

array([ 0., 0., 0., 0.]) 

>>> p = q = 2 

>>> a = zeros((p,q,3)) # p*q*3 three-dim. array 

>>> print a 

[[[ 0. 0. 0.] 

[ 0. 0. 0.]] 

[[ 0. 0. 0.] 

[ 0. 0. 0.]]] 

2,拷贝矩阵

r = x.copy()

也可以以x的模式建立矩阵

r = zeros(x.shape, x.dtype) 

3,序列矩阵,同Matlab

>>> x = linspace(-5, 5, 11) 

>>> print x 

[-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.] 

 

也可以用 r_[start,stop,incj]形式

>>> a = r_[-5:5:11j] # same as linspace(-1, 1, 11) 

>>> print a 

[-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.] 

也可以不考虑数据的数量,而是按照某种从小到大排列方法排列

>>> x = arange(-5, 5, 1, float) 

>>> print x 

[-5. -4. -3. -2. -1. 0. 1. 2. 3. 4.] 

这个方法不被作者建议,使用linspace比较好。
或者使用这个函数:

>>> x = seq(-5, 5, 1) 

>>> print x 

[-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.] 

4,矩阵的坐标

通常采用以下表示方法

a = linspace(-1, 1, 6) 

a[2:4] = -1 # set a[2] and a[3] equal to -1 

a[-1] = a[0] # set last element equal to first one 

a[:] = 0 # set all elements of a equal to 0 

a.fill(0) # set all elements of a equal to 0 

5,循环

for i in xrange(a.shape[0]): 

  for j in xrange(a.shape[1]): 

      a[i,j] = (i+1)*(j+1)*(j+2) 

      print ’a[%d,%d]=%g ’ % (i,j,a[i,j]), 

  print # newline after each row 

6,矩阵计算
例如

>>> import time # module for measuring CPU time 

>>> a = linspace(0, 1, 1E+07) # create some array 

>>> t0 = time.clock() 

>>> b = 3*a -1 

>>> t1 = time.clock() # t1-t0 is the CPU time of 3*a-1 

>>> for i in xrange(a.size): b[i] = 3*a[i] - 1 

>>> t2 = time.clock() 

>>> print ’3*a-1: %g sec, loop: %g sec’ % (t1-t0, t2-t1) 

3*a-1: 2.09 sec, loop: 31.27 sec 

这里只是个简单的介绍和应用,以后继续。。。。。。
Python scripting for KLayout is a powerful tool that allows you to automate tasks and customize the functionality of KLayout, which is a popular open-source layout viewer and editor for integrated circuit designs. With Python scripting, you can manipulate layouts, extract information, perform analysis, and generate reports. To get started with Python scripting for KLayout, you need to have KLayout installed on your system. Once installed, you can use the Python API provided by KLayout to interact with the software. Here are some key points about Python scripting for KLayout: 1. Accessing Layout Data: You can use Python to load layout files, access layers, geometries, properties, and manipulate them as needed. This allows you to automate repetitive tasks or perform complex operations on layouts. 2. Modifying Layouts: Python scripting enables you to modify layouts by adding or removing geometries, changing properties, merging or splitting layers, and performing various transformations. 3. Design Rule Checking (DRC): You can use Python scripting to perform DRC checks on layouts. This involves writing scripts that define design rules and check for violations, helping you ensure the design meets the required specifications. 4. Customizing User Interface: Python scripting allows you to customize the KLayout user interface by adding custom menus, toolbars, dialogs, and shortcuts. This can enhance your workflow and make specific functionalities easily accessible. 5. Scripting Automation: With Python scripting, you can automate repetitive tasks by writing scripts that perform a series of actions in KLayout. This can save time and improve productivity.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

why4000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值