Arithmetic operations in NumPy

Arithmetic operations in NumPy 


add(x1, x2, /[, out, where, casting, order, ...])

Add arguments element-wise.

reciprocal(x, /[, out, where, casting, ...])

Return the reciprocal of the argument, element-wise.

positive(x, /[, out, where, casting, order, ...])

Numerical positive, element-wise.

negative(x, /[, out, where, casting, order, ...])

Numerical negative, element-wise.

multiply(x1, x2, /[, out, where, casting, ...])

Multiply arguments element-wise.

divide(x1, x2, /[, out, where, casting, ...])

Returns a true division of the inputs, element-wise.

power(x1, x2, /[, out, where, casting, ...])

First array elements raised to powers from second array, element-wise.

subtract(x1, x2, /[, out, where, casting, ...])

Subtract arguments, element-wise.

true_divide(x1, x2, /[, out, where, ...])

Returns a true division of the inputs, element-wise.

floor_divide(x1, x2, /[, out, where, ...])

Return the largest integer smaller or equal to the division of the inputs.

float_power(x1, x2, /[, out, where, ...])

First array elements raised to powers from second array, element-wise.

mod(x1, x2, /[, out, where, casting, order, ...])

Returns the element-wise remainder of division.

modf(x[, out1, out2], / [[, out, where, ...])

Return the fractional and integral parts of an array, element-wise.

remainder(x1, x2, /[, out, where, casting, ...])

Returns the element-wise remainder of division.

divmod(x1, x2[, out1, out2], / [[, out, ...])

Return element-wise quotient and remainder simultaneously.


numpy.add

numpy.add(x1x2) 

  • The arrays to be added.
  • Equivalent to x1 + x2 in terms of array broadcasting.

Examples

>>> np.add(1.0, 4.0)
5.0
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> np.add(x1, x2)
array([[  0.,   2.,   4.],
       [  3.,   5.,   7.],
       [  6.,   8.,  10.]])

The + operator can be used as a shorthand for np.add on ndarrays.

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> x1 + x2
array([[ 0.,  2.,  4.],
       [ 3.,  5.,  7.],
       [ 6.,  8., 10.]])

 

numpy.reciprocal

numpy.reciprocal(x)

  • Return the reciprocal of the argument (Calculates 1/x), element-wise..
  • Note
  • This function is not designed to work with integers.

Examples

>>> np.reciprocal(2.)
0.5
>>> np.reciprocal([1, 2., 3.33])
array([ 1.       ,  0.5      ,  0.3003003])

 

numpy.positive

numpy.positive(x/)

  • Numerical positive, element-wise.

  • Notes
  • Equivalent to x.copy(), but only defined for types that support arithmetic.

Examples

>>> x1 = np.array(([1., -1.]))
>>> np.positive(x1)
array([ 1., -1.])

The unary + operator can be used as a shorthand for np.positive on ndarrays.

>>> x1 = np.array(([1., -1.]))
>>> +x1
array([ 1., -1.])

 

numpy.negative

numpy.negative(x/)

  • Numerical negative, element-wise.

Examples

>>> np.negative([1.,-1.])
array([-1.,  1.])

  • The unary - operator can be used as a shorthand for np.negative on ndarrays.

>>> x1 = np.array(([1., -1.]))
>>> -x1
array([-1.,  1.])

 

numpy.multiply

numpy.multiply(x1x2)

  • Multiply arguments element-wise.

Notes

Equivalent to x1 * x2 in terms of array broadcasting.

Examples

>>> np.multiply(2.0, 4.0)
8.0
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> np.multiply(x1, x2)
array([[  0.,   1.,   4.],
       [  0.,   4.,  10.],
       [  0.,   7.,  16.]])

  • The * operator can be used as a shorthand for np.multiply on ndarrays.

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> x1 * x2
array([[  0.,   1.,   4.],
       [  0.,   4.,  10.],
       [  0.,   7.,  16.]])

 

numpy.divide

numpy.divide(x1x2)

  • Returns a true division of the inputs, element-wise.

Notes

  • In Python, // is the floor division operator and / the true division operator. 
  • The true_divide(x1, x2) function is equivalent to true division in Python.

Examples

>>> x = np.arange(5)
>>> np.true_divide(x, 4)
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])
>>> x/4
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])
>>> x//4
array([0, 0, 0, 0, 1])

  • The / operator can be used as a shorthand for np.true_divide on ndarrays.

