forked from zhangdft/Python-scientific-computation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a848bdb
Showing
882 changed files
with
492,721 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
演示Wing IDE 101的goto define功能。使用Wing IDE 101或Spyder打开本程序之后, | ||
按住Ctrl的同时点击signal, pl, HasTraits等跳转到定义它们的程序。 | ||
""" | ||
from scipy import signal | ||
import pylab as pl | ||
from enthought.traits.api import HasTraits, Instance | ||
from enthought.traits.ui.api import View, Item | ||
|
||
signal.lfilter | ||
pl.plot | ||
pl.title |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
本程序有BUG,不能正常运行。在IPython中执行本程序进行调试。 | ||
""" | ||
import pylab as pl | ||
import numpy as np | ||
|
||
def test_debug(): | ||
x = np.linspace(1, 50, 10000) | ||
img = np.sin(x*np.cos(x)) | ||
pl.imshow(img) | ||
pl.show() | ||
|
||
test_debug() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
一个简单模块,在usemode.py中使用其中的x和y。 | ||
""" | ||
x = 100 | ||
y = 200 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
载入几个重量级的扩展库。在IPython中使用run命令可以缩短 | ||
第二次运行程序启动时间。 | ||
""" | ||
import numpy as np | ||
from scipy import signal | ||
import pylab as pl | ||
|
||
t = np.linspace(0, 10, 1000) | ||
x = signal.chirp(t, 5, 10, 30) | ||
pl.plot(t, x) | ||
pl.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
对图像进行浮雕处理 | ||
""" | ||
import pylab as pl | ||
import scipy as sp | ||
img = sp.lena() | ||
pl.imshow(img, cmap=pl.cm.gray) | ||
img2 = img[:-2,1:-1]-img[2:,1:-1]+img[1:-1, :-2]-img[1:-1,2:] | ||
pl.figure() | ||
pl.imshow(img2, cmap=pl.cm.gray) | ||
pl.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
演示Spyder的Workspace,在Spyder中运行本程序之后,可以使用 | ||
Workspace查看x, y和img数组。 | ||
""" | ||
import numpy as np | ||
x = np.linspace(0.1, 10, 100) | ||
y = np.sin(x)/x | ||
img = np.random.random((10,10)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
输出mymodel.py模块中的x和y,为了显示最新的值,使用reload()强制 | ||
重新载入模块。 | ||
""" | ||
import mymodel | ||
reload(mymodel) | ||
from mymodel import * | ||
|
||
print x, y |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
使用TraitsUI制作的简单的界面程序。使用Wing IDE 101可以查看 | ||
按钮点击事件处理函数_button_fired的调用堆栈。 | ||
""" | ||
from enthought.traits.api import HasTraits, Instance, Button, Int | ||
from enthought.traits.ui.api import View, Item | ||
|
||
class Demo(HasTraits): | ||
count = Int(0) | ||
button = Button("Click Me") | ||
view = View(Item("button"), Item("count")) | ||
|
||
def _button_fired(self): | ||
self.count += 1 | ||
|
||
demo = Demo() | ||
demo.configure_traits() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
创建一个1000*1000的24bit的BMP图像。 | ||
""" | ||
header = 'BM\xf6\xc6-\x00\x00\x00\x00\x006\x00\x00\x00(\x00\x00\x00\xe8\x03\x00\x00\xe8\x03\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\xc0\xc6-\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' | ||
f = file("tmp.bmp","wb") | ||
f.write(header) | ||
f.write("\x00"*(1000*1000*3)) | ||
f.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
18.0, 173.4 | ||
7.8, 126.2 | ||
8.5, 131.2 | ||
12.5, 155.0 | ||
7.4, 126.8 | ||
15.0, 170.4 | ||
7.1, 121.8 | ||
15.2, 169.3 | ||
19.2, 176.8 | ||
16.6, 175.2 | ||
18.7, 175.9 | ||
13.3, 160.4 | ||
19.1, 174.6 | ||
15.1, 169.9 | ||
16.7, 173.1 | ||
12.7, 155.8 | ||
19.3, 175.4 | ||
18.6, 174.4 | ||
11.8, 148.5 | ||
15.5, 172.5 | ||
17.2, 175.2 | ||
18.3, 175.6 | ||
7.1, 123.1 | ||
18.5, 170.8 | ||
7.4, 125.0 | ||
7.4, 128.4 | ||
9.8, 140.9 | ||
16.8, 175.8 | ||
10.0, 142.8 | ||
10.9, 146.3 | ||
9.4, 137.2 | ||
13.5, 163.2 | ||
15.8, 174.7 | ||
18.4, 174.3 | ||
10.4, 143.6 | ||
12.4, 153.3 | ||
7.1, 127.2 | ||
16.2, 171.9 | ||
12.2, 156.6 | ||
9.4, 135.4 | ||
16.6, 172.4 | ||
18.6, 176.8 | ||
9.9, 140.2 | ||
11.0, 148.0 | ||
18.3, 173.0 | ||
18.9, 172.0 | ||
10.1, 143.1 | ||
13.7, 165.0 | ||
15.2, 169.9 | ||
12.5, 153.6 | ||
15.9, 178.2 | ||
10.4, 143.7 | ||
17.2, 173.9 | ||
11.5, 151.1 | ||
12.5, 154.1 | ||
19.2, 178.8 | ||
8.6, 132.1 | ||
12.3, 153.6 | ||
9.3, 137.2 | ||
13.0, 161.0 | ||
18.3, 173.8 | ||
15.7, 176.3 | ||
13.0, 161.3 | ||
13.3, 160.0 | ||
18.8, 174.6 | ||
14.4, 166.6 | ||
14.0, 164.9 | ||
19.9, 173.9 | ||
8.8, 134.5 | ||
16.3, 171.4 | ||
8.0, 133.0 | ||
12.6, 153.2 | ||
7.9, 126.4 | ||
7.6, 131.2 | ||
13.4, 161.0 | ||
15.7, 172.7 | ||
10.7, 144.1 | ||
18.9, 175.7 | ||
15.6, 173.4 | ||
17.6, 175.3 | ||
17.8, 176.7 | ||
19.0, 173.0 | ||
10.2, 142.1 | ||
10.7, 143.5 | ||
11.5, 147.2 | ||
8.4, 130.6 | ||
9.6, 139.7 | ||
12.0, 151.4 | ||
12.1, 147.8 | ||
8.3, 131.0 | ||
9.4, 134.2 | ||
7.3, 123.5 | ||
13.7, 163.3 | ||
11.2, 145.9 | ||
13.8, 164.2 | ||
19.6, 175.9 | ||
19.0, 172.2 | ||
14.7, 169.1 | ||
15.8, 173.9 | ||
10.8, 145.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
演示一维数组的下标存取。 | ||
""" | ||
import numpy as np | ||
|
||
a = np.arange(10) | ||
print "a[5]", a[5] | ||
print "a[3:5]", a[3:5] | ||
print "a[:5]", a[:5] | ||
print "a[:-1]", a[:-1] | ||
a[2:4] = 100,101 | ||
print "a", a | ||
print "a[1:-1:2]", a[1:-1:2] | ||
print "a[::-1]", a[::-1] | ||
print "a[5:1:-2]", a[5:1:-2] | ||
|
||
b = a[3:7] | ||
print b | ||
b[2] = -10 | ||
print b | ||
print a | ||
|
||
x = np.arange(10,1,-1) | ||
print x | ||
print "x[[3, 3, 1, 8]]", x[[3, 3, 1, 8]] | ||
b = x[np.array([3,3,-3,8])] | ||
b[2] = 100 | ||
print b | ||
print x | ||
x[[3,5,1]] = -1, -2, -3 | ||
print x | ||
|
||
x = np.arange(5,0,-1) | ||
print x | ||
print x[np.array([True, False, True, False, False])] | ||
print x[[True, False, True, False, False]] | ||
print x[np.array([True, False, True, True])] | ||
x[np.array([True, False, True, True])] = -1, -2, -3 | ||
print x | ||
|
||
x = np.random.rand(10) | ||
print x | ||
print x>0.5 | ||
print x[x>0.5] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
演示二维数组的下标存取。 | ||
""" | ||
import numpy as np | ||
|
||
a = np.arange(0, 60, 10).reshape(-1, 1) + np.arange(0, 6) | ||
print "a[0,3:5]=", a[0,3:5] | ||
print "a[4:,4:]=", a[4:,4:] | ||
print "a[:,2]=", a[:,2] | ||
print "a[2::2,::2]=", a[2::2,::2] | ||
|
||
b = a[0,3:5] | ||
b[0] = -b[0] | ||
print "a[0,3:5]=", a[0,3:5] | ||
b[0] = -b[0] | ||
|
||
idx = slice(None, None, 2), slice(2,None) | ||
print "idx = ", idx | ||
print "a[idx]=", a[idx] # 和a[::2,2:]相同 | ||
print "a[idx][idx]=", a[idx][idx] # 和a[::2,2:][::2,2:]相同 | ||
|
||
print a[(0,1,2,3,4),(1,2,3,4,5)] | ||
print a[3:, [0,2,5]] | ||
mask = np.array([1,0,1,0,0,1], dtype=np.bool) | ||
print a[mask, 2] | ||
mask = np.array([1,0,1,0,0,1]) | ||
print a[mask, 2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
多维数组的下标存取实例。 | ||
有一个shape为(I, J, K)的三维数组v和一个shape为(I, J)的二维数组idx,idx的每个值 | ||
都是0到K-L的整数。通过下标运算得到一个数组r,对于第0和1轴的每个下标i和j都满足下 | ||
面条件: | ||
r[i,j,:] = v[i,j,idx[i,j]:idx[i,j]+L] | ||
""" | ||
import numpy as np | ||
|
||
I, J, K, L = 6, 7, 8, 3 | ||
_, _, v = np.mgrid[:I, :J, :K] | ||
idx = np.random.randint(0, K-L, size=(I,J)) | ||
|
||
idx_k = idx.reshape(I,J,1) + np.arange(3) | ||
idx_i, idx_j, _ = np.ogrid[:I, :J, :K] | ||
|
||
r = v[idx_i, idx_j, idx_k] | ||
i, j = 2,3 | ||
print r[i,j,:] | ||
print v[i,j,idx[i,j]:idx[i,j]+L] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
使用Mayavi绘制多为数组下标存取的演示图像。 | ||
""" | ||
import numpy as np | ||
from enthought.mayavi import mlab | ||
|
||
x, y, z = np.mgrid[:6,:7,:8] | ||
c = np.zeros((6, 7, 8), dtype=np.int) | ||
c.fill(1) | ||
k = np.random.randint(2,5,size=(6, 7)) | ||
|
||
idx_i, idx_j, _ = np.ogrid[:6, :7, :8] | ||
idx_k = k[:,:, np.newaxis] + np.arange(3) | ||
c[idx_i, idx_j, idx_k] = np.random.randint(2,6, size=(6,7,3)) | ||
|
||
mlab.points3d(x[c>1], y[c>1], z[c>1], c[c>1], mode="cube", scale_factor=0.8, | ||
scale_mode="none", transparent=True, vmin=0, vmax=8, colormap="Greys") | ||
|
||
mlab.points3d(x[c==1], y[c==1], z[c==1], c[c==1], mode="cube", scale_factor=0.8, | ||
scale_mode="none", transparent=True, vmin=0, vmax=8, colormap="Greys", opacity = 0.2) | ||
mlab.gcf().scene.background = (1,1,1) | ||
|
||
mlab.figure() | ||
x, y, z = np.mgrid[:6,:7,:3] | ||
mlab.points3d(x, y, z, c[idx_i, idx_j, idx_k], mode="cube", scale_factor=0.8, | ||
scale_mode="none", transparent=True, vmin=0, vmax=8, colormap="Greys", opacity = 1) | ||
mlab.gcf().scene.background = (1,1,1) | ||
mlab.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
使用内存映射数组修改BMP文件。 | ||
""" | ||
import numpy as np | ||
|
||
# shape = 高,宽,颜色 | ||
bmp = np.memmap("tmp.bmp", offset=54, shape=(1000,1000,3)) | ||
|
||
# 产生一组从0到255的渐变值 | ||
tmp = np.linspace(0, 255, 1000).astype(np.uint8) | ||
|
||
# 水平蓝色渐变 | ||
bmp[:, :, 0] = tmp | ||
# 垂直绿色渐变 | ||
bmp[:, :, 1] = tmp.reshape(-1,1) | ||
# 红色成分 | ||
bmp[:, :, 2] = 127 | ||
|
||
bmp.flush() |
Oops, something went wrong.