
from numpy import squeeze, asarray


def meshgrid_nd(arrays):
    """Like 'meshgrid()' but for arbitrary number of dimensions.
    """
    arrays = tuple(map(squeeze, arrays))

    if not len([ None for a in arrays if a.ndim != 1 ]) == 0:
        raise ValueError("Arrays must be 1-dimensional")

    # TODO: Handle one-element arrays!

    # The dimension
    D = len(arrays)

    # The number of elements in each array
    nelements = map(len, arrays)

    result = []

    for d, a in enumerate(arrays):
        # The new shape
        shape = D * [1]
        shape[d] = nelements[d]

        # Reshape
        A = asarray(a).reshape(shape)

        # And repeat as many times as necessary
        for ax, n in enumerate(nelements):
            if not ax == d:
                A = A.repeat(n, axis=ax)

        result.append(A)

    return tuple(result)


from numpy import squeeze, asarray, atleast_1d


def meshgrid_nd(arrays):
    """Like 'meshgrid()' but for arbitrary number of dimensions.

    :param arrays: A list of arrays to form the tensor grid.
                   All arrays have to be 1 oder 0 dimensional.
    :return: The full tensor product mesh grid.
    """
    arrays = map(squeeze, arrays)
    arrays = tuple(map(atleast_1d, arrays))

    if not len([ None for a in arrays if a.ndim != 1 ]) == 0:
        raise ValueError("Arrays must be 1-dimensional")

    # The dimension
    D = len(arrays)

    # The number of elements in each array
    nelements = map(len, arrays)

    result = []

    for d, a in enumerate(arrays):
        # The new shape
        shape = D * [1]
        shape[d] = nelements[d]

        # Reshape
        A = asarray(a).reshape(shape)

        # And repeat as many times as necessary
        for ax, n in enumerate(nelements):
            if not ax == d:
                A = A.repeat(n, axis=ax)

        result.append(A)

    return tuple(result)

Allow 0-dimensional arrays for tensor meshgrids
Allow 0-dimensional arrays for tensor meshgrids

  
- from numpy import squeeze, asarray
+ from numpy import squeeze, asarray, atleast_1d
  
  
  def meshgrid_nd(arrays):
      """Like 'meshgrid()' but for arbitrary number of dimensions.
+ 
+     :param arrays: A list of arrays to form the tensor grid.
+                    All arrays have to be 1 oder 0 dimensional.
+     :return: The full tensor product mesh grid.
      """
-     arrays = tuple(map(squeeze, arrays))
+     arrays = map(squeeze, arrays)
+     arrays = tuple(map(atleast_1d, arrays))
  
      if not len([ None for a in arrays if a.ndim != 1 ]) == 0:
          raise ValueError("Arrays must be 1-dimensional")
- 
-     # TODO: Handle one-element arrays!
  
      # The dimension
      D = len(arrays)
  
      # The number of elements in each array
      nelements = map(len, arrays)
  
      result = []
  
      for d, a in enumerate(arrays):
          # The new shape
          shape = D * [1]
          shape[d] = nelements[d]
  
          # Reshape
          A = asarray(a).reshape(shape)
  
          # And repeat as many times as necessary
          for ax, n in enumerate(nelements):
              if not ax == d:
                  A = A.repeat(n, axis=ax)
  
          result.append(A)
  
      return tuple(result)
  
Allow 0-dimensional arrays for tensor meshgrids
## Code Before:

from numpy import squeeze, asarray


def meshgrid_nd(arrays):
    """Like 'meshgrid()' but for arbitrary number of dimensions.
    """
    arrays = tuple(map(squeeze, arrays))

    if not len([ None for a in arrays if a.ndim != 1 ]) == 0:
        raise ValueError("Arrays must be 1-dimensional")

    # TODO: Handle one-element arrays!

    # The dimension
    D = len(arrays)

    # The number of elements in each array
    nelements = map(len, arrays)

    result = []

    for d, a in enumerate(arrays):
        # The new shape
        shape = D * [1]
        shape[d] = nelements[d]

        # Reshape
        A = asarray(a).reshape(shape)

        # And repeat as many times as necessary
        for ax, n in enumerate(nelements):
            if not ax == d:
                A = A.repeat(n, axis=ax)

        result.append(A)

    return tuple(result)

## Instruction:
Allow 0-dimensional arrays for tensor meshgrids
## Code After:

from numpy import squeeze, asarray, atleast_1d


def meshgrid_nd(arrays):
    """Like 'meshgrid()' but for arbitrary number of dimensions.

    :param arrays: A list of arrays to form the tensor grid.
                   All arrays have to be 1 oder 0 dimensional.
    :return: The full tensor product mesh grid.
    """
    arrays = map(squeeze, arrays)
    arrays = tuple(map(atleast_1d, arrays))

    if not len([ None for a in arrays if a.ndim != 1 ]) == 0:
        raise ValueError("Arrays must be 1-dimensional")

    # The dimension
    D = len(arrays)

    # The number of elements in each array
    nelements = map(len, arrays)

    result = []

    for d, a in enumerate(arrays):
        # The new shape
        shape = D * [1]
        shape[d] = nelements[d]

        # Reshape
        A = asarray(a).reshape(shape)

        # And repeat as many times as necessary
        for ax, n in enumerate(nelements):
            if not ax == d:
                A = A.repeat(n, axis=ax)

        result.append(A)

    return tuple(result)
