Numpy Math functions
Numpy 提供了丰富的数学函数,支持常用数学表达式,官方资料为:https://numpy.org/doc/1.17/reference/routines.math.html
主要分为Trigonometric functions, Hyperbolic, Rounding, sum等几个部分
Trigonometric functions
Function | Para | Description |
sin |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
正弦函数,其x可以为一个array,传递为角度值,其中2相当于360度 |
cos |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
余弦函数 |
tan |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
正切函数 |
arcsin |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
反正弦函数 |
arccos |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
反余弦函数 |
arctan |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
反正切函数 |
hypot |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'hypot'> |
计算直角三角形的斜长,等于sqrt(x1**2 + x2**2) |
arctan2 |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'hypot'> |
反正切函数,等于x1/x2,与arctan参数不一样 |
degrees |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
将pi格式的角度 转换成角度格式 |
radians |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
将角度格式转换成pi表示形式 |
unwrap | (p, discont=3.141592653589793, axis=-1) | 解卷绕 |
deg2rad |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
公式:x * pi / 180 |
rad2deg |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
公式:180 * x / pi |
Hyperbolic functions
双曲线相关函数
Function | Para | Description |
sinh |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
双曲正弦函数,表达式:
or |
cosh |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
双曲余弦函数,表达式:
or |
tanh |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
双曲正切函数,表达式:
or |
arcsinh |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
反双曲线函数,双曲正弦的反函数 |
arccosh |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
反双曲线函数,双曲余弦的反函数 |
arctanh |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
反双曲线函数,双曲正切的反函数 |
Rounding
四舍五入相关函数
Function | Para | Description |
around | (a, decimals=0, out=None)[ |
四舍五入函数,1.5和2.5之间值为2.0, -0.5到0.5之间的值为0 decimals决定了从哪位要四舍五入,默认为0,decimal大于0从小数点右边开始数 当decimal= 1是 1.35 为1.4 当decimal小于0 从小数点左边开始数 decimal =-1 , 18为20, 5为0 |
round_ | (a, decimals=0, out=None) | 与around功能一样 |
rint |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
只做小数四舍五入,整数不做 |
fix | (x, out=None) | 取整,丢弃小数部分 |
floor |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
取整,i <= x.不是直接丢弃小数部分,而是取小于x的最大整数,当-1.7时 值为-2, 1.7时值为1 |
ceil |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
取整 i >= x,取大于x的最小整数 |
trunc |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
取与x相比 距离0最近的整数, |
Sums, products, differences
Function | Para | Description |
prod | (a, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>) | 计算元素的乘积 |
sum | (a, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>) | 计算所有元素总和 |
nanprod | (a, axis=None, dtype=None, out=None, keepdims=<no value>) | 计算给指定轴上的数组元素集合,与prod不同的是,将非数字(NaN)作为1处理 |
nansum | (a, axis=None, dtype=None, out=None, keepdims=<no value>) | 求解元组和,与sum不同的是 ,非数字Nan作为0处理 |
cumprod | (a, axis=None, dtype=None, out=None) | 计算给定轴的累计积 |
cumsum | (a, axis=None, dtype=None, out=None) | 计算给定轴的累计和 |
nancumprod | (a, axis=None, dtype=None, out=None) | 计算给定轴的累计积,与cumprod不同的是,非数字Nan作为1处理 |
nancumsum | (a, axis=None, dtype=None, out=None) | 计算给定轴的累计和,与cumsum不同的是,非数字Nan被作为0处理 |
diff | (a, n=1, axis=-1, prepend=<no value>, append=<no value> | 计算前后差值 ,计算公式out[i] = a[i+1] - a[i] |
gradient | (f, *varargs, **kwargs) |
计算n维数的梯度,返回和原始数组同样大小的结果。 1维的数组,两个边界的元素直接用后一个减去前一个值,得到梯度,即b-a;对于中间的元素,取相邻两个元素差的一半,即(c-a)/2 |
cross | (a, b, axisa=-1, axisb=-1, axisc=-1, axis=None) |
两个数组的向量的叉乘 a=[x1, y1, z1] b =[x2,y2, z2] a*b =[y1z2-y2z1, -(x1z2-x2z1), x1y2-x2y1) |
trapz | (y, x=None, dx=1.0, axis=-1) | 使用复合梯形规则沿给定轴积分 |
需要复习的知识 向量,梯度
Exponents and logarithms
Function | Para | Description |
exp |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
以e为底部的指数函数 exp(x) |
expm1 |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
表达式为exp(x) -1 |
exp2 |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
计算以2为低的指数函数 |
log |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
以e为底部的对数函数 |
log10 |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
以10为底部的对数函数 |
log2 |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
以2为底部的对数函数 |
log1p |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
表达式为log(1+x) |
logaddexp |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
表达式为log(exp(x1) + exp(x2)) |
logaddexp2
|
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
表达式为log2(2**x1 + 2**x2) |
Other special functions
Function | Para | Description |
i0 | x | 0阶 修正贝塞尔函数 |
sinc | x | 表达式:sin(pi x)/(pi x) |
Floating point routines
Function | Para | Description |
signbit |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
符号标志位是否被设置,如果是负数,说明符号标志位被设置过返回true,如果为正数,说明符号标志位没有被设置过返回false, |
copysign |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
将x2的标志位符拷贝到x1,如 x1=1 x2=-3,则结果为-1 |
frexp |
(x, [out1, out2, ]/, [out=None, None), ]*, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
将x分解成尾数和指数形式 其中x = mantissa * 2**exponent mantissa尾数部分是在(-1,1)的开区间 exponent指数部分是以2为底 |
ldexp |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
与frexp公式相反,根据尾数和指数计算整数,返回结果为x1*2**x2 |
nextafter |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
返回x1后的下一个浮点值到x2 |
spaciing |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
返回x与最近邻数之间的距离 |
Rational routines
Function | Para | Description |
lcm |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
|x1| 和|x2|的最小公倍数 |
gcd |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
|x1| 和|x2|的最大公约数 |
Arithmetic operations
Function | Para | Description |
add |
(x1, x2, /, out=None, * , where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
两个元素相加 |
reciprocal |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
计算x的倒数, 公式1/x |
positive |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
逐元素正数计算:: y = +x,相当于不改变符号极性 |
negative |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
逐元素负数计算:y = -x.相当于改变符号极性 |
multiply |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
逐元素参数相乘, 相当于x1*x2 |
divide |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
逐元素参数相除, python3中//相除结果为地板除 /为除法 |
power |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
以x1为底数,x2为指数的 指数函数 |
subtract |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
逐元素相减, x1-x2 |
true_divide |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
x1/x2,与divide没发现区别 |
floor_divide |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
地板除,结果为 a = a % b + b * (a // b)
|
float_power |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
以x1为底部,x2为指数的对数函数,x1和x2的维度必须相同,与power相比,精度较高,支持浮点类型,int, float16,float32将会被提高到float64,以满足其最小精度,再次状况下,其计算结果是不准确的 |
fmod |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
除法的元素余数,与matlab 的rem函数功能相同,相当于c函数的fmod 与remainder区别为:remainder不带极性,都是正,而fmod带极性 |
mod |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
相当于x1%x2,与remainder一样 |
modf |
(x, [out1, out2, ]/, [out=None, None), ]*, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
返回其分数和整数,分数和整数部分都带极性 |
remainder |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
返回除法元素的余数,相当于x1%x2 |
divmod |
(x1, x2, [out1, out2, ]/, [out=None, None), ]*, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
与(x // y, x % y)相等 返回 两个结果 一个是x//y 一个是x%y |
Handling complex numbers
Function | Para | Description |
angle | (z, deg=False) | 返回复杂的角度 |
real | (val) | 返回复杂参数的实部 |
imag | (val) | 返回复杂参数的虚部 |
conj |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
以元素方式返回复共轭 |
conjugate |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
以元素方式返回复共轭 |
需要复习下复数
Miscellaneous
Function | Para | Description |
convolve | (a, v, mode='full' |
两个离散数组的线性卷积,其公式为: |
clip | (a, a_min, a_max, out=None, **kwargs) |
剪辑(限制)数组中的值 设在区间【0,1】,比0小的值将被变为0,比1大的值将变为1,将数组a的值剪贴为【a_min, a_max】之间的值 |
sqrt |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
逐个元素返回数组的正平方根 |
cbrt |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
逐个元素返回数组的立方根 |
square |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
返回输入的元素方块 |
absolute |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
逐个元素地计算绝对值 a + ib 等于sqrt(a**2 + b**2) |
fabs |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
x的绝对值 |
sign |
(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
如果 x<0, 则 结果为-1 如果x=0, 则结果为0 如果x > 0,则结果为1 |
heaviside |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
阶跃函数 if x1 <0 结果为0 if x1 == 0, 结果为x2 if x1 >0,结果为1 |
maximum |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
逐个比较x1与x2,返回最大的 |
minimum |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
逐个比较x1与x2,返回最小的 |
fmax |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
生成一个新的数组,返回其x1与x2最大值,与maximum不同的是,如果比较的是NnN,则返回非NaN,如果两个都是NaN,则第一个被返回 |
fmin |
(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) |
生成一个新的数字,返回其x1与x2最小值 ,与min不同的是,如果比较的是NnN,则返回非NaN,如果两个都是NaN,则第一个被返回 |
nan_to_num | (x, copy=True, nan=0.0, posinf=None, neginf=None) |
将NaN替换为0 将intf 替换成有限小数 在使用numpy数组的过程中经常会出现nan或者inf的元素,可能会造成数值计算时的一些错误。 |
real_if_close | (a, tol=100) |
如果是复杂输入,则 返回真实数组 如果是是复杂部件,则接近于零 |
interp | (x, xp, fp, left=None, right=None, period=None) | 一维线性插值 |
上述函数的相关练习在个人github:https://github.com/zhikunhuo/lpython
Numpy Math functions相关推荐
- iOS --- 数学函数 Math Functions
iOS_数学函数Math Functions floor/ceil/round 向下取整/向上取整/四舍五入 取整实例1: double d1 =10.12, d2 = 10.88; float f ...
- Numpy Mathematical functions 数学函数
https://docs.scipy.org/doc/numpy/reference/routines.math.html Trigonometric functions(三角函数) 函数 描述 si ...
- Python编程基础:第六节 math包的基础使用Math Functions
第六节 math包的基础使用 前言 实践 前言 我们通常会对数值型变量进行计算,这里我们给出一些常用的函数用于辅助你的计算过程.常用的数学计算函数均在math包. 实践 首先我们导入math包,并定义 ...
- Python 中的numpy 库
待总结 用scikit-learn和pandas学习线性回归 用scikit-learn和pandas学习Ridge回归 待整理的 Numpy & Pandas numpy--主要对其 N 维 ...
- tensorflow编程: Math
Arithmetic Operators tf.add.tf.subtract.tf.multiply.tf.scalar_mul.tf.div.tf.divide.tf.truediv.tf.flo ...
- 07.Numpy广播和ufunc
ufunc运算 ufunc 是 universal function 的缩写,它是一种能对数组的每个元素进行操作的函数.numpy 内置的许多 ufunc 函数都是在C语言级别实现的,因此它们的计算速 ...
- python科学计算笔记(一)NumPy中ndarray对象、ufunc运算、矩阵运算
标准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针.这样为了保存一个简单的[1,2,3],需要有3个指针和三个 ...
- 先进的NumPy数据科学
We will be covering some of the advanced concepts of NumPy specifically functions and methods requir ...
- [转载] python 中numpy快速去除nan, inf的方法
参考链接: Python中的numpy.isinf 参考文献Python快速转换numpy数组中Nan和Inf的方法 在使用numpy数组的过程中时常会出现nan或者inf的元素,可能会造成数值计算时 ...
最新文章
- mysql查询前10条记录
- python两行数据相加_python高手养成记08:教你快速写出自己的python程序
- 被问懵了,BeanFactory和FactoryBean接口的区别到底是什么?
- 2017.6.6 数表 思考记录
- 笨办法学 Python · 续 练习 44:使用 Python 的数据库 API
- 【TGRS】Ship Detection in Large-Scale SAR Images Via Spatial Shuffle-Group Enhance Attention译读笔记
- java日期去掉时分秒,只保留年月日
- ISO安装CoreOS
- 如何搭建简易的Wiki系统
- 【LaTeX入门】15、在文章中添加脚注
- js高级程序设计note
- html tooltips效果,html5tooltips.js – 一款轻量级的3D工具提示插件
- Godaddy上的域名如何取消自动续费?
- php实现增量更新,基于html5plus平台 实现app增量更新功能
- STM8S---电源功耗管理之停机模式(halt)实现
- 牛客练习赛91A~D
- 解读唐朝??雁门勤王,兵不厌诈
- 代码质量管理-Sonar 使用手册
- VSCode设置远程 ssh 时一直等待
- YOLO V3 一步步训练自己的目标检测
热门文章
- JEECG第17期架构培训班15号开班啦!每期十个名额,想报名的抓紧时间啦!
- springmvc+activiti 完美整合- 流程在线设计+代码生成器+UI快速开发库,提高一半的开发效率
- Easyui validatebox修改——1.当text发生变化时在校验,2.取消校验,3扩展自定义验证
- Linux基础命令---ab测试apache性能
- JavaScript函数式编程入门经典
- cobbler get-loaders 错误解决方法
- 常用sql 全记录(添加中)
- 企业级账号 版本更新总结
- LeetCode OJ:Combination Sum III(组合之和III)
- Oracle goldengate通用配置