gridtools

quantecon.gridtools.cartesian(nodes, order='C')[source]

Cartesian product of a list of arrays

Parameters:
nodeslist(array_like(ndim=1))
orderstr, optional(default=’C’)

(‘C’ or ‘F’) order in which the product is enumerated

Returns:
outndarray(ndim=2)

each line corresponds to one point of the product space

quantecon.gridtools.cartesian_nearest_index(x, nodes, order='C')[source]

Return the index of the point closest to x within the cartesian product generated by nodes. Each array in nodes must be sorted in ascending order.

Parameters:
xarray_like(ndim=1 or 2)

Point(s) to search the closest point(s) for.

nodesarray_like(array_like(ndim=1))

Array of sorted arrays.

orderstr, optional(default=’C’)

(‘C’ or ‘F’) order in which the product is enumerated.

Returns:
scalar(int) or ndarray(int, ndim=1)

Index (indices) of the closest point(s) to x.

Examples

>>> nodes = (np.arange(3), np.arange(2))
>>> prod = qe.cartesian(nodes)
>>> print(prod)
[[0 0]
 [0 1]
 [1 0]
 [1 1]
 [2 0]
 [2 1]]

Among the 6 points in the cartesian product prod, the closest to the point (0.6, 0.4) is prod[2]:

>>> x = (0.6, 0.4)
>>> qe.cartesian_nearest_index(x, nodes)  # Pass `nodes`, not `prod`
2

The closest to (-0.1, 1.2) and (2, 0) are prod[1] and prod[4], respectively:

>>> x = [(-0.1, 1.2), (2, 0)]
>>> qe.cartesian_nearest_index(x, nodes)
array([1, 4])

Internally, the index in each dimension is searched by binary search and then the index in the cartesian product is calculated (not by constructing the cartesian product and then searching linearly over it).

quantecon.gridtools.mlinspace(a, b, nums, order='C')[source]

Constructs a regular cartesian grid

Parameters:
aarray_like(ndim=1)

lower bounds in each dimension

barray_like(ndim=1)

upper bounds in each dimension

numsarray_like(ndim=1)

number of nodes along each dimension

orderstr, optional(default=’C’)

(‘C’ or ‘F’) order in which the product is enumerated

Returns:
outndarray(ndim=2)

each line corresponds to one point of the product space

quantecon.gridtools.num_compositions(m, n)[source]

The total number of m-part compositions of n, which is equal to (n+m-1) choose (m-1).

Parameters:
mscalar(int)

Number of parts of composition.

nscalar(int)

Integer to decompose.

Returns:
scalar(int)

Total number of m-part compositions of n.

quantecon.gridtools.num_compositions_jit(m, n)[source]

Numba jit version of num_compositions. Return 0 if the outcome exceeds the maximum value of np.intp.

quantecon.gridtools.simplex_grid(m, n)[source]

Construct an array consisting of the integer points in the (m-1)-dimensional simplex \(\{x \mid x_0 + \cdots + x_{m-1} = n \}\), or equivalently, the m-part compositions of n, which are listed in lexicographic order. The total number of the points (hence the length of the output array) is L = (n+m-1)!/(n!*(m-1)!) (i.e., (n+m-1) choose (m-1)).

Parameters:
mscalar(int)

Dimension of each point. Must be a positive integer.

nscalar(int)

Number which the coordinates of each point sum to. Must be a nonnegative integer.

Returns:
outndarray(int, ndim=2)

Array of shape (L, m) containing the integer points in the simplex, aligned in lexicographic order.

Notes

A grid of the (m-1)-dimensional unit simplex with n subdivisions along each dimension can be obtained by simplex_grid(m, n) / n.

References

A. Nijenhuis and H. S. Wilf, Combinatorial Algorithms, Chapter 5, Academic Press, 1978.

Examples

>>> simplex_grid(3, 4)
array([[0, 0, 4],
       [0, 1, 3],
       [0, 2, 2],
       [0, 3, 1],
       [0, 4, 0],
       [1, 0, 3],
       [1, 1, 2],
       [1, 2, 1],
       [1, 3, 0],
       [2, 0, 2],
       [2, 1, 1],
       [2, 2, 0],
       [3, 0, 1],
       [3, 1, 0],
       [4, 0, 0]])
>>> simplex_grid(3, 4) / 4
array([[ 0.  ,  0.  ,  1.  ],
       [ 0.  ,  0.25,  0.75],
       [ 0.  ,  0.5 ,  0.5 ],
       [ 0.  ,  0.75,  0.25],
       [ 0.  ,  1.  ,  0.  ],
       [ 0.25,  0.  ,  0.75],
       [ 0.25,  0.25,  0.5 ],
       [ 0.25,  0.5 ,  0.25],
       [ 0.25,  0.75,  0.  ],
       [ 0.5 ,  0.  ,  0.5 ],
       [ 0.5 ,  0.25,  0.25],
       [ 0.5 ,  0.5 ,  0.  ],
       [ 0.75,  0.  ,  0.25],
       [ 0.75,  0.25,  0.  ],
       [ 1.  ,  0.  ,  0.  ]])
quantecon.gridtools.simplex_index(x, m, n)[source]

Return the index of the point x in the lexicographic order of the integer points of the (m-1)-dimensional simplex \(\{x \mid x_0 + \cdots + x_{m-1} = n\}\).

Parameters:
xarray_like(int, ndim=1)

Integer point in the simplex, i.e., an array of m nonnegative itegers that sum to n.

mscalar(int)

Dimension of each point. Must be a positive integer.

nscalar(int)

Number which the coordinates of each point sum to. Must be a nonnegative integer.

Returns:
idxscalar(int)

Index of x.