bundles / numpy latest / numpy / ma / core / fromfunction
function
numpy.ma.core:fromfunction
source: build-install/usr/lib/python3.14/site-packages/numpy/ma/core.py :8749
Signature
def fromfunction ( function , shape , * , dtype = <class 'float'> , like = None , ** kwargs ) Summary
Construct an array by executing a function over each coordinate.
Extended Summary
The resulting array therefore has a value fn(x, y, z) at coordinate (x, y, z).
Parameters
function: callableThe function is called with N parameters, where N is the rank of
shape. Each parameter represents the coordinates of the array varying along a specific axis. For example, ifshapewere(2, 2), then the parameters would bearray([[0, 0], [1, 1]])andarray([[0, 1], [0, 1]])shape: (N,) tuple of intsShape of the output array, which also determines the shape of the coordinate arrays passed to
function.dtype: data-type, optionalData-type of the coordinate arrays passed to
function. By default,dtypeis float.like: array_like, optionalReference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as
likesupports the__array_function__protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.
Returns
: fromfunction: MaskedArrayThe result of the call to
functionis passed back directly. Therefore the shape of fromfunction is completely determined byfunction. Iffunctionreturns a scalar value, the shape of fromfunction would not match theshapeparameter.
Notes
Keywords other than dtype and like are passed to function.
Examples
import numpy as np np.fromfunction(lambda i, j: i, (2, 2), dtype=float)
np.fromfunction(lambda i, j: j, (2, 2), dtype=float)
np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int)
np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int)
See also
Aliases
-
numpy.ma.fromfunction