Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

NumPy Arithmetic Functions

NumPy Arithmetic Functions

NumPy arithmetic functions include simple addition, subtraction, multiplication, and division: add(),subtract(),multiply() and divide()。

Note that arrays must have the same shape or comply with array broadcasting rules.

Example

import

numpy

as

np

a

=

np

.

arange

(

9

,

dtype

=

np

.

float_

)

.

reshape

(

3

,

3

)

print

(

'

The first array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

The second array:

'

)

b

=

np

.

array

(

[

10

,

10

,

10

]

)

print

(

b

)

print

(

'

\n

'

)

print

(

'

Adding two arrays:

'

)

print

(

np

.

add

(

a

,

b

)

)

print

(

'

\n

'

)

print

(

'

Subtracting two arrays:

'

)

print

(

np

.

subtract

(

a

,

b

)

)

print

(

'

\n

'

)

print

(

'

Multiplying two arrays:

'

)

print

(

np

.

multiply

(

a

,

b

)

)

print

(

'

\n

'

)

print

(

'

Dividing two arrays:

'

)

print

(

np

.

divide

(

a

,

b

)

)

The output is:

第一个数组:

[[0. 1. 2.]

 [3. 4. 5.]

 [6. 7. 8.]]





Second array:

[10 10 10]





Adding two arrays:

[[10. 11. 12.]

 [13. 14. 15.]

 [16. 17. 18.]]





Array subtraction:

[[-10.  -9.  -8.]

 [ -7.  -6.  -5.]

 [ -4.  -3.  -2.]]





Multiplying two arrays:

[[ 0. 10. 20.]

 [30. 40. 50.]

 [60. 70. 80.]]





Divide two arrays:

[[0.  0.1 0.2]

 [0.3 0.4 0.5]

 [0.6 0.7 0.8]]

In addition, Numpy also includes other important arithmetic functions.

numpy.reciprocal()

The numpy.reciprocal() function returns the element-wise reciprocal of the argument. For example, 1/4 the reciprocal is 4/1。

Example

import

numpy

as

np

a

=

np

.

array

(

[

0.25

,

1.33

,

1

,

100

]

)

print

(

'

Our array is:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Calling the reciprocal function:

'

)

print

(

np

.

reciprocal

(

a

)

)

The output is:

我们的数组是:

[  0.25   1.33   1.   100.  ]





Call the reciprocal function:

[4.        0.7518797 1.        0.01     ]

numpy.power()

The numpy.power() function takes the elements in the first input array as bases and computes their powers with the corresponding elements in the second input array.

Example

import

numpy

as

np

a

=

np

.

array

(

[

10

,

100

,

1000

]

)

print

(

'

Our array is;

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Calling the power function:

'

)

print

(

np

.

power

(

a

,

2

)

)

print

(

'

\n

'

)

print

(

'

The second array:

'

)

b

=

np

.

array

(

[

1

,

2

,

3

]

)

print

(

b

)

print

(

'

\n

'

)

print

(

'

Calling the power function again:

'

)

print

(

np

.

power

(

a

,

b

)

)

The output is:

我们的数组是;

[  10  100 1000]





Call the power function:

[    100   10000 1000000]





Second array:

[1 2 3]





Call the power function again:

[        10      10000 1000000000]

numpy.mod()

numpy.mod() computes the remainder after division of corresponding elements in the input arrays. The function numpy.remainder() also produces the same result.

Example

import

numpy

as

np

a

=

np

.

array

(

[

10

,

20

,

30

]

)

b

=

np

.

array

(

[

3

,

5

,

7

]

)

print

(

'

The first array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

The second array:

'

)

print

(

b

)

print

(

'

\n

'

)

print

(

'

Calling the mod() function:

'

)

print

(

np

.

mod

(

a

,

b

)

)

print

(

'

\n

'

)

print

(

'

Calling the remainder() function:

'

)

print

(

np

.

remainder

(

a

,

b

)

)

The output is:

第一个数组:

[10 20 30]





Second array:

[3 5 7]





Call the mod() function:

[1 0 2]





Call the remainder() function:

[1 0 2]