>>> x = np.arange(5)
>>> x / 4
array([0.  , 0.25, 0.5 , 0.75, 1.  ])

 

numpy.power

numpy.power(x1x2)

  • First array elements raised to powers from second array, element-wise.
  • An integer type raised to a negative integer power will raise a ValueError.
  • Negative values raised to a non-integral value will return .
  •  
  • float_power
  • power function that promotes integers to float
  • Examples
  • Cube each element in an array.

>>> x1 = np.arange(6)
>>> x1
[0, 1, 2, 3, 4, 5]
>>> np.power(x1, 3)
array([  0,   1,   8,  27,  64, 125])

  • Raise the bases to different exponents.

>>> x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]
>>> np.power(x1, x2)
array([  0.,   1.,   8.,  27.,  16.,   5.])

The effect of broadcasting.

>>> x2 = np.array([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]])
>>> x2
array([[1, 2, 3, 3, 2, 1],
       [1, 2, 3, 3, 2, 1]])
>>> np.power(x1, x2)
array([[ 0,  1,  8, 27, 16,  5],
       [ 0,  1,  8, 27, 16,  5]])

  • The ** operator can be used as a shorthand for np.power on ndarrays.

>>> x2 = np.array([1, 2, 3, 3, 2, 1])
>>> x1 = np.arange(6)
>>> x1 ** x2
array([ 0,  1,  8, 27, 16,  5])

  • Negative values raised to a non-integral value will result in nan (and a warning will be generated).

>>> x3 = np.array([-1.0, -4.0])
>>> with np.errstate(invalid='ignore'):
...     p = np.power(x3, 1.5)
...
>>> p
array([nan, nan])

To get complex results, give the argument dtype=complex.

>>> np.power(x3, 1.5, dtype=complex)
array([-1.83697020e-16-1.j, -1.46957616e-15-8.j])

 

numpy.subtract

numpy.subtract(x1x2)

  • Subtract arguments, element-wise.

Notes

Equivalent to x1 - x2 in terms of array broadcasting.

Examples

>>> np.subtract(1.0, 4.0)
-3.0
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> np.subtract(x1, x2)
array([[ 0.,  0.,  0.],
       [ 3.,  3.,  3.],
       [ 6.,  6.,  6.]])

  • The - operator can be used as a shorthand for np.subtract on ndarrays.

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> x1 - x2
array([[0., 0., 0.],
       [3., 3., 3.],
       [6., 6., 6.]])

 

numpy.true_divide

numpy.true_divide(x1x2)

  • Returns a true division of the inputs, element-wise.
  • Unlike ‘floor division’, true division adjusts the output type to present the best answer, regardless of input types.

Notes

In Python, // is the floor division operator and / the true division operator. The true_divide(x1, x2) function is equivalent to true division in Python.

Examples

>>> x = np.arange(5)
>>> np.true_divide(x, 4)
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])
>>> x/4
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])
>>> x//4
array([0, 0, 0, 0, 1])

  • The / operator can be used as a shorthand for np.true_divide on ndarrays.

>>> x = np.arange(5)
>>> x / 4
array([0.  , 0.25, 0.5 , 0.75, 1.  ])

 

numpy.floor_divide

numpy.floor_divide(x1x2)

  • Return the largest integer smaller or equal to the division of the inputs.
  • It is equivalent to the Python // operator and pairs with the Python % (remainder), function so that a = a % b + b * (a // b) up to roundoff.

remainder

Remainder complementary to floor_divide.

divmod

Simultaneous floor division and remainder.

divide

Standard division.

floor

Round a number to the nearest integer toward minus infinity.

ceil

Round a number to the nearest integer toward infinity.

Examples

>>> np.floor_divide(7,3)
2
>>> np.floor_divide([1., 2., 3., 4.], 2.5)
array([ 0.,  0.,  1.,  1.])

The // operator can be used as a shorthand for np.floor_divide on ndarrays.

>>> x1 = np.array([1., 2., 3., 4.])
>>> x1 // 2.5
array([0., 0., 1., 1.])

 

numpy.float_power

