diff options
author | Stefan van der Walt <stefan@sun.ac.za> | 2008-08-23 23:55:01 +0000 |
---|---|---|
committer | Stefan van der Walt <stefan@sun.ac.za> | 2008-08-23 23:55:01 +0000 |
commit | 46facee53a506cd26c00ad12d482a682b0176404 (patch) | |
tree | 563e932b1888e28c19a0ca6cc0f6e02bfe0b6f3f /doc/numpybook/comparison/weave | |
parent | d14243ca1fa3373babfd5b699e12dff177f60dba (diff) | |
download | numpy-46facee53a506cd26c00ad12d482a682b0176404.tar.gz |
Move book to docs directory.
Diffstat (limited to 'doc/numpybook/comparison/weave')
-rw-r--r-- | doc/numpybook/comparison/weave/filter.py | 23 | ||||
-rw-r--r-- | doc/numpybook/comparison/weave/inline.py | 49 | ||||
-rwxr-xr-x | doc/numpybook/comparison/weave/timeme | 2 |
3 files changed, 74 insertions, 0 deletions
diff --git a/doc/numpybook/comparison/weave/filter.py b/doc/numpybook/comparison/weave/filter.py new file mode 100644 index 000000000..b2fdb277e --- /dev/null +++ b/doc/numpybook/comparison/weave/filter.py @@ -0,0 +1,23 @@ +from scipy import weave, zeros_like + +def filter(a): + if a.ndim != 2: + raise ValueError, "a must be 2-d" + code = r""" + int i,j; + for(i=1;i<Na[0]-1;i++) { + for(j=1;j<Na[1]-1;j++) { + B2(i,j) = A2(i,j) + (A2(i-1,j) + + A2(i+1,j) + A2(i,j-1) + + A2(i,j+1))*0.5 + + (A2(i-1,j-1) + + A2(i-1,j+1) + + A2(i+1,j-1) + + A2(i+1,j+1))*0.25; + } + } + """ + b = zeros_like(a) + weave.inline(code,['a','b']) + return b + diff --git a/doc/numpybook/comparison/weave/inline.py b/doc/numpybook/comparison/weave/inline.py new file mode 100644 index 000000000..31499213e --- /dev/null +++ b/doc/numpybook/comparison/weave/inline.py @@ -0,0 +1,49 @@ +from scipy import weave +from numpy import rand, zeros_like + +def example1(a): + if not isinstance(a, list): + raise ValueError, "argument must be a list" + code = r""" + int i; + py::tuple results(2); + for (i=0; i<a.length(); i++) { + a[i] = i; + } + results[0] = 3.0; + results[1] = 4.0; + return_val = results; + """ + return weave.inline(code,['a']) + +def arr(a): + if a.ndim != 2: + raise ValueError, "a must be 2-d" + code = r""" + int i,j; + for(i=1;i<Na[0]-1;i++) { + for(j=1;j<Na[1]-1;j++) { + B2(i,j) = A2(i,j) + A2(i-1,j)*0.5 + + A2(i+1,j)*0.5 + A2(i,j-1)*0.5 + + A2(i,j+1)*0.5 + + A2(i-1,j-1)*0.25 + + A2(i-1,j+1)*0.25 + + A2(i+1,j-1)*0.25 + + A2(i+1,j+1)*0.25; + } + } + """ + b = zeros_like(a) + weave.inline(code,['a','b']) + return b + +a = [None]*10 +print example1(a) +print a + +a = rand(512,512) +b = arr(a) + +h = [[0.25,0.5,0.25],[0.5,1,0.5],[0.25,0.5,0.25]] +import scipy.signal as ss +b2 = ss.convolve(h,a,'same') diff --git a/doc/numpybook/comparison/weave/timeme b/doc/numpybook/comparison/weave/timeme new file mode 100755 index 000000000..2f7cd297f --- /dev/null +++ b/doc/numpybook/comparison/weave/timeme @@ -0,0 +1,2 @@ +python2.4 -m timeit -s "import numpy as N; a=N.random.rand(100,200); import filter" "b = filter.filter(a)" + |