stable_divide#
- ivy.stable_divide(numerator, denominator, /, *, min_denominator=None)[source]#
Divide the numerator by the denominator, with min denominator added to the denominator for numerical stability.
- Parameters:
numerator (
Union
[Number
,Array
,NativeArray
]) – The numerator of the division.denominator (
Union
[Number
,Array
,NativeArray
]) – The denominator of the division.min_denominator (
Optional
[Union
[Number
,Array
,NativeArray
]], default:None
) – The minimum denominator to use, use global ivy._MIN_DENOMINATOR (1e-12) by default.
- Return type:
Union
[Number
,Array
]- Returns:
ret – The new item following the numerically stable division.
Examples
With
int
input:>>> x = ivy.stable_divide(1, 2) >>> print(x) 0.49999999999975
>>> x = ivy.stable_divide(1, 4, min_denominator=1) >>> print(x) 0.2
With float input:
>>> x = ivy.stable_divide(5.0, 3.33) >>> print(x) 1.5015015015010504
With
complex
input:>>> x = ivy.stable_divide(1+1j, 1-1j) >>> print(x) (5.000444502911705e-13+0.9999999999995j)
With
ivy.Array
input:>>> x = ivy.asarray([[10., 20., 30.], ... [40., 50., 60.]]) >>> y = ivy.stable_divide(x, 10.) >>> print(y) ivy.array([[1., 2., 3.], [4., 5., 6.]])
>>> x = ivy.asarray([1,2,3]) >>> y = np.array((1., 3., 5.)) >>> z = ivy.stable_divide(x, y) >>> print(z) ivy.array([1. , 0.667, 0.6 ])
>>> x = ivy.asarray([1., 2., 4.]) >>> y = ivy.asarray([1., 0.5, 0.25]) >>> z = ivy.asarray([0.01, 0.02, 0.03]) >>> w = ivy.stable_divide(x, y, min_denominator=z) >>> print(w) ivy.array([ 0.99, 3.85, 14.3 ])
With
ivy.Container
input:>>> x = ivy.Container(a=ivy.asarray([10., 15.]), b=ivy.asarray([20., 25.])) >>> y = ivy.stable_divide(x, 0.5) >>> print(y) { a: ivy.array([20., 30.]), b: ivy.array([40., 50.]) }
>>> x = ivy.Container(a=ivy.asarray([1., 2.]), b=ivy.asarray([3., 4.])) >>> y = ivy.Container(a=ivy.asarray([0.5, 2.5]), b=ivy.asarray([3.5, 0.4])) >>> z = ivy.stable_divide(x, y) >>> print(z) { a: ivy.array([2., 0.8]), b: ivy.array([0.857, 10.]) }
- Array.stable_divide(self, denominator, /, *, min_denominator=None)[source]#
ivy.Array instance method variant of ivy.stable_divide. This method simply wraps the function, and so the docstring for ivy.stable_divide also applies to this method with minimal changes.
- Parameters:
self – input array, used as the numerator for division.
denominator (
Union
[Number
,Array
,NativeArray
,Container
]) – denominator for division.min_denominator (
Optional
[Union
[Number
,Array
,NativeArray
,Container
]], default:None
) – the minimum denominator to use, use global ivy._MIN_DENOMINATOR by default.
- Return type:
Array
- Returns:
ret – a numpy array containing the elements of numerator divided by the corresponding element of denominator
Examples
With
ivy.Array
instance method:>>> x = ivy.asarray([4., 5., 6.]) >>> y = x.stable_divide(2) >>> print(y) ivy.array([2., 2.5, 3.])
>>> x = ivy.asarray([4, 5, 6]) >>> y = x.stable_divide(4, min_denominator=1) >>> print(y) ivy.array([0.8, 1. , 1.2])
>>> x = ivy.asarray([[4., 5., 6.], [7., 8., 9.]]) >>> y = ivy.asarray([[1., 2., 3.], [2., 3., 4.]]) >>> z = x.stable_divide(y) >>> print(z) ivy.array([[4. , 2.5 , 2. ], [3.5 , 2.67, 2.25]])
- Container.stable_divide(self, denominator, /, *, min_denominator=None, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#
ivy.Container instance method variant of ivy.stable_divide. This method simply wraps the function, and so the docstring for ivy.stable_divide also applies to this method with minimal changes.
- Parameters:
self – input container.
denominator (
Union
[Number
,Array
,NativeArray
,Container
]) – Container of the denominators of the division.min_denominator (
Optional
[Union
[Number
,Array
,NativeArray
,Container
]], default:None
) – Container of the minimum denominator to use, use global ivy.min_denominator by default.key_chains (
Optional
[Union
[List
[str
],Dict
[str
,str
],Container
]], default:None
) – The key-chains to apply or not apply the method to. Default isNone
.to_apply (
Union
[bool
,Container
], default:True
) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default isTrue
.prune_unapplied (
Union
[bool
,Container
], default:False
) – Whether to prune key_chains for which the function was not applied. Default isFalse
.map_sequences (
Union
[bool
,Container
], default:False
) – Whether to also map method to sequences (lists, tuples). Default isFalse
.
- Return type:
Container
- Returns:
ret – a container of numpy arrays copying all the element of the container
self
. A container of elements containing the new items following the numerically stable division, usingself
as the numerator.
Examples
>>> x = ivy.Container(a=ivy.asarray([3., 6.]), b=ivy.asarray([9., 12.])) >>> y = x.stable_divide(5) >>> print(y) { a: ivy.array([0.6, 1.2]), b: ivy.array([1.8, 2.4]) }
>>> x = ivy.Container(a=ivy.asarray([[2., 4.], [6., 8.]]), ... b=ivy.asarray([[10., 12.], [14., 16.]])) >>> z = x.stable_divide(2, min_denominator=2) >>> print(z) { a: ivy.array([[0.5, 1.], [1.5, 2.]]), b: ivy.array([[2.5, 3.], [3.5, 4.]]) }
>>> x = ivy.Container(a=ivy.asarray([3., 6.]), b=ivy.asarray([9., 12.])) >>> y = ivy.Container(a=ivy.asarray([6., 9.]), b=ivy.asarray([12., 15.])) >>> z = x.stable_divide(y) >>> print(z) { a: ivy.array([0.5, 0.667]), b: ivy.array([0.75, 0.8]) }