#!/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