12、numpy——数学函数
2024-10-11 10:58:57
NumPy 数学函数
NumPy 包含大量的各种数学运算的函数,包括三角函数,算术运算的函数,复数处理函数等。
1、三角函数
NumPy 提供了标准的三角函数:sin()、cos()、tan()。
import numpy as np
print(np.pi)
a = np.array([0, 30, 45, 60, 90])
print('不同角度的正弦值:')
# 通过乘 pi/180 转化为弧度
print(np.sin(a * np.pi / 180))
print('数组中角度的余弦值:')
print(np.cos(a * np.pi / 180))
print('数组中角度的正切值:')
print(np.tan(a * np.pi / 180))
执行结果:
3.141592653589793
不同角度的正弦值:
[0. 0.5 0.70710678 0.8660254 1. ]
数组中角度的余弦值:
[1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
6.12323400e-17]
数组中角度的正切值:
[0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
1.63312394e+16]
arcsin,arccos、arctan函数返回给定角度的sin,cos,tan的反三角函数
import numpy as np
a = np.array([0, 30, 45, 60, 90])
print('含有正弦值的数组:')
sin = np.sin(a * np.pi / 180)
print(sin) print('计算角度的反正弦,返回值以弧度为单位:')
inv = np.arcsin(sin)
print(inv) print('通过转化为角度制来检查结果:')
print(np.degrees(inv)) print('arccos 和 arctan 函数行为类似:')
cos = np.cos(a * np.pi / 180)
print(cos) print('反余弦:')
inv = np.arccos(cos)
print(inv) print('角度制单位:')
print(np.degrees(inv)) print('tan 函数:')
tan = np.tan(a * np.pi / 180)
print(tan) print('反正切:')
inv = np.arctan(tan)
print(inv) print('角度制单位:')
print(np.degrees(inv))
执行结果:
含有正弦值的数组:
[0. 0.5 0.70710678 0.8660254 1. ]
计算角度的反正弦,返回值以弧度为单位:
[0. 0.52359878 0.78539816 1.04719755 1.57079633]
通过转化为角度制来检查结果:
[ 0. 30. 45. 60. 90.]
arccos 和 arctan 函数行为类似:
[1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
6.12323400e-17]
反余弦:
[0. 0.52359878 0.78539816 1.04719755 1.57079633]
角度制单位:
[ 0. 30. 45. 60. 90.]
tan 函数:
[0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
1.63312394e+16]
反正切:
[0. 0.52359878 0.78539816 1.04719755 1.57079633]
角度制单位:
[ 0. 30. 45. 60. 90.]
2、舍入函数
2.1 numpy.around() 函数返回指定数字的四舍五入值。
numpy.around(a,decimals)
参数说明:
- a: 数组
- decimals: 舍入的小数位数。 默认值为0。 如果为负,整数将四舍五入到小数点左侧的位置
import numpy as np
a = np.array([1.0, 5.55, 123, 0.567, 25.532])
print('原数组:')
print(a)
print('舍入后:')
print(np.around(a))
print(np.around(a, decimals=1))
print(np.around(a, decimals=-1))
执行结果:
原数组:
[ 1. 5.55 123. 0.567 25.532]
舍入后:
[ 1. 6. 123. 1. 26.]
[ 1. 5.6 123. 0.6 25.5]
[ 0. 10. 120. 0. 30.]
2.2 numpy.floor()
numpy.floor() 返回数字的下舍整数。
import numpy as np a = np.array([-1.7, 1.5, -0.2, 0.6, 10])
print ('提供的数组:')
print (a)
print ('\n')
print ('修改后的数组:')
print (np.floor(a))
执行结果:
提供的数组:
[-1.7 1.5 -0.2 0.6 10. ] 修改后的数组:
[-2. 1. -1. 0. 10.]
2.3 numpy.ceil()
numpy.ceil() 返回数字的上入整数。
import numpy as np
a = np.array([-1.7, 1.5, -0.2, 0.6, 10])
print('提供的数组:')
print(a)
print('修改后的数组:')
print(np.ceil(a))
执行结果:
提供的数组:
[-1.7 1.5 -0.2 0.6 10. ]
修改后的数组:
[-1. 2. -0. 1. 10.]
最新文章
- 关于问题ld:library not found for -lXXX的错误
- VS2012减负:加快启动速度,减少编辑卡壳
- Building Redis for use on Cygwin(转)
- android简单的计算器
- .NET里的行为驱动开发
- JPA 系列教程18-自动把firstName+lastName合并为name字段
- js文件引用的问题顺带复习css引用
- git的使用 (一)
- 逆向工程-获得IPsearch的注册码
- 转 Visual C++6.0 与matlab联合编程(2)----Visual C++6.0 环境下编译和调试MEX文件
- Java swing皮肤(look and feel)大全
- input radio 与label文字对齐
- [转载]用NodeJS打造你的静态文件服务器
- Silverlight 之 断点调试
- DPDK架构与特点
- redis 学习记录
- localhost与127.0.0.1之间的区别
- SQL Server 笔试题总结
- Firefox 修改User Agent
- 并行编程(2) - sum.msic.Unsafe 二