MCPcopy Index your code
hub / github.com/numpy/numpy / fromfunction

Function fromfunction

numpy/_core/numeric.py:1829–1897  ·  view source on GitHub ↗

Construct an array by executing a function over each coordinate. The resulting array therefore has a value ``fn(x, y, z)`` at coordinate ``(x, y, z)``. Parameters ---------- function : callable The function is called with N parameters, where N is the rank of

(function, shape, *, dtype=float, like=None, **kwargs)

Source from the content-addressed store, hash-verified

1827@finalize_array_function_like
1828@set_module('numpy')
1829def fromfunction(function, shape, *, dtype=float, like=None, **kwargs):
1830 """
1831 Construct an array by executing a function over each coordinate.
1832
1833 The resulting array therefore has a value ``fn(x, y, z)`` at
1834 coordinate ``(x, y, z)``.
1835
1836 Parameters
1837 ----------
1838 function : callable
1839 The function is called with N parameters, where N is the rank of
1840 `shape`. Each parameter represents the coordinates of the array
1841 varying along a specific axis. For example, if `shape`
1842 were ``(2, 2)``, then the parameters would be
1843 ``array([[0, 0], [1, 1]])`` and ``array([[0, 1], [0, 1]])``
1844 shape : (N,) tuple of ints
1845 Shape of the output array, which also determines the shape of
1846 the coordinate arrays passed to `function`.
1847 dtype : data-type, optional
1848 Data-type of the coordinate arrays passed to `function`.
1849 By default, `dtype` is float.
1850 ${ARRAY_FUNCTION_LIKE}
1851
1852 .. versionadded:: 1.20.0
1853
1854 Returns
1855 -------
1856 fromfunction : any
1857 The result of the call to `function` is passed back directly.
1858 Therefore the shape of `fromfunction` is completely determined by
1859 `function`. If `function` returns a scalar value, the shape of
1860 `fromfunction` would not match the `shape` parameter.
1861
1862 See Also
1863 --------
1864 indices, meshgrid
1865
1866 Notes
1867 -----
1868 Keywords other than `dtype` and `like` are passed to `function`.
1869
1870 Examples
1871 --------
1872 >>> import numpy as np
1873 >>> np.fromfunction(lambda i, j: i, (2, 2), dtype=np.float64)
1874 array([[0., 0.],
1875 [1., 1.]])
1876
1877 >>> np.fromfunction(lambda i, j: j, (2, 2), dtype=np.float64)
1878 array([[0., 1.],
1879 [0., 1.]])
1880
1881 >>> np.fromfunction(lambda i, j: i == j, (3, 3), dtype=np.int_)
1882 array([[ True, False, False],
1883 [False, True, False],
1884 [False, False, True]])
1885
1886 >>> np.fromfunction(lambda i, j: i + j, (3, 3), dtype=np.int_)

Callers

nothing calls this directly

Calls 1

indicesFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…