discretize.utils.ndgrid

discretize.utils.ndgrid(*args, vector=True, order='F')[source]

Generate gridded locations for 1D, 2D, or 3D tensors.

For 1D, 2D, or 3D tensors, this function takes the unique positions defining a tensor along each of its axis and returns the gridded locations. For 2D and 3D meshes, the user may treat the unique x, y (and z) positions a successive positional arguments or as a single argument using a list [x, y, (z)].

For outputs, let dim be the number of dimension (1, 2 or 3) and let n be the total number of gridded locations. The gridded x, y (and z) locations can be return as a single numpy array of shape [n, ndim]. The user can also return the gridded x, y (and z) locations as a list of length ndim. The list contains entries contain the x, y (and z) locations as tensors. See examples.

Parameters
*args(n, dim) numpy.ndarray or (dim) list of (n) numpy.ndarray

Positions along each axis of the tensor. The user can define these as successive positional arguments x, y, (and z) or as a single argument using a list [x, y, (z)].

vectorbool, optional

If True, the output is a numpy array of dimension [n, ndim]. If False, the gridded x, y (and z) locations are returned as separate ndarrays in a list. Default is True.

order{‘F’, ‘C’, ‘A’}

Define ordering using one of the following options: ‘C’ is C-like ordering, ‘F’ is Fortran-like ordering, ‘A’ is Fortran ordering if memory is contigious and C-like otherwise. Default = ‘F’. See numpy.reshape() for more on this argument.

Returns
numpy.ndarray or list of numpy.ndarray

If vector = True the gridded x, y, (and z) locations are returned as a numpy array of shape [n, ndim]. If vector = False, the gridded x, y, (and z) are returned as a list of vectors.

Examples

>>> from discretize.utils import ndgrid
>>> import numpy as np
>>> x = np.array([1, 2, 3])
>>> y = np.array([2, 4])
>>> ndgrid([x, y])
array([[1, 2],
       [2, 2],
       [3, 2],
       [1, 4],
       [2, 4],
       [3, 4]])
>>> ndgrid(x, y, order='C')
array([[1, 2],
       [1, 4],
       [2, 2],
       [2, 4],
       [3, 2],
       [3, 4]])
>>> ndgrid(x, y, vector=False)
[array([[1, 1],
       [2, 2],
       [3, 3]]), array([[2, 4],
       [2, 4],
       [2, 4]])]