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
#!/usr/bin/env python

import numpy as n

print "Exercise (a)"
a = n.array ( [[1, 2, 3], [2, 6, 10],[1, 3, 5]] )
b = a.repeat(3,0)
b = b.repeat(2,1)
print a.size, b.size

print "Exercise (b)"
c = b[3:7,:2:6].copy()
print c.size,c

print "Exercise (c)"
c[:] = n.pi * 3/2.
print b,c

print "Exercise (d)"
d = n.logspace(n.log10(1),n.log10(2),100)
e = n.empty(100)
print len(d),len(e)
print d
print e

print "Exercise (e)"
f = n.resize(b,(4,13))
g = b.flatten()
print f
print g

print "Exercise (f)"
print "select:"
# select expects two arguments: conditions, choices.
# It has only one optional argument: the default value.
h = n.select([f<3, n.sin(f.astype(float))>.5],[f,n.sin(f.astype(float))])
print h

print "piecewise:"
# piecewise expects three arguments: the array, conditions, functions.
# It can handle additional arguments to pass to those funtions!
i = n.piecewise(f,[f<=3,f>1],[lambda (x): -x,lambda (x): 3*x])
print i

print "where:"
# where only requires a condition. Optionally it accepts return values
# for True and False. If no return values are specified indices of 
# elements fulfilling the condition is returned.
# Notice the behavior of where when condition is a sequence.
j = n.random.random(f.shape)
k = n.where([j<.2, j>.7],n.reshape(n.arange(f.size),f.shape),0)
print k