map#
- ivy.map(fn, constant=None, unique=None, mean=False)[source]#
Apply a function on each item of an iterable x.
- Parameters:
fn (
Callable
) – The function to map onto x.constant (
Optional
[Dict
[str
,Any
]], default:None
) – keyword arguments which remain constant between each function call. Default isNone
.unique (
Optional
[Dict
[str
,Iterable
[Any
]]], default:None
) – keyword arguments which are unique for each function call. Default isNone
.mean (
bool
, default:False
) – Whether to compute the mean across the return values, and return this mean. Default isFalse
.
- Return type:
List
- Returns:
ret – x following the application of fn to each of its iterated items.
Examples
With
int
inputs:>>> def special_square(x : float) -> float : return np.square(x) >>> results = ivy.map(fn = special_square, ... constant = None, ... unique = {'x' : [1,2,3]}, ... mean = False) >>> print(results) [1, 4, 9]
>>> results = ivy.map(fn = special_square, ... constant = None, ... unique = {'x':[0,1,2]}, ... mean = True) >>> print(results) 1.6666666666666667
>>> def special_pow(x:float,y:float) ->float : return np.power(x,y) >>> results = ivy.map(fn = special_pow, ... constant = {'y':[0,1]}, ... unique = {'x':[1,2,3]}, ... mean = False) >>> print(results) [array([1,1]), array([1,2]), array([1,3])]
>>> results = ivy.map(fn = special_pow, ... constant = {'y':[0,1]}, ... unique = {'x':[1,2,3]}, ... mean = True) >>> print(results) [1. 2.]
With float inputs:
>>> def linear_model(w:float, x:float, b:float) -> float: return w*x + b >>> results = ivy.map(fn = linear_model, ... constant = {'w':10., 'b':1.}, ... unique = {'x':[0.,1.,2.]}, ... mean = False) >>> print(results) [1.0, 11.0, 21.0]
With
ivy.Array
inputs:>>> results = ivy.map(fn = linear_model, ... constant = {'w':ivy.array([1.,0.,1.]), 'b':ivy.array([0.,10.,100.])}, ... unique = {'x':[ivy.array([0.,1.,0.]), ivy.array([1.,1.,1.])]}, ... mean = False) >>> print(results) [ivy.array([0., 10., 100.]), ivy.array([1., 10., 101.])]
>>> results = ivy.map(fn = linear_model, ... constant = {'w':ivy.array([1.,0.,1.]), 'b':ivy.array([0.,10.,100.])}, ... unique = {'x':[ivy.array([0.,1.,0.]), ivy.array([1.,1.,1.])]}, ... mean = True) >>> print(results) ivy.array([ 0.5, 10. , 100. ])