sqrt#

ivy.sqrt(x, /, *, out=None)[source]#

Calculate the square root, having domain [0, +infinity] and codomain [0, +infinity], for each element x_i of the input array x. After rounding, each result must be indistinguishable from the infinitely precise result (as required by IEEE 754).

Note

After rounding, each result must be indistinguishable from the infinitely precise result (as required by IEEE 754).

Note

For complex floating-point operands, sqrt(conj(x)) must equal conj(sqrt(x)).

Note

By convention, the branch cut of the square root is the negative real axis \((-\infty, 0)\).

The square root is a continuous function from above the branch cut, taking into account the sign of the imaginary component.

Accordingly, for complex arguments, the function returns the square root in the range of the right half-plane, including the imaginary axis (i.e., the plane defined by \([0, +\infty)\) along the real axis and \((-\infty, +\infty)\) along the imaginary axis).

Special cases

For floating-point operands,

  • If x_i is NaN, the result is NaN.

  • If x_i is less than 0, the result is NaN.

  • If x_i is +0, the result is +0.

  • If x_i is -0, the result is -0.

  • If x_i is +infinity, the result is +infinity.

For complex floating-point operands, let a = real(x_i), b = imag(x_i), and

  • If a is either +0 or -0 and b is +0, the result is +0 + 0j.

  • If a is any value (including NaN) and b is +infinity, the result is +infinity + infinity j.

  • If a is a finite number and b is NaN, the result is NaN + NaN j.

  • If a -infinity and b is a positive (i.e., greater than 0) finite number, the result is NaN + NaN j.

  • If a is +infinity and b is a positive (i.e., greater than 0) finite number, the result is +0 + infinity j.

  • If a is -infinity and b is NaN, the result is NaN + infinity j (sign of the imaginary component is unspecified).

  • If a is +infinity and b is NaN, the result is +infinity + NaN j.

  • If a is NaN and b is any value, the result is NaN + NaN j.

  • If a is NaN and b is NaN, the result is NaN + NaN j.

Parameters:
  • x (Union[Array, NativeArray]) – input array. Should have a floating-point data type.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – an array containing the square root of each element in x. The returned array must have a floating-point data type determined by type-promotion.

This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.

Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts ivy.Container instances in place of any of the arguments

Examples

With ivy.Array input:

>>> x = ivy.array([0, 4., 8.])
>>> y = ivy.sqrt(x)
>>> print(y)
ivy.array([0., 2., 2.83])
>>> x = ivy.array([1, 2., 4.])
>>> y = ivy.zeros(3)
>>> ivy.sqrt(x, out=y)
ivy.array([1., 1.41, 2.])
>>> X = ivy.array([40., 24., 100.])
>>> ivy.sqrt(x, out=x)
>>> ivy.array([6.32455532, 4.89897949, 10.])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([44., 56., 169.]), b=ivy.array([[49.,1.], [0,20.]])) # noqa
>>> y = ivy.sqrt(x)
>>> print(y)
{
    a: ivy.array([6.63, 7.48, 13.]),
    b: ivy.array([[7., 1.],
                  [0., 4.47]])
}
Array.sqrt(self, *, out=None)[source]#

ivy.Array instance method variant of ivy.sqrt. This method simply wraps the function, and so the docstring for ivy.sqrt also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array. Should have a real-valued floating-point data type.

  • out (Optional[Array], default: None) – optional output, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – an array containing the square root of each element in self. The returned array must have a real-valued floating-point data type determined by type-promotion.

Examples

Using ivy.Array instance method:

>>> x = ivy.array([[1., 2.],  [3., 4.]])
>>> y = x.sqrt()
>>> print(y)
ivy.array([[1.  , 1.41],
           [1.73, 2.  ]])
Container.sqrt(self, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.sqrt. This method simply wraps the function, and so the docstring for ivy.sqrt also applies to this method with minimal changes.

Parameters:
  • self (Container) – input container. Should have a real-valued floating-point data type.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • out (Optional[Container], default: None) – optional output container, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

Returns:

ret – a container containing the square root of each element in self. The returned container must have a real-valued floating-point data type determined by type-promotion.

Examples

with ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0., 100., 27.]),
...                   b=ivy.native_array([93., 54., 25.]))
>>> y = x.sqrt()
>>> print(y)
{
    a: ivy.array([0., 10., 5.2]),
    b: ivy.array([9.64, 7.35, 5.])
}