ivy.gradient(x, /, *, spacing=1, edge_order=1, axis=None)[source]#

Calculate gradient of x with respect to (w.r.t.) spacing.

Parameters:
• x (`Union`[`Array`, `NativeArray`]) – input array representing outcomes of the function

• spacing (`Union`[`int`, `list`, `tuple`], default: `1`) – if not given, indices of x will be used if scalar indices of x will be scaled with this value if array gradient of x w.r.t. spacing

• edge_order (`int`, default: `1`) – 1 or 2, for ‘frist order’ and ‘second order’ estimation of boundary values of gradient respectively. Note: jax supports edge_order=1 case only

• axis (`Optional`[`Union`[`int`, `list`, `tuple`]], default: `None`) – dimension(s) to approximate the gradient over by default partial gradient is computed in every dimension

Return type:

`Union`[`Array`, `List`[`Array`]]

Returns:

ret – Array with values computed from gradient function from inputs

Examples

```>>> spacing = (ivy.array([-2., -1., 1., 4.]),)
>>> x = ivy.array([4., 1., 1., 16.], )
ivy.array([-3., -2.,  2.,  5.])
```
```>>> x = ivy.array([[1, 2, 4, 8], [10, 20, 40, 80]])
[ivy.array([[ 9., 18., 36., 72.],
[ 9., 18., 36., 72.]]), ivy.array([[ 1. ,  1.5,  3. ,  4. ],
[10. , 15. , 30. , 40. ]])]
```
```>>> x = ivy.array([[1, 2, 4, 8], [10, 20, 40, 80]])
[ivy.array([[ 4.5,  9. , 18. , 36. ],
[ 4.5,  9. , 18. , 36. ]]), ivy.array([[ 0.5 ,  0.75,  1.5 ,  2.  ],
[ 5.  ,  7.5 , 15.  , 20.  ]])]
```
```>>> x = ivy.array([[1, 2, 4, 8], [10, 20, 40, 80]])
ivy.array([[ 1. ,  1.5,  3. ,  4. ],
[10. , 15. , 30. , 40. ]])
```
```>>> x = ivy.array([[1, 2, 4, 8], [10, 20, 40, 80]])
[ivy.array([[ 3.,  6., 12., 24.],
[ 3.,  6., 12., 24.]]), ivy.array([[ 0.5 ,  0.75,  1.5 ,  2.  ],
[ 5.  ,  7.5 , 15.  , 20.  ]])]
```
```>>> spacing = (ivy.array([0, 2]), ivy.array([0, 3, 6, 9]))
[ivy.array([[ 4.5,  9. , 18. , 36. ],
[ 4.5,  9. , 18. , 36. ]]), ivy.array([[ 0.33333333, 0.5,  1., 1.33333333],
[ 3.33333333,  5.        , 10.        , 13.33333333]])]
```
Array.gradient(self, /, *, spacing=1, edge_order=1, axis=None)[source]#

Calculate gradient of x with respect to (w.r.t.) spacing.

Parameters:
• self (`Union`[`Array`, `NativeArray`]) – input array representing outcomes of the function

• spacing (`Union`[`int`, `list`, `tuple`], default: `1`) – if not given, indices of x will be used if scalar indices of x will be scaled with this value if array gradient of x w.r.t. spacing

• edge_order (`int`, default: `1`) – 1 or 2, for ‘first order’ and ‘second order’ estimation of boundary values of gradient respectively. Note: jax supports edge_order=1 case only

• axis (`Optional`[`Union`[`int`, `list`, `tuple`]], default: `None`) – dimension(s) to approximate the gradient over by default partial gradient is computed in every dimension

Return type:

`Union`[`Array`, `List`[`Array`]]

Returns:

ret – Array with values computed from gradient function from inputs

Examples

```>>> spacing = (ivy.array([-2., -1., 1., 4.]),)
>>> x = ivy.array([4., 1., 1., 16.], )
ivy.array([-3., -2.,  2.,  5.])
```
```>>> x = ivy.array([[1, 2, 4, 8], [10, 20, 40, 80]])
[ivy.array([[ 9., 18., 36., 72.],
[ 9., 18., 36., 72.]]), ivy.array([[ 1. ,  1.5,  3. ,  4. ],
[10. , 15. , 30. , 40. ]])]
```
```>>> x = ivy.array([[1, 2, 4, 8], [10, 20, 40, 80]])
[ivy.array([[ 4.5,  9. , 18. , 36. ],
[ 4.5,  9. , 18. , 36. ]]), ivy.array([[ 0.5 ,  0.75,  1.5 ,  2.  ],
[ 5.  ,  7.5 , 15.  , 20.  ]])]
```
```>>> x = ivy.array([[1, 2, 4, 8], [10, 20, 40, 80]])
ivy.array([[ 1. ,  1.5,  3. ,  4. ],
[10. , 15. , 30. , 40. ]])
```
```>>> x = ivy.array([[1, 2, 4, 8], [10, 20, 40, 80]])
[ivy.array([[ 3.,  6., 12., 24.],
[ 3.,  6., 12., 24.]]), ivy.array([[ 0.5 ,  0.75,  1.5 ,  2.  ],
[ 5.  ,  7.5 , 15.  , 20.  ]])]
```
```>>> spacing = (ivy.array([0, 2]), ivy.array([0, 3, 6, 9]))
[ivy.array([[ 4.5,  9. , 18. , 36. ],
[ 4.5,  9. , 18. , 36. ]]), ivy.array([[ 0.33333333,  0.5,  1., 1.33333333],
[ 3.33333333,  5.        , 10.        , 13.33333333]])]
```
Container.gradient(self, /, *, spacing=1, edge_order=1, axis=None)[source]#