numpy.float_power(x1x2)

  • First array elements raised to powers from second array, element-wise.
  • Raise each base in x1 to the positionally-corresponding power in x2x1 and x2 must be broadcastable to the same shape.
  •  This differs from the power function in those integers, float16, and float32 which are promoted to floats with a minimum precision of float64 so that the result is always inexact. 
  • The intent is that the function will return a usable result for negative powers and seldom overflow for positive powers.
  • Negative values raised to a non-integral value will return nan. To get complex results, cast the input to complex, or specify the dtype to be complex.

power

power function that preserves type

Examples

Cube each element in a list.

>>> x1 = range(6)
>>> x1
[0, 1, 2, 3, 4, 5]
>>> np.float_power(x1, 3)
array([   0.,    1.,    8.,   27.,   64.,  125.])

Raise the bases to different exponents.

>>> x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]
>>> np.float_power(x1, x2)
array([  0.,   1.,   8.,  27.,  16.,   5.])

The effect of broadcasting.

>>> x2 = np.array([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]])
>>> x2
array([[1, 2, 3, 3, 2, 1],
       [1, 2, 3, 3, 2, 1]])
>>> np.float_power(x1, x2)
array([[  0.,   1.,   8.,  27.,  16.,   5.],
       [  0.,   1.,   8.,  27.,  16.,   5.]])

  • Negative values raised to a non-integral value will result in nan (and a warning will be generated).

>>> x3 = np.array([-1, -4])
>>> with np.errstate(invalid='ignore'):
...     p = np.float_power(x3, 1.5)
...
>>> p
array([nan, nan])

To get complex results, give the argument dtype=complex.

>>> np.float_power(x3, 1.5, dtype=complex)
array([-1.83697020e-16-1.j, -1.46957616e-15-8.j])

 

numpy.mod

numpy.mod(x1x2)

  • Returns the element-wise remainder of division.
  • Computes the remainder complementary to the floor_divide function. It is equivalent to the Python modulus operator``x1 % x2`` and has the same sign as the divisor x2. The MATLAB function equivalent to np.remainder is mod.

floor_divide

Equivalent of Python // operator.

divmod

Simultaneous floor division and remainder.

fmod

Equivalent of the MATLAB rem function.

dividefloor

Notes

Returns 0 when x2 is 0 and both x1 and x2 are (arrays of) integers. the mod is an alias of the remainder.

Examples

>>> np.remainder([4, 7], [2, 3])
array([0, 1])
>>> np.remainder(np.arange(7), 5)
array([0, 1, 2, 3, 4, 0, 1])

  • The % operator can be used as a shorthand for np.remainder on ndarrays.

>>> x1 = np.arange(7)
>>> x1 % 5
array([0, 1, 2, 3, 4, 0, 1])

 

numpy.remainder

numpy.remainder(x1x2)

  • Computes the remainder complementary to the floor_divide function. 
  • It is equivalent to the Python modulus operator``x1 % x2`` and has the same sign as the divisor x2.

Notes

Returns 0 when x2 is 0 and both x1 and x2 are (arrays of) integers. the mod is an alias of the remainder.

Examples

>>> np.remainder([4, 7], [2, 3])
array([0, 1])
>>> np.remainder(np.arange(7), 5)
array([0, 1, 2, 3, 4, 0, 1])

  • The % operator can be used as a shorthand for np.remainder on ndarrays.

>>> x1 = np.arange(7)
>>> x1 % 5
array([0, 1, 2, 3, 4, 0, 1])

 

numpy.divmod

numpy.divmod(x1x2)

  • Return element-wise quotient and remainder simultaneously.
  • np.divmod(x, y) is equivalent to (x // y, x % y), but faster because it avoids redundant work. It is used to implement the Python built-in function divmod on NumPy arrays.

modf

  • Equivalent to divmod(x, 1) for positive x with the return values switched.

Examples

>>> np.divmod(np.arange(5), 3)
(array([0, 0, 0, 1, 1]), array([0, 1, 2, 0, 1]))

The divmod function can be used as a shorthand for np.divmod on ndarrays.

>>> x = np.arange(5)
>>> divmod(x, 3)
(array([0, 0, 0, 1, 1]), array([0, 1, 2, 0, 1]))

 

====================================================================

 


















Post a Comment

0 Comments