MCPcopy Create free account
hub / github.com/numpy/numpy / fromfunction

Function fromfunction

numpy/core/numeric.py:1778–1845  ·  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

1776@set_array_function_like_doc
1777@set_module('numpy')
1778def fromfunction(function, shape, *, dtype=float, like=None, **kwargs):
1779 """
1780 Construct an array by executing a function over each coordinate.
1781
1782 The resulting array therefore has a value ``fn(x, y, z)`` at
1783 coordinate ``(x, y, z)``.
1784
1785 Parameters
1786 ----------
1787 function : callable
1788 The function is called with N parameters, where N is the rank of
1789 `shape`. Each parameter represents the coordinates of the array
1790 varying along a specific axis. For example, if `shape`
1791 were ``(2, 2)``, then the parameters would be
1792 ``array([[0, 0], [1, 1]])`` and ``array([[0, 1], [0, 1]])``
1793 shape : (N,) tuple of ints
1794 Shape of the output array, which also determines the shape of
1795 the coordinate arrays passed to `function`.
1796 dtype : data-type, optional
1797 Data-type of the coordinate arrays passed to `function`.
1798 By default, `dtype` is float.
1799 ${ARRAY_FUNCTION_LIKE}
1800
1801 .. versionadded:: 1.20.0
1802
1803 Returns
1804 -------
1805 fromfunction : any
1806 The result of the call to `function` is passed back directly.
1807 Therefore the shape of `fromfunction` is completely determined by
1808 `function`. If `function` returns a scalar value, the shape of
1809 `fromfunction` would not match the `shape` parameter.
1810
1811 See Also
1812 --------
1813 indices, meshgrid
1814
1815 Notes
1816 -----
1817 Keywords other than `dtype` and `like` are passed to `function`.
1818
1819 Examples
1820 --------
1821 >>> np.fromfunction(lambda i, j: i, (2, 2), dtype=float)
1822 array([[0., 0.],
1823 [1., 1.]])
1824
1825 >>> np.fromfunction(lambda i, j: j, (2, 2), dtype=float)
1826 array([[0., 1.],
1827 [0., 1.]])
1828
1829 >>> np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int)
1830 array([[ True, False, False],
1831 [False, True, False],
1832 [False, False, True]])
1833
1834 >>> np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int)
1835 array([[0, 1, 2],

Callers

nothing calls this directly

Calls 1

indicesFunction · 0.85

Tested by

no test coverage detected