if is_obj_array(field): ls = field.shape else: ls = () else: ls = log_shape(field) if ls != (): from pytools import indices_in_shape result = np.zeros(ls, dtype=object) for i in indices_in_shape(ls): result[i] = f(field[i]) return result else: return f(field) as_oarray_func = decorator(with_object_array_or_scalar) def with_object_array_or_scalar_n_args(f, *args): oarray_arg_indices = [] for i, arg in enumerate(args): if is_obj_array(arg): oarray_arg_indices.append(i) if not oarray_arg_indices: return f(*args) leading_oa_index = oarray_arg_indices[0] ls = log_shape(args[leading_oa_index]) if ls != ():
ls = field.shape else: ls = () else: ls = log_shape(field) if ls != (): from pytools import indices_in_shape result = np.zeros(ls, dtype=object) for i in indices_in_shape(ls): result[i] = f(field[i]) return result else: return f(field) as_oarray_func = decorator(with_object_array_or_scalar) def with_object_array_or_scalar_n_args(f, *args): oarray_arg_indices = [] for i, arg in enumerate(args): if is_obj_array(arg): oarray_arg_indices.append(i) if not oarray_arg_indices: return f(*args) leading_oa_index = oarray_arg_indices[0] ls = log_shape(args[leading_oa_index]) if ls != ():
.. note :: This function exists because :func:`numpy.vectorize` suffers from the same issue described under :func:`make_obj_array`. """ if isinstance(ary, np.ndarray) and ary.dtype.char == "O": result = np.empty_like(ary) for i in np.ndindex(ary.shape): result[i] = f(ary[i]) return result else: return f(ary) obj_array_vectorized = decorator(obj_array_vectorize) def rec_obj_array_vectorize(f, ary): """Apply the function *f* to all entries of the object array *ary*. Return an object array of the same shape consisting of the return values. If the elements of *ary* are further object arrays, recurse until non-object-arrays are found and then apply *f* to those entries. If *ary* is not an object array, return ``f(ary)``. .. note :: This function exists because :func:`numpy.vectorize` suffers from the same issue described under :func:`make_obj_array`.