import numpy def dist(size): """ title:: dist description:: This method will create a rectangular array (numpy.ndarray) in which each element represents the distance from the upper left position -or- in which each element is proportional to its frequency. This method is modeled directly after the IDL DIST function. attributes:: size a scalar or 2-element tuple/list representing the square dimension or the (rows, columns) of the array to be producedi, respectively. author:: Carl Salvaggio copyright:: Copyright (C) 2014, Rochester Institute of Technology license:: GPL version:: 1.0.0 disclaimer:: This source code is provided "as is" and without warranties as to performance or merchantability. The author and/or distributors of this source code may have made statements about this source code. Any such statements do not constitute warranties and shall not be relied on by the user in deciding whether to use this source code. This source code is provided without any express or implied warranties whatsoever. Because of the diversity of conditions and hardware under which this source code may be used, no warranty of fitness for a particular purpose is offered. The user is advised to test the source code thoroughly before relying on it. The user must assume the entire risk of using the source code. """ (rows, columns) = size if isinstance(size, (list, tuple)) else (size, size) x = numpy.arange(columns, dtype=numpy.float32) x = numpy.where(x < (columns-x), x**2, (columns-x)**2) a = numpy.zeros((rows, columns), dtype=numpy.float32) for i in range(rows/2+1): y = numpy.sqrt(x + i**2) a[i,:] = y if i != 0: a[rows-i,:] = y return a if __name__ == '__main__': r = 5 c = 3 distanceArray = dist((r, c)) print 'Distance array ...' print distanceArray print '' print 'Distance array (centered) ...' print numpy.roll(numpy.roll(distanceArray, r/2, axis=0), c/2, axis=1) s = 3 distanceArray = dist(s) print 'Distance array ...' print distanceArray print '' print 'Distance array (centered) ...' print numpy.roll(numpy.roll(distanceArray, s/2, axis=0), s/2, axis=1)