add#

ivy.add(x1, x2, /, *, alpha=None, out=None)[source]#

Calculate the sum for each element x1_i of the input array x1 with the respective element x2_i of the input array x2.

Special cases

For floating-point operands,

  • If either x1_i or x2_i is NaN, the result is NaN.

  • If x1_i is +infinity and x2_i is -infinity, the result is NaN.

  • If x1_i is -infinity and x2_i is +infinity, the result is NaN.

  • If x1_i is +infinity and x2_i is +infinity, the result is +infinity.

  • If x1_i is -infinity and x2_i is -infinity, the result is -infinity.

  • If x1_i is +infinity and x2_i is a finite number, the result is +infinity.

  • If x1_i is -infinity and x2_i is a finite number, the result is -infinity.

  • If x1_i is a finite number and x2_i is +infinity, the result is +infinity.

  • If x1_i is a finite number and x2_i is -infinity, the result is -infinity.

  • If x1_i is -0 and x2_i is -0, the result is -0.

  • If x1_i is -0 and x2_i is +0, the result is +0.

  • If x1_i is +0 and x2_i is -0, the result is +0.

  • If x1_i is +0 and x2_i is +0, the result is +0.

  • If x1_i is either +0 or -0 and x2_i is a nonzero finite number, the result is x2_i.

  • If x1_i is a nonzero finite number and x2_i is either +0 or -0, the result is x1_i.

  • If x1_i is a nonzero finite number and x2_i is -x1_i, the result is +0.

  • In the remaining cases, when neither infinity, +0, -0, nor a NaN is involved, and the operands have the same mathematical sign or have different magnitudes, the sum must be computed and rounded to the nearest representable value according to IEEE 754-2019 and a supported round mode. If the magnitude is too large to represent, the operation overflows and the result is an infinity of appropriate mathematical sign.

Note

Floating-point addition is a commutative operation, but not always associative.

For complex floating-point operands, addition is defined according to the following table. For real components a and c, and imaginary components b and d,

c

dj

c+dj

a

a + c

a + dj

(a+c) + dj

bj

c + bj

(b+d)j

c + (b+d)j

a+bj

(a+c) + bj

a + (b+d)j

(a+c) + (b+d)j

For complex floating-point operands, the real valued floating-point special cases must independently apply to the real and imaginary component operation involving real numbers as described in the above table. For example, let a = real(x1_i), c = real(x2_i), d = imag(x2_i), and - if a is -0, the real component of the result is -0. - Similarly, if b is +0 and d is -0, the imaginary component of the result is +0.

Hence, if z1 = a + bj = -0 + 0j and z2 = c + dj = -0 - 0j, then the result of z1 + z2 is -0 + 0j.

Parameters:
  • x1 (Union[float, Array, NativeArray]) – first input array. Should have a numeric data type.

  • x2 (Union[float, Array, NativeArray]) – second input array. Must be compatible with x1 (see broadcasting). Should have a numeric data type.

  • alpha (Optional[Union[int, float]], default: None) – optional scalar multiplier for x2.

  • 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 element-wise sums. The returned array must have a 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 inputs:

>>> x = ivy.array([1, 2, 3])
>>> y = ivy.array([4, 5, 6])
>>> z = ivy.add(x, y)
>>> print(z)
ivy.array([5, 7, 9])
>>> x = ivy.array([1, 2, 3])
>>> y = ivy.array([4, 5, 6])
>>> z = ivy.add(x, y, alpha=2)
>>> print(z)
ivy.array([9, 12, 15])
>>> x = ivy.array([[1.1, 2.3, -3.6]])
>>> y = ivy.array([[4.8], [5.2], [6.1]])
>>> z = ivy.zeros((3, 3))
>>> ivy.add(x, y, out=z)
>>> print(z)
ivy.array([[5.9, 7.1, 1.2],
           [6.3, 7.5, 1.6],
           [7.2, 8.4, 2.5]])
>>> x = ivy.array([[[1.1], [3.2], [-6.3]]])
>>> y = ivy.array([[8.4], [2.5], [1.6]])
>>> ivy.add(x, y, out=x)
>>> print(x)
ivy.array([[[9.5],
            [5.7],
            [-4.7]]])
Array.add(self, x2, /, *, alpha=None, out=None)[source]#

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

Parameters:
  • self (Array) – first input array. Should have a numeric data type.

  • x2 (Union[Array, NativeArray]) – second input array. Must be compatible with self (see broadcasting). Should have a numeric data type.

  • alpha (Optional[Union[int, float]], default: None) – optional scalar multiplier for x2.

  • 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 element-wise sums. The returned array must have a data type determined by type-promotion.

Examples

>>> x = ivy.array([1, 2, 3])
>>> y = ivy.array([4, 5, 6])
>>> z = x.add(y)
>>> print(z)
ivy.array([5, 7, 9])
>>> x = ivy.array([1, 2, 3])
>>> y = ivy.array([4, 5, 6])
>>> z = x.add(y, alpha=2)
>>> print(z)
ivy.array([9, 12, 15])
Container.add(self, x2, /, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, alpha=None, out=None)[source]#

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

Parameters:
  • self (Container) – first input container. Should have a numeric data type.

  • x2 (Union[Container, Array, NativeArray]) – second input array or container. Must be compatible with self (see broadcasting). Should have a numeric 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.

  • alpha (Optional[Union[int, float, Container]], default: None) – scalar multiplier for x2.

  • 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 element-wise sums. The returned container must have a data type determined by type-promotion.

Examples

>>> x = ivy.Container(a=ivy.array([1, 2, 3]),
...                   b=ivy.array([2, 3, 4]))
>>> y = ivy.Container(a=ivy.array([4, 5, 6]),
...                   b=ivy.array([5, 6, 7]))
>>> z = x.add(y)
>>> print(z)
{
    a: ivy.array([5, 7, 9]),
    b: ivy.array([7, 9, 11])
}
>>> z = x.add(y, alpha=3)
>>> print(z)
{
    a: ivy.array([13, 17, 21]),
    b: ivy.array([17, 21, 25])
}