pythonmathcot函数_NumPy数学函数介绍和用法

本文介绍了Numpy库中的数学函数,特别是三角函数,如sin, cos, tan及其逆函数arcsin, arccos, arctan。还探讨了舍入函数numpy.around, numpy.floor和numpy.ceil的使用。" 125853345,14834498,理解HTML基础模板及其元数据,"['HTML', '前端开发', 'Web开发', '文档结构', '元数据']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文概述

Numpy包含大量数学函数, 可用于执行各种数学运算。数学函数包括三角函数, 算术函数和用于处理复数的函数。让我们讨论一下数学函数。

三角函数

Numpy包含三角函数, 用于计算弧度中不同角度的正弦, 余弦和正切。

sin, cos和tan函数返回指定角度的三角比。考虑以下示例。

例子

import numpy as np

arr = np.array([0, 30, 60, 90, 120, 150, 180])

print("\nThe sin value of the angles", end = " ")

print(np.sin(arr * np.pi/180))

print("\nThe cosine value of the angles", end = " ")

print(np.cos(arr * np.pi/180))

print("\nThe tangent value of the angles", end = " ")

print(np.tan(arr * np.pi/180))

输出

The sin value of the angles

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

8.66025404e-01 5.00000000e-01 1.22464680e-16]

The cosine value of the angles

[ 1.00000000e+00 8.66025404e-01 5.00000000e-01 6.12323400e-17

-5.00000000e-01 -8.66025404e-01 -1.00000000e+00]

The tangent value of the angles [ 0.00000000e+00 5.77350269e-01 1.73205081e+00 1.63312394e+16

-1.73205081e+00 -5.77350269e-01 -1.22464680e-16]

另一方面, arcsin(), arccos()和arctan()函数返回指定角度的三角逆。

numpy.degrees()函数可用于验证这些三角函数的结果。考虑以下示例。

例子

import numpy as np

arr = np.array([0, 30, 60, 90])

print("printing the sin values of different angles")

sinval = np.sin(arr*np.pi/180)

print(sinval)

print("printing the inverse of the sin")

cosec = np.arcsin(sinval)

print(cosec)

print("printing the values in degrees")

print(np.degrees(cosec))

print("\nprinting the cos values of different angles")

cosval = np.cos(arr*np.pi/180)

print(cosval)

print("printing the inverse of the cos")

sec = np.arccos(cosval)

print(sec)

print("\nprinting the values in degrees")

print(np.degrees(sec))

print("\nprinting the tan values of different angles")

tanval = np.tan(arr*np.pi/180)

print(tanval)

print("printing the inverse of the tan")

cot = np.arctan(tanval)

print(cot)

print("\nprinting the values in degrees")

print(np.degrees(cot))

输出

printing the sin values of different angles

[0. 0.5 0.8660254 1. ]

printing the inverse of the sin

[0. 0.52359878 1.04719755 1.57079633]

printing the values in degrees

[ 0. 30. 60. 90.]

printing the cos values of different angles

[1.00000000e+00 8.66025404e-01 5.00000000e-01 6.12323400e-17]

printing the inverse of the cos

[0. 0.52359878 1.04719755 1.57079633]

printing the values in degrees

[ 0. 30. 60. 90.]

printing the tan values of different angles

[0.00000000e+00 5.77350269e-01 1.73205081e+00 1.63312394e+16]

printing the inverse of the tan

[0. 0.52359878 1.04719755 1.57079633]

printing the values in degrees

[ 0. 30. 60. 90.]

舍入函数

numpy提供了各种功能, 可用于截断十进制浮点数的值, 将其舍入为十进制数的特定精度。让我们讨论舍入函数。

numpy.around()函数

此函数返回一个十进制值, 四舍五入到所需的小数位。该函数的语法如下。

numpy.around(num, decimals)

它接受以下参数。

SN

Parameter

Description

1

num

它是输入数字。

2

decimals

它是要舍入到的小数位数。默认值为0。如果此值为负, 则小数点将移到左侧。

考虑以下示例。

例子

import numpy as np

arr = np.array([12.202, 90.23120, 123.020, 23.202])

print("printing the original array values:", end = " ")

print(arr)

print("Array values rounded off to 2 decimal position", np.around(arr, 2))

print("Array values rounded off to -1 decimal position", np.around(arr, -1))

输出

printing the original array values: [ 12.202 90.2312 123.02 23.202 ]

Array values rounded off to 2 decimal position [ 12.2 90.23 123.02 23.2 ]

Array values rounded off to -2 decimal position [ 10. 90. 120. 20.]

numpy.floor()函数

此函数用于返回输入数据的下限值, 该下限值是不大于输入值的最大整数。考虑以下示例。

例子

import numpy as np

arr = np.array([12.202, 90.23120, 123.020, 23.202])

print(np.floor(arr))

输出

[ 12. 90. 123. 23.]

numpy.ceil()函数

此函数用于返回数组值的上限值, 该值是大于数组元素的最小整数值。考虑以下示例。

例子

import numpy as np

arr = np.array([12.202, 90.23120, 123.020, 23.202])

print(np.ceil(arr))

输出

[ 13. 91. 124. 24.]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值