Menu

[r6218]: / trunk / py4science / examples / weave_examples_simple.py  Maximize  Restore  History

Download this file

85 lines (73 with data), 2.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
"""Some simple examples of weave.inline use"""
import numpy as np
from scipy.weave import inline,converters
#-----------------------------------------------------------------------------
# Returning a scalar quantity computed from an array.
def trace(mat):
"""Return the trace of a matrix.
"""
nrow,ncol = mat.shape
code = \
"""
double tr=0.0;
for(int i=0;i<nrow;++i)
tr += mat(i,i);
return_val = tr;
"""
return inline(code,['mat','nrow','ncol'],
type_converters = converters.blitz)
# In-place operations on arrays in general work without any problems
def in_place_mult(num,mat):
"""In-place multiplication of a matrix by a scalar.
"""
nrow,ncol = mat.shape
code = \
"""
for(int i=0;i<nrow;++i)
for(int j=0;j<ncol;++j)
mat(i,j) *= num;
"""
inline(code,['num','mat','nrow','ncol'],
type_converters = converters.blitz)
def prod(m, v):
#C++ version
nrows, ncolumns = m.shape
assert v.ndim==1 and ncolumns==v.shape[0],"Shape mismatch in prod"
res = np.zeros(nrows, float)
code = r"""
for (int i=0; i<nrows; i++)
{
for (int j=0; j<ncolumns; j++)
{
res(i) += m(i,j)*v(j);
}
}
"""
err = inline(code,['nrows', 'ncolumns', 'res', 'm', 'v'], verbose=2,
type_converters=converters.blitz)
return res
if __name__=='__main__':
print 'zz is all zeros'
zz = np.zeros([10,10])
print 'tr(zz)=',trace(zz)
print 'oo is all ones'
oo = np.ones([4,4],float)
print 'tr(oo)=',trace(oo)
print 'aa is random'
aa = np.random.rand(128,128)
print 'tr(aa)=',trace(aa)
print 'tr(aa)=',np.trace(aa),' (via numpy)'
print
print 'Modify oo in place:'
print 'oo:',oo
in_place_mult(3,oo)
print '3*oo:',oo
print
print 'Simple matrix-vector multiply'
nr,nc = 20,10
m = np.random.rand(nr,nc)
v = np.random.rand(nc)
mv = prod(m,v)
mvd = np.dot(m,v)
print 'Mat*vec error:',np.linalg.norm(mv-mvd)
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.