Calculate gradient of x with respect to (w.r.t.) spacing.

Parameters:
• x – input array representing outcomes of the function spacing if not given, indices of x will be used if scalar indices of x will be scaled with this value if array gradient of x w.r.t. spacing

• edge_order (`Union`[`int`, `Container`], default: `1`) – 1 or 2, for ‘frist order’ and ‘second order’ estimation of boundary values of gradient respectively.

• axis (`Optional`[`Union`[`int`, `list`, `tuple`, `Container`]], default: `None`) – dimension(s) to approximate the gradient over. By default, partial gradient is computed in every dimension

Return type:

`Container`

Returns:

ret – Array with values computed from gradient function from inputs

Examples

```>>> coordinates = ivy.Container(
>>>     a=(ivy.array([-2., -1., 1., 4.]),),
>>>     b=(ivy.array([2., 1., -1., -4.]),)
>>> )
>>> values = ivy.Container(
>>>     a=ivy.array([4., 1., 1., 16.]),
>>>     b=ivy.array([4., 1., 1., 16.])
>>> )
{
a: ivy.array([-3., -2., 2., 5.]),
b: ivy.array([3., 2., -2., -5.])
}
```
```>>> values = ivy.Container(
>>>     a=ivy.array([[1, 2, 4, 8], [10, 20, 40, 80]]),
>>>     b=ivy.array([[-1, -2, -4, -8], [-10, -20, -40, -80]])
>>> )
[{
a: ivy.array([[9., 18., 36., 72.],
[9., 18., 36., 72.]]),
b: ivy.array([[-9., -18., -36., -72.],
[-9., -18., -36., -72.]])
}, {
a: ivy.array([[1., 1.5, 3., 4.],
[10., 15., 30., 40.]]),
b: ivy.array([[-1., -1.5, -3., -4.],
[-10., -15., -30., -40.]])
}]
```
```>>> values = ivy.Container(
>>>     a=ivy.array([[1, 2, 4, 8], [10, 20, 40, 80]]),
>>>     b=ivy.array([[-1, -2, -4, -8], [-10, -20, -40, -80]])
>>> )
[{
a: ivy.array([[4.5, 9., 18., 36.],
[4.5, 9., 18., 36.]]),
b: ivy.array([[-4.5, -9., -18., -36.],
[-4.5, -9., -18., -36.]])
}, {
a: ivy.array([[0.5, 0.75, 1.5, 2.],
[5., 7.5, 15., 20.]]),
b: ivy.array([[-0.5, -0.75, -1.5, -2.],
[-5., -7.5, -15., -20.]])
}]
```
```>>> values = ivy.Container(
>>>     a=ivy.array([[1, 2, 4, 8], [10, 20, 40, 80]]),
>>>     b=ivy.array([[-1, -2, -4, -8], [-10, -20, -40, -80]])
>>> )
{
a: ivy.array([[1., 1.5, 3., 4.],
[10., 15., 30., 40.]]),
b: ivy.array([[-1., -1.5, -3., -4.],
[-10., -15., -30., -40.]])
}
```
```>>> values = ivy.Container(
>>>     a=ivy.array([[1, 2, 4, 8], [10, 20, 40, 80]]),
>>>     b=ivy.array([[-1, -2, -4, -8], [-10, -20, -40, -80]])
>>> )
>>> ivy.gradient(values, spacing = [3., 2.])
[{
a: ivy.array([[3., 6., 12., 24.],
[3., 6., 12., 24.]]),
b: ivy.array([[-3., -6., -12., -24.],
[-3., -6., -12., -24.]])
}, {
a: ivy.array([[0.5, 0.75, 1.5, 2.],
[5., 7.5, 15., 20.]]),
b: ivy.array([[-0.5, -0.75, -1.5, -2.],
[-5., -7.5, -15., -20.]])
}]
```
```>>> coords = ivy.Container(
>>>    a=(ivy.array([0, 2]), ivy.array([0, 3, 6, 9])),
>>>    b=(ivy.array([0, -2]), ivy.array([0, -3, -6, -9]))
>>>)
>>> values = ivy.Container(
>>>     a=ivy.array([[1, 2, 4, 8], [10, 20, 40, 80]]),
>>>     b=ivy.array([[-1, -2, -4, -8], [-10, -20, -40, -80]])
>>>)