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

NumPy Mathematical Functions

NumPy Mathematical Functions

NumPy contains a large number of functions for various mathematical operations, including trigonometric functions, arithmetic functions, complex number processing functions, and more.

Trigonometric functions

NumPy provides standard trigonometric functions: sin(), cos(), tan()

Example

import

numpy

as

np

a

=

np

.

array

(

[

0

,

30

,

45

,

60

,

90

]

)

print

(

'

Sine values of different angles:

'

)

Convert to radians by multiplying by pi/180

print

(

np

.

sin

(

a

np

.

pi

/

180

)

)

print

(

'

\n

'

)

print

(

'

Cosine values of angles in the array:

'

)

print

(

np

.

cos

(

a

np

.

pi

/

180

)

)

print

(

'

\n

'

)

print

(

'

Tangent values of angles in the array:

'

)

print

(

np

.

tan

(

a

np

.

pi

/

180

)

)

The output is:

不同角度的正弦值:

[0.         0.5        0.70710678 0.8660254  1.        ]





Cosine values of angles in the array:

[1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01

 6.12323400e-17]





Tangent values of angles in the array:

[0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00

 1.63312394e+16]

The arcsin, arccos, and arctan functions return the inverse trigonometric functions of sin, cos, and tan for given angles.

The results of these functions can be converted from radians to degrees using the numpy.degrees() function.

Example

import

numpy

as

np

a

=

np

.

array

(

[

0

,

30

,

45

,

60

,

90

]

)

print

(

'

An array containing sine values:

'

)

sin

=

np

.

sin

(

a

np

.

pi

/

180

)

print

(

sin

)

print

(

'

\n

'

)

print

(

'

Compute the arcsine of the angles; the returned values are in radians:

'

)

inv

=

np

.

arcsin

(

sin

)

print

(

inv

)

print

(

'

\n

'

)

print

(

'

Check the result by converting to degrees:

'

)

print

(

np

.

degrees

(

inv

)

)

print

(

'

\n

'

)

print

(

'

The arccos and arctan functions behave similarly:

'

)

cos

=

np

.

cos

(

a

np

.

pi

/

180

)

print

(

cos

)

print

(

'

\n

'

)

print

(

'

Arccosine:

'

)

inv

=

np

.

arccos

(

cos

)

print

(

inv

)

print

(

'

\n

'

)

print

(

'

In degrees:

'

)

print

(

np

.

degrees

(

inv

)

)

print

(

'

\n

'

)

print

(

'

tan function:

'

)

tan

=

np

.

tan

(

a

np

.

pi

/

180

)

print

(

tan

)

print

(

'

\n

'

)

print

(

'

Arctangent:

'

)

inv

=

np

.

arctan

(

tan

)

print

(

inv

)

print

(

'

\n

'

)

print

(

'

In degrees:

'

)

print

(

np

.

degrees

(

inv

)

)

The output is:

含有正弦值的数组:

[0.         0.5        0.70710678 0.8660254  1.        ]





// Calculate the arcsin of the angle, return value in radians:

[0.         0.52359878 0.78539816 1.04719755 1.57079633]





// Check the results by converting to degrees:

[ 0. 30. 45. 60. 90.]





arccos and arctan functions behave similarly:

[1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01

 6.12323400e-17]





Inverse cosine:

[0.         0.52359878 0.78539816 1.04719755 1.57079633]





Degree unit:

[ 0. 30. 45. 60. 90.]





tan 函数:

[0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00

 1.63312394e+16]





Anyway, switch:

[0.         0.52359878 0.78539816 1.04719755 1.57079633]





Degree unit:

[ 0. 30. 45. 60. 90.]

Rounding Functions

The numpy.around() function returns the rounded value of a specified number.

numpy.around(a,decimals)

Parameter Description:

Example

import

numpy

as

np

a

=

np

.

array

(

[

1.0

,

5.55

,

123

,

0.567

,

25.532

]

)

print

(

'

Original array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

After rounding:

'

)

print

(

np

.

around

(

a

)

)

print

(

np

.

around

(

a

,

decimals

=

1

)

)

print

(

np

.

around

(

a

,

decimals

= -

1

)

)

The output is:

原数组:

[  1.      5.55  123.      0.567  25.532]





After rounding:

[  1.   6. 123.   1.  26.]

[  1.    5.6 123.    0.6  25.5]

[  0.  10. 120.   0.  30.]

numpy.floor()

numpy.floor() returns the largest integer less than or equal to the specified expression, i.e., rounding down.

Example

import

numpy

as

np

a

=

np

.

array

(

[

1.7

,

1.5

, -

0.2

,

0.6

,

10

]

)

print

(

'

The provided array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Modified array:

'

)

print

(

np

.

floor

(

a

)

)

The output is:

提供的数组:

[-1.7  1.5 -0.2  0.6 10. ]





Modified array:

[-2.  1. -1.  0. 10.]

numpy.ceil()

numpy.ceil() returns the smallest integer greater than or equal to the specified expression, i.e., rounding up.

Example

import

numpy

as

np

a

=

np

.

array

(

[

1.7

,

1.5

, -

0.2

,

0.6

,

10

]

)

print

(

'

The provided array:

'

)

print

(

a

)

print

(

'

\n

'

)

print

(

'

Modified array:

'

)

print

(

np

.

ceil

(

a

)

)

The output is:

提供的数组:

[-1.7  1.5 -0.2  0.6 10. ]





Modified array:

[-1.  2. -0.  1. 10.]