具有多重不确定性和相关性的科学数字,高斯传播。
scinum的Python项目详细描述
scinum提供了一个简单的Number
类,该类包装了普通浮点数或NumPy数组,并增加了对多种不确定性、自动(高斯)误差传播和科学舍入的支持。
使用量
下面的示例演示了最常见的用例。有关详细信息,请参见API documentation或打开活页夹上的example.ipynb笔记本:
数字定义
fromscinumimportNumber,UP,DOWNnum=Number(5,(2,1))print(num)# -> 5.00 +2.00-1.00# get the nominal valueprint(num.nominal)# -> 5.0print(num.n)# -> 5.0 (shorthand)print(num())# -> 5.0 (shorthand)# get uncertaintiesprint(num.get_uncertainty())# -> (2.0, 1.0)print(num.u())# -> (2.0, 1.0) (shorthand)print(num.u(direction=UP))# -> 2.0# get shifted valuesprint(num.get())# -> 5.0 (no shift)print(num.get(UP))# -> 7.0 (up shift)print(num(UP))# -> 7.0 (up shift, shorthand)print(num.get(DOWN))# -> 4.0 (down shift)print(num(DOWN))# -> 4.0 (down shift, shorthand)
多重不确定性
fromscinumimportNumber,ABS,RELnum=Number(2.5,{"sourceA":0.5,# absolute 0.5, both up and down"sourceB":(1.0,1.5),# absolute 1.0 up, 1.5 down"sourceC":(REL,0.1),# relative 10%, both up and down"sourceD":(REL,0.1,0.2),# relative 10% up, 20% down"sourceE":(1.0,REL,0.2),# absolute 1.0 up, relative 20% down"sourceF":(REL,0.3,ABS,0.3)# relative 30% up, absolute 0.3 down})
格式化和舍入
Number.str()
提供一些简单的格式化工具,包括latex
和root latex
支持,以及科学的舍入规则:
# output formattingn=Number(8848,10)n.str(unit="m")# -> "8848.0 +- 10.0 m"n.str(unit="m",force_asymmetric=True)# -> "8848.0 +10.0-10.0 m"n.str(unit="m",scientific=True)# -> "8.848 +- 0.01 x 1E3 m"n.str(unit="m",si=True)# -> "8.848 +- 0.01 km"n.str(unit="m",style="latex")# -> "$8848.0 \pm 10.0\,m$"n.str(unit="m",style="latex",si=True)# -> "8.848 \pm 0.01\,km"n.str(unit="m",style="root")# -> "8848.0 #pm 10.0 m"n.str(unit="m",style="root",si=True)# -> "8.848 #pm 0.01 km"# output roundingn=Number(17.321,{"a":1.158,"b":0.453})n.str()# -> '17.321 +- 1.158 (a) +- 0.453 (b)'n.str("%.1f")# -> '17.3 +- 1.2 (a) +- 0.5 (b)'n.str("publication")# -> '17.32 +- 1.16 (a) +- 0.45 (b)'n.str("pdg")# -> '17.3 +- 1.2 (a) +- 0.5 (b)'
对于需要更复杂的舍入和格式规则的情况,您可能需要签出:
numpy数组
fromscinumimportNumber,ABS,RELimportnumpyasnpnum=Number(np.array([3,4,5]),2)print(num)# [ 3. 4. 5.]# + [ 2. 2. 2.]# - [ 2. 2. 2.]num=Number(np.array([3,4,5]),{"sourceA":(np.array([0.1,0.2,0.3]),REL,0.5)# absolute values for up, 50% down})print(num)# [ 3. 4. 5.]# + sourceA [ 0.1 0.2 0.3]# - sourceA [ 1.5 2. 2.5]
不确定度传播
fromscinumimportNumbernum=Number(5,1)print(num+2)# -> '7.0 +- 1.0'print(num*3)# -> '15.0 +- 3.0'num2=Number(2.5,1.5)print(num+num2)# -> '7.5 +- 1.80277563773'print(num*num2)# -> '12.5 +- 7.90569415042'# add num2 to num and consider their uncertainties to be fully correlated, i.e. rho = 1num.add(num2,rho=1)print(num)# -> '7.5 +- 2.5'
数学运算
作为math
模块的替换,scinum提供了一个对象ops
,该对象包含知道guassian错误传播的数学操作。
fromscinumimportNumber,opsnum=ops.log(Number(5,2))print(num)# -> 1.61 (+0.40, -0.40)num=ops.exp(ops.tan(Number(5,2)))print(num)# -> 0.03 (+0.85, -0.85)
自定义操作
在某些情况下,ops
对象中可能还没有包含特定的操作。在这种情况下,您可以通过:
fromscinumimportNumber,ops@ops.registerdefmy_op(x):returnx*2+1@my_op.derivedefmy_op(x):return2num=ops.my_op(Number(5,2))print(num)# -> 11.00 (+4.00, -4.00)
请注意,不需要像上面的特定示例那样注册simple函数,因为它们大多数只是传播规则(派生)已知的复合操作。
安装和依赖项
通过pip
pip install scinum
或者简单地将文件复制到项目中。
numpy是一个可选的依赖项。
贡献
如果你愿意贡献,我很高兴收到请求。只需确保添加一个新的测试用例并通过:
> python -m unittest tests
测试
一般来说,测试应该在不同的环境下运行:
- python 2.7版
- Python3.x(x≥5)
码头工人
要在Docker容器中运行测试,请执行:
git clone https://github.com/riga/scinum.git cd scinum docker run --rm -v `pwd`:/scinum -w /scinum python:3.6 python -m unittest tests
发展
- 源位于GitHub
- 在GitHub Issues上报告问题、问题和功能请求