├── .gitignore ├── LICENSE ├── MANIFEST ├── README.md ├── analyticlab ├── __init__.py ├── amath.py ├── const.py ├── latexoutput.py ├── lookup │ ├── DixonTable.py │ ├── FTable.py │ ├── GrubbsTable.py │ ├── NairTable.py │ ├── Physics_tTable.py │ ├── RangeTable.py │ ├── SkewKuriTable.py │ ├── __init__.py │ ├── alphaToConf.py │ └── tTable.py ├── lsym.py ├── lsymitem.py ├── measure │ ├── ACategory.py │ ├── BCategory.py │ ├── __init__.py │ ├── basemeasure.py │ ├── ins.py │ ├── measure.py │ └── std.py ├── num.py ├── numitem.py ├── outlier.py ├── system │ ├── __init__.py │ ├── exceptions.py │ ├── format_units.py │ ├── numberformat.py │ ├── statformat.py │ ├── text_unicode.py │ └── unit_open.py └── twoitems.py ├── demo ├── 功能演示-LSymItem及LSym使用示例.ipynb ├── 功能演示-四种离群值检验.ipynb ├── 功能演示-数理统计功能.ipynb ├── 功能演示-相关系数与显著性检验.ipynb ├── 实例-拉伸法杨氏模量的测量.ipynb ├── 实例-液体粘滞系数.ipynb └── 性能测试与数值运算案例.py ├── setup.py └── tutorials ├── 第1章 数值运算.ipynb ├── 第2章 数理统计.ipynb ├── 第3章 离群值处理.ipynb ├── 第4章 符号表达式生成.ipynb ├── 第5章 不确定度计算.ipynb ├── 第6章 LaTeX公式集类.ipynb └── 第7章 Const常数类.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | whereToUpdate.md 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 xingrongtech 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MANIFEST: -------------------------------------------------------------------------------- 1 | # file GENERATED by distutils, do NOT edit 2 | setup.py 3 | analyticlab/__init__.py 4 | analyticlab/amath.py 5 | analyticlab/const.py 6 | analyticlab/latexoutput.py 7 | analyticlab/lsym.py 8 | analyticlab/lsymitem.py 9 | analyticlab/num.py 10 | analyticlab/numitem.py 11 | analyticlab/outlier.py 12 | analyticlab/twoitems.py 13 | analyticlab/lookup/DixonTable.py 14 | analyticlab/lookup/FTable.py 15 | analyticlab/lookup/GrubbsTable.py 16 | analyticlab/lookup/NairTable.py 17 | analyticlab/lookup/Physics_tTable.py 18 | analyticlab/lookup/RangeTable.py 19 | analyticlab/lookup/SkewKuriTable.py 20 | analyticlab/lookup/__init__.py 21 | analyticlab/lookup/alphaToConf.py 22 | analyticlab/lookup/tTable.py 23 | analyticlab/system/__init__.py 24 | analyticlab/system/anonymIds.py 25 | analyticlab/system/exceptions.py 26 | analyticlab/system/numberformat.py 27 | analyticlab/system/statformat.py 28 | analyticlab/system/unitTreat.py 29 | analyticlab/uncertainty/ACategory.py 30 | analyticlab/uncertainty/BCategory.py 31 | analyticlab/uncertainty/__init__.py 32 | analyticlab/uncertainty/ins.py 33 | analyticlab/uncertainty/measure.py 34 | analyticlab/uncertainty/std.py 35 | analyticlab/uncertainty/unc.py 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | analyticlab(分析实验室) 2 | ==== 3 | analyticlab是一个实验数据计算、分析和计算过程展示的Python库,可应用于大学物理实验、分析化学等实验类学科以及大创、工艺流程等科技类竞赛的数据处理。该库包含以下5个部分: 4 | * 数值运算(num、numitem模块):按照有效数字运算规则的数值运算。 5 | * 数理统计(numitem、twoitems模块):包括偏差、误差、置信区间、协方差、相关系数、单个样本的显著性检验、两个样本的显著性检验。 6 | * 离群值处理(outlier模块):包括Nair检验、Grubbs检验、Dixon检验和偏度-峰度检验。 7 | * 符号表达式合成(lsym、lsymitem模块):根据符号和关系式,得到LaTeX格式的计算式。 8 | * 测量及不确定度计算(measure包):根据实验数据或其他不确定度报告中的数据、测量仪器/方法和测量公式,得出测量结果,包含测量值和不确定度。 9 | 10 | 模块定义 11 | ---- 12 | 库中定义了9个类: 13 | * Num:分析数值运算类,位于num模块。 14 | * NumItem:分析数组类,位于numitem模块。 15 | * LSym:LaTeX符号生成类,位于lsym模块。 16 | * LSymItem:LaTeX符号组类,位于lsymitem模块。 17 | * Const:常数类,位于const模块。 18 | * LaTeX:公式集类,位于latexoutput模块。 19 | * measure.Ins:测量仪器类,位于measure.ins模块。 20 | * measure.BaseMeasure:基本测量类,位于measure.BaseMeasure模块。 21 | * measure.Measure:测量类,位于measure.Measure模块。 22 | 23 | 7个函数模块: 24 | * amath:对数值、符号、测量的求根、对数、三角函数运算。 25 | * twoitems:两组数据的数理统计。 26 | * outlier:离群值处理。 27 | * latexoutput:数学公式、表格、LaTeX符号和不确定度等的输出。 28 | * measure.std:计算标准偏差。 29 | * measure.ACategory:计算A类不确定度。 30 | * measure.BCategory:计算B类不确定度。 31 | 32 | 类的主要功能和绝大多数函数支持process(显示计算过程),通过在调用类方法或函数时,附加参数`process=True`实现。具体哪些类方法和函数支持process,可以查阅使用教程,或者通过help函数查询其说明文档。注意计算过程是以LaTeX格式输出的,因此计算过程展示功能只有在Jupyter Notebook环境下才能使用。如果只需要得到计算结果而不需要展示其过程,那么一般的Python3开发环境即可。 33 | 34 | 如何安装或更新 35 | ---- 36 | 1.通过pip安装: 37 | * `pip install analyticlab` 38 | 39 | 2.通过pip更新版本: 40 | * `pip install analyticlab --upgrade` 41 |
如果更新失败,可以尝试先卸载旧版本,再安装新版本: 42 | * `pip uninstall analyticlab` 43 | * `pip install analyticlab` 44 | 45 | 3.在pypi上下载analyticlab源代码并安装: 46 | * 打开网址https://pypi.python.org/pypi/analyticlab 47 | * 通过download下载tar.gz文件,解压到本地,通过cd指令切换到解压的文件夹内 48 | * 通过`python setup.py install`实现安装 49 | 50 | 运行环境 51 | ---- 52 | analyticlab只能在Python 3.x环境下运行,不支持Python 2.x环境。要求系统已安装numpy、scipy、sympy、quantities库。可以在绝大多数Python平台下运行,但计算过程只有在Jupyter Notebook环境下才能显示出来。 53 | 54 | 参照标准文件 55 | ---- 56 | * GBT 8170-2008 数值修约规则与极限数值的表示和判定 57 | * GBT 4883-2008 数据的统计处理和解释正态样本离群值的判断和处理 58 | * JJF1059.1-2012 测量不确定度评定与表示 59 | * CNAS-GL06 化学领域不确定度指南 60 | -------------------------------------------------------------------------------- /analyticlab/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Feb 18 09:25:00 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | 8 | from . import outlier, lookup, measure 9 | from .num import Num 10 | from .numitem import NumItem 11 | from .lsym import LSym 12 | from .lsymitem import LSymItem 13 | from .latexoutput import LaTeX, dispTable, dispLSym, dispLSymItem, dispMeasure, dispRelErr 14 | from .const import Const, PI, E, hPercent, t1e, ut1e 15 | from .amath import sqrt, ln, lg, sin, cos, tan, csc, sec, cot, arcsin, arccos, arctan, arccsc, arcsec, arccot 16 | from .twoitems import cov, corrCoef, sigDifference, linear_fit 17 | from .system.numberformat import f, fstr 18 | -------------------------------------------------------------------------------- /analyticlab/latexoutput.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Feb 5 08:41:19 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | 8 | import analyticlab 9 | from .system.exceptions import expressionInvalidException, processStateWrongException 10 | from IPython.display import display, Latex 11 | from .system.format_units import format_units_latex 12 | 13 | def latex_table(table): 14 | tex = r'\begin{array}{' + ('|'.join(['c']*len(table[0]))) + '}' 15 | for row in table: 16 | for j in range(len(row)): 17 | if type(row[j]) != str: 18 | if str(type(row[j])) == "": 19 | row[j] = row[j].latex() 20 | elif str(type(row[j])) == "": 21 | row[j] = ' & '.join([ci.latex() for ci in row[j]]) 22 | elif type(row[j]) == float: 23 | row[j] = '%g' % row[j] 24 | else: 25 | row[j] = str(row[j]) 26 | tex += r'\hline ' + (' & '.join([cell for cell in row])) + r'\\' 27 | tex += r'\hline\end{array}' 28 | return LaTeX(tex) 29 | 30 | def latex_lsym(lSym, resSym): 31 | if resSym == None: 32 | resSymExpr = '' 33 | else: 34 | resSymExpr = resSym + '=' 35 | if lSym._LSym__genSym and lSym._LSym__genCal: 36 | return LaTeX(r'%s%s=%s=%s' % (resSymExpr, lSym.sym(), lSym.cal(), lSym.num().latex())) 37 | elif lSym._LSym__genSym: 38 | return LaTeX(r'%s%s' % (resSymExpr, lSym.sym())) 39 | elif lSym._LSym__genCal: 40 | return LaTeX(r'%s%s=%s' % (resSymExpr, lSym.cal(), lSym.num().latex())) 41 | 42 | def latex_lsymitem(lSymItem, resSym, headExpr, showMean, meanExpr): 43 | latex = LaTeX() 44 | if analyticlab.lsymitem.LSymItem.sepSymCalc: 45 | latex.add((r'\text{' + headExpr + '}') % (resSym + '=' + lSymItem.getSepSym().sym())) 46 | if type(lSymItem._LSymItem__lsyms) == list: 47 | if resSym == None: 48 | for i in range(len(lSymItem)): 49 | latex.add(r'%s=%s' % (lSymItem[i].cal(), lSymItem[i].num().latex())) 50 | else: 51 | for i in range(len(lSymItem)): 52 | latex.add(r'{%s}_{%d}=%s=%s' % (resSym, i+1, lSymItem[i].cal(), lSymItem[i].num().latex())) 53 | else: 54 | if resSym == None: 55 | for ki in lSymItem._LSymItem__lsyms.keys(): 56 | latex.add(r'%s=%s' % (lSymItem[ki].cal(), lSymItem[ki].num().latex())) 57 | else: 58 | for ki in lSymItem._LSymItem__lsyms.keys(): 59 | latex.add(r'{%s}_{%s}=%s=%s' % (resSym, ki, lSymItem[ki].cal(), lSymItem[ki].num().latex())) 60 | else: 61 | if type(lSymItem._LSymItem__lsyms) == list: 62 | if resSym == None: 63 | for i in range(len(lSymItem)): 64 | latex.add(r'%s=%s=%s' % (lSymItem[i].sym(), lSymItem[i].cal(), lSymItem[i].num().latex())) 65 | else: 66 | for i in range(len(lSymItem)): 67 | latex.add(r'{%s}_{%d}=%s=%s=%s' % (resSym, i+1, lSymItem[i].sym(), lSymItem[i].cal(), lSymItem[i].num().latex())) 68 | else: 69 | if resSym == None: 70 | for ki in lSymItem._LSymItem__lsyms.keys(): 71 | latex.add(r'%s=%s=%s' % (lSymItem[ki].sym(), lSymItem[ki].cal(), lSymItem[ki].num().latex())) 72 | else: 73 | for ki in lSymItem._LSymItem__lsyms.keys(): 74 | latex.add(r'{%s}_{%s}=%s=%s=%s' % (resSym, ki, lSymItem[ki].sym(), lSymItem[ki].cal(), lSymItem[ki].num().latex())) 75 | if showMean and resSym != None: 76 | mitem = analyticlab.numitem.NumItem(lSymItem, sym=resSym) 77 | if meanExpr != None: 78 | latex.add((r'\text{' + meanExpr + '}') % mitem.mean(process=True)._LaTeX__lines[0]) 79 | else: 80 | latex.add(mitem.mean(process=True)) 81 | return latex 82 | 83 | def latex_measure(resMea, resSym, resDescription): 84 | latex = LaTeX() 85 | res = resMea._Measure__res() 86 | if res == None: 87 | raise processStateWrongException() 88 | if str(type(resMea)) =="": 89 | #针对BaseMeasure的测量值输出 90 | eqNum = resMea.value() 91 | for mi in resMea._Measure__baseMeasures.values(): 92 | if mi[0]._BaseMeasure__uA != None: 93 | latex.add(mi[0].value(process=True)) 94 | latex.add(latex_lsym(resMea._Measure__vl, r'\overline{%s}' % resSym)) 95 | #针对Measure的不确定度输出 96 | measures = [mi[0] for mi in res['baseMeasures'].values()] 97 | for i in range(len(measures)): 98 | latex.add(r'(%d)\text{对于}%s\text{:}' % (i+1, measures[i]._BaseMeasure__description)) 99 | latex.add(measures[i].unc(process=True, remainOneMoreDigit=True)) 100 | latex.add(r'\text{计算合成不确定度:}') 101 | if resMea.useRelUnc: 102 | if res['isRate']: 103 | urNum = res['unc'].latex() 104 | latex.add(r'\cfrac{u_{%s}}{%s}=%s\\&\quad=%s\\&\quad=%s' % (resSym, resSym, res['uncLSym'].sym(), res['uncLSym'].cal(), urNum.dlatex())) 105 | else: 106 | urNum = res['unc'] / eqNum 107 | urNum.setIsRelative(True) 108 | #给出不确定度计算定义式 109 | pExpr = '+'.join([r'\left(\cfrac{\partial %s}{\partial %s}\right)^2 u_{%s}^2' % (resSym, mi[0]._BaseMeasure__sym, mi[0]._BaseMeasure__sym) for mi in res['baseMeasures'].values()]) 110 | pExpr = r'\sqrt{%s}' % pExpr 111 | latex.add(r'u_{%s}=%s\\&\quad=%s\\&\quad=%s\\&\quad=%s' % (resSym, pExpr, res['uncLSym'].sym(), res['uncLSym'].cal(), res['unc'].latex())) 112 | latex.add(r'\cfrac{u_{%s}}{%s}=\cfrac{%s}{%s}\times 100\%%=%s' % (resSym, resSym, res['unc'].dlatex(), eqNum.dlatex(), urNum.dlatex())) 113 | else: 114 | if res['isRate']: 115 | uNum = res['unc']._Num__getRelative(dec=True) * eqNum 116 | latex.add(r'\cfrac{u_{%s}}{%s}=%s\\&\quad=%s\\&\quad=%s' % (resSym, resSym, res['uncLSym'].sym(), res['uncLSym'].cal(), res['unc'].latex())) 117 | latex.add(r'u_{%s}=\cfrac{u_{%s}}{%s}\cdot %s=%s\times %s=%s' % (resSym, resSym, resSym, resSym, res['unc'].dlatex(), eqNum.dlatex(), uNum.latex())) 118 | else: 119 | uNum = res['unc'] 120 | #给出不确定度计算定义式 121 | pExpr = '+'.join([r'\left(\cfrac{\partial %s}{\partial %s}\right)^2 u_{%s}^2' % (resSym, mi[0]._BaseMeasure__sym, mi[0]._BaseMeasure__sym) for mi in res['baseMeasures'].values()]) 122 | pExpr = r'\sqrt{%s}' % pExpr 123 | latex.add(r'u_{%s}=%s\\&\quad=%s\\&\quad=%s\\&\quad=%s' % (resSym, pExpr, res['uncLSym'].sym(), res['uncLSym'].cal(), res['unc'].latex())) 124 | elif str(type(resMea)) =="": 125 | if resSym == None: 126 | resSym = resMea._BaseMeasure__sym 127 | #针对BaseMeasure的测量值输出 128 | eqNum, proc_eq = resMea.value(process=True, needValue=True) 129 | #针对BaseMeasure的不确定度输出 130 | uNum, proc_u = resMea.unc(process=True, needValue=True, remainOneMoreDigit=True) 131 | latex.add(proc_eq) 132 | latex.add(proc_u) 133 | if resMea.useRelUnc: 134 | urNum = uNum / eqNum 135 | urNum.setIsRelative(True) 136 | latex.add(r'\cfrac{u_{%s}}{%s}=\cfrac{%s}{%s}\times 100\%%=%s' % (resSym, resSym, uNum.dlatex(), eqNum.dlatex(), urNum.dlatex())) 137 | sciDigit = eqNum._Num__sciDigit() 138 | unitExpr = format_units_latex(eqNum._Num__q) 139 | if resMea.useRelUnc: 140 | if res['K'] != None: 141 | urNum = res['K'] * urNum 142 | latex.add(r'\cfrac{u_{%s,%s}}{%s}=%g \cfrac{u_{%s}}{%s}=%s' % (resSym, res['P'][1], resSym, res['K'], resSym, resSym, urNum.dlatex())) 143 | finalExpr = r'\text{%s} %s=%s\left(1 \pm %s\right)%s' % (resDescription, resSym, eqNum.dlatex(), urNum.dlatex(), unitExpr) 144 | else: 145 | if res['K'] != None: 146 | uNum = res['K'] * uNum 147 | latex.add(r'u_{%s,%s}=%g u_{%s}=%s' % (resSym, res['P'][1], res['K'], resSym, uNum.latex())) 148 | if sciDigit == 0: 149 | uNum._Num__setDigit(eqNum._Num__d_front, eqNum._Num__d_behind, eqNum._Num__d_valid) 150 | finalExpr = r'\text{%s} %s=\left(%s \pm %s\right)%s' % (resDescription, resSym, eqNum.dlatex(), uNum.dlatex(), unitExpr) 151 | else: 152 | eqNum *= 10**(-sciDigit) 153 | uNum *= 10**(-sciDigit) 154 | uNum._Num__setDigit(eqNum._Num__d_front, eqNum._Num__d_behind, eqNum._Num__d_valid) 155 | finalExpr = r'\text{%s} %s=\left(%s \pm %s\right)\times 10^{%d}%s' % (resDescription, resSym, eqNum.dlatex(), uNum.dlatex(), sciDigit, unitExpr) 156 | if res['K'] == None: 157 | finalExpr += r'\qquad {\rm P=0.6826}' 158 | else: 159 | finalExpr += r'\qquad {\rm P=%s}' % res['P'][0] 160 | latex.add(finalExpr) 161 | return latex 162 | 163 | def latex_relerr(num, mu, sym, muSym, ESym): 164 | #当num不是Num数值类型时,将其转换为Num类型 165 | if type(num) == analyticlab.Num: 166 | pass 167 | elif type(num) == str: 168 | num = analyticlab.Num(num) 169 | elif type(num) == analyticlab.LSym: 170 | if sym == None: 171 | sym = num.sym() 172 | num = num.num() 173 | else: 174 | raise expressionInvalidException('数值num参数无效') 175 | #当mu不是Num数值类型时,将其转换为Num类型 176 | if type(mu) == analyticlab.Num: 177 | pass 178 | elif type(mu) == str: 179 | mu = analyticlab.Num(mu) 180 | elif type(mu) == analyticlab.LSym: 181 | if muSym == None: 182 | muSym = mu.sym() 183 | mu = mu.num() 184 | else: 185 | raise expressionInvalidException('真值mu参数无效') 186 | if muSym == None: 187 | muSym = num.latex() 188 | rel = abs(num - mu) / mu 189 | rel._Num__isRelative = True 190 | if ESym == None: 191 | return LaTeX(r'\cfrac{\left\lvert %s-%s \right\rvert}{%s}\times 100\%%=\cfrac{\left\lvert %s-%s \right\rvert}{%s}\times 100\%%=%s' % (sym, muSym, muSym, num.dlatex(), mu.dlatex(), mu.dlatex(), rel.latex())) 192 | else: 193 | return LaTeX(r'%s=\cfrac{\left\lvert %s-%s \right\rvert}{%s}\times 100\%%=\cfrac{\left\lvert %s-%s \right\rvert}{%s}\times 100\%%=%s' % (ESym, sym, muSym, muSym, num.dlatex(), mu.dlatex(), mu.dlatex(), rel.latex())) 194 | 195 | class LaTeX(object): 196 | '''LaTeX为公式集类,该类用于储存一系列数学公式的LaTeX代码并输出公式。''' 197 | def __init__(self, line=None): 198 | '''初始化一个LaTeX公式集 199 | 【参数说明】 200 | line(可选):在初始化LaTeX公式集时,要加入的公式。默认line=None,即初始化时不加入公式。line可以是以下数据类型: 201 | (1)str:给出一行要加入公式的字符串。 202 | (2)list:给出多行要加入公式的字符串列表。''' 203 | self.__lines = [] 204 | if line != None: 205 | if type(line) == list: 206 | self.__lines += line 207 | elif type(line) == str: 208 | self.__lines += [line] 209 | else: 210 | raise expressionInvalidException('用于初始化公式集的参数无效') 211 | 212 | def add(self, line): 213 | '''添加一行或多行公式 214 | 【参数说明】 215 | line:要加入的一行或多行公式。line可以是以下数据类型: 216 | (1)str:给出一行要加入公式的字符串。 217 | (2)LaTeX:将一个公式集追加到当前公式集。 218 | (3)list:给出多行要加入公式的字符串列表。 219 | (4)list:给出多个要追加的公式集。''' 220 | if type(line) == list: 221 | if type(line[0]) == str: 222 | self.__lines += line 223 | elif type(line[0]) == LaTeX: 224 | for oi in line: 225 | self.__lines += oi.__lines 226 | else: 227 | raise expressionInvalidException('要添加公式的参数无效') 228 | elif type(line) == str: 229 | self.__lines += [line] 230 | elif type(line) == LaTeX: 231 | self.__lines += line.__lines 232 | else: 233 | raise expressionInvalidException('要添加公式的参数无效') 234 | 235 | def show(self): 236 | '''展示公式集''' 237 | slines = ['&'+li for li in self.__lines] 238 | lExpr = r'$\begin{align}' + ('\\\\ \n'.join(slines)) + r'\end{align}$' 239 | display(Latex(lExpr)) 240 | 241 | def _repr_latex_(self): 242 | slines = ['&'+li for li in self.__lines] 243 | lExpr = r'$\begin{align}' + ('\\\\ \n'.join(slines)) + r'\end{align}$' 244 | return lExpr 245 | 246 | def addTable(self, table): 247 | '''添加一个简单格式的表格。表格的格式为m行n列,列宽由公式长度而定,公式居中。 248 | 【参数说明】 249 | table(list):由表格各个单元格的内容(字符串格式)组成的二维列表,该列表的第一维度为行,第二维度为列。''' 250 | self.add(latex_table(table)) 251 | 252 | def addLSym(self, lsym, resSym=None): 253 | '''添加一个LSym计算过程(根据原始LSym是否有符号和对应数值,决定是否显示符号表达式、计算表达式和计算结果) 254 | 【参数说明】 255 | 1.lSym(LSym):要展示的LaTeX符号。通过lSym,可以获得符号表达式、计算表达式和计算结果数值及单位。 256 | 2.resSym(可选,str):计算结果的符号。当不给出符号时,生成的计算过程将只有代数表达式和数值表达式,而没有计算结果的符号。默认resSym=None。''' 257 | self.add(latex_lsym(lsym, resSym)) 258 | 259 | def addLSymItem(self, lSymItem, resSym=None, headExpr='根据公式$%s$,得', showMean=True, meanExpr=None): 260 | '''添加一个LSymItem计算过程(包括符号表达式和计算表达式) 261 | 【参数说明】 262 | 1.lSymItem(LSymItem):要展示的LaTeX符号组。通过lSymItem,可以获得符号表达式、计算表达式和计算结果。 263 | 2.resSym(可选,str):计算结果的符号。当不给出符号时,生成的计算过程将只有代数表达式和数值表达式,而没有计算结果的符号。默认resSym=None。 264 | 3.headExpr(可选,str):当符号表达式与计算表达式相分离时,对符号表达式进行语言修饰;当符号表达式与计算表达式在同一个等式中展示出来时,该参数无意义。默认headExpr='根据公式$%s$,得'。 265 | 4.showMean(可选,bool):是否展示符号组中各运算结果的均值及其运算过程。默认showMean=True。 266 | 5.meanExpr(可选,str):对均值的计算式进行语言修饰。默认meanExpr=None,即没有语言修饰。''' 267 | self.add(latex_lsymitem(lSymItem, resSym, headExpr, showMean, meanExpr)) 268 | 269 | def addMeasure(self, resMea, resSym=None, resDescription=''): 270 | '''添加测量(含测量值及不确定度)的计算过程 271 | 【参数说明】 272 | 1.resMea(Measure或BaseMeasure):最终测量。 273 | 2.resSym(可选,str):最终测量的符号。当resMea为Measure时,不需要给出,否则必须给出。默认resSym=None。 274 | 3.resDescription(可选,str):对最终测量的描述。默认resDescription=''。''' 275 | self.add(latex_measure(resMea, resSym, resDescription)) 276 | 277 | def addRelErr(self, num, mu, sym=None, muSym=None, ESym='E_r'): 278 | '''添加相对误差的计算过程 279 | 【参数说明】 280 | 1.num和mu分别为测量值和真值。num、mu可以是以下数据类型: 281 | (1)Num:直接给出测量值、真值对应的数值 282 | (2)str:通过给出测量值、真值的字符串表达式,得到对应数值。 283 | (3)LSym:给出测量值、真值的LaTeX符号,得到对应数值。 284 | 2.sym(可选,str):测量值的符号。当测量值以LSym形式给出时,可不给出测量值符号,此时将使用num的符号作为测量值符号;否则必须给出符号。默认sym=None。 285 | 3.muSym(可选,str):真值的符号。当真值以LSym形式给出时,可不给出真值符号,此时将使用mu的符号作为真值符号;否则默认muSym为'\mu'。默认muSym=None。 286 | 4.ESym(可选,str):相对误差的符号。当给出相对误差的符号时,会在输出的表达式中加入相对误差符号那一项;当ESym为None时,没有那一项。默认ESym='E_r'。''' 287 | self.add(latex_relerr(num, mu, sym, muSym, ESym)) 288 | 289 | def dispTable(table): 290 | '''展示一个简单格式的表格。表格的格式为m行n列,列宽由公式长度而定,公式居中。 291 | 【参数说明】 292 | table(list):由表格各个单元格的内容(字符串格式)组成的二维列表,该列表的第一维度为行,第二维度为列。 293 | 【应用举例】 294 | dispTable([['a/m', 'b/m', 'S/m^2'], ['1.36', '2.32', '9.91'], ['1.33', '2.35', '9.82'], ['1.35', '2.34', '9.92']]) 295 | ''' 296 | latex_table(table).show() 297 | 298 | def dispLSym(lSym, resSym=None): 299 | '''展示一个LSym计算过程(根据原始LSym是否有符号和对应数值,决定是否显示符号表达式、计算表达式和计算结果) 300 | 【参数说明】 301 | 1.lSym(LSym):要展示的LaTeX符号。通过lSym,可以获得符号表达式、计算表达式和计算结果数值及单位。 302 | 2.resSym(可选,str):计算结果的符号。当不给出符号时,生成的计算过程将只有代数表达式和数值表达式,而没有计算结果的符号。默认resSym=None。''' 303 | latex_lsym(lSym, resSym).show() 304 | 305 | def dispLSymItem(lSymItem, resSym=None, headExpr='根据公式$%s$,得', showMean=True, meanExpr=None): 306 | '''展示一个LSymItem计算过程(包括符号表达式和计算表达式) 307 | 【参数说明】 308 | 1.lSymItem(LSymItem):要展示的LaTeX符号组。通过lSymItem,可以获得符号表达式、计算表达式和计算结果。 309 | 2.resSym(可选,str):计算结果的符号。当不给出符号时,生成的计算过程将只有代数表达式和数值表达式,而没有计算结果的符号。默认resSym=None。 310 | 3.headExpr(可选,str):当符号表达式与计算表达式相分离时,对符号表达式进行语言修饰;当符号表达式与计算表达式在同一个等式中展示出来时,该参数无意义。默认headExpr='根据公式$%s$,得'。 311 | 4.showMean(可选,bool):是否展示符号组中各运算结果的均值及其运算过程。默认showMean=True。 312 | 5.meanExpr(可选,str):对均值的计算式进行语言修饰。默认meanExpr=None,即没有语言修饰。''' 313 | latex_lsymitem(lSymItem, resSym, headExpr, showMean, meanExpr).show() 314 | 315 | def dispMeasure(resMea, resSym=None, resDescription=''): 316 | '''展示测量(含测量值和不确定度) 317 | 【参数说明】 318 | 1.resMea(Measure或BaseMeasure):最终测量。 319 | 2.resSym(可选,str):最终测量的符号。当resMea为Measure时,不需要给出,否则必须给出。默认resSym=None。 320 | 3.resDescription(可选,str):对最终测量的描述。默认resDescription=''。''' 321 | latex_measure(resMea, resSym, resDescription).show() 322 | 323 | def dispRelErr(num, mu, sym=None, muSym=None, ESym='E_r'): 324 | '''展示相对误差 325 | 【参数说明】 326 | 1.num和mu分别为测量值和真值。num、mu可以是以下数据类型: 327 | (1)Num:直接给出测量值、真值对应的数值 328 | (2)str:通过给出测量值、真值的字符串表达式,得到对应数值。 329 | (3)LSym:给出测量值、真值的LaTeX符号,得到对应数值。 330 | 2.sym(可选,str):测量值的符号。当测量值以LSym形式给出时,可不给出测量值符号,此时将使用num的符号作为测量值符号;否则必须给出符号。默认sym=None。 331 | 3.muSym(可选,str):真值的符号。当真值以LSym形式给出时,可不给出真值符号,此时将使用mu的符号作为真值符号;否则不给出真值符号时,会用真值数值取代muSym。默认muSym=None。 332 | 4.ESym(可选,str):相对误差的符号。当给出相对误差的符号时,会在输出的表达式中加入相对误差符号那一项;当ESym为None时,没有那一项。默认ESym='E_r'。''' 333 | latex_relerr(num, mu, sym, muSym, ESym).show() 334 | -------------------------------------------------------------------------------- /analyticlab/lookup/DixonTable.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sat Jan 27 15:06:33 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | from analyticlab.system.exceptions import tooMuchDataForTestException, tooLessDataForTestException, confLevelUnsupportedException 8 | 9 | oneSide = {} 10 | oneSide[0.95]=(0.941, 0.765, 0.642, 0.562, 0.507, 0.554, 0.512, 0.477, 0.575, 0.546, 0.521, 0.546, 0.524, 0.505, 0.489, 0.475, 0.462, 0.450, 0.440, 0.431, 0.422, 0.413, 0.406, 0.399, 0.393, 0.387, 0.381, 0.376, 0.371, 0.367, 0.362, 0.358, 0.354, 0.350, 0.347, 0.343, 0.340, 0.337, 0.334, 0.331, 0.328, 0.326, 0.323, 0.321, 0.318, 0.316, 0.314, 0.312, 0.310, 0.308, 0.306, 0.304, 0.302, 0.300, 0.298, 0.297, 0.295, 0.294, 0.292, 0.291, 0.289, 0.288, 0.287, 0.285, 0.284, 0.283, 0.282, 0.280, 0.279, 0.278, 0.277, 0.276, 0.275, 0.274, 0.273, 0.272, 0.271, 0.270, 0.269, 0.268, 0.267, 0.266, 0.265, 0.264, 0.263, 0.262, 0.262, 0.261, 0.260, 0.259, 0.259, 0.258, 0.257, 0.256, 0.255, 0.255, 0.254, 0.254) 11 | oneSide[0.99]=(0.988, 0.889, 0.782, 0.698, 0.637, 0.681, 0.635, 0.597, 0.674, 0.642, 0.617, 0.640, 0.618, 0.597, 0.580, 0.564, 0.550, 0.538, 0.526, 0.516, 0.507, 0.497, 0.489, 0.482, 0.474, 0.468, 0.462, 0.456, 0.450, 0.445, 0.441, 0.436, 0.432, 0.427, 0.423, 0.419, 0.416, 0.413, 0.409, 0.406, 0.403, 0.400, 0.397, 0.394, 0.391, 0.389, 0.386, 0.384, 0.382, 0.379, 0.377, 0.375, 0.373, 0.371, 0.369, 0.367, 0.366, 0.363, 0.362, 0.361, 0.359, 0.357, 0.355, 0.354, 0.353, 0.351, 0.350, 0.348, 0.347, 0.346, 0.344, 0.343, 0.342, 0.341, 0.340, 0.338, 0.337, 0.336, 0.335, 0.334, 0.333, 0.332, 0.331, 0.330, 0.329, 0.328, 0.327, 0.326, 0.325, 0.324, 0.323, 0.323, 0.322, 0.321, 0.320, 0.320, 0.319, 0.318) 12 | 13 | twoSides = {} 14 | twoSides[0.95]=(0.970, 0.829, 0.710, 0.628, 0.569, 0.608, 0.564, 0.530, 0.619, 0.583, 0.557, 0.587, 0.565, 0.547, 0.527, 0.513, 0.500, 0.488, 0.479, 0.469, 0.460, 0.449, 0.441, 0.436, 0.427, 0.420, 0.415, 0.409, 0.403, 0.399, 0.395, 0.390, 0.388, 0.438, 0.380, 0.377, 0.375, 0.370, 0.367, 0.364, 0.362, 0.359, 0.357, 0.353, 0.352, 0.350, 0.346, 0.343, 0.342, 0.340, 0.338, 0.337, 0.335, 0.334, 0.330, 0.329, 0.327, 0.325, 0.323, 0.321, 0.320, 0.319, 0.318, 0.316, 0.315, 0.313, 0.313, 0.312, 0.310, 0.309, 0.308, 0.306, 0.305, 0.304, 0.304, 0.303, 0.303, 0.302, 0.301, 0.301, 0.301, 0.298, 0.297, 0.297, 0.296, 0.295, 0.294, 0.293, 0.291, 0.290, 0.289, 0.289, 0.288, 0.288, 0.286, 0.285, 0.285, 0.284) 15 | twoSides[0.99]=(0.994, 0.926, 0.821, 0.740, 0.680, 0.717, 0.672, 0.635, 0.709, 0.660, 0.638, 0.669, 0.646, 0.629, 0.614, 0.602, 0.582, 0.570, 0.560, 0.548, 0.537, 0.522, 0.518, 0.509, 0.504, 0.497, 0.489, 0.480, 0.473, 0.468, 0.463, 0.460, 0.458, 0.442, 0.450, 0.447, 0.442, 0.438, 0.433, 0.432, 0.428, 0.425, 0.422, 0.419, 0.416, 0.413, 0.412, 0.409, 0.407, 0.405, 0.402, 0.400, 0.399, 0.399, 0.396, 0.393, 0.390, 0.389, 0.387, 0.385, 0.383, 0.382, 0.379, 0.377, 0.375, 0.376, 0.375, 0.375, 0.373, 0.373, 0.371, 0.370, 0.368, 0.363, 0.363, 0.362, 0.361, 0.358, 0.358, 0.355, 0.355, 0.353, 0.351, 0.351, 0.349, 0.349, 0.347, 0.347, 0.344, 0.344, 0.343, 0.343, 0.343, 0.342, 0.340, 0.340, 0.339, 0.339) 16 | 17 | 18 | def D(confLevel, n, side): 19 | if n > 100: 20 | raise tooMuchDataForTestException('Dixon检验最多只支持100个数据的检验') 21 | elif n < 3: 22 | raise tooLessDataForTestException('Dixon检验') 23 | if side == 1: 24 | return oneSide[confLevel][n-3] 25 | elif side == 2: 26 | return twoSides[confLevel][n-3] -------------------------------------------------------------------------------- /analyticlab/lookup/FTable.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu Jan 25 18:58:20 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | 8 | from scipy.stats import f as ff 9 | 10 | table = {} 11 | table[0.6826] = ((3.374,4.463,4.917,5.163,5.317,5.423,5.499,5.557,5.603,5.640), 12 | (1.745,2.151,2.300,2.377,2.424,2.456,2.479,2.496,2.509,2.520), 13 | (1.432,1.724,1.821,1.868,1.896,1.915,1.927,1.937,1.945,1.950), 14 | (1.303,1.550,1.626,1.662,1.682,1.694,1.703,1.709,1.714,1.717), 15 | (1.233,1.456,1.522,1.550,1.566,1.575,1.581,1.585,1.588,1.591), 16 | (1.189,1.398,1.456,1.481,1.493,1.500,1.505,1.508,1.510,1.511), 17 | (1.159,1.358,1.412,1.433,1.443,1.449,1.452,1.454,1.456,1.456), 18 | (1.137,1.329,1.379,1.398,1.407,1.412,1.414,1.415,1.416,1.416), 19 | (1.120,1.307,1.355,1.372,1.380,1.383,1.385,1.386,1.386,1.386), 20 | (1.107,1.290,1.335,1.351,1.358,1.361,1.362,1.362,1.362,1.362)) 21 | 22 | table[0.90] = ((39.863,49.500,53.593,55.833,57.240,58.204,58.906,59.439,59.858,60.195), 23 | (8.526,9.000,9.162,9.243,9.293,9.326,9.349,9.367,9.381,9.392), 24 | (5.538,5.462,5.391,5.343,5.309,5.285,5.266,5.252,5.240,5.230), 25 | (4.545,4.325,4.191,4.107,4.051,4.010,3.979,3.955,3.936,3.920), 26 | (4.060,3.780,3.619,3.520,3.453,3.405,3.368,3.339,3.316,3.297), 27 | (3.776,3.463,3.289,3.181,3.108,3.055,3.014,2.983,2.958,2.937), 28 | (3.589,3.257,3.074,2.961,2.883,2.827,2.785,2.752,2.725,2.703), 29 | (3.458,3.113,2.924,2.806,2.726,2.668,2.624,2.589,2.561,2.538), 30 | (3.360,3.006,2.813,2.693,2.611,2.551,2.505,2.469,2.440,2.416), 31 | (3.285,2.924,2.728,2.605,2.522,2.461,2.414,2.377,2.347,2.323)) 32 | 33 | table[0.95] = ((161.448,199.500,215.707,224.583,230.162,233.986,236.768,238.883,240.543,241.882), 34 | (18.513,19.000,19.164,19.247,19.296,19.330,19.353,19.371,19.385,19.396), 35 | (10.128,9.552,9.277,9.117,9.013,8.941,8.887,8.845,8.812,8.786), 36 | (7.709,6.944,6.591,6.388,6.256,6.163,6.094,6.041,5.999,5.964), 37 | (6.608,5.786,5.409,5.192,5.050,4.950,4.876,4.818,4.772,4.735), 38 | (5.987,5.143,4.757,4.534,4.387,4.284,4.207,4.147,4.099,4.060), 39 | (5.591,4.737,4.347,4.120,3.972,3.866,3.787,3.726,3.677,3.637), 40 | (5.318,4.459,4.066,3.838,3.687,3.581,3.500,3.438,3.388,3.347), 41 | (5.117,4.256,3.863,3.633,3.482,3.374,3.293,3.230,3.179,3.137), 42 | (4.965,4.103,3.708,3.478,3.326,3.217,3.135,3.072,3.020,2.978)) 43 | 44 | table[0.98] = ((1012.545,1249.500,1350.505,1405.833,1440.612,1464.455,1481.803,1494.986,1505.341,1513.687), 45 | (48.505,49.000,49.166,49.249,49.299,49.332,49.356,49.373,49.387,49.398), 46 | (20.618,18.858,18.110,17.694,17.429,17.245,17.110,17.007,16.926,16.860), 47 | (14.040,12.142,11.344,10.899,10.616,10.419,10.274,10.162,10.074,10.003), 48 | (11.323,9.454,8.670,8.233,7.953,7.758,7.614,7.503,7.415,7.344), 49 | (9.876,8.052,7.287,6.859,6.585,6.393,6.251,6.141,6.055,5.984), 50 | (8.988,7.203,6.454,6.035,5.765,5.576,5.435,5.327,5.241,5.171), 51 | (8.389,6.637,5.901,5.489,5.223,5.036,4.897,4.790,4.705,4.635), 52 | (7.961,6.234,5.510,5.103,4.840,4.654,4.517,4.410,4.325,4.256), 53 | (7.638,5.934,5.218,4.816,4.555,4.371,4.235,4.129,4.044,3.975)) 54 | 55 | table[0.99] = ((4052.181,4999.500,5403.352,5624.583,5763.650,5858.986,5928.356,5981.070,6022.473,6055.847), 56 | (98.503,99.000,99.166,99.249,99.299,99.333,99.356,99.374,99.388,99.399), 57 | (34.116,30.817,29.457,28.710,28.237,27.911,27.672,27.489,27.345,27.229), 58 | (21.198,18.000,16.694,15.977,15.522,15.207,14.976,14.799,14.659,14.546), 59 | (16.258,13.274,12.060,11.392,10.967,10.672,10.456,10.289,10.158,10.051), 60 | (13.745,10.925,9.780,9.148,8.746,8.466,8.260,8.102,7.976,7.874), 61 | (12.246,9.547,8.451,7.847,7.460,7.191,6.993,6.840,6.719,6.620), 62 | (11.259,8.649,7.591,7.006,6.632,6.371,6.178,6.029,5.911,5.814), 63 | (10.561,8.022,6.992,6.422,6.057,5.802,5.613,5.467,5.351,5.257), 64 | (10.044,7.559,6.552,5.994,5.636,5.386,5.200,5.057,4.942,4.849)) 65 | 66 | def F(confLevel, n1, n2): 67 | #若能查表则查表,表中无相关数据则计算 68 | if n1 <= 10 and n2 <= 10 and confLevel in (0.6826, 0.90, 0.95, 0.98, 0.99): 69 | return table[confLevel][n1][n2] 70 | else: 71 | return float(ff.ppf(confLevel, n1, n2)) -------------------------------------------------------------------------------- /analyticlab/lookup/GrubbsTable.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Jan 23 22:19:47 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | from analyticlab.system.exceptions import tooMuchDataForTestException, tooLessDataForTestException 8 | 9 | table = {} 10 | table[0.90] = (1.148, 1.425, 1.602, 1.729, 1.828, 1.909, 1.977, 2.036, 11 | 2.088, 2.134, 2.175, 2.213, 2.247, 2.279, 2.309, 2.335, 2.361, 2.385, 12 | 2.408, 2.429, 2.448, 2.467, 2.486, 2.502, 2.519, 2.534, 2.549, 2.563, 13 | 2.577, 2.591, 2.604, 2.616, 2.628, 2.639, 2.650, 2.661, 2.671, 2.682, 14 | 2.692, 2.700, 2.710, 2.719, 2.727, 2.736, 2.744, 2.753, 2.760, 2.768, 15 | 2.775, 2.783, 2.790, 2.798, 2.804, 2.811, 2.818, 2.824, 2.831, 2.837, 16 | 2.842, 2.849, 2.854, 2.860, 2.866, 2.871, 2.877, 2.883, 2.888, 2.893, 17 | 2.897, 2.903, 2.908, 2.912, 2.917, 2.922, 2.927, 2.931, 2.935, 2.940, 18 | 2.945, 2.949, 2.953, 2.957, 2.961, 2.966, 2.970, 2.973, 2.977, 2.981, 19 | 2.984, 2.989, 2.993, 2.996, 3.000, 3.003, 3.006, 3.011, 3.014, 3.017) 20 | table[0.95] = (1.153, 1.463, 1.672, 1.822, 1.938, 2.032, 2.110, 2.176, 21 | 2.234, 2.285, 2.331, 2.371, 2.409, 2.443, 2.475, 2.504, 2.532, 2.557, 22 | 2.580, 2.603, 2.624, 2.644, 2.663, 2.681, 2.698, 2.714, 2.730, 2.745, 23 | 2.759, 2.773, 2.786, 2.799, 2.811, 2.823, 2.835, 2.846, 2.857, 2.866, 24 | 2.877, 2.887, 2.896, 2.905, 2.914, 2.923, 2.931, 2.940, 2.948, 2.956, 25 | 2.964, 2.971, 2.978, 2.986, 2.992, 3.000, 3.006, 3.013, 3.019, 3.025, 26 | 3.032, 3.037, 3.044, 3.049, 3.055, 3.061, 3.066, 3.071, 3.076, 3.082, 27 | 3.087, 3.092, 3.098, 3.102, 3.107, 3.111, 3.117, 3.121, 3.125, 3.130, 28 | 3.134, 3.139, 3.143, 3.147, 3.151, 3.155, 3.160, 3.163, 3.167, 3.171, 29 | 3.174, 3.179, 3.182, 3.186, 3.189, 3.193, 3.196, 3.201, 3.204, 3.207) 30 | table[0.975] = (1.155, 1.481, 1.715, 1.887, 2.020, 2.126, 2.215, 2.290, 31 | 2.355, 2.412, 2.462, 2.507, 2.549, 2.585, 2.620, 2.651, 2.681, 2.709, 32 | 2.733, 2.758, 2.781, 2.802, 2.822, 2.841, 2.859, 2.876, 2.893, 2.908, 33 | 2.924, 2.938, 2.952, 2.965, 2.979, 2.991, 3.003, 3.014, 3.025, 3.036, 34 | 3.046, 3.057, 3.067, 3.075, 3.085, 3.094, 3.103, 3.111, 3.120, 3.128, 35 | 3.136, 3.143, 3.151, 3.158, 3.166, 3.172, 3.180, 3.186, 3.193, 3.199, 36 | 3.205, 3.212, 3.218, 3.224, 3.230, 3.235, 3.241, 3.246, 3.252, 3.257, 37 | 3.262, 3.267, 3.272, 3.278, 3.282, 3.287, 3.291, 3.297, 3.301, 3.305, 38 | 3.309, 3.315, 3.319, 3.323, 3.327, 3.331, 3.335, 3.339, 3.343, 3.347, 39 | 3.350, 3.355, 3.358, 3.362, 3.365, 3.369, 3.372, 3.377, 3.380, 3.383) 40 | table[0.99] = (1.155, 1.492, 1.749, 1.944, 2.097, 2.221, 2.323, 2.410, 41 | 2.485, 2.550, 2.607, 2.659, 2.705, 2.747, 2.785, 2.821, 2.854, 2.884, 42 | 2.912, 2.939, 2.963, 2.987, 3.009, 3.029, 3.049, 3.068, 3.085, 3.103, 43 | 3.119, 3.135, 3.150, 3.164, 3.178, 3.191, 3.204, 3.216, 3.228, 3.240, 44 | 3.251, 3.261, 3.271, 3.282, 3.292, 3.302, 3.310, 3.319, 3.329, 3.336, 45 | 3.345, 3.353, 3.361, 3.368, 3.376, 3.383, 3.391, 3.397, 3.405, 3.411, 46 | 3.418, 3.424, 3.430, 3.437, 3.442, 3.449, 3.454, 3.460, 3.466, 3.471, 47 | 3.476, 3.482, 3.487, 3.492, 3.496, 3.502, 3.507, 3.511, 3.516, 3.521, 48 | 3.525, 3.529, 3.534, 3.539, 3.543, 3.547, 3.551, 3.555, 3.559, 3.563, 49 | 3.567, 3.570, 3.575, 3.579, 3.582, 3.586, 3.589, 3.593, 3.597, 3.600) 50 | table[0.995] = (1.155, 1.496, 1.764, 1.973, 2.139, 2.274, 2.387, 2.482, 51 | 2.564, 2.636, 2.699, 2.755, 2.806, 2.852, 2.894, 2.932, 2.968, 3.001, 52 | 3.031, 3.060, 3.087, 3.112, 3.135, 3.157, 3.178, 3.199, 3.218, 3.236, 53 | 3.253, 3.270, 3.286, 3.301, 3.316, 3.330, 3.343, 3.356, 3.369, 3.381, 54 | 3.393, 3.404, 3.415, 3.425, 3.435, 3.445, 3.455, 3.464, 3.474, 3.483, 55 | 3.491, 3.500, 3.507, 3.516, 3.524, 3.531, 3.539, 3.546, 3.553, 3.560, 56 | 3.566, 3.573, 3.579, 3.586, 3.592, 3.598, 3.605, 3.610, 3.617, 3.622, 57 | 3.627, 3.633, 3.638, 3.643, 3.648, 3.654, 3.658, 3.663, 3.669, 3.673, 58 | 3.677, 3.682, 3.687, 3.691, 3.695, 3.699, 3.704, 3.708, 3.712, 3.716, 59 | 3.720, 3.725, 3.728, 3.732, 3.736, 3.739, 3.744, 3.747, 3.750, 3.754) 60 | 61 | def G(confLevel, n): 62 | if n > 100: 63 | raise tooMuchDataForTestException('Grubbs检验不支持超过100组数据') 64 | elif n < 3: 65 | raise tooLessDataForTestException('Grubbs检验') 66 | return table[confLevel][n-3] -------------------------------------------------------------------------------- /analyticlab/lookup/NairTable.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sat Feb 3 20:56:17 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | from analyticlab.system.exceptions import tooMuchDataForTestException, tooLessDataForTestException 8 | 9 | table = {} 10 | table[0.90]=(1.497, 1.696, 1.835, 1.939, 2.022, 2.091, 2.150, 2.200, 2.245, 2.284, 2.320, 2.352, 2.382, 2.409, 2.434, 2.458, 2.480, 2.500, 2.519, 2.538, 2.555, 2.571, 2.587, 2.602, 2.616, 2.630, 2.643, 2.656, 2.668, 2.679, 2.690, 2.701, 2.712, 2.722, 2.732, 2.741, 2.750, 2.759, 2.768, 2.776, 2.784, 2.792, 2.800, 2.808, 2.815, 2.822, 2.829, 2.836, 2.843, 2.849, 2.856, 2.862, 2.868, 2.874, 2.880, 2.886, 2.892, 2.897, 2.903, 2.908, 2.913, 2.919, 2.924, 2.929, 2.934, 2.938, 2.943, 2.948, 2.952, 2.957, 2.961, 2.966, 2.970, 2.974, 2.978, 2.983, 2.987, 2.991, 2.995, 2.999, 3.002, 3.006, 3.010, 3.014, 3.017, 3.021, 3.024, 3.028, 3.031, 3.035, 3.038, 3.042, 3.045, 3.048, 3.052, 3.055, 3.058, 3.061) 11 | table[0.95]=(1.738, 1.941, 2.080, 2.184, 2.267, 2.334, 2.392, 2.441, 2.484, 2.523, 2.557, 2.589, 2.617, 2.644, 2.668, 2.691, 2.712, 2.732, 2.750, 2.768, 2.784, 2.800, 2.815, 2.829, 2.843, 2.856, 2.869, 2.881, 2.892, 2.903, 2.914, 2.924, 2.934, 2.944, 2.953, 2.962, 2.971, 2.980, 2.988, 2.996, 3.004, 3.011, 3.019, 3.026, 3.033, 3.040, 3.047, 3.053, 3.060, 3.066, 3.072, 3.078, 3.084, 3.090, 3.095, 3.101, 3.106, 3.112, 3.117, 3.122, 3.127, 3.132, 3.137, 3.142, 3.146, 3.151, 3.155, 3.160, 3.164, 3.169, 3.173, 3.177, 3.181, 3.185, 3.189, 3.193, 3.197, 3.201, 3.205, 3.208, 3.212, 3.216, 3.219, 3.223, 3.226, 3.230, 3.233, 3.236, 3.240, 3.243, 3.246, 3.249, 3.253, 3.256, 3.259, 3.262, 3.265, 3.268) 12 | table[0.975]=(1.955, 2.163, 2.304, 2.408, 2.490, 2.557, 2.613, 2.662, 2.704, 2.742, 2.776, 2.806, 2.834, 2.860, 2.883, 2.905, 2.926, 2.945, 2.963, 2.980, 2.996, 3.011, 3.026, 3.039, 3.053, 3.065, 3.077, 3.089, 3.100, 3.111, 3.121, 3.131, 3.140, 3.150, 3.159, 3.167, 3.176, 3.184, 3.192, 3.200, 3.207, 3.215, 3.222, 3.229, 3.235, 3.242, 3.249, 3.255, 3.261, 3.267, 3.273, 3.279, 3.284, 3.290, 3.295, 3.300, 3.306, 3.311, 3.316, 3.321, 3.326, 3.330, 3.335, 3.339, 3.344, 3.348, 3.353, 3.357, 3.361, 3.365, 3.369, 3.373, 3.377, 3.381, 3.385, 3.389, 3.393, 3.396, 3.400, 3.403, 3.407, 3.410, 3.414, 3.417, 3.421, 3.424, 3.427, 3.430, 3.433, 3.437, 3.440, 3.443, 3.446, 3.449, 3.452, 3.455, 3.458, 3.460) 13 | table[0.99]=(2.215, 2.431, 2.574, 2.679, 2.761, 2.828, 2.884, 2.931, 2.973, 3.010, 3.043, 3.072, 3.099, 3.124, 3.147, 3.168, 3.188, 3.207, 3.224, 3.240, 3.256, 3.270, 3.284, 3.298, 3.310, 3.322, 3.334, 3.345, 3.356, 3.366, 3.376, 3.385, 3.394, 3.403, 3.412, 3.420, 3.428, 3.436, 3.444, 3.451, 3.458, 3.465, 3.472, 3.479, 3.485, 3.491, 3.498, 3.504, 3.509, 3.515, 3.521, 3.526, 3.532, 3.537, 3.542, 3.547, 3.552, 3.557, 3.562, 3.566, 3.571, 3.575, 3.580, 3.584, 3.588, 3.593, 3.597, 3.601, 3.605, 3.609, 3.613, 3.617, 3.620, 3.624, 3.628, 3.631, 3.635, 3.638, 3.642, 3.645, 3.648, 3.652, 3.655, 3.658, 3.661, 3.665, 3.668, 3.671, 3.674, 3.677, 3.680, 3.683, 3.685, 3.688, 3.691, 3.694, 3.697, 3.699) 14 | table[0.995]=(2.396, 2.618, 2.764, 2.870, 2.952, 3.019, 3.074, 3.122, 3.163, 3.199, 3.232, 3.261, 3.287, 3.312, 3.334, 3.355, 3.374, 3.392, 3.409, 3.425, 3.440, 3.455, 3.468, 3.481, 3.493, 3.505, 3.516, 3.527, 3.538, 3.548, 3.557, 3.566, 3.575, 3.584, 3.592, 3.600, 3.608, 3.616, 3.623, 3.630, 3.637, 3.644, 3.651, 3.657, 3.663, 3.669, 3.675, 3.681, 3.687, 3.692, 3.698, 3.703, 3.708, 3.713, 3.718, 3.723, 3.728, 3.733, 3.737, 3.742, 3.746, 3.751, 3.755, 3.759, 3.763, 3.767, 3.771, 3.775, 3.779, 3.783, 3.787, 3.791, 3.794, 3.798, 3.801, 3.805, 3.808, 3.812, 3.815, 3.818, 3.821, 3.825, 3.828, 3.831, 3.834, 3.837, 3.840, 3.843, 3.846, 3.849, 3.852, 3.854, 3.857, 3.860, 3.863, 3.865, 3.868, 3.871) 15 | 16 | def R(confLevel, n): 17 | if n > 100: 18 | raise tooMuchDataForTestException('Nair检验不支持超过100组数据') 19 | elif n < 3: 20 | raise tooLessDataForTestException('Nair检验') 21 | return table[confLevel][n-3] -------------------------------------------------------------------------------- /analyticlab/lookup/Physics_tTable.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Feb 6 10:56:28 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | 8 | table = [1.84, 1.32, 1.20, 1.14, 1.11, 1.09, 1.08, 1.07, 1.06, 1.05, 1.05, 1.04, 1.04, 1.04, 1.03] 9 | 10 | def phy_t(n): 11 | if n <= 16: 12 | return table[n-2] 13 | elif n < 30: 14 | return 1.03 15 | elif n < 40: 16 | return 1.02; 17 | elif n < 200: 18 | return 1.01; 19 | else: 20 | return 1.00; -------------------------------------------------------------------------------- /analyticlab/lookup/RangeTable.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Feb 6 10:25:11 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | from analyticlab.system.exceptions import tooMuchDataForTestException, tooLessDataForTestException 8 | 9 | table_C = [1.13, 1.64, 2.06, 2.33, 2.53, 2.70, 2.85, 2.97] 10 | table_v = [0.9, 1.8, 2.7, 3.6, 4.5, 5.3, 6.0, 6.8] 11 | 12 | def C(n): 13 | try: 14 | return table_C[n-2] 15 | except: 16 | if n > 9: 17 | raise tooMuchDataForTestException('贝塞尔公式法不支持超过9组数据') 18 | elif n < 2: 19 | raise tooLessDataForTestException('贝塞尔公式法') 20 | 21 | def v(n): 22 | try: 23 | return table_v[n-2] 24 | except: 25 | if n > 9: 26 | raise tooMuchDataForTestException('贝塞尔公式法不支持超过9组数据') 27 | elif n < 2: 28 | raise tooLessDataForTestException('贝塞尔公式法') -------------------------------------------------------------------------------- /analyticlab/lookup/SkewKuriTable.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sat Feb 3 08:41:33 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | from analyticlab.system.exceptions import tooMuchDataForTestException, tooLessDataForTestException 8 | from analyticlab.system.numberformat import f 9 | 10 | sTable = {} 11 | sTable[0.95] = {8:0.99, 9:0.97, 10:0.95, 12:0.91, 15:0.85, 20:0.77, 25:0.71, 30:0.66, 35:0.62, 40:0.59, 45:0.56, 50:0.53, 60:0.49, 70:0.46, 80:0.43, 90:0.41, 100:0.39} 12 | sTable[0.99] = {8:1.42, 9:1.41, 10:1.39, 12:1.34, 15:1.26, 20:1.15, 25:1.06, 30:0.98, 35:0.92, 40:0.87, 45:0.82, 50:0.79, 60:0.72, 70:0.67, 80:0.63, 90:0.60, 100:0.57} 13 | 14 | kTable = {} 15 | kTable[0.95] = {8:3.70, 9:3.86, 10:3.95, 12:4.05, 15:4.13, 20:4.17, 25:4.14, 30:4.11, 35:4.08, 40:4.05, 45:4.02, 50:3.99, 60:3.93, 70:3.88, 80:3.84, 90:3.80, 100:3.77} 16 | kTable[0.99] = {8:4.53, 9:4.82, 10:5.00, 12:5.20, 15:5.30, 20:5.38, 25:5.29, 30:5.20, 35:5.11, 40:5.02, 45:4.94, 50:4.87, 60:4.73, 70:4.62, 80:4.52, 90:4.45, 100:4.37} 17 | 18 | def b(confLevel, n, side=2): 19 | if n > 100: 20 | raise tooMuchDataForTestException('偏度-峰度检验最多只支持100个数据的检验') 21 | elif n < 8: 22 | raise tooLessDataForTestException('偏度-峰度检验') 23 | if side == 1: 24 | y = sTable[confLevel] 25 | elif side == 2: 26 | y = kTable[confLevel] 27 | if n in y: #当表中有值时,直接取出值 28 | return y[n] 29 | else: #当表中没有值时,使用插值法 30 | nl = n - min([n-k for k in sTable[confLevel].keys() if k<14]) #找出右侧的值 31 | nr = n + min([k-n for k in sTable[confLevel].keys() if k>14]) #找出左侧的值 32 | return f(y[nl] + (y[nr]-y[nl])/(nr-nl)*(n-nl), 2) #给出插值结果 -------------------------------------------------------------------------------- /analyticlab/lookup/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Feb 18 09:25:00 2018 4 | 5 | @author: xingtongtech 6 | """ 7 | 8 | from .alphaToConf import rep 9 | from .DixonTable import D 10 | from .RangeTable import C, v 11 | from .Physics_tTable import phy_t 12 | from .NairTable import R 13 | from .GrubbsTable import G 14 | from .SkewKuriTable import b 15 | from .tTable import t, t_repl 16 | from .FTable import F -------------------------------------------------------------------------------- /analyticlab/lookup/alphaToConf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Feb 2 14:59:30 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | 8 | from analyticlab.system.exceptions import alphaUnsupportedException 9 | 10 | oneSide = {0.01: 0.99, 0.05: 0.95, 0.10: 0.90} 11 | twoSide = {0.01: 0.995, 0.05: 0.975, 0.10: 0.95} 12 | 13 | def rep(alpha, side): 14 | try: 15 | if side == 2: 16 | return twoSide[alpha] 17 | else: 18 | return oneSide[alpha] 19 | except: 20 | raise alphaUnsupportedException(alpha) -------------------------------------------------------------------------------- /analyticlab/lookup/tTable.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu Jan 25 18:58:04 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | from scipy.stats import t as tt 8 | 9 | rep = {0.6826: 0.3652, 0.90: 0.80, 0.95: 0.90, 0.98: 0.96, 0.99: 0.98} 10 | 11 | table = {} 12 | table[0.3652] = (0.646, 0.555, 0.527, 0.513, 0.505, 0.500, 0.496, 0.494, 13 | 0.492, 0.490, 0.488, 0.487, 0.486, 0.486, 0.485, 0.484, 0.484, 0.483, 14 | 0.483, 0.482, 0.482, 0.482, 0.481, 0.481, 0.481, 0.481, 0.480, 0.480, 15 | 0.480, 0.480, 0.480, 0.480, 0.479, 0.479, 0.479, 0.479, 0.479, 0.479, 16 | 0.479, 0.479, 0.479, 0.478, 0.478, 0.478, 0.478, 0.478, 0.478, 0.478, 17 | 0.478, 0.478) 18 | table[0.6826] = (1.837, 1.321, 1.197, 1.141, 1.110, 1.090, 1.076, 1.066, 19 | 1.059, 1.052, 1.047, 1.043, 1.040, 1.037, 1.034, 1.032, 1.030, 1.028, 20 | 1.027, 1.025, 1.024, 1.023, 1.022, 1.021, 1.020, 1.019, 1.019, 1.018, 21 | 1.017, 1.017, 1.016, 1.016, 1.015, 1.015, 1.014, 1.014, 1.014, 1.013, 22 | 1.013, 1.012, 1.012, 1.012, 1.012, 1.011, 1.011, 1.011, 1.011, 1.010, 23 | 1.010, 1.010) 24 | table[0.80] = (3.078, 1.886, 1.638, 1.533, 1.476, 1.440, 1.415, 1.397, 25 | 1.383, 1.372, 1.363, 1.356, 1.350, 1.345, 1.341, 1.337, 1.333, 1.330, 26 | 1.328, 1.325, 1.323, 1.321, 1.319, 1.318, 1.316, 1.315, 1.314, 1.313, 27 | 1.311, 1.310, 1.309, 1.309, 1.308, 1.307, 1.306, 1.306, 1.305, 1.304, 28 | 1.304, 1.303, 1.303, 1.302, 1.302, 1.301, 1.301, 1.300, 1.300, 1.299, 29 | 1.299, 1.299) 30 | table[0.90] = (6.314, 2.920, 2.353, 2.132, 2.015, 1.943, 1.895, 1.860, 31 | 1.833, 1.812, 1.796, 1.782, 1.771, 1.761, 1.753, 1.746, 1.740, 1.734, 32 | 1.729, 1.725, 1.721, 1.717, 1.714, 1.711, 1.708, 1.706, 1.703, 1.701, 33 | 1.699, 1.697, 1.696, 1.694, 1.692, 1.691, 1.690, 1.688, 1.687, 1.686, 34 | 1.685, 1.684, 1.683, 1.682, 1.681, 1.680, 1.679, 1.679, 1.678, 1.677, 35 | 1.677, 1.676) 36 | table[0.95] = (12.706, 4.303, 3.182, 2.776, 2.571, 2.447, 2.365, 2.306, 37 | 2.262, 2.228, 2.201, 2.179, 2.160, 2.145, 2.131, 2.120, 2.110, 2.101, 38 | 2.093, 2.086, 2.080, 2.074, 2.069, 2.064, 2.060, 2.056, 2.052, 2.048, 39 | 2.045, 2.042, 2.040, 2.037, 2.035, 2.032, 2.030, 2.028, 2.026, 2.024, 40 | 2.023, 2.021, 2.020, 2.018, 2.017, 2.015, 2.014, 2.013, 2.012, 2.011, 41 | 2.010, 2.009) 42 | table[0.9545] = (13.968, 4.527, 3.307, 2.869, 2.649, 2.517, 2.429, 2.366, 43 | 2.320, 2.284, 2.255, 2.231, 2.212, 2.195, 2.181, 2.169, 2.158, 2.149, 44 | 2.140, 2.133, 2.126, 2.120, 2.115, 2.110, 2.105, 2.101, 2.097, 2.093, 45 | 2.090, 2.087, 2.084, 2.081, 2.079, 2.076, 2.074, 2.072, 2.070, 2.068, 46 | 2.066, 2.064, 2.063, 2.061, 2.060, 2.058, 2.057, 2.056, 2.055, 2.053, 47 | 2.052, 2.051) 48 | table[0.96] = (15.895, 4.849, 3.482, 2.999, 2.757, 2.612, 2.517, 2.449, 49 | 2.398, 2.359, 2.328, 2.303, 2.282, 2.264, 2.249, 2.235, 2.224, 2.214, 50 | 2.205, 2.197, 2.189, 2.183, 2.177, 2.172, 2.167, 2.162, 2.158, 2.154, 51 | 2.150, 2.147, 2.144, 2.141, 2.138, 2.136, 2.133, 2.131, 2.129, 2.127, 52 | 2.125, 2.123, 2.121, 2.120, 2.118, 2.116, 2.115, 2.114, 2.112, 2.111, 53 | 2.110, 2.109) 54 | table[0.98] = (31.821, 6.965, 4.541, 3.747, 3.365, 3.143, 2.998, 2.896, 55 | 2.821, 2.764, 2.718, 2.681, 2.650, 2.624, 2.602, 2.583, 2.567, 2.552, 56 | 2.539, 2.528, 2.518, 2.508, 2.500, 2.492, 2.485, 2.479, 2.473, 2.467, 57 | 2.462, 2.457, 2.453, 2.449, 2.445, 2.441, 2.438, 2.434, 2.431, 2.429, 58 | 2.426, 2.423, 2.421, 2.418, 2.416, 2.414, 2.412, 2.410, 2.408, 2.407, 59 | 2.405, 2.403) 60 | table[0.99] = (63.657, 9.925, 5.841, 4.604, 4.032, 3.707, 3.499, 3.355, 61 | 3.250, 3.169, 3.106, 3.055, 3.012, 2.977, 2.947, 2.921, 2.898, 2.878, 62 | 2.861, 2.845, 2.831, 2.819, 2.807, 2.797, 2.787, 2.779, 2.771, 2.763, 63 | 2.756, 2.750, 2.744, 2.738, 2.733, 2.728, 2.724, 2.719, 2.715, 2.712, 64 | 2.708, 2.704, 2.701, 2.698, 2.695, 2.692, 2.690, 2.687, 2.685, 2.682, 65 | 2.680, 2.678) 66 | table[0.9973] = (235.784, 19.206, 9.219, 6.620, 5.507, 4.904, 4.530, 4.277, 67 | 4.094, 3.957, 3.850, 3.764, 3.694, 3.636, 3.586, 3.544, 3.507, 3.475, 68 | 3.447, 3.422, 3.400, 3.380, 3.361, 3.345, 3.330, 3.316, 3.303, 3.291, 69 | 3.280, 3.270, 3.261, 3.252, 3.244, 3.236, 3.229, 3.222, 3.216, 3.210, 70 | 3.204, 3.199, 3.194, 3.189, 3.184, 3.180, 3.175, 3.171, 3.168, 3.164, 71 | 3.160, 3.157) 72 | 73 | def t_repl(confLevel): 74 | return 1-2*(1-confLevel) 75 | 76 | def t(confLevel, n, side=2): 77 | #若能查表则查表,表中无相关数据则计算 78 | if side == 2: 79 | if n <= 50 and confLevel in table: 80 | return table[confLevel][n-1] 81 | else: 82 | return float(tt.ppf(confLevel, n)) 83 | elif side == 1: 84 | if n <= 50 and confLevel in rep: 85 | return table[rep[confLevel]][n-1] 86 | else: 87 | return float(tt.ppf(1-2*(1-confLevel), n)) 88 | -------------------------------------------------------------------------------- /analyticlab/measure/ACategory.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Feb 6 10:15:25 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | 8 | from . import std 9 | from ..amath import sqrt 10 | from ..latexoutput import LaTeX 11 | from ..numitem import NumItem 12 | from ..lookup.Physics_tTable import phy_t 13 | from ..lookup.RangeTable import v as rv 14 | from ..lookup.RangeTable import C as rC 15 | from ..system.unit_open import openUnit, closeUnit 16 | 17 | def Bessel(item, process=False, needValue=False, remainOneMoreDigit=False): 18 | '''贝塞尔公式法计算A类不确定度 19 | 【参数说明】 20 | 1.item(NumItem):用于A类不确定度计算的样本数据。 21 | 2.process(可选,bool):是否获得计算过程。默认proces=False。 22 | 3.needValue(可选,bool):当获得计算过程时,是否返回计算结果。默认needValue=False。 23 | 4.remainOneMoreDigit(可选,bool):结果是否多保留一位有效数字。默认remainOneMoreDigit=False。 24 | 【返回值】 25 | ①process为False时,返回值为Num类型的A类不确定度。 26 | ②process为True且needValue为False时,返回值为LaTeX类型的计算过程。 27 | ③process为True且needValue为True时,返回值为Num类型的A类不确定度和LaTeX类型的计算过程组成的元组。''' 28 | result = std.Bessel(item, remainOneMoreDigit) / len(item)**0.5 29 | if process: 30 | signal = item._NumItem__sym 31 | sciDigit = item._NumItem__sciDigit() 32 | if sciDigit == 0: 33 | p_mean = item.mean() 34 | sumExpr = '+'.join([(r'%s^{2}' % (xi - p_mean).dlatex(1)) for xi in item._NumItem__arr]) 35 | latex = LaTeX(r'u_{%s A}=\sqrt{\cfrac{1}{n(n-1)}\sum\limits_{i=1}^n\left(%s_{i}-\overline{%s}\right)^{2}}=\sqrt{\cfrac{1}{%d\times %d}\left[%s\right]}=%s' % (signal, signal, signal, len(item), len(item)-1, sumExpr, result.latex())) 36 | else: 37 | d_arr = item * 10**(-sciDigit) 38 | p_mean = item.mean() * 10**(-sciDigit) 39 | sumExpr = '+'.join([(r'%s^{2}' % (xi - p_mean).dlatex(1)) for xi in d_arr._NumItem__arr]) 40 | latex = LaTeX(r'u_{%s A}=\sqrt{\cfrac{1}{n(n-1)}\sum\limits_{i=1}^n\left(%s_{i}-\overline{%s}\right)^{2}}=\sqrt{\cfrac{1}{%d \times %d}\left[%s\right]}\times 10^{%d}=%s' % (signal, signal, signal, len(item), len(item)-1, sumExpr, sciDigit, result.latex())) 41 | if needValue: 42 | return result, latex 43 | else: 44 | return latex 45 | return result 46 | 47 | def Range(item, process=False, needValue=False, remainOneMoreDigit=False): 48 | '''极差法计算A类不确定度 49 | 【参数说明】 50 | 1.item(NumItem):用于A类不确定度计算的样本数据。 51 | 2.process(可选,bool):是否获得计算过程。默认proces=False。 52 | 3.needValue(可选,bool):当获得计算过程时,是否返回计算结果。默认needValue=False。 53 | 4.remainOneMoreDigit(可选,bool):结果是否多保留一位有效数字。默认remainOneMoreDigit=False。 54 | 【返回值】 55 | ①process为False时,返回值为Num类型的A类不确定度。 56 | ②process为True且needValue为False时,返回值为LaTeX类型的计算过程。 57 | ③process为True且needValue为True时,返回值为Num类型的A类不确定度和LaTeX类型的计算过程组成的元组。''' 58 | result = std.Range(item, remainOneMoreDigit) / len(item._NumItem__arr)**0.5 59 | if process: 60 | signal = item._NumItem__sym 61 | p_max, p_min = max(item._NumItem__arr), min(item._NumItem__arr) 62 | C = rC(len(item)) 63 | latex = LaTeX(r'u_{%s A}=\cfrac{R}{C\sqrt{n}}=\cfrac{%s-%s}{%s\times\sqrt{%s}}=%s' % (signal, p_max.dlatex(), p_min.dlatex(2), C, len(item), result.latex())) 64 | if needValue: 65 | return result, latex 66 | else: 67 | return latex 68 | return result 69 | 70 | def CollegePhysics(item, process=False, needValue=False, remainOneMoreDigit=False): 71 | '''大学物理实验中的A类不确定度计算 72 | 【参数说明】 73 | 1.item(NumItem):用于A类不确定度计算的样本数据。 74 | 2.process(可选,bool):是否获得计算过程。默认proces=False。 75 | 3.needValue(可选,bool):当获得计算过程时,是否返回计算结果。默认needValue=False。 76 | 4.remainOneMoreDigit(可选,bool):结果是否多保留一位有效数字。默认remainOneMoreDigit=False。 77 | 【返回值】 78 | ①process为False时,返回值为Num类型的A类不确定度。 79 | ②process为True且needValue为False时,返回值为LaTeX类型的计算过程。 80 | ③process为True且needValue为True时,返回值为Num类型的A类不确定度和LaTeX类型的计算过程组成的元组。''' 81 | n = len(item._NumItem__arr) 82 | result = phy_t(n) * std.CollegePhysics(item, remainOneMoreDigit) / n**0.5 83 | if process: 84 | signal = item._NumItem__sym 85 | sciDigit = item._NumItem__sciDigit() 86 | if sciDigit == 0: 87 | p_mean = item.mean() 88 | sumExpr = '+'.join([(r'%s^{2}' % (xi - p_mean).dlatex(1)) for xi in item._NumItem__arr]) 89 | latex = LaTeX(r'u_{%s A}=t_{n}\sqrt{\cfrac{1}{n(n-1)}\sum\limits_{i=1}^n\left(%s_{i}-\overline{%s}\right)^{2}}=%.2f \times\sqrt{\cfrac{1}{%d\times %d}\left[%s\right]}=%s' % (signal, signal, signal, phy_t(len(item)), len(item), len(item)-1, sumExpr, result.latex())) 90 | else: 91 | d_arr = item * 10**(-sciDigit) 92 | p_mean = item.mean() * 10**(-sciDigit) 93 | sumExpr = '+'.join([(r'%s^{2}' % (xi - p_mean).dlatex(1)) for xi in d_arr._NumItem__arr]) 94 | latex = LaTeX(r'u_{%s A}=t_{n}\sqrt{\cfrac{1}{n(n-1)}\sum\limits_{i=1}^n\left(%s_{i}-\overline{%s}\right)^{2}}=%.2f \times\sqrt{\cfrac{1}{%d \times %d}\left[%s\right]}\times 10^{%d}=%s' % (signal, signal, signal, phy_t(len(item)), len(item), len(item)-1, sumExpr, sciDigit, result.latex())) 95 | if needValue: 96 | return result, latex 97 | else: 98 | return latex 99 | return result 100 | 101 | def CombSamples(items, method='auto', process=False, needValue=False, sym=None, remainOneMoreDigit=False): 102 | '''合并样本的A类不确定度计算 103 | 【参数说明】 104 | 1.items(list):用于A类不确定度计算的多个样本数据。 105 | 2.method(可选,str):使用何种方法计算每个样本的A类不确定度,从以下列表中取值: 106 | (1)'auto':根据样本数量的大小,决定使用那种方法,即样本数量最大的组为9以上时,使用Bessel法;否则用极差法。 107 | (2)'Bessel':使用贝塞尔公式法。 108 | (3)'Range':使用极差法。 109 | (4)'CollegePhysics':使用大学物理实验中的不确定度公式。 110 | 3.process(可选,bool):是否获得计算过程。默认proces=False。 111 | 4.needValue(可选,bool):当获得计算过程时,是否返回计算结果。默认needValue=False。 112 | 5.sym(可选,str):合并样本的符号。默认sym=None。 113 | 6.remainOneMoreDigit(可选,bool):结果是否多保留一位有效数字。默认remainOneMoreDigit=False。 114 | 【返回值】 115 | ①process为False时,返回值为Num类型的合并样本的A类不确定度。 116 | ②process为True且needValue为False时,返回值为LaTeX类型的计算过程。 117 | ③process为True且needValue为True时,返回值为Num类型的合并样本的A类不确定度和LaTeX类型的计算过程组成的元组。''' 118 | m = len(items) 119 | #计算各组样本的标准偏差 120 | if method == 'auto': 121 | if max([len(item) for item in items]) > 9: #样本数量最大的组为9以上时,使用Bessel法 122 | s = [std.Bessel(item, remainOneMoreDigit=True) for item in items] 123 | method = 'Bessel' 124 | else: #样本数量均不超过9时,使用极差法 125 | s = [std.Range(item, remainOneMoreDigit=True) for item in items] 126 | method = 'Range' 127 | elif method == 'Bessel': 128 | s = [std.Bessel(item, remainOneMoreDigit=True) for item in items] 129 | elif method == 'Range': 130 | s = [std.Range(item, remainOneMoreDigit=True) for item in items] 131 | elif method == 'CollegePhysics': 132 | s = [std.CollegePhysics(item, remainOneMoreDigit=True) for item in items] 133 | if process: 134 | resArr = [] 135 | latex = LaTeX() 136 | if method == 'Range': 137 | for item in items: 138 | res = std.Range(item, remainOneMoreDigit=True) 139 | resArr.append(res) 140 | signal = item._NumItem__sym 141 | p_max, p_min = max(item._NumItem__arr), min(item._NumItem__arr) 142 | C = rC(len(item)) 143 | latex.add(r's_{%s}=\cfrac{R}{C}=\cfrac{%s-%s}{%s}=%s' % (signal, p_max.dlatex(), p_min.dlatex(2), C, res.latex())) 144 | else: 145 | for item in items: 146 | res = std.Bessel(item, remainOneMoreDigit=True) 147 | resArr.append(res) 148 | signal = item._NumItem__sym 149 | sciDigit = item._NumItem__sciDigit() 150 | if sciDigit == 0: 151 | p_mean = item.mean() 152 | sumExpr = '+'.join([(r'%s^{2}' % (xi - p_mean).dlatex(1)) for xi in item._NumItem__arr]) 153 | latex.add(r's_{%s}=\sqrt{\cfrac{1}{n-1}\sum\limits_{i=1}^n\left(%s_{i}-\overline{%s}\right)^{2}}=\sqrt{\cfrac{1}{%d}\left[%s\right]}=%s' % (signal, signal, signal, len(item)-1, sumExpr, res.latex())) 154 | else: 155 | d_arr = item * 10**(-sciDigit) 156 | p_mean = item.mean() * 10**(-sciDigit) 157 | sumExpr = '+'.join([(r'%s^{2}' % (xi - p_mean).dlatex(1)) for xi in d_arr._NumItem__arr]) 158 | latex.add(r's_{%s}=\sqrt{\cfrac{1}{n-1}\sum\limits_{i=1}^n\left(%s_{i}-\overline{%s}\right)^{2}}=\sqrt{\cfrac{1}{%d}\left[%s\right]}\times 10^{%d}=%s' % (signal, signal, signal, len(item)-1, sumExpr, sciDigit, res.latex())) 159 | #根据所有样本的样本数量是否一致,判断使用哪个公式 160 | nSame = True 161 | n = len(items[0]) 162 | for item in items: 163 | if len(item._NumItem__arr) != n: 164 | nSame = False 165 | break 166 | closeUnit() 167 | if nSame: 168 | nTotal = m*n 169 | sp = sqrt(sum([si**2 for si in s]) / m) 170 | if method == 'CollegePhysics': 171 | vSum = m*(n-1) 172 | else: 173 | nTotal = sum([len(item._NumItem__arr) for item in items]) 174 | dSum = 0 175 | vSum = 0 176 | if method == 'Range': 177 | for i in range(m): 178 | v = rv(len(items[i])) 179 | dSum += v * s[i]**2 180 | vSum += v 181 | sp = sqrt(dSum / vSum) 182 | else: 183 | for i in range(m): 184 | v = len(items[i]) - 1 185 | dSum += v * s[i]**2 186 | vSum += v 187 | sp = sqrt(dSum / vSum) 188 | openUnit() 189 | sp._Num__q = items[0]._NumItem__q 190 | if method == 'CollegePhysics': 191 | result = phy_t(vSum+1) * sp / nTotal**0.5 192 | else: 193 | result = sp / nTotal**0.5 194 | if not remainOneMoreDigit: 195 | result.cutOneDigit() 196 | if process: 197 | resItem = NumItem(resArr) 198 | sciDigit = resItem._NumItem__sciDigit() 199 | if sciDigit != 0: 200 | resItem = resItem * 10**(-sciDigit) 201 | if nSame: 202 | sumExpr = '+'.join([('%s^{2}' % res.dlatex(1)) for res in resItem._NumItem__arr]) 203 | if sciDigit == 0: 204 | latex.add(r's_{p}=\sqrt{\cfrac{\sum\limits_{i=1}^m {s_{%s i}}^{2}}{m}}=\sqrt{\cfrac{%s}{%d}}=%s' % (sym, sumExpr, m, sp.latex())) 205 | else: 206 | latex.add(r's_{p}=\sqrt{\cfrac{\sum\limits_{i=1}^m {s_{%s i}}^{2}}{m}}=\sqrt{\cfrac{%s}{%d}}\times 10^{%d}=%s' % (sym, sumExpr, m, sciDigit, sp.latex())) 207 | if method == 'CollegePhysics': 208 | latex.add(r'u_{%s A}=\cfrac{t_{v+1}s_p}{\sqrt{mn}}=\cfrac{%.2f \times %s}{\sqrt{%s}}=%s' % (sym, phy_t(vSum+1), sp.dlatex(2), nTotal, result.latex())) 209 | else: 210 | latex.add(r'u_{%s A}=\cfrac{s_p}{\sqrt{mn}}=\cfrac{%s}{\sqrt{%s}}=%s' % (sym, sp.dlatex(), nTotal, result.latex())) 211 | else: 212 | sumExpr = '' 213 | if method == 'Range': 214 | for i in range(len(items)): 215 | sumExpr += r'%s \times %s^{2}+' % (rv(len(items[i]._NumItem__arr)), resItem._NumItem__arr[i].dlatex(1)) 216 | vSumExpr = '+'.join(['%s' % rv(len(item._NumItem__arr)) for item in items]) 217 | else: 218 | for i in range(len(items)): 219 | sumExpr += r'%s \times %s^{2}+' % (len(items[i]._NumItem__arr) - 1, resItem._NumItem__arr[i].dlatex(1)) 220 | vSumExpr = '+'.join(['%s' % (len(item._NumItem__arr) - 1) for item in items]) 221 | sumExpr = sumExpr[:-1] 222 | if sciDigit == 0: 223 | latex.add(r's_{p}=\sqrt{\cfrac{\sum\limits_{i=1}^m \left(v_{i}s_{%s i}^{2}\right)}{\sum\limits_{i=1}^m v_{i}}}=\sqrt{\cfrac{%s}{%s}}=%s' % (sym, sumExpr, vSumExpr, sp.latex())) 224 | else: 225 | latex.add(r's_{p}=\sqrt{\cfrac{\sum\limits_{i=1}^m \left(v_{i}s_{%s i}^{2}\right)}{\sum\limits_{i=1}^m v_{i}}}=\sqrt{\cfrac{%s}{%s}}\times 10^{%d}=%s' % (sym, sumExpr, vSumExpr, sciDigit, sp.latex())) 226 | if method == 'CollegePhysics': 227 | latex.add(r'u_{%s A}=\cfrac{t_{v+1}s_p}{\sqrt{\sum\limits_{i=1}^m n_{i}}}=\cfrac{%s \times %s}{\sqrt{%s}}=%s' % (sym, phy_t(vSum+1), sp.dlatex(2), nTotal, result.latex())) 228 | else: 229 | latex.add(r'u_{%s A}=\cfrac{s_p}{\sqrt{\sum\limits_{i=1}^m n_{i}}}=\cfrac{%s}{\sqrt{%s}}=%s' % (sym, sp.dlatex(), nTotal, result.latex())) 230 | if needValue: 231 | return result, latex 232 | else: 233 | return latex 234 | return result -------------------------------------------------------------------------------- /analyticlab/measure/BCategory.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Feb 6 18:22:57 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | from ..num import Num 8 | from ..latexoutput import LaTeX 9 | 10 | kValue = (1, 3**0.5, 6**0.5, 2**0.5, 1) 11 | kExpr = ('1', r'\sqrt{3}', r'\sqrt{6}', r'\sqrt{2}', '1') 12 | 13 | def b(instrument, sym=None, process=False, needValue=False, remainOneMoreDigit=True): 14 | '''计算B类不确定度 15 | 【参数说明】 16 | 1.instrument(Ins):测量仪器。可以在analyticlab.uncertainty.ins模块中选择已经预设好的测量仪器,或者通过Ins类创建新的测量仪器。 17 | 2.sym(可选,str):符号。默认sym=None。 18 | 3.process(可选,bool):是否展示计算过程。默认proces=False。 19 | 4.needValue(可选,bool):当获得计算过程时,是否返回计算结果。默认needValue=False。 20 | 5.remainOneMoreDigit(可选,bool):结果是否多保留一位有效数字。默认remainOneMoreDigit=True。 21 | 【返回值】 22 | ①process为False时,返回值为Num类型的B类不确定度。 23 | ②process为True且needValue为False时,返回值为LaTeX类型的计算过程。 24 | ③process为True且needValue为True时,返回值为Num类型的B类不确定度和LaTeX类型的计算过程组成的元组。''' 25 | a = instrument.a 26 | a.setSciBound(9) 27 | if instrument.distribution <= 4: 28 | uB = a / kValue[instrument.distribution] 29 | else: 30 | uB = a / (6/(1+instrument.beta**2))**0.5 31 | if remainOneMoreDigit: 32 | uB.remainOneMoreDigit() 33 | if process: 34 | if instrument.distribution <= 4: 35 | latex = LaTeX(r'u_{%s B}=\cfrac{a}{k}=\cfrac{%s}{%s}=%s' % (sym, a.dlatex(), kExpr[instrument.distribution], uB.latex())) 36 | else: 37 | latex = LaTeX(r'u_{%s B}=\cfrac{a}{k}=\cfrac{%s}{\sqrt{6/(1+%g^{2})}}=%s' % (sym, a.dlatex(), instrument.beta, uB.latex())) 38 | if needValue: 39 | return uB, latex 40 | else: 41 | return latex 42 | return uB 43 | -------------------------------------------------------------------------------- /analyticlab/measure/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Feb 18 09:25:00 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | 8 | from . import ins, std, ACategory, BCategory 9 | from .basemeasure import BaseMeasure 10 | from .measure import Measure 11 | from .ins import Ins 12 | -------------------------------------------------------------------------------- /analyticlab/measure/ins.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Feb 6 19:20:06 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | 8 | from quantities.quantity import Quantity 9 | from ..num import Num 10 | 11 | class Ins(): 12 | '''Ins为测量仪器类,该类用于描述一个测量仪器的B类不确定度和仪器测量值的单位。''' 13 | norm = 0 #正态分布 14 | rectangle = 1 #矩形分布 15 | triangle = 2 #三角分布 16 | arcsin = 3 #反正弦分布 17 | twopoints = 4 #两点分布 18 | trapezoid = 5 #梯形分布 19 | 20 | def __init__(self, halfWidth, distribution=1, unit=None, **param): 21 | '''初始化一个测量仪器 22 | 【参数说明】 23 | 1.halfWidth(str或Num):半宽度。可以选择以字符串或Num数值形式给出。 24 | 2.distribution(可选,int):分布类型,从以下列表中取值。默认distribution=Ins.rectangle。 25 | ①Ins.norm:正态分布; 26 | ②Ins.rectangle:矩形分布; 27 | ③Ins.triangle:三角分布; 28 | ④Ins.arcsin:反正弦分布; 29 | ⑤Ins.twopoints:两点分布; 30 | ⑥Ins.trapezoid:梯形分布,此分布下需要通过附加参数beta给出β值。 31 | 3.unit(可选,str):测量结果的单位。默认unit=None。 32 | ''' 33 | self.distribution = distribution 34 | if unit == None: 35 | self.q = 1 36 | else: 37 | self.q = Quantity(1., unit) if type(unit) == str else unit 38 | if type(halfWidth) == str: 39 | self.a = Num(halfWidth, self.q) 40 | elif type(halfWidth) == Num: 41 | self.a = halfWidth 42 | if unit == None: 43 | self.q = halfWidth._Num__q 44 | else: 45 | halfWidth._Num__q = self.q 46 | self.halfWidth = halfWidth 47 | if len(param) > 0: 48 | self.beta = param['beta'] 49 | 50 | 刻度尺_毫米_1 = Ins('0.5', unit='mm') 51 | 游标卡尺_毫米_2 = Ins('0.02', unit='mm') 52 | 游标卡尺_厘米_3 = Ins('0.002', unit='cm') 53 | 游标卡尺_米_5 = Ins('0.00002', unit='m') 54 | 一级千分尺_毫米_3 = Ins('0.004', unit='mm') 55 | 显微镜螺旋测微器_毫米_3 = Ins('0.015', unit='mm') 56 | 米尺_米_4 = Ins('0.0005', unit='m') 57 | 米尺_厘米_2 = Ins('0.05', unit='cm') 58 | 米尺_厘米_1 = Ins('0.5', unit='cm') 59 | 电子天平_克_4 = Ins('0.0005', unit='g') 60 | 分析天平_克_5 = Ins('0.00005', unit='g') 61 | -------------------------------------------------------------------------------- /analyticlab/measure/measure.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Feb 6 22:21:31 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | 8 | import re, sympy, copy 9 | from sympy import Symbol, diff 10 | from quantities.quantity import Quantity 11 | from ..amath import sqrt 12 | from ..const import Const 13 | from ..lsym import LSym 14 | from ..system.exceptions import keyNotInTableException 15 | from ..system.text_unicode import usub 16 | from ..system.unit_open import openUnit, closeUnit 17 | from ..system.format_units import format_units_unicode, format_units_latex 18 | 19 | KTable = {0.67:(0.50,'50'), 1.645:(0.90,'90'), 1.960:(0.95,'95'), 2:(0.9545,'95'), 2.576:(0.99,'99'), 3:(0.9973,'99')} 20 | 21 | class Measure(): 22 | ## vl的含义: 23 | ## vl = value + lsym:process为True时,返回lsym符号;为False时,返回Num数值 24 | process = False #静态属性,表明是否展示计算过程 25 | simplifyUnc = False #静态属性,表明是否化简不确定度计算式 26 | 27 | __isPureMulDiv = True 28 | __K = None 29 | __q_rep = None 30 | useRelUnc = False 31 | 32 | def __newInstance(self, symbol, vl, baseMeasures, consts, lsyms, isPureMulDiv): 33 | new = Measure() 34 | new.useRelUnc = False 35 | new.__symbol = symbol 36 | new.__vl = vl 37 | new.__baseMeasures = baseMeasures 38 | new.__consts = consts 39 | new.__lsyms = lsyms 40 | if self.__isPureMulDiv == False or isPureMulDiv == False: 41 | new.__isPureMulDiv = False 42 | return new 43 | 44 | def __process(self): 45 | return Measure.process 46 | 47 | def __getSymbol(self, obj): 48 | '''获得用于符号运算的符号(包括sObj和vlObj),以及基本组成baseMeasures、consts和lsyms''' 49 | baseMeasures = self.__baseMeasures 50 | consts = self.__consts 51 | lsyms = self.__lsyms 52 | if str(type(self)) == "" and self._BaseMeasure__sym not in baseMeasures: #对于测量,判断是否要将该测量添加到测量列表中 53 | if Measure.process: 54 | baseMeasures[self._BaseMeasure__sym] = (self, self.__vl, LSym('u_{%s}' % self._BaseMeasure__sym, self.unc(remainOneMoreDigit=True))) 55 | self.__vl = copy.deepcopy(self.__vl) 56 | self.__vl._LSym__symText = r'{\overline%s}' % self.__vl._LSym__symText 57 | else: 58 | baseMeasures[self._BaseMeasure__sym] = (self, self.__vl, self.unc(remainOneMoreDigit=True)) 59 | if type(obj) == Measure: 60 | for oKey in obj._Measure__baseMeasures.keys(): 61 | if oKey not in baseMeasures: 62 | baseMeasures[oKey] = obj._Measure__baseMeasures[oKey] 63 | for oKey in obj._Measure__consts.keys(): 64 | if oKey not in consts: 65 | consts[oKey] = obj._Measure__consts[oKey] 66 | for oKey in obj._Measure__lsyms.keys(): 67 | if oKey not in lsyms: 68 | lsyms[oKey] = obj._Measure__lsyms[oKey] 69 | sObj = obj._Measure__symbol 70 | vlObj = obj.__vl 71 | elif str(type(obj)) == "": 72 | vlObj = None 73 | if obj._BaseMeasure__sym not in baseMeasures: 74 | if Measure.process: 75 | baseMeasures[obj._BaseMeasure__sym] = (obj, obj.__vl, LSym('u_{%s}' % obj._BaseMeasure__sym, obj.unc(remainOneMoreDigit=True))) 76 | vlObj = copy.deepcopy(obj.__vl) 77 | vlObj._LSym__symText = r'{\overline%s}' % vlObj._LSym__symText 78 | else: 79 | baseMeasures[obj._BaseMeasure__sym] = (obj, obj.__vl, obj.unc(remainOneMoreDigit=True)) 80 | sObj = obj._Measure__symbol 81 | else: 82 | sObj = baseMeasures[obj._BaseMeasure__sym][0]._Measure__symbol 83 | if vlObj == None: 84 | vlObj = obj.__vl 85 | elif type(obj) == Const: 86 | if obj._Const__symText not in self.__consts: 87 | sObj = Symbol(obj._Const__symText, real=True) 88 | consts[obj._Const__symText] = (obj, sObj) 89 | else: 90 | sObj = self.__consts[obj._Const__symText][1] 91 | if Measure.process: 92 | vlObj = obj 93 | else: 94 | vlObj = obj.value() 95 | elif type(obj) == LSym: 96 | if obj._LSym__symText not in self.__lsyms: 97 | sObj = Symbol(obj._LSym__symText, real=True) 98 | lsyms[obj._LSym__symText] = (obj, sObj) 99 | else: 100 | sObj = self.__lsyms[obj._LSym__symText][1] 101 | if Measure.process: 102 | vlObj = obj 103 | else: 104 | vlObj = obj.num() 105 | else: 106 | sObj = obj 107 | vlObj = obj 108 | return sObj, vlObj, baseMeasures, consts, lsyms 109 | 110 | def __neg__(self): 111 | return self.__newInstance(-self.__symbol, -self.__vl, self.__baseMeasures, self.__consts, self.__lsyms, True) 112 | 113 | def __add__(self, obj): 114 | sObj, vlObj, baseMeasures, consts, lsyms = self.__getSymbol(obj) 115 | symbol = self.__symbol + sObj 116 | vl = self.__vl + vlObj 117 | return self.__newInstance(symbol, vl, baseMeasures, consts, lsyms, False) 118 | 119 | def __radd__(self, obj): 120 | sObj, vlObj, baseMeasures, consts, lsyms = self.__getSymbol(obj) 121 | symbol = sObj + self.__symbol 122 | vl = vlObj + self.__vl 123 | return self.__newInstance(symbol, vl, baseMeasures, consts, lsyms, False) 124 | 125 | def __sub__(self, obj): 126 | sObj, vlObj, baseMeasures, consts, lsyms = self.__getSymbol(obj) 127 | symbol = self.__symbol - sObj 128 | vl = self.__vl - vlObj 129 | return self.__newInstance(symbol, vl, baseMeasures, consts, lsyms, False) 130 | 131 | def __rsub__(self, obj): 132 | sObj, vlObj, baseMeasures, consts, lsyms = self.__getSymbol(obj) 133 | symbol = sObj - self.__symbol 134 | vl = vlObj - self.__vl 135 | return self.__newInstance(symbol, vl, baseMeasures, consts, lsyms, False) 136 | 137 | def __mul__(self, obj): 138 | sObj, vlObj, baseMeasures, consts, lsyms = self.__getSymbol(obj) 139 | symbol = self.__symbol * sObj 140 | vl = self.__vl * vlObj 141 | if type(obj) == Measure: 142 | return self.__newInstance(symbol, vl, baseMeasures, consts, lsyms, obj.__isPureMulDiv) 143 | else: 144 | return self.__newInstance(symbol, vl, baseMeasures, consts, lsyms, True) 145 | 146 | def __rmul__(self, obj): 147 | sObj, vlObj, baseMeasures, consts, lsyms = self.__getSymbol(obj) 148 | symbol = sObj * self.__symbol 149 | vl = vlObj * self.__vl 150 | if type(obj) == Measure: 151 | return self.__newInstance(symbol, vl, baseMeasures, consts, lsyms, obj.__isPureMulDiv) 152 | else: 153 | return self.__newInstance(symbol, vl, baseMeasures, consts, lsyms, True) 154 | 155 | def __truediv__(self, obj): 156 | sObj, vlObj, baseMeasures, consts, lsyms = self.__getSymbol(obj) 157 | symbol = self.__symbol / sObj 158 | vl = self.__vl / vlObj 159 | if type(obj) == Measure: 160 | return self.__newInstance(symbol, vl, baseMeasures, consts, lsyms, obj.__isPureMulDiv) 161 | else: 162 | return self.__newInstance(symbol, vl, baseMeasures, consts, lsyms, True) 163 | 164 | def __rtruediv__(self, obj): 165 | sObj, vlObj, baseMeasures, consts, lsyms = self.__getSymbol(obj) 166 | symbol = sObj / self.__symbol 167 | vl = vlObj / self.__vl 168 | if type(obj) == Measure: 169 | return self.__newInstance(symbol, vl, baseMeasures, consts, lsyms, obj.__isPureMulDiv) 170 | else: 171 | return self.__newInstance(symbol, vl, baseMeasures, consts, lsyms, True) 172 | 173 | def __pow__(self, obj): 174 | '''幂运算''' 175 | sObj, vlObj, baseMeasures, consts, lsyms = self.__getSymbol(obj) 176 | symbol = self.__symbol ** sObj 177 | vl = self.__vl ** vlObj 178 | if type(obj) == Measure: 179 | return self.__newInstance(symbol, vl, baseMeasures, consts, lsyms, obj.__isPureMulDiv) 180 | else: 181 | return self.__newInstance(symbol, vl, baseMeasures, consts, lsyms, True) 182 | 183 | def __rpow__(self, obj): 184 | '''指数运算''' 185 | sObj, vlObj, baseMeasures, consts, lsyms = self.__getSymbol(obj) 186 | symbol = sObj ** self.__symbol 187 | vl = vlObj ** self.__vl 188 | return self.__newInstance(symbol, vl, baseMeasures, consts, lsyms, False) 189 | 190 | def setK(self, K): 191 | '''设置扩展不确定度的K值''' 192 | if K not in KTable: 193 | raise keyNotInTableException('找不到K=%s时对应的置信区间') 194 | self.__K = K 195 | 196 | def __checkIsNumber(s): 197 | '''检查一个字符串是否为数字''' 198 | if s.find('^'): 199 | s = s.split('^')[0] 200 | try: 201 | float(s) 202 | return True 203 | except ValueError: 204 | return False 205 | 206 | def __getUnc(self): 207 | '''获得合成不确定度''' 208 | ms = self.__baseMeasures 209 | if self.__isPureMulDiv: 210 | #对于纯乘除测量公式的不确定度计算 211 | meaTimes = [] 212 | symbolExpr = str(self.__symbol) #获得诸如x1**2*x2或4*x1**2*x2**3/(x3*x4)的符号表达式 213 | symbolExpr = symbolExpr.replace('**', '^') #将**替换成^,防止**干扰* 214 | for ci in self.__consts.values(): #将所有Const常数替换为1,防止符号干扰 215 | symbolExpr = symbolExpr.replace(ci[0]._Const__symText, '1') 216 | for li in self.__lsyms.values(): #将所有LSym替换为1,防止符号干扰 217 | symbolExpr = symbolExpr.replace(li[0]._LSym__symText, '1') 218 | mulArr = symbolExpr.split('/') #以除号为界分割符号表达式 219 | if len(mulArr) == 2: #如果除号右边有表达式,去掉右边其中的括号,得到['4*x1^2*x2^3', 'x3*x4'] 220 | mulArr[1] = mulArr[1].replace('(', '').replace(')', '') 221 | mulArr = [mri.split('*') for mri in mulArr] #分割乘号,得到['4', 'x1^2', 'x2^3'] 222 | if len(mulArr) == 2: #合并所有的被乘项 223 | mulArr = mulArr[0] + mulArr[1] 224 | else: 225 | mulArr = mulArr[0] 226 | for mri in mulArr: 227 | if not Measure.__checkIsNumber(mri): #只对非纯数字和非常数进行处理 228 | si = mri.split('^') #将每个符号对应的测量和次数映射到dict中 229 | if len(si) == 1: 230 | meaTimes.append([si[0], 1]) 231 | else: 232 | meaTimes.append([si[0], float(si[1])]) 233 | closeUnit() 234 | def times(mea): 235 | if mea[1] == 1: 236 | return (ms[mea[0]][2]/ms[mea[0]][1])**2 237 | else: 238 | return (mea[1]*ms[mea[0]][2]/ms[mea[0]][1])**2 239 | uSum = times(meaTimes[0]) 240 | for mea in meaTimes[1:]: 241 | uSum += times(mea) 242 | res = sqrt(uSum) 243 | openUnit() 244 | else: 245 | #对于纯乘除测量公式的不确定度计算,使用sympy求偏导实现 246 | y = self.__symbol 247 | m = list(self.__baseMeasures.values()) 248 | cs, ls = self.__consts, self.__lsyms 249 | um0 = Symbol('u_{%s}' % m[0][0]._BaseMeasure__sym, real=True) 250 | ssum = diff(y, m[0][0]._Measure__symbol)**2 * um0**2 251 | for mi in m[1:]: 252 | umi = Symbol('u_{%s}' % mi[0]._BaseMeasure__sym, real=True) 253 | ssum += diff(y, mi[0]._Measure__symbol)**2 * umi**2 254 | u = sympy.sqrt(ssum) 255 | if Measure.simplifyUnc: 256 | u = sympy.simplify(u) 257 | uExpr = str(u) 258 | def repRat(matched): 259 | return 'Rational%s' % matched.group('rat').replace('/', ',') 260 | uExpr = re.sub('(?P\((-?\d+)(\.\d+)?/(-?\d+)(\.\d+)?\))', repRat, uExpr) 261 | uExpr = uExpr.replace('log(10)', '2.303').replace('Abs', 'abs') 262 | for mi in m: 263 | si = mi[0]._BaseMeasure__sym 264 | uExpr = uExpr.replace(si, 'ms[r"%s"][1]' % si) 265 | uExpr = uExpr.replace('u_{ms[r"%s"][1]}' % si, 'ms[r"%s"][2]' % si) 266 | for ci in self.__consts.values(): 267 | uExpr = uExpr.replace(ci[0]._Const__symText, 'cs[r"%s"][0]' % ci[0]._Const__symText) 268 | for li in self.__lsyms.values(): 269 | uExpr = uExpr.replace(li[0]._LSym__symText, 'ls[r"%s"][0]' % li[0]._LSym__symText) 270 | closeUnit() 271 | res = eval(uExpr) 272 | openUnit() 273 | if Measure.process: 274 | res._LSym__sNum._Num__q = self.__q_rep if self.__q_rep != None else self.__vl._LSym__sNum._Num__q 275 | else: 276 | res._Num__q = self.__q_rep if self.__q_rep != None else self.__vl._Num__q 277 | return res 278 | 279 | def __res(self): 280 | '''返回供展示不确定度使用的结果''' 281 | if not Measure.process: 282 | return 283 | res = {} 284 | res['K'] = self.__K 285 | if self.__K != None: 286 | res['P'] = KTable[self.__K] 287 | if type(self) == Measure: 288 | uncLSym = self.__getUnc() 289 | unc = uncLSym._LSym__sNum 290 | unc.setIsRelative(self.__isPureMulDiv) 291 | res['unc'] = uncLSym._LSym__sNum 292 | res['isRate'] = self.__isPureMulDiv 293 | if Measure.process: 294 | res['uncLSym'] = uncLSym 295 | if not self.__isPureMulDiv: 296 | res['baseMeasures'] = self.__baseMeasures 297 | return res 298 | 299 | def value(self): 300 | '''获得测量值 301 | 【返回值】 302 | Num:测量值。''' 303 | if Measure.process: 304 | return self.__vl.num() 305 | else: 306 | return self.__vl 307 | 308 | def valueLSym(self): 309 | assert Measure.process, 'Measure.process为False时,无法获取LSym' 310 | return self.__vl 311 | 312 | def unc(self): 313 | '''获得不确定度 314 | 【返回值】 315 | Num:K=1时,为标准不确定度数值;K>1时,为扩展不确定度数值。''' 316 | if Measure.process: 317 | unc = self.__getUnc()._LSym__sNum 318 | else: 319 | unc = self.__getUnc() 320 | if self.__K != None: 321 | unc *= self.__K 322 | return unc 323 | 324 | def relUnc(self): 325 | '''获得相对不确定度 326 | 【返回值】 327 | Num:K=1时,为相对标准不确定度数值;K>1时,为相对扩展不确定度数值。''' 328 | ur = self.unc() / self.value() 329 | ur.setIsRelative = True 330 | return ur 331 | 332 | def uncLSym(self): 333 | assert Measure.process, 'Measure.process为False时,无法获取LSym' 334 | u = self.__getUnc() 335 | return u 336 | 337 | def resetUnit(self, unit=None): 338 | '''重设测量(含测量值和不确定度)的单位 339 | 【参数说明】 340 | unit(可选,str):重设后的单位。默认unit=None,即没有单位。''' 341 | assert type(self) == Measure, '单位重设仅供Measure使用,不可用于BaseMeasure' 342 | if unit == None: 343 | q = 1 344 | else: 345 | q = Quantity(1., unit) if type(unit) == str else unit 346 | self.value()._Num__q = q 347 | self.__q_rep = q 348 | 349 | def __str__(self): 350 | '''获得测量值和不确定度的字符串形式 351 | 【返回值】 352 | str:(测量值±不确定度),如已给出单位,会附加单位''' 353 | val = self.value() 354 | u = self.unc() 355 | if Measure.process: 356 | unitExpr = format_units_unicode(self.__vl._LSym__sNum._Num__q) 357 | else: 358 | unitExpr = format_units_unicode(self.__vl._Num__q) 359 | sciDigit = val._Num__sciDigit() 360 | if self.useRelUnc: 361 | ur = u / val 362 | ur.setIsRelative(True) 363 | expr = r'%s(1±%s)%s' % (val.strNoUnit(), ur, unitExpr) 364 | else: 365 | if sciDigit == 0: 366 | u._Num__setDigit(val._Num__d_front, val._Num__d_behind, val._Num__d_valid) 367 | while float(u.strNoUnit()) == 0: 368 | u.remainOneMoreDigit() 369 | expr = r'%s±%s' % (val.strNoUnit(), u.strNoUnit()) 370 | if unitExpr != '': 371 | expr = '(%s)%s' % (expr, unitExpr) 372 | else: 373 | val *= 10**(-sciDigit) 374 | u *= 10**(-sciDigit) 375 | u._Num__setDigit(val._Num__d_front, val._Num__d_behind, val._Num__d_valid) 376 | while float(u.strNoUnit()) == 0: 377 | u.remainOneMoreDigit() 378 | expr = r'(%s±%s)×10%s%s' % (val.strNoUnit(), u.strNoUnit(), usub(sciDigit), unitExpr) 379 | return expr 380 | 381 | def __repr__(self): 382 | '''获得测量值和不确定度的字符串形式 383 | 【返回值】 384 | str:(测量值±不确定度),如已给出单位,会附加单位''' 385 | return self.__str__() 386 | 387 | def latex(self): 388 | val = self.value() 389 | u = self.unc() 390 | if Measure.process: 391 | unitExpr = format_units_latex(self.__vl._LSym__sNum._Num__q) 392 | else: 393 | unitExpr = format_units_latex(self.__vl._Num__q) 394 | sciDigit = val._Num__sciDigit() 395 | if self.useRelUnc: 396 | ur = u / val 397 | ur.setIsRelative(True) 398 | expr = r'%s\left(1 \pm %s\right)%s' % (val.strNoUnit(), ur.dlatex(), unitExpr) 399 | else: 400 | <<<<<<< HEAD 401 | if sciDigit == 0: 402 | u._Num__setDigit(val._Num__d_front, val._Num__d_behind, val._Num__d_valid) 403 | while float(u.strNoUnit()) == 0: 404 | u.remainOneMoreDigit() 405 | expr = r'\left(%s \pm %s\right)%s' % (val.strNoUnit(), u.strNoUnit(), unitExpr) 406 | else: 407 | val *= 10**(-sciDigit) 408 | u *= 10**(-sciDigit) 409 | u._Num__setDigit(val._Num__d_front, val._Num__d_behind, val._Num__d_valid) 410 | while float(u.strNoUnit()) == 0: 411 | u.remainOneMoreDigit() 412 | expr = r'\left(%s \pm %s\right)\times 10^{%d}%s' % (val.strNoUnit(), u.strNoUnit(), sciDigit, unitExpr) 413 | return expr 414 | 415 | def _repr_latex_(self): 416 | return r'$\begin{align}%s\end{align}$' % self.latex() 417 | ======= 418 | val *= 10**(-sciDigit) 419 | u *= 10**(-sciDigit) 420 | u._Num__setDigit(val._Num__d_front, val._Num__d_behind, val._Num__d_valid) 421 | while float(u.strNoUnit()) == 0: 422 | u.remainOneMoreDigit() 423 | expr = r'\left(%s \pm %s\right)\times 10^{%d}%s' % (val.strNoUnit(), u.strNoUnit(), sciDigit, unitExpr) 424 | return expr 425 | 426 | def _repr_latex_(self): 427 | return r'$\begin{align}%s\end{align}$' % self.latex() 428 | >>>>>>> fcd8aeb38c983d9242fa950ef4c982492c7b950e 429 | -------------------------------------------------------------------------------- /analyticlab/measure/std.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Feb 6 10:18:22 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | 8 | from ..system.statformat import statFormat, getMaxDeltaDigit 9 | from ..lookup.RangeTable import C as rC 10 | 11 | def Bessel(item, remainOneMoreDigit=False): 12 | '''贝塞尔公式法计算标准偏差 13 | 【参数说明】 14 | 1.item:用于计算标准偏差的样本数据。 15 | 2.remainOneMoreDigit(可选,bool):结果是否多保留一位有效数字。默认remainOneMoreDigit=False。 16 | 【返回值】 17 | Num:标准偏差数值。''' 18 | mean = item.mean() 19 | dsum = sum([(ni._Num__num - mean._Num__num)**2 for ni in item._NumItem__arr]) 20 | s = (dsum / (len(item._NumItem__arr) - 1))**0.5 21 | result = statFormat(getMaxDeltaDigit(item, mean), s) 22 | result._Num__q = item._NumItem__q 23 | if remainOneMoreDigit: 24 | result.remainOneMoreDigit() 25 | return result 26 | 27 | def Range(item, remainOneMoreDigit=False): 28 | '''极差法计算标准偏差 29 | 【参数说明】 30 | 1.item:用于计算标准偏差的样本数据。 31 | 2.remainOneMoreDigit(可选,bool):结果是否多保留一位有效数字。默认remainOneMoreDigit=False。 32 | 【返回值】 33 | Num:标准偏差数值。''' 34 | R = max(item._NumItem__arr) - min(item._NumItem__arr) 35 | C = rC(len(item._NumItem__arr)) 36 | result = R/C 37 | if remainOneMoreDigit: 38 | result.remainOneMoreDigit() 39 | return result 40 | 41 | def CollegePhysics(item, remainOneMoreDigit=False): 42 | '''大学物理实验中的标准偏差计算 43 | 【参数说明】 44 | 1.item:用于计算标准偏差的样本数据。 45 | 2.remainOneMoreDigit(可选,bool):结果是否多保留一位有效数字。默认remainOneMoreDigit=False。 46 | 【返回值】 47 | Num:标准偏差数值。''' 48 | return Bessel(item, remainOneMoreDigit) 49 | -------------------------------------------------------------------------------- /analyticlab/system/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Feb 18 09:25:00 2018 4 | 5 | @author: wzv100 6 | """ 7 | 8 | -------------------------------------------------------------------------------- /analyticlab/system/exceptions.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Jan 23 18:59:21 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | 8 | class confLevelUnsupportedException(BaseException): 9 | def __init__(self, confLevel, tableType): 10 | print('不支持置信度为%s的%s' % (confLevel, tableType)) 11 | 12 | class alphaUnsupportedException(BaseException): 13 | def __init__(self, alpha, tableType): 14 | print('不支持显著性水平为%s的%s' % (alpha, tableType)) 15 | 16 | class tooMuchDataForTestException(BaseException): 17 | def __init__(self, msg): 18 | print(msg) 19 | 20 | class tooLessDataForTestException(BaseException): 21 | def __init__(self, tableType): 22 | print('可供%s的数据个数太少,无法进行%s' % (tableType, tableType)) 23 | 24 | class expressionInvalidException(BaseException): 25 | def __init__(self, msg): 26 | print(msg) 27 | 28 | class muNotFoundException(BaseException): 29 | def __init__(self, msg): 30 | print(msg) 31 | 32 | class itemNotSameLengthException(BaseException): 33 | def __init__(self, msg): 34 | print(msg) 35 | 36 | class itemNotSameTypeException(BaseException): 37 | def __init__(self, msg): 38 | print(msg) 39 | 40 | class itemNotSameKeysException(BaseException): 41 | def __init__(self, msg): 42 | print(msg) 43 | 44 | class keyNotInTableException(BaseException): 45 | def __init__(self, msg): 46 | print(msg) 47 | 48 | class processStateWrongException(BaseException): 49 | def __init__(self): 50 | print('Uncertainty.process没有初始化为True,不能展示不确定度') 51 | 52 | class subUnsupportedException(BaseException): 53 | def __init__(self, char): 54 | print('不支持字符\'%s\'的上标' % char) 55 | 56 | class supUnsupportedException(BaseException): 57 | def __init__(self, char): 58 | print('不支持字符\'%s\'的下标' % char) -------------------------------------------------------------------------------- /analyticlab/system/format_units.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Wed Aug 15 10:25:06 2018 5 | 6 | @author: xingrongtech 7 | Referenced and modified by `markup` module, `quantities` package written by bjodah and apdavison 8 | """ 9 | 10 | import re 11 | from quantities.quantity import Quantity 12 | 13 | superscripts = ['⁰', '¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸', '⁹'] 14 | rad = Quantity(1., 'rad') 15 | deg = Quantity(1., 'deg') 16 | 17 | def superscript(val): 18 | items = re.split(r'\*{2}([\d]+)(?!\.)', val) 19 | ret = [] 20 | while items: 21 | try: 22 | s = items.pop(0) 23 | e = items.pop(0) 24 | ret.append(s+''.join(superscripts[int(i)] for i in e)) 25 | except IndexError: 26 | ret.append(s) 27 | return ''.join(ret) 28 | 29 | 30 | def format_units_unicode(q): 31 | if type(q) != Quantity or len(q.dimensionality.items()) == 0: 32 | return '' 33 | res = str(q.dimensionality) 34 | res = superscript(res) 35 | res = res.replace('**', '^').replace('*','·').replace('deg', '°') 36 | return res 37 | 38 | def format_units_latex(q, paren=False): 39 | ''' 40 | Replace the units string provided with an equivalent latex string. 41 | 42 | Exponentiation (m**2) will be replaced with superscripts (m^{2}) 43 | 44 | Multiplication (*) are replaced with the symbol specified by the mult argument. 45 | By default this is the latex \cdot symbol. Other useful 46 | options may be '' or '*'. 47 | 48 | If paren=True, encapsulate the string in '\left(' and '\right)' 49 | 50 | The result of format_units_latex is encapsulated in $. This allows the result 51 | to be used directly in Latex in normal text mode, or in Matplotlib text via the 52 | MathText feature. 53 | 54 | Restrictions: 55 | This routine will not put CompoundUnits into a fractional form. 56 | ''' 57 | if type(q) != Quantity or len(q.dimensionality.items()) == 0: 58 | return '' 59 | res = str(q.dimensionality) 60 | if res.startswith('(') and res.endswith(')'): 61 | # Compound Unit 62 | compound = True 63 | else: 64 | # Not a compound unit 65 | compound = False 66 | # Replace division (num/den) with \frac{num}{den} 67 | #res = re.sub(r'(?P.+)/(?P.+)',r'\\frac{\g}{\g}',res) 68 | # Replace exponentiation (**exp) with ^{exp} 69 | res = re.sub(r'\*{2,2}(?P\d+)',r'^{\g}',res) 70 | # Remove multiplication signs 71 | res = re.sub(r'\*',r' \cdot ',res) 72 | res = res.replace('deg', r'{^\circ}') 73 | if paren and not compound: 74 | res = r'\left(%s\right)' % res 75 | res = r'{\rm %s}' % res 76 | return res -------------------------------------------------------------------------------- /analyticlab/system/numberformat.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Jan 22 06:17:01 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | 8 | from math import log10, floor 9 | 10 | def f(num, digit): 11 | '''实现四舍六入五成双''' 12 | numConverted = num * 10**(digit+1) 13 | numInt = int(numConverted) 14 | unit = numInt % 10 15 | if unit <= 4: #四舍 16 | numInt = (numInt - unit) // 10 17 | elif unit >= 6: #六入 18 | numInt = (numInt - unit) // 10 + 1 19 | elif unit == 5: #五成双 20 | if numConverted - numInt > 0: #若保留位后面有剩余数字,则直接进一位 21 | numInt = (numInt - unit) // 10 + 1 22 | else: 23 | decade = (numInt - unit) % 100 // 10 24 | if decade % 2 == 0: #偶数保留 25 | numInt = (numInt - unit) // 10 26 | else: #奇数进一 27 | numInt = (numInt - unit) // 10 + 1 28 | return numInt * 10**(-digit) 29 | 30 | def fstr(num, digit): 31 | if digit > 0: 32 | return ('%.' + str(digit) + 'f') % f(num, digit) 33 | else: 34 | return '%d' % f(num, digit) 35 | 36 | def cutInt(number): 37 | '''去除数字的整数部分''' 38 | return number - int(number); 39 | 40 | def getDigitFront(usign): 41 | '''获得小数点前的数字位数''' 42 | usign = int(usign) 43 | if usign == 0: #重置小数点前位数 44 | return 0 45 | else: 46 | return int(log10(usign * 1.00000000001)) + 1 47 | 48 | def getDigitBehind(usign, digit_valid, digit_front): 49 | '''根据已知的有效数字位数和小数点前位数,得到小数点后的应有位数''' 50 | digit_behind = digit_valid - digit_front 51 | if digit_front == 0: 52 | cut = cutInt(usign) 53 | if (cut > 0): 54 | zero_minus = -1 - floor(log10(cut * 1.00000000001)) #计算要减去的无效位数0 55 | digit_behind += zero_minus #重置小数点后位数 56 | ''' 按应有的小数点后位数,若生成的有效数值越界(如0.099973,保留3位有效数字, 57 | 则理论上小数点后保留4位,而实际生成的小数越界成为0.10000,故此时小数点后应少 58 | 一位,即0.1000) 59 | 判断越界的方法:以0.099973为例,保留小数点后4位,则取10**(-4)的一半,即 60 | 10**(-4)/2=0.00005,无效位数0有1个,故理论上生成的数值不应超过10**(-1)=0.1, 61 | 而0.099973+0.00005=0.100023>0.1,故越界。''' 62 | if usign + 10**(-digit_behind)/2 > 10**(-zero_minus): 63 | digit_behind -= 1 64 | if (digit_behind < 0): 65 | digit_behind = 0 66 | return digit_behind 67 | 68 | def getBound(num): 69 | '''获得数字的科学记数法指数''' 70 | return abs(floor(log10(abs(num._Num__value * 1.00000000001)))) 71 | 72 | def dec2Latex(dec, noBracket=False): 73 | if type(dec) == int: 74 | expr = str(dec) 75 | else: 76 | expr = '%g' % dec 77 | if len(expr) >= 16: 78 | expr = '%.3g' % dec 79 | if 'e' in expr: 80 | e_list = expr.split('e') 81 | expr = r'%s \times 10^{%s}' % (e_list[0], e_list[1].replace('+', '')) 82 | if (not noBracket) and dec < 0: 83 | expr = '(%s)' % expr 84 | return expr 85 | -------------------------------------------------------------------------------- /analyticlab/system/statformat.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Feb 6 11:50:19 2018 4 | 5 | @author: wzv100 6 | """ 7 | 8 | from math import fabs 9 | from ..num import Num 10 | from .numberformat import getDigitFront, getDigitBehind 11 | 12 | def statFormat(gd_valid, number, isRelative=False): 13 | '''统计结果格式化''' 14 | s = Num(None) 15 | s._Num__value = number 16 | usign = fabs(number) 17 | s._Num__d_valid = gd_valid 18 | s._Num__d_front = getDigitFront(number) 19 | s._Num__d_behind = getDigitBehind(usign, s._Num__d_valid, s._Num__d_front) 20 | s.setIsRelative(isRelative) 21 | return s.fix() 22 | 23 | def getMaxDeltaDigit(item, mean): 24 | '''获得与均值差值最大的数值的有效数字位数''' 25 | dmax = max(item._NumItem__arr) - mean 26 | dmin = mean - min(item._NumItem__arr) 27 | d = max(dmax, dmin) 28 | d._Num__resetDigit() 29 | return d._Num__d_valid 30 | -------------------------------------------------------------------------------- /analyticlab/system/text_unicode.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Wed Aug 15 10:54:15 2018 5 | 6 | @author: xingrongtech 7 | """ 8 | 9 | from .exceptions import subUnsupportedException, supUnsupportedException 10 | 11 | sub_dict = {'0':'⁰', '1':'¹', '2':'²', '3':'³', '4':'⁴', '5':'⁵', '6':'⁶', 12 | '7':'⁷', '8':'⁸', '9':'⁹', 13 | '-':'⁻', '+':'⁺', '=':'⁼', '(':'⁽', ')':'⁾', 14 | 'a':'ᵃ', 'b':'ᵇ', 'c':'ᶜ', 'd':'ᵈ', 'e':'ᵉ', 'f':'ᶠ', 'g':'ᵍ', 15 | 'h':'ʰ', 'i':'ⁱ', 'j':'ʲ', 'k':'ᵏ', 'l':'ˡ', 'm':'ᵐ', 'n':'ⁿ', 16 | 'o':'ᵒ', 'p':'ᵖ', 'r':'ʳ', 's':'ˢ', 't':'ᵗ', 'u':'ᵘ', 'v':'ᵛ', 17 | 'w':'ʷ', 'x':'ˣ', 'y':'ʸ', 'z':'ᶻ'} 18 | 19 | sup_dict = {'0':'₀', '1':'₁', '2':'₂', '3':'₃', '4':'₄', '5':'₅', '6':'₆', 20 | '7':'₇', '8':'₈', '9':'₉', 21 | '-':'₋', '+':'₊', '=':'₌', '(':'₍', ')':'₎', 22 | 'a':'ₐ', 'e':'ₑ', 'h':'ₕ', 'i':'ᵢ', 'k':'ₖ', 'l':'ₗ', 'm':'ₘ', 23 | 'n':'ₙ', 'o':'ₒ', 'p':'ₚ', 'r':'ᵣ', 's':'ₛ', 't':'ₜ', 'u':'ᵤ', 24 | 'v':'ᵥ', 'x':'ₓ'} 25 | 26 | def usub(text): 27 | if type(text) == int: 28 | text = str(text) 29 | elif type(text) == float: 30 | text = '%g' % text 31 | res = [] 32 | for ti in text: 33 | if ti not in sub_dict: 34 | raise subUnsupportedException(ti) 35 | else: 36 | res.append(sub_dict[ti]) 37 | return ''.join(res) 38 | 39 | def usup(text): 40 | if type(text) == int: 41 | text = str(text) 42 | elif type(text) == float: 43 | text = '%g' % text 44 | res = [] 45 | for ti in text: 46 | if ti not in sup_dict: 47 | raise supUnsupportedException(ti) 48 | else: 49 | res.append(sup_dict[ti]) 50 | return ''.join(res) -------------------------------------------------------------------------------- /analyticlab/system/unit_open.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Thu Aug 16 22:47:34 2018 5 | 6 | @author: xingrongtech 7 | """ 8 | 9 | UnitIsOpen = True 10 | 11 | def openUnit(): 12 | global UnitIsOpen 13 | UnitIsOpen = True 14 | 15 | def closeUnit(): 16 | global UnitIsOpen 17 | UnitIsOpen = False 18 | 19 | def unitIsOpen(): 20 | global UnitIsOpen 21 | return UnitIsOpen -------------------------------------------------------------------------------- /analyticlab/twoitems.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Jan 29 22:02:20 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | 8 | from .amath import sqrt 9 | from .latexoutput import LaTeX 10 | from .lookup import F, t 11 | from .measure.basemeasure import BaseMeasure, Ins 12 | from .system.statformat import statFormat, getMaxDeltaDigit 13 | from .system.exceptions import itemNotSameLengthException 14 | 15 | def cov(X, Y, process=False, processWithMean=True, needValue=False, dec=False, remainOneMoreDigit=False): 16 | '''获得两组数据的协方差 17 | 【参数说明】 18 | 1.X(NumItem):甲组数据。 19 | 2.Y(NumItem):乙组数据。 20 | 3.process(可选,bool):是否获得计算过程,默认process=False。 21 | 4.needValue(可选,bool):当获得计算过程时,是否返回计算结果。默认needValue=False。 22 | 5.dec(可选,bool):是否已纯数字形式(int或float)给出样本均值。dec为True时,以纯数字形式给出;为False时,以数值(Num)形式给出。注意当dec=True时,process将会无效。默认dec=False。 23 | 6.remainOneMoreDigit(可选,bool):结果是否多保留一位有效数字。默认remainOneMoreDigit=False。 24 | 【返回值】 25 | ①dec为True时,返回值为float类型的纯数字协方差; 26 | ②dec为False时: 27 | A.process为False时,返回值为Num类型的协方差。 28 | B.process为True且needValue为False时,返回值为LaTeX类型的计算过程。 29 | C.process为True且needValue为True时,返回值为Num类型的协方差和LaTeX类型的计算过程组成的元组。 30 | 【应用举例】 31 | >>> d1 = NumItem('10.69 10.67 10.74 10.72') 32 | >>> d2 = NumItem('5.38e-7 5.34e-7 5.37e-7 5.33e-7') 33 | >>> cov(d1, d2, remainOneMoreDigit=True) 34 | 6.7e-12 35 | ''' 36 | rX, rY = X._NumItem__arr, Y._NumItem__arr 37 | if len(rX) != len(rY): 38 | raise itemNotSameLengthException('进行协方差运算的两组数据长度必须一致') 39 | n = len(X) 40 | meanX, meanY = X.mean(), Y.mean() 41 | dsum = 0 42 | for i in range(n): 43 | dsum += (rX[i]._Num__value - meanX._Num__value) * (rY[i]._Num__value - meanY._Num__value) 44 | if dec: 45 | return dsum / (n - 1) 46 | else: 47 | if process and processWithMean: 48 | meanX, lsub1 = X.mean(process=True, needValue=True) 49 | meanY, lsub2 = Y.mean(process=True, needValue=True) 50 | res = dsum / (n - 1) 51 | result = statFormat(min(getMaxDeltaDigit(X, meanX), getMaxDeltaDigit(Y, meanY)), res) 52 | result._Num__q = X._NumItem__q * Y._NumItem__q 53 | if remainOneMoreDigit: 54 | result.remainOneMoreDigit() 55 | if process: 56 | latex = LaTeX() 57 | if processWithMean: 58 | latex.add([lsub1, lsub2]) 59 | symX, symY = X._NumItem__sym, Y._NumItem__sym 60 | sumExpr = '' 61 | sciDigit = max(X._NumItem__sciDigit(), Y._NumItem__sciDigit()) 62 | if sciDigit == 0: 63 | for i in range(n): 64 | sumExpr += r'%s\times%s+' % ((X[i] - meanX).dlatex(1), (Y[i] - meanY).dlatex(1)) 65 | sumExpr = sumExpr[:-1] 66 | <<<<<<< HEAD 67 | latex.add(r's_{%s%s}=\cfrac{1}{n-1}\sum\limits_{i=1}^n [(%s_{i}-\overline{%s})(%s_{i}-\overline{%s})]=\cfrac{1}{%d}\left[%s\right]=%s' % (symX, symY, symX, symX, symY, symY, n-1, sumExpr, result.latex())) 68 | ======= 69 | latex.add(r's_{%s%s}=\frac{1}{n-1}\sum\limits_{i=1}^n [(%s_{i}-\overline{%s})(%s_{i}-\overline{%s})]=\frac{1}{%d}\left[%s\right]=%s' % (symX, symY, symX, symX, symY, symY, n-1, sumExpr, result.latex())) 70 | >>>>>>> fcd8aeb38c983d9242fa950ef4c982492c7b950e 71 | else: 72 | dX = X * 10**(-sciDigit) 73 | dY = Y * 10**(-sciDigit) 74 | dmeanX = meanX * 10**(-sciDigit) 75 | dmeanY = meanY * 10**(-sciDigit) 76 | for i in range(n): 77 | sumExpr += r'%s\times%s+' % ((dX[i] - dmeanX).dlatex(1), (dY[i] - dmeanY).dlatex(1)) 78 | sumExpr = sumExpr[:-1] 79 | <<<<<<< HEAD 80 | latex.add(r's_{%s%s}=\cfrac{1}{n-1}\sum\limits_{i=1}^n [(%s_{i}-\overline{%s})(%s_{i}-\overline{%s})]=\cfrac{1}{%d}\left[%s\right]\times 10^{%d}=%s' % (symX, symY, symX, symX, symY, symY, n-1, sumExpr, sciDigit*2, result.latex())) 81 | ======= 82 | latex.add(r's_{%s%s}=\frac{1}{n-1}\sum\limits_{i=1}^n [(%s_{i}-\overline{%s})(%s_{i}-\overline{%s})]=\frac{1}{%d}\left[%s\right]\times 10^{%d}=%s' % (symX, symY, symX, symX, symY, symY, n-1, sumExpr, sciDigit*2, result.latex())) 83 | >>>>>>> fcd8aeb38c983d9242fa950ef4c982492c7b950e 84 | if needValue: 85 | return result, latex 86 | else: 87 | return latex 88 | return result 89 | 90 | def corrCoef(X, Y, process=False, needValue=False, remainOneMoreDigit=False): 91 | '''获得两组数据的相关系数 92 | 【参数说明】 93 | 1.X(NumItem):甲组数据。 94 | 2.Y(NumItem):乙组数据。 95 | 3.process(可选,bool):是否获得计算过程,默认process=False。 96 | 4.needValue(可选,bool):当获得计算过程时,是否返回计算结果。默认needValue=False。 97 | 5.remainOneMoreDigit(可选,bool):结果是否多保留一位有效数字。默认remainOneMoreDigit=False。 98 | 【返回值】 99 | ①process为False时,返回值为Num类型的相关系数。 100 | ②process为True且needValue为False时,返回值为LaTeX类型的计算过程。 101 | ③process为True且needValue为True时,返回值为Num类型的相关系数和LaTeX类型的计算过程组成的元组。 102 | 【应用举例】 103 | >>> d1 = NumItem('10.69 10.67 10.74 10.72') 104 | >>> d2 = NumItem('5.38e-7 5.34e-7 5.37e-7 5.33e-7') 105 | >>> corrCoef(d1, d2, remainOneMoreDigit=True) 106 | 0.086''' 107 | if process: 108 | latex = LaTeX() 109 | sXY, lsub1 = cov(X, Y, process, processWithMean=True, needValue=True, remainOneMoreDigit=True) 110 | sX, lsub2 = X.staDevi(process, processWithMean=False, needValue=True, remainOneMoreDigit=True) 111 | sY, lsub3 = Y.staDevi(process, processWithMean=False, needValue=True, remainOneMoreDigit=True) 112 | latex.add([lsub1, lsub2, lsub3]) 113 | else: 114 | sXY = cov(X, Y, remainOneMoreDigit=True) 115 | sX, sY = X.staDevi(remainOneMoreDigit=True), Y.staDevi(remainOneMoreDigit=True) 116 | result = sXY / (sX * sY) 117 | if not remainOneMoreDigit: 118 | result.cutOneDigit() 119 | if process: 120 | symX, symY = X._NumItem__sym, Y._NumItem__sym 121 | <<<<<<< HEAD 122 | latex.add(r'r_{%s%s}=\cfrac{s_{%s%s}}{s_{%s}s_{%s}}=\cfrac{%s}{%s\times %s}=%s' % (symX, symY, symX, symY, symX, symY, sXY.dlatex(), sX.dlatex(2), sY.dlatex(2), result.latex())) 123 | ======= 124 | latex.add(r'r_{%s%s}=\frac{s_{%s%s}}{s_{%s}s_{%s}}=\frac{%s}{%s\times %s}=%s' % (symX, symY, symX, symY, symX, symY, sXY.dlatex(), sX.dlatex(2), sY.dlatex(2), result.latex())) 125 | >>>>>>> fcd8aeb38c983d9242fa950ef4c982492c7b950e 126 | if needValue: 127 | return result, latex 128 | else: 129 | return latex 130 | return result 131 | 132 | def sigDifference(X, Y, confLevel=0.95, process=False, needValue=False): 133 | '''检测两组数据是否有显著性差异 134 | 【参数说明】 135 | 1.X(NumItem):甲组数据。 136 | 2.Y(NumItem):乙组数据。 137 | 3.confLevel(可选,float):置信水平,建议选择0.6826、0.90、0.95、0.98、0.99中的一个(选择推荐值意外的值会使程序变慢),默认confLevel=0.95。 138 | 4.process(可选,bool):是否获得计算过程,默认process=False。 139 | 5.needValue(可选,bool):当获得计算过程时,是否返回计算结果。默认needValue=False。 140 | 【返回值】 141 | bool:有显著性差异为True,无显著性差异为False 142 | ①process为False时,返回值为bool。 143 | ②process为True且needValue为False时,返回值为LaTeX类型的计算过程。 144 | ③process为True且needValue为True时,返回值为bool和LaTeX类型的计算过程组成的元组。 145 | 【应用举例】 146 | >>> Al_1 = NumItem('10.69 10.67 10.74 10.72') 147 | >>> Al_2 = NumItem('10.76 10.68 10.73 10.74') 148 | >>> hasDifference = sigDifference(Al_1, Al_2) 149 | >>> print('两组数据是否有显著性差异:%s' % (Al_1, Al_2, hasDifference)) 150 | 两组数据是否有显著性差异:False 151 | ''' 152 | #先进行F检验:比较两组的精密度 153 | item = (X, Y) 154 | s = (X.staDevi(dec=True), Y.staDevi(dec=True)) 155 | if s[0] > s[1]: 156 | maxid, minid = 0, 1 157 | else: 158 | maxid, minid = 1, 0 159 | n_min = len(item[minid]._NumItem__arr) 160 | n_max = len(item[maxid]._NumItem__arr) 161 | FCal = s[maxid]**2 / s[minid]**2 162 | sDifferent = (FCal >= F(confLevel, n_min-1, n_max-1)) 163 | if process: 164 | symX, symY = X._NumItem__sym, Y._NumItem__sym 165 | latex = LaTeX(r'\bbox[gainsboro, 2px]{\text{【通过$F$检验,比较两组的精密度】}}') 166 | p_meanX, p_meanY = X.mean(), Y.mean() 167 | p_sX, lsub1 = X.staDevi(process=True, needValue=True, remainOneMoreDigit=True) 168 | p_sY, lsub2 = Y.staDevi(process=True, needValue=True, remainOneMoreDigit=True) 169 | latex.add([lsub1, lsub2]) 170 | #比较方差大小 171 | if (p_sX >= p_sY): 172 | latex.add(r'\text{其中}s_{%s}>s_{%s}\text{,故}s_{\rm max}=s_{%s}\text{,}s_{\rm min}=s_{%s}' % (symX, symY, symX, symY)) 173 | latex.add(r'F=\cfrac{s_{\rm max}^2}{s_{\rm min}^2}=\cfrac{{%s}^2}{{%s}^2}=%.3f' % (p_sX.dlatex(1), p_sY.dlatex(1), FCal)) 174 | else: 175 | latex.add(r'\text{其中}s_{%s}F_{%g}(%d,%d)\text{,表明两组测量结果的方差有显著性差异,即两组数据采用了精密度不同的两种测量方法}' % (confLevel, n_min-1, n_max-1)) 181 | if needValue: 182 | return True, latex 183 | else: 184 | return latex 185 | return True 186 | else: 187 | #再进行t检验:比较两组的平均值,是否有显著差异 188 | nX, nY = len(X._NumItem__arr), len(Y._NumItem__arr) 189 | sp = (((nX-1)*s[0]**2 + (nY-1)*s[1]**2) / (nX+nY-2))**0.5 190 | tCal = abs(X.mean(dec=True) - Y.mean(dec=True)) / sp * ((nX*nY)/(nX+nY))**0.5 191 | tv = t(confLevel, nX+nY-2) 192 | if process: 193 | latex.add(r'F= tv: 200 | latex.add(r't>t_{%g}(%d)\text{,说明两组测量结果的均值有明显差异,不属于同一群体,两者之间存在系统误差}' % (confLevel, nX+nY-2)) 201 | else: 202 | latex.add(r't= tv, latex 205 | else: 206 | return latex 207 | return tCal > tv 208 | 209 | def linear_fit(X, Y, process=False, needValue=False): 210 | '''通过采集的自变量和因变量数据,通过线性拟合得出截距a和斜率b 211 | 【参数说明】 212 | 1.X(NumItem):自变量数据。 213 | 2.Y(NumItem):因变量数据。 214 | 3.process(可选,bool):是否获得计算过程,默认process=False。 215 | 4.needValue(可选,bool):当获得计算过程时,是否返回计算结果。默认needValue=False。 216 | 【返回值】 217 | ①process为False时,返回值有2个,为Num类型的a、b。 218 | ②process为True且needValue为False时,返回值有1个,为LaTeX类型的计算过程。 219 | ③process为True且needValue为True时,返回值有3个,为Num类型的a、b和LaTeX类型的计算过程。 220 | 【应用举例】 221 | >>> x1 = NumItem('1.00 2.00 4.00 6.00', 'mm') 222 | >>> r1 = NumItem('9.5 11.5 12.4 14.8', 'kg') 223 | >>> linear_fit(x1, r1) 224 | ((8.9±1.0)kg, (0.97±0.27)kg/mm)''' 225 | if len(X) != len(Y): 226 | raise itemNotSameLengthException('进行协方差运算的两组数据长度必须一致') 227 | n = len(X) 228 | if process: 229 | latex = LaTeX() 230 | #计算均值 231 | meanX, lsub1 = X.mean(process, needValue=True) 232 | meanY, lsub2 = Y.mean(process, needValue=True) 233 | sX, l_sX = X.staDevi(process, processWithMean=False, needValue=True) 234 | sXY, l_sXY = cov(X, Y, process, processWithMean=False, needValue=True) 235 | latex.add([lsub1, lsub2, l_sX, l_sXY]) 236 | b = sXY / sX**2 237 | a = meanY - b * meanX 238 | #计算不确定度 239 | v = Y-(a+b*X) 240 | s = sqrt((v**2).isum() / (n-2)) 241 | ub = s/sX 242 | ua = ub * sqrt((X**2).isum() / n) 243 | else: 244 | #计算均值 245 | meanX, meanY = X.mean(), Y.mean() 246 | sX = X.staDevi() 247 | sXY = cov(X, Y) 248 | b = sXY / sX**2 249 | a = meanY - b * meanX 250 | #计算不确定度 251 | v = Y-(a+b*X) 252 | s = sqrt((v**2).isum() / (n-2)) 253 | ub = s/sX 254 | ua = ub * sqrt((X**2).isum() / n) 255 | #生成BaseMeasure 256 | A = BaseMeasure(a, Ins(ua, 0), sym='a', description='截距$a$') 257 | B = BaseMeasure(b, Ins(ub, 0), sym='b', description='斜率$b$') 258 | if process: 259 | <<<<<<< HEAD 260 | latex.add(r'b=\cfrac{s_{%s%s}}{s_{%s}^2}=\cfrac{%s}{%s^2}=%s' % (X._NumItem__sym, Y._NumItem__sym, X._NumItem__sym, sXY.dlatex(), sX.dlatex(1), b.latex())) 261 | ======= 262 | latex.add(r'b=\frac{s_{%s%s}}{s_{%s}^2}=\frac{%s}{%s^2}=%s' % (X._NumItem__sym, Y._NumItem__sym, X._NumItem__sym, sXY.dlatex(), sX.dlatex(1), b.latex())) 263 | >>>>>>> fcd8aeb38c983d9242fa950ef4c982492c7b950e 264 | latex.add(r'a=\overline{%s}-b\overline{%s}=%s-%s \times %s=%s' % (Y._NumItem__sym, X._NumItem__sym, meanY.dlatex(), b.dlatex(2), meanX.dlatex(2), a.latex())) 265 | sciDigit = v._NumItem__sciDigit() 266 | if sciDigit == 0: 267 | sumExpr = '+'.join([(r'%s^2' % vi.dlatex(1)) for vi in v._NumItem__arr]) 268 | <<<<<<< HEAD 269 | latex.add(r's=\sqrt{\cfrac{1}{n-2}\sum\limits_{i=1}^n \left[y_i-\left(a+bx_i\right)\right]^2}=\sqrt{\cfrac{1}{%d}\left[%s\right]}=%s' % (n-2, sumExpr, s.latex())) 270 | else: 271 | v_cut = v * 10**(-sciDigit) 272 | sumExpr = '+'.join([(r'%s^2' % vi.dlatex(1)) for vi in v_cut._NumItem__arr]) 273 | latex.add(r's=\sqrt{\cfrac{1}{n-2}\sum\limits_{i=1}^n \left[y_i-\left(a+bx_i\right)\right]^2}=\sqrt{\cfrac{1}{%d}\left[%s\right]}\times 10^{%d}=%s' % (n-2, sumExpr, sciDigit, s.latex())) 274 | latex.add(r'u_b=\cfrac{s}{s_{%s}}=\cfrac{%s}{%s}=%s' % (X._NumItem__sym, s.dlatex(), sX.dlatex(), ub.latex())) 275 | ======= 276 | latex.add(r's=\sqrt{\frac{1}{n-2}\sum\limits_{i=1}^n \left[y_i-\left(a+bx_i\right)\right]^2}=\sqrt{\frac{1}{%d}\left[%s\right]}=%s' % (n-2, sumExpr, s.latex())) 277 | else: 278 | v_cut = v * 10**(-sciDigit) 279 | sumExpr = '+'.join([(r'%s^2' % vi.dlatex(1)) for vi in v_cut._NumItem__arr]) 280 | latex.add(r's=\sqrt{\frac{1}{n-2}\sum\limits_{i=1}^n \left[y_i-\left(a+bx_i\right)\right]^2}=\sqrt{\frac{1}{%d}\left[%s\right]}\times 10^{%d}=%s' % (n-2, sumExpr, sciDigit, s.latex())) 281 | latex.add(r'u_b=\frac{s}{s_{%s}}=\frac{%s}{%s}=%s' % (X._NumItem__sym, s.dlatex(), sX.dlatex(), ub.latex())) 282 | >>>>>>> fcd8aeb38c983d9242fa950ef4c982492c7b950e 283 | sciDigit = X._NumItem__sciDigit() 284 | if sciDigit == 0: 285 | sumExpr = '+'.join([(r'%s^2' % xi.dlatex(1)) for xi in X._NumItem__arr]) 286 | else: 287 | X_cut = X * 10**(-sciDigit) 288 | sumExpr = '+'.join([(r'%s^2' % xi.dlatex(1)) for xi in X_cut._NumItem__arr]) 289 | if len([xi for xi in X._NumItem__arr if xi < 0]) == 0: 290 | sumExpr = r'\left(%s\right)' % sumExpr 291 | else: 292 | sumExpr = r'\left[%s\right]' % sumExpr 293 | if sciDigit == 0: 294 | <<<<<<< HEAD 295 | latex.add(r'u_a=u_b\sqrt{\cfrac{1}{n}\sum\limits_{i=1}^n %s^2}=%s \times\sqrt{\cfrac{1}{%d}%s}=%s' % (X._NumItem__sym, ub.dlatex(2), n, sumExpr, ua.latex())) 296 | else: 297 | latex.add(r'u_a=u_b\sqrt{\cfrac{1}{n}\sum\limits_{i=1}^n %s^2}=%s \times\sqrt{\cfrac{1}{%d}%s}\times 10^{%d}=%s' % (X._NumItem__sym, ub.dlatex(2), n, sumExpr, sciDigit, ua.latex())) 298 | ======= 299 | latex.add(r'u_a=u_b\sqrt{\frac{1}{n}\sum\limits_{i=1}^n %s^2}=%s \times\sqrt{\frac{1}{%d}%s}=%s' % (X._NumItem__sym, ub.dlatex(2), n, sumExpr, ua.latex())) 300 | else: 301 | latex.add(r'u_a=u_b\sqrt{\frac{1}{n}\sum\limits_{i=1}^n %s^2}=%s \times\sqrt{\frac{1}{%d}%s}\times 10^{%d}=%s' % (X._NumItem__sym, ub.dlatex(2), n, sumExpr, sciDigit, ua.latex())) 302 | >>>>>>> fcd8aeb38c983d9242fa950ef4c982492c7b950e 303 | latex.add(r'综上,拟合结果为') 304 | latex.add(r'a=%s' % A.latex()) 305 | latex.add(r'b=%s' % B.latex()) 306 | if needValue: 307 | return A, B, latex 308 | else: 309 | return latex 310 | <<<<<<< HEAD 311 | return A, B 312 | ======= 313 | return A, B 314 | >>>>>>> fcd8aeb38c983d9242fa950ef4c982492c7b950e 315 | -------------------------------------------------------------------------------- /demo/功能演示-LSymItem及LSym使用示例.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/latex": [ 11 | "$\\begin{align}&\\text{根据公式$S=\\ln^{2}{\\left[2 \\left(1+\\cfrac{\\pi }{2}\\right){d}^{2}\\right]}$,得}\\\\ \n", 12 | "&{S}_{a}=\\ln^{2}{\\left[2 \\left(1+\\cfrac{\\pi }{2}\\right) \\times {1.63}^{2}\\right]}=6.836{\\rm m^{2}}\\\\ \n", 13 | "&{S}_{b}=\\ln^{2}{\\left[2 \\left(1+\\cfrac{\\pi }{2}\\right) \\times {1.68}^{2}\\right]}=7.155{\\rm m^{2}}\\\\ \n", 14 | "&{S}_{c}=\\ln^{2}{\\left[2 \\left(1+\\cfrac{\\pi }{2}\\right) \\times {1.66}^{2}\\right]}=7.028{\\rm m^{2}}\\\\ \n", 15 | "&{S}_{d}=\\ln^{2}{\\left[2 \\left(1+\\cfrac{\\pi }{2}\\right) \\times {1.62}^{2}\\right]}=6.772{\\rm m^{2}}\\\\ \n", 16 | "&\\overline{{S}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {S}_{i}=\\frac{1}{4}\\left(6.836+7.155+7.028+6.772\\right)=6.948{\\rm m^{2}}\\end{align}$" 17 | ], 18 | "text/plain": [ 19 | "" 20 | ] 21 | }, 22 | "execution_count": 1, 23 | "metadata": {}, 24 | "output_type": "execute_result" 25 | } 26 | ], 27 | "source": [ 28 | "import analyticlab as ab\n", 29 | "from analyticlab import ln, sqrt, PI, LSym, LSymItem, LaTeX, dispLSym, dispLSymItem\n", 30 | "LSymItem.sepSymCalc = True\n", 31 | "d = LSymItem('d', '1.63 1.68 1.66 1.62', 'a b c d')\n", 32 | "k = 2*(1+PI/2)\n", 33 | "S = ln(k*d**2)**2\n", 34 | "dispLSymItem(S, 'S', 'm^{2}')" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 2, 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "data": { 44 | "text/plain": [ 45 | "'{1.63}'" 46 | ] 47 | }, 48 | "execution_count": 2, 49 | "metadata": {}, 50 | "output_type": "execute_result" 51 | } 52 | ], 53 | "source": [ 54 | "d['a']" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 3, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "data": { 64 | "text/plain": [ 65 | "'\\\\ln^{2}{\\\\left[2 \\\\left(1+\\\\cfrac{\\\\pi }{2}\\\\right) \\\\times {1.63}^{2}\\\\right]}'" 66 | ] 67 | }, 68 | "execution_count": 3, 69 | "metadata": {}, 70 | "output_type": "execute_result" 71 | } 72 | ], 73 | "source": [ 74 | "S['a']" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 4, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "data": { 84 | "text/latex": [ 85 | "$\\begin{align}&\\text{根据公式$S=\\sqrt[3]{\\left\\lvert \\pi {d}^{2} \\right\\rvert}$,得}\\\\ \n", 86 | "&{S}_{a}=\\sqrt[3]{\\left\\lvert \\pi \\times {1.63}^{2} \\right\\rvert}=2.03{\\rm m^{2}}\\\\ \n", 87 | "&{S}_{b}=\\sqrt[3]{\\left\\lvert \\pi \\times \\left({-1.68}\\right)^{2} \\right\\rvert}=2.07{\\rm m^{2}}\\\\ \n", 88 | "&{S}_{c}=\\sqrt[3]{\\left\\lvert \\pi \\times {1.69}^{2} \\right\\rvert}=2.08{\\rm m^{2}}\\\\ \n", 89 | "&{S}_{d}=\\sqrt[3]{\\left\\lvert \\pi \\times {1.62}^{2} \\right\\rvert}=2.02{\\rm m^{2}}\\\\ \n", 90 | "&\\overline{{S}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {S}_{i}=\\frac{1}{4}\\left(2.03+2.07+2.08+2.02\\right)=2.05{\\rm m^{2}}\\end{align}$" 91 | ], 92 | "text/plain": [ 93 | "" 94 | ] 95 | }, 96 | "execution_count": 4, 97 | "metadata": {}, 98 | "output_type": "execute_result" 99 | } 100 | ], 101 | "source": [ 102 | "d = LSymItem('d', '1.63 -1.68 1.69 1.62', 'a b c d')\n", 103 | "S = sqrt(abs(PI*d**2), 3)\n", 104 | "dispLSymItem(S, 'S', 'm^{2}')" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 5, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "data": { 114 | "text/latex": [ 115 | "$\\begin{align}&V=\\cfrac{\\left({a}+{b}\\right){c}}{3}\\\\ \n", 116 | "&V_{2}=\\cfrac{{a}-{b}}{{c}}\\end{align}$" 117 | ], 118 | "text/plain": [ 119 | "" 120 | ] 121 | }, 122 | "execution_count": 5, 123 | "metadata": {}, 124 | "output_type": "execute_result" 125 | } 126 | ], 127 | "source": [ 128 | "a = LSym('a')\n", 129 | "b = LSym('b')\n", 130 | "c = LSym('c')\n", 131 | "V = (a+b)*c/3\n", 132 | "V2 = (a-b)/c\n", 133 | "lx = LaTeX()\n", 134 | "lx.addLSym(V, 'V', 'm^{3}')\n", 135 | "lx.addLSym(V2, 'V_{2}', 'm^{3}')\n", 136 | "lx" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 6, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "data": { 146 | "text/latex": [ 147 | "$\\begin{align}&V=\\cfrac{{a}{b}{c}}{3}=\\cfrac{{11.3} \\times {6.41} \\times {9.32}}{3}=225{\\rm m^{3}}\\end{align}$" 148 | ], 149 | "text/plain": [ 150 | "" 151 | ] 152 | }, 153 | "execution_count": 6, 154 | "metadata": {}, 155 | "output_type": "execute_result" 156 | } 157 | ], 158 | "source": [ 159 | "a = LSym('a', '11.3')\n", 160 | "b = LSym('b', '6.41')\n", 161 | "c = LSym('c', '9.32')\n", 162 | "V = a*b*c/3\n", 163 | "dispLSym(V, 'V', 'm^{3}')" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 7, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "data": { 173 | "text/latex": [ 174 | "$\\begin{align}&\\text{根据公式$S=\\ln^{2}{\\left[{\\alpha}\\left(\\cfrac{{d}}{2}\\right)^{2}\\right]}$,得}\\\\ \n", 175 | "&{S}_{1}=\\ln^{2}{\\left[{0.987} \\times \\left(\\cfrac{{1.63}}{2}\\right)^{2}\\right]}=0.178{\\rm m^{2}}\\\\ \n", 176 | "&{S}_{2}=\\ln^{2}{\\left[{0.991} \\times \\left(\\cfrac{{1.68}}{2}\\right)^{2}\\right]}=0.128{\\rm m^{2}}\\\\ \n", 177 | "&{S}_{3}=\\ln^{2}{\\left[{0.982} \\times \\left(\\cfrac{{1.66}}{2}\\right)^{2}\\right]}=0.153{\\rm m^{2}}\\\\ \n", 178 | "&{S}_{4}=\\ln^{2}{\\left[{0.989} \\times \\left(\\cfrac{{1.62}}{2}\\right)^{2}\\right]}=0.187{\\rm m^{2}}\\\\ \n", 179 | "&\\overline{{S}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {S}_{i}=\\frac{1}{4}\\left(0.178+0.128+0.153+0.187\\right)=0.162{\\rm m^{2}}\\end{align}$" 180 | ], 181 | "text/plain": [ 182 | "" 183 | ] 184 | }, 185 | "execution_count": 7, 186 | "metadata": {}, 187 | "output_type": "execute_result" 188 | } 189 | ], 190 | "source": [ 191 | "d = LSymItem('d', '1.63 1.68 1.66 1.62')\n", 192 | "alpha = LSymItem(r'\\alpha', '0.987 0.991 0.982 0.989')\n", 193 | "S = ln(alpha*(d/2)**2)**2\n", 194 | "dispLSymItem(S, 'S', 'm^{2}')" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 8, 200 | "metadata": {}, 201 | "outputs": [ 202 | { 203 | "data": { 204 | "text/latex": [ 205 | "$\\begin{align}&{S}_{1}=\\ln^{2}{\\left[\\pi {{\\alpha}}_{1}\\left(\\cfrac{{{d}}_{1}}{2}\\right)^{2}\\right]}=\\ln^{2}{\\left[\\pi \\times {0.987} \\times \\left(\\cfrac{{1.63}}{2}\\right)^{2}\\right]}=0.522{\\rm m^{2}}\\\\ \n", 206 | "&{S}_{2}=\\ln^{2}{\\left[\\pi {{\\alpha}}_{2}\\left(\\cfrac{{{d}}_{2}}{2}\\right)^{2}\\right]}=\\ln^{2}{\\left[\\pi \\times {0.991} \\times \\left(\\cfrac{{1.68}}{2}\\right)^{2}\\right]}=0.619{\\rm m^{2}}\\\\ \n", 207 | "&{S}_{3}=\\ln^{2}{\\left[\\pi {{\\alpha}}_{3}\\left(\\cfrac{{{d}}_{3}}{2}\\right)^{2}\\right]}=\\ln^{2}{\\left[\\pi \\times {0.982} \\times \\left(\\cfrac{{1.66}}{2}\\right)^{2}\\right]}=0.568{\\rm m^{2}}\\\\ \n", 208 | "&{S}_{4}=\\ln^{2}{\\left[\\pi {{\\alpha}}_{4}\\left(\\cfrac{{{d}}_{4}}{2}\\right)^{2}\\right]}=\\ln^{2}{\\left[\\pi \\times {0.989} \\times \\left(\\cfrac{{1.62}}{2}\\right)^{2}\\right]}=0.507{\\rm m^{2}}\\\\ \n", 209 | "&\\overline{{S}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {S}_{i}=\\frac{1}{4}\\left(0.522+0.619+0.568+0.507\\right)=0.554{\\rm m^{2}}\\end{align}$" 210 | ], 211 | "text/plain": [ 212 | "" 213 | ] 214 | }, 215 | "execution_count": 8, 216 | "metadata": {}, 217 | "output_type": "execute_result" 218 | } 219 | ], 220 | "source": [ 221 | "LSymItem.sepSymCalc = False\n", 222 | "d = LSymItem('d', '1.63 1.68 1.66 1.62')\n", 223 | "alpha = LSymItem(r'\\alpha', '0.987 0.991 0.982 0.989')\n", 224 | "S = ln(PI*alpha*(d/2)**2)**2\n", 225 | "dispLSymItem(S, 'S', 'm^{2}')" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 9, 231 | "metadata": {}, 232 | "outputs": [ 233 | { 234 | "data": { 235 | "text/latex": [ 236 | "$\\begin{align}&\\text{根据公式$S=-\\ln^{2}{\\left[\\pi \\left(\\cfrac{{d}}{2}\\right)^{2}\\right]}$,得}\\\\ \n", 237 | "&{S}_{1}=-\\ln^{2}{\\left[\\pi \\times \\left(\\cfrac{{1.63}}{2}\\right)^{2}\\right]}=-0.541{\\rm m^{2}}\\\\ \n", 238 | "&{S}_{2}=-\\ln^{2}{\\left[\\pi \\times \\left(\\cfrac{{1.68}}{2}\\right)^{2}\\right]}=-0.634{\\rm m^{2}}\\\\ \n", 239 | "&{S}_{3}=-\\ln^{2}{\\left[\\pi \\times \\left(\\cfrac{{1.66}}{2}\\right)^{2}\\right]}=-0.596{\\rm m^{2}}\\\\ \n", 240 | "&{S}_{4}=-\\ln^{2}{\\left[\\pi \\times \\left(\\cfrac{{1.62}}{2}\\right)^{2}\\right]}=-0.523{\\rm m^{2}}\\\\ \n", 241 | "&\\overline{{S}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {S}_{i}=\\frac{1}{4}\\left[\\left(-0.541\\right)+\\left(-0.634\\right)+\\left(-0.596\\right)+\\left(-0.523\\right)\\right]=-0.573{\\rm m^{2}}\\end{align}$" 242 | ], 243 | "text/plain": [ 244 | "" 245 | ] 246 | }, 247 | "execution_count": 9, 248 | "metadata": {}, 249 | "output_type": "execute_result" 250 | } 251 | ], 252 | "source": [ 253 | "LSymItem.sepSymCalc = True\n", 254 | "d = LSymItem('d', '1.63 1.68 1.66 1.62')\n", 255 | "S = -ln(PI*(d/2)**2)**2\n", 256 | "dispLSymItem(S, 'S', 'm^{2}')" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 10, 262 | "metadata": {}, 263 | "outputs": [ 264 | { 265 | "data": { 266 | "text/latex": [ 267 | "$\\begin{align}&\\Delta t=2-\\left({6.35}+{6.32}\\right)=-10.67{\\rm s}\\end{align}$" 268 | ], 269 | "text/plain": [ 270 | "" 271 | ] 272 | }, 273 | "execution_count": 10, 274 | "metadata": {}, 275 | "output_type": "execute_result" 276 | } 277 | ], 278 | "source": [ 279 | "t = LSymItem('t', '6.31 6.35 6.32 6.37')\n", 280 | "dT = 2 - (t[1] + t[2])\n", 281 | "dispLSym(dT, r'\\Delta t', 's')" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": 11, 287 | "metadata": {}, 288 | "outputs": [ 289 | { 290 | "data": { 291 | "text/latex": [ 292 | "$\\begin{align}&\\Delta t={6.31}-\\left({6.35}+{6.32}\\right)=-6.36{\\rm s}\\end{align}$" 293 | ], 294 | "text/plain": [ 295 | "" 296 | ] 297 | }, 298 | "execution_count": 11, 299 | "metadata": {}, 300 | "output_type": "execute_result" 301 | } 302 | ], 303 | "source": [ 304 | "t = LSymItem('t', '6.31 6.35 6.32 6.37')\n", 305 | "dT = t[0] - (t[1] + t[2])\n", 306 | "dispLSym(dT, r'\\Delta t', 's')" 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": 12, 312 | "metadata": {}, 313 | "outputs": [ 314 | { 315 | "data": { 316 | "text/latex": [ 317 | "$\\begin{align}&\\text{根据公式$S=\\sin^{2}{\\left(\\left\\lvert \\cfrac{{d}}{2} \\right\\rvert\\right)}$,得}\\\\ \n", 318 | "&{S}_{1}=\\sin^{2}{\\left(\\left\\lvert \\cfrac{{79.52}}{2} \\right\\rvert\\right)}=0.4091{\\rm m^{2}}\\\\ \n", 319 | "&{S}_{2}=\\sin^{2}{\\left(\\left\\lvert \\cfrac{{-79.63}}{2} \\right\\rvert\\right)}=0.4100{\\rm m^{2}}\\\\ \n", 320 | "&{S}_{3}=\\sin^{2}{\\left(\\left\\lvert \\cfrac{{79.41}}{2} \\right\\rvert\\right)}=0.4081{\\rm m^{2}}\\\\ \n", 321 | "&{S}_{4}=\\sin^{2}{\\left(\\left\\lvert \\cfrac{{-79.45}}{2} \\right\\rvert\\right)}=0.4085{\\rm m^{2}}\\\\ \n", 322 | "&\\overline{{S}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {S}_{i}=\\frac{1}{4}\\left(0.4091+0.4100+0.4081+0.4085\\right)=0.4089{\\rm m^{2}}\\end{align}$" 323 | ], 324 | "text/plain": [ 325 | "" 326 | ] 327 | }, 328 | "execution_count": 12, 329 | "metadata": {}, 330 | "output_type": "execute_result" 331 | } 332 | ], 333 | "source": [ 334 | "LSymItem.sepSymCalc = True\n", 335 | "d = LSymItem('d', '79.52 -79.63 79.41 -79.45')\n", 336 | "S = ab.sin(abs(d/2))**2\n", 337 | "dispLSymItem(S, 'S', 'm^{2}')" 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": 13, 343 | "metadata": {}, 344 | "outputs": [ 345 | { 346 | "data": { 347 | "text/latex": [ 348 | "$\\begin{align}&\\text{根据公式$\\theta=\\arcsin{{k}}$,得}\\\\ \n", 349 | "&{\\theta}_{a}=\\arcsin{{0.656}}=41.0{\\rm ^{\\circ}}\\\\ \n", 350 | "&{\\theta}_{b}=\\arcsin{{0.687}}=43.4{\\rm ^{\\circ}}\\\\ \n", 351 | "&{\\theta}_{c}=\\arcsin{{0.669}}=42.0{\\rm ^{\\circ}}\\\\ \n", 352 | "&{\\theta}_{d}=\\arcsin{{0.675}}=42.5{\\rm ^{\\circ}}\\\\ \n", 353 | "&\\overline{{\\theta}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {\\theta}_{i}=\\frac{1}{4}\\left(41.0+43.4+42.0+42.5\\right)=42.2{\\rm ^{\\circ}}\\end{align}$" 354 | ], 355 | "text/plain": [ 356 | "" 357 | ] 358 | }, 359 | "execution_count": 13, 360 | "metadata": {}, 361 | "output_type": "execute_result" 362 | } 363 | ], 364 | "source": [ 365 | "k = LSymItem('k', '0.656 0.687 0.669 0.675', 'a b c d')\n", 366 | "theta = ab.arcsin(k)\n", 367 | "dispLSymItem(theta, r'\\theta', r'^{\\circ}')" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": null, 373 | "metadata": {}, 374 | "outputs": [], 375 | "source": [] 376 | } 377 | ], 378 | "metadata": { 379 | "kernelspec": { 380 | "display_name": "Python 3", 381 | "language": "python", 382 | "name": "python3" 383 | }, 384 | "language_info": { 385 | "codemirror_mode": { 386 | "name": "ipython", 387 | "version": 3 388 | }, 389 | "file_extension": ".py", 390 | "mimetype": "text/x-python", 391 | "name": "python", 392 | "nbconvert_exporter": "python", 393 | "pygments_lexer": "ipython3", 394 | "version": "3.6.3" 395 | } 396 | }, 397 | "nbformat": 4, 398 | "nbformat_minor": 2 399 | } 400 | -------------------------------------------------------------------------------- /demo/功能演示-四种离群值检验.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/latex": [ 11 | "$\\begin{align}&\\text{将测量数据由小到大排序:}[13.12, 13.51, 13.52, 13.53, 13.54, 13.54, 13.55, 13.56, 13.56, 13.58, 13.74]\\\\ \n", 12 | "&\\text{共有}11\\text{个观测值(}n=11\\text{),使用}\\\\ \n", 13 | "&D_{11}=r_{21}=\\frac{{m}_{(11)}-{m}_{(9)}}{{m}_{(11)}-{m}_{(2)}}=\\frac{13.74-13.56}{13.74-13.51}=0.783\\\\ \n", 14 | "&D_{11}'=r_{21}'=\\frac{{m}_{(3)}-{m}_{(1)}}{{m}_{(10)}-{m}_{(1)}}=\\frac{13.52-13.12}{13.58-13.12}=0.870\\\\ \n", 15 | "&\\text{确定检出水平}\\alpha=0.05\\text{,查表得临界值}\\tilde{D}_{1-\\alpha}(n)=\\tilde{D}_{0.95}(11)=0.619\\\\ \n", 16 | "&\\text{因}D_{11}'> D_{11}\\text{且}D_{11}'>\\tilde{D}_{0.95}(11)\\text{,故判定}{m}_{(1)}\\text{为离群值}\\\\ \n", 17 | "&\\text{对于检出的离群值}{m}_{(11)}\\text{,确定剔除水平}\\alpha^{*}=0.01\\text{,查表得临界值}\\tilde{D}_{1-\\alpha^{*}}(n)=\\tilde{D}_{0.99}(11)=0.709\\\\ \n", 18 | "&\\text{因}D_{11}'>\\tilde{D}_{0.99}(11)\\text{,故判定}{m}_{(1)}=13.12\\text{为统计离群值}\\\\ \n", 19 | "&\\text{取出这个观测值之后,余下的数据为}[13.51, 13.52, 13.53, 13.54, 13.54, 13.55, 13.56, 13.56, 13.58, 13.74]\\\\ \n", 20 | "&\\text{对于剩余的}10\\text{个值(}n=10\\text{),使用}\\\\ \n", 21 | "&D_{10}=r_{11}=\\frac{{m}_{(10)}-{m}_{(9)}}{{m}_{(10)}-{m}_{(2)}}=\\frac{13.74-13.58}{13.74-13.52}=0.727\\\\ \n", 22 | "&D_{10}'=r_{11}'=\\frac{{m}_{(2)}-{m}_{(1)}}{{m}_{(9)}-{m}_{(1)}}=\\frac{13.52-13.51}{13.58-13.51}=0.143\\\\ \n", 23 | "&\\text{确定检出水平}\\alpha=0.05\\text{,查表得临界值}\\tilde{D}_{1-\\alpha}(n)=\\tilde{D}_{0.95}(10)=0.530\\\\ \n", 24 | "&\\text{因}D_{10}> D_{10}'\\text{且}D_{10}>\\tilde{D}_{0.95}(10)\\text{,故判定}{m}_{(10)}\\text{为离群值}\\\\ \n", 25 | "&\\text{对于检出的离群值}{m}_{(10)}\\text{,确定剔除水平}\\alpha^{*}=0.01\\text{,查表得临界值}\\tilde{D}_{1-\\alpha^{*}}(n)=\\tilde{D}_{0.99}(10)=0.635\\\\ \n", 26 | "&\\text{因}D_{10}>\\tilde{D}_{0.99}(10)\\text{,故判定}{m}_{(10)}=13.74\\text{为统计离群值}\\\\ \n", 27 | "&\\text{取出这个观测值之后,余下的数据为}[13.51, 13.52, 13.53, 13.54, 13.54, 13.55, 13.56, 13.56, 13.58]\\\\ \n", 28 | "&\\text{对于剩余的}9\\text{个值(}n=9\\text{),使用}\\\\ \n", 29 | "&D_{9}=r_{11}=\\frac{{m}_{(9)}-{m}_{(8)}}{{m}_{(9)}-{m}_{(2)}}=\\frac{13.58-13.56}{13.58-13.52}=0.333\\\\ \n", 30 | "&D_{9}'=r_{11}'=\\frac{{m}_{(2)}-{m}_{(1)}}{{m}_{(8)}-{m}_{(1)}}=\\frac{13.52-13.51}{13.56-13.51}=0.200\\\\ \n", 31 | "&\\text{确定检出水平}\\alpha=0.05\\text{,查表得临界值}\\tilde{D}_{1-\\alpha}(n)=\\tilde{D}_{0.95}(9)=0.564\\\\ \n", 32 | "&\\text{因}D_{9}>D_{9}'\\text{,}D_{9}<\\tilde{D}_{0.95}(9)\\text{,故不能再检出离群值}\\\\ \n", 33 | "&\\text{综上,共检验到2个离群值,其中}\\text{统计离群值为}[13.12, 13.74]\\end{align}$" 34 | ], 35 | "text/plain": [ 36 | "" 37 | ] 38 | }, 39 | "execution_count": 1, 40 | "metadata": {}, 41 | "output_type": "execute_result" 42 | } 43 | ], 44 | "source": [ 45 | "from analyticlab import outlier, NumItem\n", 46 | "Al = NumItem('13.52 13.56 13.55 13.54 13.12 13.54 13.58 13.53 13.74 13.56 13.51', sym='m', unit='g')\n", 47 | "outlier.Dixon(Al, process=True)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 2, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/latex": [ 58 | "$\\begin{align}&\\text{将测量数据由小到大排序:}[13.12, 13.52, 13.54, 13.54, 13.55, 13.56]\\\\ \n", 59 | "&\\overline{{{m_{4}}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {{m_{4}}}_{i}=\\frac{1}{6}\\left(13.12+13.52+13.54+13.54+13.55+13.56\\right)=13.47\\\\ \n", 60 | "&s_{{{m_{4}}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({{m_{4}}}_{i}-\\overline{{{m_{4}}}}\\right)^{2}}=\\sqrt{\\frac{1}{5}\\left[\\left(-0.35\\right)^{2}+0.05^{2}+0.07^{2}+0.07^{2}+0.08^{2}+0.09^{2}\\right]}=0.170\\\\ \n", 61 | "&G_{6}=\\frac{{m_{4}}_{(6)}-\\overline{{m_{4}}}}{s}=\\frac{13.56-13.47}{0.170}=0.511\\\\ \n", 62 | "&G_{6}'=\\frac{\\overline{{m_{4}}}-{m_{4}}_{(1)}}{s}=\\frac{13.47-13.12}{0.170}=2.035\\\\ \n", 63 | "&\\text{确定检验水平}\\alpha=0.05\\text{,查表得临界值}G_{1-\\alpha/2}(n)=G_{0.975}(6)=1.887\\\\ \n", 64 | "&\\text{因}G_{6}'> G_{6}\\text{且}G_{6}'>G_{0.975}(6)\\text{,故判定}{m_{4}}_{(1)}\\text{为离群值}\\\\ \n", 65 | "&\\text{对于检出的离群值}{m_{4}}_{(1)}\\text{,确定剔除水平}\\alpha^{*}=0.01\\text{,查表得临界值}G_{1-\\alpha^{*}/2}(n)=G_{0.975}(6)=1.973\\\\ \n", 66 | "&\\text{因}G_{6}'>G_{0.975}(6)\\text{,故判定}{m_{4}}_{(1)}=13.12\\text{为统计离群值}\\end{align}$" 67 | ], 68 | "text/plain": [ 69 | "" 70 | ] 71 | }, 72 | "execution_count": 2, 73 | "metadata": {}, 74 | "output_type": "execute_result" 75 | } 76 | ], 77 | "source": [ 78 | "Al = NumItem('13.52 13.56 13.55 13.54 13.12 13.54', sym = 'm_{4}', unit='g')\n", 79 | "outlier.Grubbs(Al, process=True)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 3, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "data": { 89 | "text/latex": [ 90 | "$\\begin{align}&\\text{将测量数据由小到大排序:}[3.13, 3.49, 4.01, 4.48, 4.61, 4.76, 4.98, 5.25, 5.32, 5.39, 5.42, 5.57, 5.59, 5.59, 5.63, 5.63, 5.65, 5.66, 5.67, 5.69, 5.71, 6.00, 6.03, 6.12, 6.76]\\\\ \n", 91 | "&\\text{共有}25\\text{个观测值(}n=25\\text{),求其样本均值}\\\\ \n", 92 | "&\\overline{{{x}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {{x}}_{i}=\\frac{1}{25}\\left(3.13+3.49+4.01+4.48+4.61+4.76+4.98+5.25+5.32+5.39+5.42+5.57+5.59+5.59+5.63+5.63+5.65+5.66+5.67+5.69+5.71+6.00+6.03+6.12+6.76\\right)=5.29\\\\ \n", 93 | "&\\text{使用}\\\\ \n", 94 | "&R'_{25}=\\frac{\\overline{{x}}-{x}_{(1)}}{\\sigma}=\\frac{5.29-3.13}{0.65}=3.316\\\\ \n", 95 | "&\\text{确定检出水平}\\alpha=0.05\\text{,查表得临界值}R_{1-\\alpha}(n)=R_{0.95}(25)=2.815\\\\ \n", 96 | "&\\text{因}R_{25}'>R_{0.95}(25)\\text{,故判定}{x}_{(1)}\\text{为离群值}\\\\ \n", 97 | "&\\text{对于检出的离群值}{x}_{(25)}\\text{,确定剔除水平}\\alpha^{*}=0.01\\text{,查表得临界值}R_{1-\\alpha^{*}}(n)=R_{0.99}(25)=3.284\\\\ \n", 98 | "&\\text{因}R_{25}'>R_{0.99}(25)\\text{,故判定}{x}_{(1)}=3.13\\text{为统计离群值}\\\\ \n", 99 | "&\\text{取出这个观测值之后,余下的数据为}[3.49, 4.01, 4.48, 4.61, 4.76, 4.98, 5.25, 5.32, 5.39, 5.42, 5.57, 5.59, 5.59, 5.63, 5.63, 5.65, 5.66, 5.67, 5.69, 5.71, 6.00, 6.03, 6.12, 6.76]\\\\ \n", 100 | "&\\text{对于剩余的}24\\text{个值(}n=24\\text{),求其样本均值}\\\\ \n", 101 | "&\\overline{{{x}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {{x}}_{i}=\\frac{1}{24}\\left(3.49+4.01+4.48+4.61+4.76+4.98+5.25+5.32+5.39+5.42+5.57+5.59+5.59+5.63+5.63+5.65+5.66+5.67+5.69+5.71+6.00+6.03+6.12+6.76\\right)=5.38\\\\ \n", 102 | "&\\text{使用}\\\\ \n", 103 | "&R'_{24}=\\frac{\\overline{{x}}-{x}_{(1)}}{\\sigma}=\\frac{5.38-3.49}{0.65}=2.901\\\\ \n", 104 | "&\\text{确定检出水平}\\alpha=0.05\\text{,查表得临界值}R_{1-\\alpha}(n)=R_{0.95}(24)=2.800\\\\ \n", 105 | "&\\text{因}R_{24}'>R_{0.95}(24)\\text{,故判定}{x}_{(1)}\\text{为离群值}\\\\ \n", 106 | "&\\text{对于检出的离群值}{x}_{(24)}\\text{,确定剔除水平}\\alpha^{*}=0.01\\text{,查表得临界值}R_{1-\\alpha^{*}}(n)=R_{0.99}(24)=3.270\\\\ \n", 107 | "&\\text{因}R_{24}'" 119 | ] 120 | }, 121 | "execution_count": 3, 122 | "metadata": {}, 123 | "output_type": "execute_result" 124 | } 125 | ], 126 | "source": [ 127 | "Al = NumItem(\"3.13 3.49 4.01 4.48 4.61 4.76 4.98 5.25 5.32 5.39 5.42 5.57 5.59 5.59 5.63 5.63 5.65 5.66 5.67 5.69 5.71 6.00 6.03 6.12 6.76\")\n", 128 | "outlier.Nair(Al, 0.65, side='down', process=True)" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 4, 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "data": { 138 | "text/latex": [ 139 | "$\\begin{align}&\\text{将测量数据由小到大排序:}[13.12, 13.51, 13.52, 13.53, 13.54, 13.54, 13.55, 13.56, 13.56, 13.58, 13.64]\\\\ \n", 140 | "&\\text{共有}11\\text{个观测值(}n=11\\text{),求其样本均值}\\\\ \n", 141 | "&\\overline{{{d}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {{d}}_{i}=\\frac{1}{11}\\left(13.12+13.51+13.52+13.53+13.54+13.54+13.55+13.56+13.56+13.58+13.64\\right)=13.51\\\\ \n", 142 | "&\\text{使用}\\\\ \n", 143 | "&b_{k}=\\frac{n\\sum\\limits_{i=1}^n \\left({d}_{i}-\\overline{{d}}\\right)^{4}}{\\left[\\sum\\limits_{i=1}^n \\left({d}_{i}-\\overline{{d}}\\right)^{2}\\right]^{2}}=\\frac{11\\times\\left[\\left(-0.39\\right)^{4}+\\left(-0.00\\right)^{4}+0.01^{4}+0.02^{4}+0.03^{4}+0.03^{4}+0.04^{4}+0.05^{4}+0.05^{4}+0.07^{4}+0.13^{4}\\right]}{\\left[\\left(-0.39\\right)^{2}+\\left(-0.00\\right)^{2}+0.01^{2}+0.02^{2}+0.03^{2}+0.03^{2}+0.04^{2}+0.05^{2}+0.05^{2}+0.07^{2}+0.13^{2}\\right]^{2}}=8.01\\\\ \n", 144 | "&\\text{确定检出水平}\\alpha=0.05\\text{,查表得临界值}b'_{1-\\alpha}(n)=b'_{0.95}(11)=4.02\\\\ \n", 145 | "&\\text{因}b_{k}>b'_{0.95}(11)\\text{,故判定距离均值}13.51\\text{最远的值}{d}_{(1)}=13.12\\text{为离群值}\\\\ \n", 146 | "&\\text{对于检出的离群值}{d}_{(11)}\\text{,确定剔除水平}\\alpha^{*}=0.01\\text{,查表得临界值}b'_{1-\\alpha^{*}}(n)=b'_{0.99}(11)=5.17\\\\ \n", 147 | "&\\text{因}b_{k}>b'_{0.99}(11)\\text{,故判定}{d}_{(1)}=13.12\\text{为统计离群值}\\\\ \n", 148 | "&\\text{取出这个观测值之后,余下的数据为}[13.51, 13.52, 13.53, 13.54, 13.54, 13.55, 13.56, 13.56, 13.58, 13.64]\\\\ \n", 149 | "&\\text{对于剩余的}10\\text{个值(}n=10\\text{),求其样本均值}\\\\ \n", 150 | "&\\overline{{{d}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {{d}}_{i}=\\frac{1}{10}\\left(13.51+13.52+13.53+13.54+13.54+13.55+13.56+13.56+13.58+13.64\\right)=13.55\\\\ \n", 151 | "&\\text{使用}\\\\ \n", 152 | "&b_{k}=\\frac{n\\sum\\limits_{i=1}^n \\left({d}_{i}-\\overline{{d}}\\right)^{4}}{\\left[\\sum\\limits_{i=1}^n \\left({d}_{i}-\\overline{{d}}\\right)^{2}\\right]^{2}}=\\frac{10\\times\\left[\\left(-0.04\\right)^{4}+\\left(-0.03\\right)^{4}+\\left(-0.02\\right)^{4}+\\left(-0.01\\right)^{4}+\\left(-0.01\\right)^{4}+\\left(-0.00\\right)^{4}+0.01^{4}+0.01^{4}+0.03^{4}+0.09^{4}\\right]}{\\left[\\left(-0.04\\right)^{2}+\\left(-0.03\\right)^{2}+\\left(-0.02\\right)^{2}+\\left(-0.01\\right)^{2}+\\left(-0.01\\right)^{2}+\\left(-0.00\\right)^{2}+0.01^{2}+0.01^{2}+0.03^{2}+0.09^{2}\\right]^{2}}=4.21\\\\ \n", 153 | "&\\text{确定检出水平}\\alpha=0.05\\text{,查表得临界值}b'_{1-\\alpha}(n)=b'_{0.95}(10)=3.95\\\\ \n", 154 | "&\\text{因}b_{k}>b'_{0.95}(10)\\text{,故判定距离均值}13.55\\text{最远的值}{d}_{(10)}=13.64\\text{为离群值}\\\\ \n", 155 | "&\\text{对于检出的离群值}{d}_{(10)}\\text{,确定剔除水平}\\alpha^{*}=0.01\\text{,查表得临界值}b'_{1-\\alpha^{*}}(n)=b'_{0.99}(10)=5.00\\\\ \n", 156 | "&\\text{因}b_{k}" 168 | ] 169 | }, 170 | "execution_count": 4, 171 | "metadata": {}, 172 | "output_type": "execute_result" 173 | } 174 | ], 175 | "source": [ 176 | "Al = NumItem('13.52 13.56 13.55 13.54 13.12 13.54 13.58 13.53 13.64 13.56 13.51', sym='d')\n", 177 | "outlier.SkewKuri(Al, process=True)" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": null, 183 | "metadata": {}, 184 | "outputs": [], 185 | "source": [] 186 | } 187 | ], 188 | "metadata": { 189 | "kernelspec": { 190 | "display_name": "Python 3", 191 | "language": "python", 192 | "name": "python3" 193 | }, 194 | "language_info": { 195 | "codemirror_mode": { 196 | "name": "ipython", 197 | "version": 3 198 | }, 199 | "file_extension": ".py", 200 | "mimetype": "text/x-python", 201 | "name": "python", 202 | "nbconvert_exporter": "python", 203 | "pygments_lexer": "ipython3", 204 | "version": "3.6.3" 205 | } 206 | }, 207 | "nbformat": 4, 208 | "nbformat_minor": 2 209 | } 210 | -------------------------------------------------------------------------------- /demo/功能演示-数理统计功能.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from analyticlab import NumItem\n", 10 | "Al_real = '10.70'\n", 11 | "Al = NumItem('10.69 10.26 10.71 10.72', Al_real, sym='m_{1}', unit='g')" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 2, 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "data": { 21 | "text/latex": [ 22 | "$\\begin{align}&\\text{根据公式}E_{i}={m_{1}}_{i}-\\mu,得\\\\ \n", 23 | "&E_{1}=10.69-10.70=-0.01{\\rm g}\\\\ \n", 24 | "&E_{2}=10.26-10.70=-0.44{\\rm g}\\\\ \n", 25 | "&E_{3}=10.71-10.70=0.01{\\rm g}\\\\ \n", 26 | "&E_{4}=10.72-10.70=0.02{\\rm g}\\end{align}$" 27 | ], 28 | "text/plain": [ 29 | "" 30 | ] 31 | }, 32 | "execution_count": 2, 33 | "metadata": {}, 34 | "output_type": "execute_result" 35 | } 36 | ], 37 | "source": [ 38 | "Al.absErr(process=True)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 3, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "data": { 48 | "text/latex": [ 49 | "$\\begin{align}&\\text{根据公式}E_{ri}=\\frac{\\left\\lvert {m_{1}}_{i}-\\mu\\right\\rvert}{\\mu}\\times 100\\%,得\\\\ \n", 50 | "&E_{r1}=\\frac{\\left\\lvert 10.69-10.70 \\right\\rvert}{10.70}\\times 100\\%=0.09\\%\\\\ \n", 51 | "&E_{r2}=\\frac{\\left\\lvert 10.26-10.70 \\right\\rvert}{10.70}\\times 100\\%=4\\%\\\\ \n", 52 | "&E_{r3}=\\frac{\\left\\lvert 10.71-10.70 \\right\\rvert}{10.70}\\times 100\\%=0.09\\%\\\\ \n", 53 | "&E_{r4}=\\frac{\\left\\lvert 10.72-10.70 \\right\\rvert}{10.70}\\times 100\\%=0.19\\%\\end{align}$" 54 | ], 55 | "text/plain": [ 56 | "" 57 | ] 58 | }, 59 | "execution_count": 3, 60 | "metadata": {}, 61 | "output_type": "execute_result" 62 | } 63 | ], 64 | "source": [ 65 | "Al.relErr(process=True)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 4, 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/latex": [ 76 | "$\\begin{align}&\\sum\\limits_{i=1}^n {m_{1}}_{i}=10.69+10.26+10.71+10.72=42.38{\\rm g}\\end{align}$" 77 | ], 78 | "text/plain": [ 79 | "" 80 | ] 81 | }, 82 | "execution_count": 4, 83 | "metadata": {}, 84 | "output_type": "execute_result" 85 | } 86 | ], 87 | "source": [ 88 | "Al.isum(process=True)" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 5, 94 | "metadata": {}, 95 | "outputs": [ 96 | { 97 | "data": { 98 | "text/latex": [ 99 | "$\\begin{align}&\\overline{{m_{1}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{1}}_{i}=\\frac{1}{4}\\left(10.69+10.26+10.71+10.72\\right)=10.60{\\rm g}\\end{align}$" 100 | ], 101 | "text/plain": [ 102 | "" 103 | ] 104 | }, 105 | "execution_count": 5, 106 | "metadata": {}, 107 | "output_type": "execute_result" 108 | } 109 | ], 110 | "source": [ 111 | "Al.mean(process=True)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 6, 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "data": { 121 | "text/latex": [ 122 | "$\\begin{align}&\\text{根据公式}d_{i}={m_{1}}_{i}-\\overline{{m_{1}}}\\text{,得}\\\\ \n", 123 | "&d_{1}=10.69-10.60=0.09{\\rm g}\\\\ \n", 124 | "&d_{2}=10.26-10.60=-0.34{\\rm g}\\\\ \n", 125 | "&d_{3}=10.71-10.60=0.12{\\rm g}\\\\ \n", 126 | "&d_{4}=10.72-10.60=0.12{\\rm g}\\end{align}$" 127 | ], 128 | "text/plain": [ 129 | "" 130 | ] 131 | }, 132 | "execution_count": 6, 133 | "metadata": {}, 134 | "output_type": "execute_result" 135 | } 136 | ], 137 | "source": [ 138 | "Al.devi(process=True)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 7, 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "data": { 148 | "text/latex": [ 149 | "$\\begin{align}&\\overline{{m_{1}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{1}}_{i}=\\frac{1}{4}\\left(10.69+10.26+10.71+10.72\\right)=10.60{\\rm g}\\\\ \n", 150 | "&s_{{m_{1}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({m_{1}}_{i}-\\overline{{m_{1}}}\\right)^{2}}=\\sqrt{\\frac{1}{3}\\left[0.09^{2}+\\left(-0.34\\right)^{2}+0.12^{2}+0.12^{2}\\right]}=0.22{\\rm g}\\end{align}$" 151 | ], 152 | "text/plain": [ 153 | "" 154 | ] 155 | }, 156 | "execution_count": 7, 157 | "metadata": {}, 158 | "output_type": "execute_result" 159 | } 160 | ], 161 | "source": [ 162 | "Al.staDevi(process=True)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 8, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "data": { 172 | "text/latex": [ 173 | "$\\begin{align}&\\overline{{m_{1}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{1}}_{i}=\\frac{1}{4}\\left(10.69+10.26+10.71+10.72\\right)=10.60{\\rm g}\\\\ \n", 174 | "&\\overline{{m_{1}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{1}}_{i}=\\frac{1}{4}\\left(10.69+10.26+10.71+10.72\\right)=10.60{\\rm g}\\\\ \n", 175 | "&s_{{m_{1}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({m_{1}}_{i}-\\overline{{m_{1}}}\\right)^{2}}=\\sqrt{\\frac{1}{3}\\left[0.09^{2}+\\left(-0.34\\right)^{2}+0.12^{2}+0.12^{2}\\right]}=0.220{\\rm g}\\\\ \n", 176 | "&s_{r}=\\frac{s_{{m_{1}}}}{\\overline{{m_{1}}}}\\times 100\\%=\\frac{0.220}{10.60}\\times 100\\%=2.1\\%\\end{align}$" 177 | ], 178 | "text/plain": [ 179 | "" 180 | ] 181 | }, 182 | "execution_count": 8, 183 | "metadata": {}, 184 | "output_type": "execute_result" 185 | } 186 | ], 187 | "source": [ 188 | "Al.relStaDevi(process=True)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 9, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "data": { 198 | "text/latex": [ 199 | "$\\begin{align}&\\overline{{m_{1}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{1}}_{i}=\\frac{1}{4}\\left(10.69+10.26+10.71+10.72\\right)=10.60{\\rm g}\\\\ \n", 200 | "&根据公式d_{ri}=\\frac{{m_{1}}_{i}-\\overline{{m_{1}}}}{\\overline{{m_{1}}}}\\times 100\\%,得\\\\ \n", 201 | "&d_{r1}=\\frac{10.69-10.60}{10.60}\\times 100\\%=0.9\\%\\\\ \n", 202 | "&d_{r2}=\\frac{10.26-10.60}{10.60}\\times 100\\%=-3\\%\\\\ \n", 203 | "&d_{r3}=\\frac{10.71-10.60}{10.60}\\times 100\\%=1.1\\%\\\\ \n", 204 | "&d_{r4}=\\frac{10.72-10.60}{10.60}\\times 100\\%=1.2\\%\\end{align}$" 205 | ], 206 | "text/plain": [ 207 | "" 208 | ] 209 | }, 210 | "execution_count": 9, 211 | "metadata": {}, 212 | "output_type": "execute_result" 213 | } 214 | ], 215 | "source": [ 216 | "Al.relDevi(process=True)" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 10, 222 | "metadata": {}, 223 | "outputs": [ 224 | { 225 | "data": { 226 | "text/latex": [ 227 | "$\\begin{align}&\\overline{{m_{1}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{1}}_{i}=\\frac{1}{4}\\left(10.69+10.26+10.71+10.72\\right)=10.60{\\rm g}\\\\ \n", 228 | "&\\overline{d}=\\frac{1}{n}\\sum\\limits_{i=1}^n\\left\\lvert {m_{1}}_{i}-\\overline{{m_{1}}}\\right\\rvert= \\frac{1}{4} \\left(\\left\\lvert 10.69-10.60\\right\\rvert+\\left\\lvert 10.26-10.60\\right\\rvert+\\left\\lvert 10.71-10.60\\right\\rvert+\\left\\lvert 10.72-10.60\\right\\rvert\\right)=0.17{\\rm g}\\end{align}$" 229 | ], 230 | "text/plain": [ 231 | "" 232 | ] 233 | }, 234 | "execution_count": 10, 235 | "metadata": {}, 236 | "output_type": "execute_result" 237 | } 238 | ], 239 | "source": [ 240 | "Al.avgDevi(process=True)" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 11, 246 | "metadata": {}, 247 | "outputs": [ 248 | { 249 | "data": { 250 | "text/latex": [ 251 | "$\\begin{align}&\\overline{{m_{1}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{1}}_{i}=\\frac{1}{4}\\left(10.69+10.26+10.71+10.72\\right)=10.60{\\rm g}\\\\ \n", 252 | "&\\overline{{m_{1}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{1}}_{i}=\\frac{1}{4}\\left(10.69+10.26+10.71+10.72\\right)=10.60{\\rm g}\\\\ \n", 253 | "&\\overline{d}=\\frac{1}{n}\\sum\\limits_{i=1}^n\\left\\lvert {m_{1}}_{i}-\\overline{{m_{1}}}\\right\\rvert= \\frac{1}{4} \\left(\\left\\lvert 10.69-10.60\\right\\rvert+\\left\\lvert 10.26-10.60\\right\\rvert+\\left\\lvert 10.71-10.60\\right\\rvert+\\left\\lvert 10.72-10.60\\right\\rvert\\right)=0.17{\\rm g}\\\\ \n", 254 | "&\\overline{d}_{r}=\\frac{\\overline{d}}{\\overline{{m_{1}}}}\\times 100\\%=\\frac{0.17}{10.60} \\times 100\\%=1.6\\%\\end{align}$" 255 | ], 256 | "text/plain": [ 257 | "" 258 | ] 259 | }, 260 | "execution_count": 11, 261 | "metadata": {}, 262 | "output_type": "execute_result" 263 | } 264 | ], 265 | "source": [ 266 | "Al.relAvgDevi(process=True)" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 12, 272 | "metadata": {}, 273 | "outputs": [ 274 | { 275 | "data": { 276 | "text/latex": [ 277 | "$\\begin{align}&\\overline{{m_{1}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{1}}_{i}=\\frac{1}{4}\\left(10.69+10.26+10.71+10.72\\right)=10.60{\\rm g}\\\\ \n", 278 | "&s_{{m_{1}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({m_{1}}_{i}-\\overline{{m_{1}}}\\right)^{2}}=\\sqrt{\\frac{1}{3}\\left[0.09^{2}+\\left(-0.34\\right)^{2}+0.12^{2}+0.12^{2}\\right]}=0.220{\\rm g}\\\\ \n", 279 | "&\\text{对于双侧区间,}P=1-\\frac{\\alpha}{2}=0.95\\text{,查表得n=4时,}t_{0.95}\\left(3\\right)=3.182\\\\ \n", 280 | "&\\text{置信区间为}\\left(\\overline{{m_{1}}}-\\frac{ts_{{m_{1}}}}{\\sqrt{n}},\\overline{{m_{1}}}+\\frac{ts_{{m_{1}}}}{\\sqrt{n}}\\right)\\text{,代入得}\\left(10.24,10.95\\right)\\end{align}$" 281 | ], 282 | "text/plain": [ 283 | "" 284 | ] 285 | }, 286 | "execution_count": 12, 287 | "metadata": {}, 288 | "output_type": "execute_result" 289 | } 290 | ], 291 | "source": [ 292 | "Al.samConfIntv(process=True)" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 13, 298 | "metadata": {}, 299 | "outputs": [ 300 | { 301 | "data": { 302 | "text/latex": [ 303 | "$\\begin{align}&\\overline{{m_{1}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{1}}_{i}=\\frac{1}{4}\\left(10.69+10.26+10.71+10.72\\right)=10.60{\\rm g}\\\\ \n", 304 | "&s_{{m_{1}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({m_{1}}_{i}-\\overline{{m_{1}}}\\right)^{2}}=\\sqrt{\\frac{1}{3}\\left[0.09^{2}+\\left(-0.34\\right)^{2}+0.12^{2}+0.12^{2}\\right]}=0.220{\\rm g}\\\\ \n", 305 | "&\\text{对于单侧区间,}P=1-\\frac{\\alpha}{2}=0.6826\\text{时,}P'=1-\\alpha=0.3652\\text{,查表得n=4时,}t_{0.3652}\\left(3\\right)=0.527\\\\ \n", 306 | "&\\text{置信区间为}\\left(\\overline{{m_{1}}}-\\frac{ts_{{m_{1}}}}{\\sqrt{n}},+\\infty\\right)\\text{,代入得}\\left(10.54, +\\infty\\right)\\end{align}$" 307 | ], 308 | "text/plain": [ 309 | "" 310 | ] 311 | }, 312 | "execution_count": 13, 313 | "metadata": {}, 314 | "output_type": "execute_result" 315 | } 316 | ], 317 | "source": [ 318 | "Al.samConfIntv(confLevel=0.6826, side='left', process=True)" 319 | ] 320 | }, 321 | { 322 | "cell_type": "code", 323 | "execution_count": 14, 324 | "metadata": {}, 325 | "outputs": [ 326 | { 327 | "data": { 328 | "text/latex": [ 329 | "$\\begin{align}&\\overline{{m_{1}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{1}}_{i}=\\frac{1}{4}\\left(10.69+10.26+10.71+10.72\\right)=10.60{\\rm g}\\\\ \n", 330 | "&s_{{m_{1}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({m_{1}}_{i}-\\overline{{m_{1}}}\\right)^{2}}=\\sqrt{\\frac{1}{3}\\left[0.09^{2}+\\left(-0.34\\right)^{2}+0.12^{2}+0.12^{2}\\right]}=0.220{\\rm g}\\\\ \n", 331 | "&\\text{对于单侧区间,}P=1-\\frac{\\alpha}{2}=0.6826\\text{时,}P'=1-\\alpha=0.3652\\text{,查表得n=4时,}t_{0.3652}\\left(3\\right)=0.527\\\\ \n", 332 | "&\\text{置信区间为}\\left(-\\infty,\\overline{{m_{1}}}+\\frac{ts_{{m_{1}}}}{\\sqrt{n}}\\right)\\text{,代入得}\\left(-\\infty, 10.65\\right)\\end{align}$" 333 | ], 334 | "text/plain": [ 335 | "" 336 | ] 337 | }, 338 | "execution_count": 14, 339 | "metadata": {}, 340 | "output_type": "execute_result" 341 | } 342 | ], 343 | "source": [ 344 | "Al.samConfIntv(confLevel=0.6826, side='right', process=True)" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": 15, 350 | "metadata": {}, 351 | "outputs": [ 352 | { 353 | "data": { 354 | "text/latex": [ 355 | "$\\begin{align}&\\overline{{m_{1}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{1}}_{i}=\\frac{1}{4}\\left(10.69+10.26+10.71+10.72\\right)=10.60{\\rm g}\\\\ \n", 356 | "&s_{{m_{1}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({m_{1}}_{i}-\\overline{{m_{1}}}\\right)^{2}}=\\sqrt{\\frac{1}{3}\\left[0.09^{2}+\\left(-0.34\\right)^{2}+0.12^{2}+0.12^{2}\\right]}=0.220{\\rm g}\\\\ \n", 357 | "&t=\\frac{\\left\\lvert\\overline{{m_{1}}}-\\mu\\right\\rvert}{s_{{m_{1}}}/\\sqrt{n}}=\\frac{\\left\\lvert10.60-10.70\\right\\rvert}{0.220/\\sqrt{4}}=0.939\\\\ \n", 358 | "&\\text{对于双侧区间,}P=1-\\frac{\\alpha}{2}=0.95\\text{,查表得n=4时,}t_{1-\\alpha/2}(n-1)=t_{0.95}(3)=3.182\\\\ \n", 359 | "&t" 363 | ] 364 | }, 365 | "execution_count": 15, 366 | "metadata": {}, 367 | "output_type": "execute_result" 368 | } 369 | ], 370 | "source": [ 371 | "Al.tTest(process=True)" 372 | ] 373 | }, 374 | { 375 | "cell_type": "code", 376 | "execution_count": 16, 377 | "metadata": {}, 378 | "outputs": [], 379 | "source": [ 380 | "from analyticlab import cov, corrCoef, sigDifference\n", 381 | "Al_1 = NumItem('10.69 10.67 10.74 10.72', Al_real, sym='m_{2}', unit='g')\n", 382 | "Al_2 = NumItem('10.76 10.68 10.73 10.74', Al_real, sym='m_{3}', unit='g')" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 17, 388 | "metadata": {}, 389 | "outputs": [ 390 | { 391 | "data": { 392 | "text/latex": [ 393 | "$\\begin{align}&\\overline{{m_{2}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{2}}_{i}=\\frac{1}{4}\\left(10.69+10.67+10.74+10.72\\right)=10.71{\\rm g}\\\\ \n", 394 | "&\\overline{{m_{3}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{3}}_{i}=\\frac{1}{4}\\left(10.76+10.68+10.73+10.74\\right)=10.73{\\rm g}\\\\ \n", 395 | "&s({m_{2}},{m_{3}})=\\frac{1}{n-1}\\sum\\limits_{i=1}^n [({m_{2}}_{i}-\\overline{{m_{2}}})({m_{3}}_{i}-\\overline{{m_{3}}})]=\\frac{1}{3}\\left[\\left(-0.02\\right)\\times0.03+\\left(-0.04\\right)\\times\\left(-0.05\\right)+0.04\\times0.00+0.02\\times0.01\\right]=5.0\\times 10^{-4}{\\rm {g}^2}\\end{align}$" 396 | ], 397 | "text/plain": [ 398 | "" 399 | ] 400 | }, 401 | "execution_count": 17, 402 | "metadata": {}, 403 | "output_type": "execute_result" 404 | } 405 | ], 406 | "source": [ 407 | "cov(Al_1, Al_2, process=True, remainOneMoreDigit=True)" 408 | ] 409 | }, 410 | { 411 | "cell_type": "code", 412 | "execution_count": 18, 413 | "metadata": {}, 414 | "outputs": [ 415 | { 416 | "data": { 417 | "text/latex": [ 418 | "$\\begin{align}&s({m_{2}},{m_{3}})=\\frac{1}{n-1}\\sum\\limits_{i=1}^n [({m_{2}}_{i}-\\overline{{m_{2}}})({m_{3}}_{i}-\\overline{{m_{3}}})]=\\frac{1}{3}\\left[\\left(-0.02\\right)\\times0.03+\\left(-0.04\\right)\\times\\left(-0.05\\right)+0.04\\times0.00+0.02\\times0.01\\right]=5.0\\times 10^{-4}{\\rm {g}^2}\\\\ \n", 419 | "&\\overline{{m_{2}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{2}}_{i}=\\frac{1}{4}\\left(10.69+10.67+10.74+10.72\\right)=10.71{\\rm g}\\\\ \n", 420 | "&s_{{m_{2}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({m_{2}}_{i}-\\overline{{m_{2}}}\\right)^{2}}=\\sqrt{\\frac{1}{3}\\left[\\left(-0.02\\right)^{2}+\\left(-0.04\\right)^{2}+0.04^{2}+0.02^{2}\\right]}=0.030{\\rm g}\\\\ \n", 421 | "&\\overline{{m_{3}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{3}}_{i}=\\frac{1}{4}\\left(10.76+10.68+10.73+10.74\\right)=10.73{\\rm g}\\\\ \n", 422 | "&s_{{m_{3}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({m_{3}}_{i}-\\overline{{m_{3}}}\\right)^{2}}=\\sqrt{\\frac{1}{3}\\left[0.03^{2}+\\left(-0.05\\right)^{2}+0.00^{2}+0.01^{2}\\right]}=0.030{\\rm g}\\\\ \n", 423 | "&r({m_{2}},{m_{3}})=\\frac{s({m_{2}},{m_{3}})}{s_{{m_{2}}}s_{{m_{3}}}}=\\frac{5.0\\times 10^{-4}}{0.030\\times 0.030}=0.6\\end{align}$" 424 | ], 425 | "text/plain": [ 426 | "" 427 | ] 428 | }, 429 | "execution_count": 18, 430 | "metadata": {}, 431 | "output_type": "execute_result" 432 | } 433 | ], 434 | "source": [ 435 | "corrCoef(Al_1, Al_2, process=True)" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 19, 441 | "metadata": {}, 442 | "outputs": [ 443 | { 444 | "data": { 445 | "text/latex": [ 446 | "$\\begin{align}&\\bbox[gainsboro, 2px]{\\text{【通过F检验,比较两组的精密度】}}\\\\ \n", 447 | "&\\overline{{m_{2}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{2}}_{i}=\\frac{1}{4}\\left(10.69+10.67+10.74+10.72\\right)=10.71{\\rm g}\\\\ \n", 448 | "&s_{{m_{2}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({m_{2}}_{i}-\\overline{{m_{2}}}\\right)^{2}}=\\sqrt{\\frac{1}{3}\\left[\\left(-0.02\\right)^{2}+\\left(-0.04\\right)^{2}+0.04^{2}+0.02^{2}\\right]}=0.030{\\rm g}\\\\ \n", 449 | "&\\overline{{m_{3}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{3}}_{i}=\\frac{1}{4}\\left(10.76+10.68+10.73+10.74\\right)=10.73{\\rm g}\\\\ \n", 450 | "&s_{{m_{3}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({m_{3}}_{i}-\\overline{{m_{3}}}\\right)^{2}}=\\sqrt{\\frac{1}{3}\\left[0.03^{2}+\\left(-0.05\\right)^{2}+0.00^{2}+0.01^{2}\\right]}=0.030{\\rm g}\\\\ \n", 451 | "&\\text{其中}s_{{m_{2}}}>s_{{m_{3}}}\\text{,故}s_{\\rm max}=s_{{m_{2}}}\\text{,}s_{\\rm min}=s_{{m_{3}}}\\\\ \n", 452 | "&F=\\frac{s_{\\rm max}^2}{s_{\\rm min}^2}=\\frac{{0.030}^2}{{0.030}^2}=1.198\\\\ \n", 453 | "&P=1-\\frac{\\alpha}{2}=0.95\\text{,}n_{\\rm min}=4\\text{,}n_{\\rm max}=4\\text{,查表得:}F_{1-\\alpha/2}(n_{\\rm min}-1,n_{\\rm max}-1)=F_{0.95}(3,3)=6.388\\\\ \n", 454 | "&F" 463 | ] 464 | }, 465 | "execution_count": 19, 466 | "metadata": {}, 467 | "output_type": "execute_result" 468 | } 469 | ], 470 | "source": [ 471 | "sigDifference(Al_1, Al_2, process=True)" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": null, 477 | "metadata": {}, 478 | "outputs": [], 479 | "source": [] 480 | } 481 | ], 482 | "metadata": { 483 | "kernelspec": { 484 | "display_name": "Python 3", 485 | "language": "python", 486 | "name": "python3" 487 | }, 488 | "language_info": { 489 | "codemirror_mode": { 490 | "name": "ipython", 491 | "version": 3 492 | }, 493 | "file_extension": ".py", 494 | "mimetype": "text/x-python", 495 | "name": "python", 496 | "nbconvert_exporter": "python", 497 | "pygments_lexer": "ipython3", 498 | "version": "3.6.3" 499 | } 500 | }, 501 | "nbformat": 4, 502 | "nbformat_minor": 2 503 | } 504 | -------------------------------------------------------------------------------- /demo/功能演示-相关系数与显著性检验.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/latex": [ 11 | "$\\begin{align}&\\overline{{m_{1}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{1}}_{i}=\\frac{1}{4}\\left(10.69+10.67+10.74+10.72\\right)=10.71{\\rm g}\\\\ \n", 12 | "&\\overline{{m_{2}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{2}}_{i}=\\frac{1}{4}\\left(10.76+10.68+10.73+10.74\\right)=10.73{\\rm g}\\\\ \n", 13 | "&s({m_{1}},{m_{2}})=\\frac{1}{n-1}\\sum\\limits_{i=1}^n [({m_{1}}_{i}-\\overline{{m_{1}}})({m_{2}}_{i}-\\overline{{m_{2}}})]=\\frac{1}{3}\\left[\\left(-0.02\\right)\\times0.03+\\left(-0.04\\right)\\times\\left(-0.05\\right)+0.04\\times0.00+0.02\\times0.01\\right]=5\\times 10^{-4}{\\rm {g}^2}\\end{align}$" 14 | ], 15 | "text/plain": [ 16 | "" 17 | ] 18 | }, 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "output_type": "execute_result" 22 | } 23 | ], 24 | "source": [ 25 | "from analyticlab import cov, corrCoef, sigDifference, NumItem\n", 26 | "Al_1 = NumItem('10.69 10.67 10.74 10.72', sym='m_{1}', unit='g')\n", 27 | "Al_2 = NumItem('10.76 10.68 10.73 10.74', sym='m_{2}', unit='g')\n", 28 | "cov(Al_1, Al_2, process=True)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 2, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "data": { 38 | "text/latex": [ 39 | "$\\begin{align}&s({m_{1}},{m_{2}})=\\frac{1}{n-1}\\sum\\limits_{i=1}^n [({m_{1}}_{i}-\\overline{{m_{1}}})({m_{2}}_{i}-\\overline{{m_{2}}})]=\\frac{1}{3}\\left[\\left(-0.02\\right)\\times0.03+\\left(-0.04\\right)\\times\\left(-0.05\\right)+0.04\\times0.00+0.02\\times0.01\\right]=5.0\\times 10^{-4}{\\rm {g}^2}\\\\ \n", 40 | "&\\overline{{m_{1}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{1}}_{i}=\\frac{1}{4}\\left(10.69+10.67+10.74+10.72\\right)=10.71{\\rm g}\\\\ \n", 41 | "&s_{{m_{1}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({m_{1}}_{i}-\\overline{{m_{1}}}\\right)^{2}}=\\sqrt{\\frac{1}{3}\\left[\\left(-0.02\\right)^{2}+\\left(-0.04\\right)^{2}+0.04^{2}+0.02^{2}\\right]}=0.030{\\rm g}\\\\ \n", 42 | "&\\overline{{m_{2}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{2}}_{i}=\\frac{1}{4}\\left(10.76+10.68+10.73+10.74\\right)=10.73{\\rm g}\\\\ \n", 43 | "&s_{{m_{2}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({m_{2}}_{i}-\\overline{{m_{2}}}\\right)^{2}}=\\sqrt{\\frac{1}{3}\\left[0.03^{2}+\\left(-0.05\\right)^{2}+0.00^{2}+0.01^{2}\\right]}=0.030{\\rm g}\\\\ \n", 44 | "&r({m_{1}},{m_{2}})=\\frac{s({m_{1}},{m_{2}})}{s_{{m_{1}}}s_{{m_{2}}}}=\\frac{5.0\\times 10^{-4}}{0.030\\times 0.030}=0.6\\end{align}$" 45 | ], 46 | "text/plain": [ 47 | "" 48 | ] 49 | }, 50 | "execution_count": 2, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "corrCoef(Al_1, Al_2, process=True)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 3, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/latex": [ 67 | "$\\begin{align}&\\bbox[gainsboro, 2px]{\\text{【通过F检验,比较两组的精密度】}}\\\\ \n", 68 | "&\\overline{{m_{1}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{1}}_{i}=\\frac{1}{4}\\left(10.69+10.67+10.74+10.72\\right)=10.71{\\rm g}\\\\ \n", 69 | "&s_{{m_{1}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({m_{1}}_{i}-\\overline{{m_{1}}}\\right)^{2}}=\\sqrt{\\frac{1}{3}\\left[\\left(-0.02\\right)^{2}+\\left(-0.04\\right)^{2}+0.04^{2}+0.02^{2}\\right]}=0.030{\\rm g}\\\\ \n", 70 | "&\\overline{{m_{2}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {m_{2}}_{i}=\\frac{1}{4}\\left(10.76+10.68+10.73+10.74\\right)=10.73{\\rm g}\\\\ \n", 71 | "&s_{{m_{2}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({m_{2}}_{i}-\\overline{{m_{2}}}\\right)^{2}}=\\sqrt{\\frac{1}{3}\\left[0.03^{2}+\\left(-0.05\\right)^{2}+0.00^{2}+0.01^{2}\\right]}=0.030{\\rm g}\\\\ \n", 72 | "&\\text{其中}s_{{m_{1}}}>s_{{m_{2}}}\\text{,故}s_{\\rm max}=s_{{m_{1}}}\\text{,}s_{\\rm min}=s_{{m_{2}}}\\\\ \n", 73 | "&F=\\frac{s_{\\rm max}^2}{s_{\\rm min}^2}=\\frac{{0.030}^2}{{0.030}^2}=1.198\\\\ \n", 74 | "&P=1-\\frac{\\alpha}{2}=0.95\\text{,}n_{\\rm min}=4\\text{,}n_{\\rm max}=4\\text{,查表得:}F_{1-\\alpha/2}(n_{\\rm min}-1,n_{\\rm max}-1)=F_{0.95}(3,3)=6.388\\\\ \n", 75 | "&F" 84 | ] 85 | }, 86 | "execution_count": 3, 87 | "metadata": {}, 88 | "output_type": "execute_result" 89 | } 90 | ], 91 | "source": [ 92 | "sigDifference(Al_1, Al_2, process=True)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": null, 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [] 101 | } 102 | ], 103 | "metadata": { 104 | "kernelspec": { 105 | "display_name": "Python 3", 106 | "language": "python", 107 | "name": "python3" 108 | }, 109 | "language_info": { 110 | "codemirror_mode": { 111 | "name": "ipython", 112 | "version": 3 113 | }, 114 | "file_extension": ".py", 115 | "mimetype": "text/x-python", 116 | "name": "python", 117 | "nbconvert_exporter": "python", 118 | "pygments_lexer": "ipython3", 119 | "version": "3.6.3" 120 | } 121 | }, 122 | "nbformat": 4, 123 | "nbformat_minor": 2 124 | } 125 | -------------------------------------------------------------------------------- /demo/实例-拉伸法杨氏模量的测量.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/latex": [ 11 | "$\\begin{align}&\\bbox[gainsboro, 2px]{\\textbf{【逐差法计算待测钢丝的杨氏模量】}}\\\\ \n", 12 | "&\\overline{{D}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {D}_{i}=\\frac{1}{3}\\left(0.385+0.387+0.385\\right)=0.386{\\rm mm}\\\\ \n", 13 | "&S=\\cfrac{\\pi {\\overline{D}}^{2}}{4}=\\cfrac{\\pi \\times {0.386}^{2}}{4}=0.117{\\rm mm^2}\\\\ \n", 14 | "&F=3\\times 0.360 \\times 9.8066=10.6{\\rm N}\\\\ \n", 15 | "&\\begin{array}{c|c|c|c}\\hline \\Delta n_1=\\overline{n}_4-\\overline{n}_1/{\\rm cm} & \\Delta n_2=\\overline{n}_5-\\overline{n}_2/{\\rm cm} & \\Delta n_3=\\overline{n}_6-\\overline{n}_3/{\\rm cm} & \\Delta \\overline{n}/{\\rm cm}\\\\\\hline 0.77 & 0.79 & 0.74 & 0.77\\\\\\hline\\end{array}\\\\ \n", 16 | "&E=\\cfrac{8{F}{L}{d_1}}{\\pi {\\overline{D}}^{2}{\\Delta \\overline{n}}{d_2}}=\\cfrac{8 \\times 10.6 \\times {46.7} \\times 10^{-2} \\times {106.3} \\times 10^{-2}}{\\pi \\times \\left({0.386} \\times 10^{-3}\\right)^{2} \\times {0.77} \\times 10^{-2} \\times {6.607} \\times 10^{-2}}=1.78\\times 10^{11}{\\rm N/m^2}\\\\ \n", 17 | "&\\bbox[gainsboro, 2px]{\\textbf{【计算不确定度】}}\\\\ \n", 18 | "&(1)\\text{对于钢丝长度$L$:}\\\\ \n", 19 | "&u_{{{L}} B}=\\frac{a}{k}=\\frac{0.5}{\\sqrt{3}}=0.29{\\rm cm}\\\\ \n", 20 | "&u_{{{L}}}=u_{{{L}} B}=0.29{\\rm cm}\\\\ \n", 21 | "&(2)\\text{对于标尺到小镜的距离$d_1$:}\\\\ \n", 22 | "&u_{{{d_1}} B}=\\frac{a}{k}=\\frac{0.5}{\\sqrt{3}}=0.29{\\rm cm}\\\\ \n", 23 | "&u_{{{d_1}}}=u_{{{d_1}} B}=0.29{\\rm cm}\\\\ \n", 24 | "&(3)\\text{对于钢丝直径$D$:}\\\\ \n", 25 | "&u_{{D} A}=t_{n}\\sqrt{\\frac{1}{n(n-1)}\\sum\\limits_{i=1}^n\\left({D}_{i}-\\overline{{D}}\\right)^{2}}=1.32 \\times\\sqrt{\\frac{1}{3\\times 2}\\left[\\left(-0.001\\right)^{2}+0.001^{2}+\\left(-0.001\\right)^{2}\\right]}=7.6\\times 10^{-4}{\\rm mm}\\\\ \n", 26 | "&u_{{D} B}=\\frac{a}{k}=\\frac{0.004}{\\sqrt{3}}=2.3\\times 10^{-3}{\\rm mm}\\\\ \n", 27 | "&u_{{D}}=\\sqrt{{u_{{D} A}}^{2}+{u_{{D} B}}^{2}}=\\sqrt{\\left(7.6\\times 10^{-4}\\right)^{2}+\\left(2.3\\times 10^{-3}\\right)^{2}}=2.4\\times 10^{-3}{\\rm mm}\\\\ \n", 28 | "&(4)\\text{对于光标尺读数差值$\\Delta n$:}\\\\ \n", 29 | "&u_{{\\Delta n} A}=t_{n}\\sqrt{\\frac{1}{n(n-1)}\\sum\\limits_{i=1}^n\\left({\\Delta n}_{i}-\\overline{{\\Delta n}}\\right)^{2}}=1.32 \\times\\sqrt{\\frac{1}{3\\times 2}\\left[0.00^{2}+0.02^{2}+\\left(-0.03\\right)^{2}\\right]}=0.023{\\rm cm}\\\\ \n", 30 | "&u_{{\\Delta n} B}=\\frac{a}{k}=\\frac{0.05}{\\sqrt{3}}=0.029{\\rm cm}\\\\ \n", 31 | "&u_{{\\Delta n}}=\\sqrt{{u_{{\\Delta n} A}}^{2}+{u_{{\\Delta n} B}}^{2}}=\\sqrt{0.023^{2}+0.029^{2}}=0.037{\\rm cm}\\\\ \n", 32 | "&(5)\\text{对于光杠杆小镜前后支脚间的距离$d_2$:}\\\\ \n", 33 | "&u_{{{d_2}} B}=\\frac{a}{k}=\\frac{0.002}{\\sqrt{3}}=1.2\\times 10^{-3}{\\rm cm}\\\\ \n", 34 | "&u_{{{d_2}}}=u_{{{d_2}} B}=1.2\\times 10^{-3}{\\rm cm}\\\\ \n", 35 | "&\\text{计算合成不确定度:}\\\\ \n", 36 | "&\\frac{u_{E}}{E}=\\sqrt{\\left(\\cfrac{{u_{{{L}}}}}{{{{L}}}}\\right)^{2}+\\left(\\cfrac{{u_{{{d_1}}}}}{{{{d_1}}}}\\right)^{2}+\\left(\\cfrac{2{u_{{D}}}}{{{D}}}\\right)^{2}+\\left(\\cfrac{{u_{{\\Delta n}}}}{{{\\Delta n}}}\\right)^{2}+\\left(\\cfrac{{u_{{{d_2}}}}}{{{{d_2}}}}\\right)^{2}}\\\\&\\quad=\\sqrt{\\left(\\cfrac{{0.29}}{{46.7}}\\right)^{2}+\\left(\\cfrac{{0.29}}{{106.3}}\\right)^{2}+\\left(\\cfrac{2 \\times {2.4\\times 10^{-3}}}{{0.386}}\\right)^{2}+\\left(\\cfrac{{0.037}}{{0.77}}\\right)^{2}+\\left(\\cfrac{{1.2\\times 10^{-3}}}{{6.607}}\\right)^{2}}\\\\&\\quad=5\\%\\\\ \n", 37 | "&u_{E}=\\frac{u_{E}}{E}\\cdot E=5\\%\\times 1.78\\times 10^{11}=8.89\\times 10^{9}{\\rm N/m^2}\\\\ \n", 38 | "&\\text{}E=(1.78 \\pm 0.09)\\times 10^{11}{\\rm N/m^2}\\qquad {\\rm P=0.6826}\\\\ \n", 39 | "&E_r=\\cfrac{\\left\\lvert E-\\mu \\right\\rvert}{\\mu}\\times 100\\%=\\cfrac{\\left\\lvert 1.78\\times 10^{11}-2.05\\times 10^{11} \\right\\rvert}{2.05\\times 10^{11}}\\times 100\\%=13\\%\\end{align}$" 40 | ], 41 | "text/plain": [ 42 | "" 43 | ] 44 | }, 45 | "execution_count": 1, 46 | "metadata": {}, 47 | "output_type": "execute_result" 48 | } 49 | ], 50 | "source": [ 51 | "################################################################################\n", 52 | "###############################函数体部分(开始)###############################\n", 53 | "def 拉伸法杨氏模量的测量(n, D, L, d1, d2):\n", 54 | " from analyticlab import LSym, LSymItem, NumItem, ins, Uncertainty, Measure, ut1e, PI, LaTeX\n", 55 | "\n", 56 | " #设置环境参数\n", 57 | " Uncertainty.process = True\n", 58 | " Measure.AMethod = 'CollegePhysics'\n", 59 | " LSymItem.sepSymCalc = True\n", 60 | "\n", 61 | " #定义实验常量\n", 62 | " F = LSym('F', 10.6)\n", 63 | "\n", 64 | " #导入实验数据\n", 65 | " ns = NumItem(n, unit='cm') #砝码个数为1~6时对应的6组平均光标尺读数(cm)\n", 66 | " Ds = NumItem(D, sym='D', unit='mm') #三次钢丝直径的测量值(mm)\n", 67 | " L = LSym('L', L) #钢丝长度(cm)\n", 68 | " d1 = LSym('d_1', d1) #标尺到小镜的距离(cm)\n", 69 | " d2 = LSym('d_2', d2) #光杠杆小镜前后支脚间的距离(cm)\n", 70 | "\n", 71 | " #逐差法计算待测钢丝的杨氏模量\n", 72 | " lx = LaTeX(r'\\bbox[gainsboro, 2px]{\\textbf{【逐差法计算待测钢丝的杨氏模量】}}')\n", 73 | " meanD, lsub = Ds.mean(process=True, needValue=True)\n", 74 | " lx.add(lsub)\n", 75 | " D = LSym(r'\\overline{D}', meanD) #平均钢丝直径\n", 76 | " S = PI*D**2/4\n", 77 | " lx.addLSym(S, 'S', 'mm^2')\n", 78 | " lx.add(r'F=3\\times 0.360 \\times 9.8066=10.6{\\rm N}')\n", 79 | " d_n1 = ns[3] - ns[0]\n", 80 | " d_n2 = ns[4] - ns[1]\n", 81 | " d_n3 = ns[5] - ns[2]\n", 82 | " dns = NumItem([d_n1, d_n2, d_n3])\n", 83 | " dn = dns.mean()\n", 84 | " lx.addTable([\n", 85 | " [(r'\\Delta n_%d=\\overline{n}_%d-\\overline{n}_%d/{\\rm cm}' % (i, i+3, i)) for i in (1,2,3)] + [r'\\Delta \\overline{n}/{\\rm cm}'],\n", 86 | " [str(d_n1), str(d_n2), str(d_n3), str(dn)]\n", 87 | " ])\n", 88 | " dn = LSym(r'\\Delta \\overline{n}', dn)\n", 89 | " E = (8*F*L*ut1e(-2)*d1*ut1e(-2)) / (PI*(D*ut1e(-3))**2*dn*ut1e(-2)*d2*ut1e(-2))\n", 90 | " lx.addLSym(E, 'E', r'N/m^2')\n", 91 | "\n", 92 | " #计算不确定度\n", 93 | " lx.add(r'\\bbox[gainsboro, 2px]{\\textbf{【计算不确定度】}}')\n", 94 | " D = Measure(Ds, ins.一级千分尺_毫米_3, description='钢丝直径$D$')\n", 95 | " dn = Measure(dns, ins.Ins('0.05', ins.Ins.rectangle, 'cm'), sym='\\Delta n', description=r'光标尺读数差值$\\Delta n$')\n", 96 | " L = Measure(L, ins.米尺_厘米_1, description='钢丝长度$L$')\n", 97 | " d1 = Measure(d1, ins.米尺_厘米_1, description='标尺到小镜的距离$d_1$')\n", 98 | " d2 = Measure(d2, ins.游标卡尺_厘米_3, description='光杠杆小镜前后支脚间的距离$d_2$')\n", 99 | " u_E = (8*F*L*ut1e(-2)*d1*ut1e(-2)) / (PI*(D*ut1e(-3))**2*dn*ut1e(-2)*d2*ut1e(-2))\n", 100 | " lx.addUnc(u_E, E, 'E', r'N/m^2')\n", 101 | " lx.addRelErr(E, '2.05e11', 'E', r'\\mu')\n", 102 | " return lx\n", 103 | "###############################函数体部分(结束)###############################\n", 104 | "################################################################################\n", 105 | "\n", 106 | "#######################————在这里输入实验数据————#######################\n", 107 | "拉伸法杨氏模量的测量(\n", 108 | " n = '0.34 0.61 0.93 1.11 1.40 1.67', #砝码个数为1~6时对应的6组平均光标尺读数(cm)\n", 109 | " D = '0.385 0.387 0.385', #三次钢丝直径的测量值(mm)\n", 110 | " L = '46.7', #钢丝长度(cm)\n", 111 | " d1 = '106.3', #标尺到小镜的距离(cm)\n", 112 | " d2 = '6.607' #光杠杆小镜前后支脚间的距离(cm)\n", 113 | ")" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": null, 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [] 122 | } 123 | ], 124 | "metadata": { 125 | "kernelspec": { 126 | "display_name": "Python 3", 127 | "language": "python", 128 | "name": "python3" 129 | }, 130 | "language_info": { 131 | "codemirror_mode": { 132 | "name": "ipython", 133 | "version": 3 134 | }, 135 | "file_extension": ".py", 136 | "mimetype": "text/x-python", 137 | "name": "python", 138 | "nbconvert_exporter": "python", 139 | "pygments_lexer": "ipython3", 140 | "version": "3.6.3" 141 | } 142 | }, 143 | "nbformat": 4, 144 | "nbformat_minor": 2 145 | } 146 | -------------------------------------------------------------------------------- /demo/实例-液体粘滞系数.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/latex": [ 11 | "$\\begin{align}&\\bbox[gainsboro, 2px]{\\textbf{【液体粘滞系数的计算】}}\\\\ \n", 12 | "&\\overline{{d_{1}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {d_{1}}_{i}=\\frac{1}{3}\\left(3.007+3.010+3.008\\right)=3.008{\\rm mm}\\\\ \n", 13 | "&\\overline{{d_{2}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {d_{2}}_{i}=\\frac{1}{3}\\left(3.005+3.009+3.007\\right)=3.007{\\rm mm}\\\\ \n", 14 | "&\\overline{{d_{3}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {d_{3}}_{i}=\\frac{1}{3}\\left(2.996+3.001+2.999\\right)=2.999{\\rm mm}\\\\ \n", 15 | "&\\overline{{d_{4}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {d_{4}}_{i}=\\frac{1}{3}\\left(3.001+2.998+2.997\\right)=2.999{\\rm mm}\\\\ \n", 16 | "&\\overline{{d_{5}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {d_{5}}_{i}=\\frac{1}{3}\\left(3.006+3.009+3.011\\right)=3.009{\\rm mm}\\\\ \n", 17 | "&\\text{根据液体粘滞系数的计算公式$\\eta=\\cfrac{\\left({\\rho'}-{\\rho}\\right){g}{d}^{2}{t}}{18{L}}\\cfrac{1}{\\left(1+2.4\\cfrac{{d}}{{D}}\\right)\\left(1+1.6\\cfrac{{d}}{{H}}\\right)}$,得}\\\\ \n", 18 | "&{\\eta}_{1}=\\cfrac{\\left(7.9-0.958\\right) \\times 10^{3} \\times 9.7964 \\times \\left({3.008} \\times 10^{-3}\\right)^{2} \\times {24.98}}{18 \\times {0.5069}} \\times \\cfrac{1}{\\left(1+2.4 \\times \\cfrac{{3.008} \\times 10^{-3}}{{0.11996}}\\right) \\times \\left(1+1.6 \\times \\cfrac{{3.008} \\times 10^{-3}}{{0.6271}}\\right)}=1.577{\\rm Pa\\cdot s}\\\\ \n", 19 | "&{\\eta}_{2}=\\cfrac{\\left(7.9-0.958\\right) \\times 10^{3} \\times 9.7964 \\times \\left({3.007} \\times 10^{-3}\\right)^{2} \\times {25.02}}{18 \\times {0.5081}} \\times \\cfrac{1}{\\left(1+2.4 \\times \\cfrac{{3.007} \\times 10^{-3}}{{0.12001}}\\right) \\times \\left(1+1.6 \\times \\cfrac{{3.007} \\times 10^{-3}}{{0.6266}}\\right)}=1.575{\\rm Pa\\cdot s}\\\\ \n", 20 | "&{\\eta}_{3}=\\cfrac{\\left(7.9-0.958\\right) \\times 10^{3} \\times 9.7964 \\times \\left({2.999} \\times 10^{-3}\\right)^{2} \\times {25.00}}{18 \\times {0.5066}} \\times \\cfrac{1}{\\left(1+2.4 \\times \\cfrac{{2.999} \\times 10^{-3}}{{0.12014}}\\right) \\times \\left(1+1.6 \\times \\cfrac{{2.999} \\times 10^{-3}}{{0.6259}}\\right)}=1.570{\\rm Pa\\cdot s}\\\\ \n", 21 | "&{\\eta}_{4}=\\cfrac{\\left(7.9-0.958\\right) \\times 10^{3} \\times 9.7964 \\times \\left({2.999} \\times 10^{-3}\\right)^{2} \\times {25.06}}{18 \\times {0.5075}} \\times \\cfrac{1}{\\left(1+2.4 \\times \\cfrac{{2.999} \\times 10^{-3}}{{0.11967}}\\right) \\times \\left(1+1.6 \\times \\cfrac{{2.999} \\times 10^{-3}}{{0.6273}}\\right)}=1.570{\\rm Pa\\cdot s}\\\\ \n", 22 | "&{\\eta}_{5}=\\cfrac{\\left(7.9-0.958\\right) \\times 10^{3} \\times 9.7964 \\times \\left({3.009} \\times 10^{-3}\\right)^{2} \\times {25.01}}{18 \\times {0.5073}} \\times \\cfrac{1}{\\left(1+2.4 \\times \\cfrac{{3.009} \\times 10^{-3}}{{0.11992}}\\right) \\times \\left(1+1.6 \\times \\cfrac{{3.009} \\times 10^{-3}}{{0.6268}}\\right)}=1.578{\\rm Pa\\cdot s}\\\\ \n", 23 | "&\\overline{{\\eta}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {\\eta}_{i}=\\frac{1}{5}\\left(1.577+1.575+1.570+1.570+1.578\\right)=1.574{\\rm Pa\\cdot s}\\\\ \n", 24 | "&\\bbox[gainsboro, 2px]{\\textbf{【计算不确定度】}}\\\\ \n", 25 | "&(1)\\text{对于小球直径$d_{i}$:}\\\\ \n", 26 | "&s_{{d_{1}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({d_{1}}_{i}-\\overline{{d_{1}}}\\right)^{2}}=\\sqrt{\\frac{1}{2}\\left[\\left(-0.001\\right)^{2}+0.002^{2}+\\left(-0.000\\right)^{2}\\right]}=2.0\\times 10^{-3}{\\rm mm}\\\\ \n", 27 | "&s_{{d_{2}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({d_{2}}_{i}-\\overline{{d_{2}}}\\right)^{2}}=\\sqrt{\\frac{1}{2}\\left[\\left(-0.002\\right)^{2}+0.002^{2}+0.000^{2}\\right]}=2.0\\times 10^{-3}{\\rm mm}\\\\ \n", 28 | "&s_{{d_{3}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({d_{3}}_{i}-\\overline{{d_{3}}}\\right)^{2}}=\\sqrt{\\frac{1}{2}\\left[\\left(-0.003\\right)^{2}+0.002^{2}+0.000^{2}\\right]}=3.0\\times 10^{-3}{\\rm mm}\\\\ \n", 29 | "&s_{{d_{4}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({d_{4}}_{i}-\\overline{{d_{4}}}\\right)^{2}}=\\sqrt{\\frac{1}{2}\\left[0.002^{2}+\\left(-0.001\\right)^{2}+\\left(-0.002\\right)^{2}\\right]}=2.0\\times 10^{-3}{\\rm mm}\\\\ \n", 30 | "&s_{{d_{5}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({d_{5}}_{i}-\\overline{{d_{5}}}\\right)^{2}}=\\sqrt{\\frac{1}{2}\\left[\\left(-0.003\\right)^{2}+0.000^{2}+0.002^{2}\\right]}=3.0\\times 10^{-3}{\\rm mm}\\\\ \n", 31 | "&s_{p}=\\sqrt{\\frac{\\sum\\limits_{i=1}^m {s_{{d} i}}^{2}}{m}}=\\sqrt{\\frac{2.0^{2}+2.0^{2}+3.0^{2}+2.0^{2}+3.0^{2}}{5}}\\times 10^{-3}=2.4\\times 10^{-3}{\\rm mm}\\\\ \n", 32 | "&u_{{d} A}=\\frac{t_{v+1}s_p}{\\sqrt{mn}}=\\frac{1.05 \\times 2.4\\times 10^{-3}}{\\sqrt{15}}=6.6\\times 10^{-4}{\\rm mm}\\\\ \n", 33 | "&u_{{d} B}=\\frac{a}{k}=\\frac{0.004}{\\sqrt{3}}=2.3\\times 10^{-3}{\\rm mm}\\\\ \n", 34 | "&u_{{d}}=\\sqrt{{u_{{d} A}}^{2}+{u_{{d} B}}^{2}}=\\sqrt{\\left(6.6\\times 10^{-4}\\right)^{2}+\\left(2.3\\times 10^{-3}\\right)^{2}}=2.4\\times 10^{-3}{\\rm mm}\\\\ \n", 35 | "&(2)\\text{对于下落时间$t$:}\\\\ \n", 36 | "&u_{{t} A}=t_{n}\\sqrt{\\frac{1}{n(n-1)}\\sum\\limits_{i=1}^n\\left({t}_{i}-\\overline{{t}}\\right)^{2}}=1.14 \\times\\sqrt{\\frac{1}{5\\times 4}\\left[\\left(-0.03\\right)^{2}+0.01^{2}+\\left(-0.01\\right)^{2}+0.05^{2}+\\left(-0.00\\right)^{2}\\right]}=0.015{\\rm s}\\\\ \n", 37 | "&u_{{t}}=u_{{t} A}=0.015{\\rm s}\\\\ \n", 38 | "&(3)\\text{对于下落距离$L$:}\\\\ \n", 39 | "&u_{{L} A}=t_{n}\\sqrt{\\frac{1}{n(n-1)}\\sum\\limits_{i=1}^n\\left({L}_{i}-\\overline{{L}}\\right)^{2}}=1.14 \\times\\sqrt{\\frac{1}{5\\times 4}\\left[\\left(-0.0004\\right)^{2}+0.0008^{2}+\\left(-0.0007\\right)^{2}+0.0002^{2}+0.0000^{2}\\right]}=3.1\\times 10^{-4}{\\rm m}\\\\ \n", 40 | "&u_{{L} B}=\\frac{a}{k}=\\frac{0.0005}{\\sqrt{3}}=2.9\\times 10^{-4}{\\rm m}\\\\ \n", 41 | "&u_{{L}}=\\sqrt{{u_{{L} A}}^{2}+{u_{{L} B}}^{2}}=\\sqrt{\\left(3.1\\times 10^{-4}\\right)^{2}+\\left(2.9\\times 10^{-4}\\right)^{2}}=4.2\\times 10^{-4}{\\rm m}\\\\ \n", 42 | "&(4)\\text{对于圆筒内径$D$:}\\\\ \n", 43 | "&u_{{D} A}=t_{n}\\sqrt{\\frac{1}{n(n-1)}\\sum\\limits_{i=1}^n\\left({D}_{i}-\\overline{{D}}\\right)^{2}}=1.14 \\times\\sqrt{\\frac{1}{5\\times 4}\\left[0.00002^{2}+0.00007^{2}+0.00020^{2}+\\left(-0.00027\\right)^{2}+\\left(-0.00002\\right)^{2}\\right]}=8.67\\times 10^{-5}{\\rm m}\\\\ \n", 44 | "&u_{{D} B}=\\frac{a}{k}=\\frac{0.00002}{\\sqrt{3}}=1.2\\times 10^{-5}{\\rm m}\\\\ \n", 45 | "&u_{{D}}=\\sqrt{{u_{{D} A}}^{2}+{u_{{D} B}}^{2}}=\\sqrt{\\left(8.67\\times 10^{-5}\\right)^{2}+\\left(1.2\\times 10^{-5}\\right)^{2}}=8.74\\times 10^{-5}{\\rm m}\\\\ \n", 46 | "&(5)\\text{对于蓖麻油深度$H$:}\\\\ \n", 47 | "&u_{{H} A}=t_{n}\\sqrt{\\frac{1}{n(n-1)}\\sum\\limits_{i=1}^n\\left({H}_{i}-\\overline{{H}}\\right)^{2}}=1.14 \\times\\sqrt{\\frac{1}{5\\times 4}\\left[0.0004^{2}+\\left(-0.0001\\right)^{2}+\\left(-0.0008\\right)^{2}+0.0006^{2}+0.0001^{2}\\right]}=2.5\\times 10^{-4}{\\rm m}\\\\ \n", 48 | "&u_{{H} B}=\\frac{a}{k}=\\frac{0.0005}{\\sqrt{3}}=2.9\\times 10^{-4}{\\rm m}\\\\ \n", 49 | "&u_{{H}}=\\sqrt{{u_{{H} A}}^{2}+{u_{{H} B}}^{2}}=\\sqrt{\\left(2.5\\times 10^{-4}\\right)^{2}+\\left(2.9\\times 10^{-4}\\right)^{2}}=3.9\\times 10^{-4}{\\rm m}\\\\ \n", 50 | "&\\text{计算合成不确定度:}\\\\ \n", 51 | "&u_{\\eta}=\\sqrt{\\left(\\cfrac{\\partial \\eta}{\\partial {d}}\\right)^2 u_{{d}}^2+\\left(\\cfrac{\\partial \\eta}{\\partial {t}}\\right)^2 u_{{t}}^2+\\left(\\cfrac{\\partial \\eta}{\\partial {L}}\\right)^2 u_{{L}}^2+\\left(\\cfrac{\\partial \\eta}{\\partial {D}}\\right)^2 u_{{D}}^2+\\left(\\cfrac{\\partial \\eta}{\\partial {H}}\\right)^2 u_{{H}}^2}\\\\&\\quad=\\sqrt{\\cfrac{0.0177778 {\\left(10^{-3}\\right)}^{6}\\left[\\left({\\rho'}-{\\rho}\\right){g}\\right]^{2}{u_{{D}}}^{2}{{d}}^{6}{{t}}^{2}}{{{D}}^{4}{{L}}^{2}\\left(\\cfrac{2.4 10^{-3}{{d}}}{{{D}}}+1\\right)^{4}\\left(\\cfrac{1.6 10^{-3}{{d}}}{{{H}}}+1\\right)^{2}}+\\cfrac{0.00790123 {\\left(10^{-3}\\right)}^{6}\\left[\\left({\\rho'}-{\\rho}\\right){g}\\right]^{2}{u_{{H}}}^{2}{{d}}^{6}{{t}}^{2}}{{{H}}^{4}{{L}}^{2}\\left(\\cfrac{2.4 10^{-3}{{d}}}{{{D}}}+1\\right)^{2}\\left(\\cfrac{1.6 10^{-3}{{d}}}{{{H}}}+1\\right)^{4}}+\\cfrac{{\\left(10^{-3}\\right)}^{4}\\left[\\left({\\rho'}-{\\rho}\\right){g}\\right]^{2}{u_{{L}}}^{2}{{d}}^{4}{{t}}^{2}}{324{{L}}^{4}\\left(\\cfrac{2.4 10^{-3}{{d}}}{{{D}}}+1\\right)^{2}\\left(\\cfrac{1.6 10^{-3}{{d}}}{{{H}}}+1\\right)^{2}}+\\cfrac{{\\left(10^{-3}\\right)}^{4}\\left[\\left({\\rho'}-{\\rho}\\right){g}\\right]^{2}{u_{{t}}}^{2}{{d}}^{4}}{324{{L}}^{2}\\left(\\cfrac{2.4 10^{-3}{{d}}}{{{D}}}+1\\right)^{2}\\left(\\cfrac{1.6 10^{-3}{{d}}}{{{H}}}+1\\right)^{2}}+{u_{{d}}}^{2}\\left[\\cfrac{-0.0888889 {\\left(10^{-3}\\right)}^{3}\\left({\\rho'}-{\\rho}\\right){g}{{d}}^{2}{{t}}}{{{H}}{{L}}\\left(\\cfrac{2.4 10^{-3}{{d}}}{{{D}}}+1\\right)\\left(\\cfrac{1.6 10^{-3}{{d}}}{{{H}}}+1\\right)^{2}}-\\cfrac{0.133333 {\\left(10^{-3}\\right)}^{3}\\left({\\rho'}-{\\rho}\\right){g}{{d}}^{2}{{t}}}{{{D}}{{L}}\\left(\\cfrac{2.4 10^{-3}{{d}}}{{{D}}}+1\\right)^{2}\\left(\\cfrac{1.6 10^{-3}{{d}}}{{{H}}}+1\\right)}+\\cfrac{{\\left(10^{-3}\\right)}^{2}\\left({\\rho'}-{\\rho}\\right){g}{{d}}{{t}}}{9{{L}}\\left(\\cfrac{2.4 10^{-3}{{d}}}{{{D}}}+1\\right)\\left(\\cfrac{1.6 10^{-3}{{d}}}{{{H}}}+1\\right)}\\right]^{2}}\\\\&\\quad=\\sqrt{\\cfrac{0.0177778 {\\left(10^{-3}\\right)}^{6} \\times \\left[\\left(7.9-0.958\\right) \\times 10^{3} \\times 9.7964\\right]^{2} \\times \\left({8.74\\times 10^{-5}}\\right)^{2} \\times {3.004}^{6} \\times {25.01}^{2}}{{0.11994}^{4} \\times {0.5073}^{2} \\times \\left(\\cfrac{2.4 10^{-3} \\times {3.004}}{{0.11994}}+1\\right)^{4} \\times \\left(\\cfrac{1.6 10^{-3} \\times {3.004}}{{0.6267}}+1\\right)^{2}}+\\cfrac{0.00790123 {\\left(10^{-3}\\right)}^{6} \\times \\left[\\left(7.9-0.958\\right) \\times 10^{3} \\times 9.7964\\right]^{2} \\times \\left({3.9\\times 10^{-4}}\\right)^{2} \\times {3.004}^{6} \\times {25.01}^{2}}{{0.6267}^{4} \\times {0.5073}^{2} \\times \\left(\\cfrac{2.4 10^{-3} \\times {3.004}}{{0.11994}}+1\\right)^{2} \\times \\left(\\cfrac{1.6 10^{-3} \\times {3.004}}{{0.6267}}+1\\right)^{4}}+\\cfrac{{\\left(10^{-3}\\right)}^{4} \\times \\left[\\left(7.9-0.958\\right) \\times 10^{3} \\times 9.7964\\right]^{2} \\times \\left({4.2\\times 10^{-4}}\\right)^{2} \\times {3.004}^{4} \\times {25.01}^{2}}{324 \\times {0.5073}^{4} \\times \\left(\\cfrac{2.4 10^{-3} \\times {3.004}}{{0.11994}}+1\\right)^{2} \\times \\left(\\cfrac{1.6 10^{-3} \\times {3.004}}{{0.6267}}+1\\right)^{2}}+\\cfrac{{\\left(10^{-3}\\right)}^{4} \\times \\left[\\left(7.9-0.958\\right) \\times 10^{3} \\times 9.7964\\right]^{2} \\times {0.015}^{2} \\times {3.004}^{4}}{324 \\times {0.5073}^{2} \\times \\left(\\cfrac{2.4 10^{-3} \\times {3.004}}{{0.11994}}+1\\right)^{2} \\times \\left(\\cfrac{1.6 10^{-3} \\times {3.004}}{{0.6267}}+1\\right)^{2}}+\\left({2.4\\times 10^{-3}}\\right)^{2} \\times \\left[\\cfrac{-0.0888889 {\\left(10^{-3}\\right)}^{3} \\times \\left(7.9-0.958\\right) \\times 10^{3} \\times 9.7964 \\times {3.004}^{2} \\times {25.01}}{{0.6267} \\times {0.5073} \\times \\left(\\cfrac{2.4 10^{-3} \\times {3.004}}{{0.11994}}+1\\right) \\times \\left(\\cfrac{1.6 10^{-3} \\times {3.004}}{{0.6267}}+1\\right)^{2}}-\\cfrac{0.133333 {\\left(10^{-3}\\right)}^{3} \\times \\left(7.9-0.958\\right) \\times 10^{3} \\times 9.7964 \\times {3.004}^{2} \\times {25.01}}{{0.11994} \\times {0.5073} \\times \\left(\\cfrac{2.4 10^{-3} \\times {3.004}}{{0.11994}}+1\\right)^{2} \\times \\left(\\cfrac{1.6 10^{-3} \\times {3.004}}{{0.6267}}+1\\right)}+\\cfrac{{\\left(10^{-3}\\right)}^{2} \\times \\left(7.9-0.958\\right) \\times 10^{3} \\times 9.7964 \\times {3.004} \\times {25.01}}{9 \\times {0.5073} \\times \\left(\\cfrac{2.4 10^{-3} \\times {3.004}}{{0.11994}}+1\\right) \\times \\left(\\cfrac{1.6 10^{-3} \\times {3.004}}{{0.6267}}+1\\right)}\\right]^{2}}\\\\&\\quad=2.9\\times 10^{-3}{\\rm Pa\\cdot s}\\\\ \n", 52 | "&\\text{故粘滞系数为}\\eta=(1.574 \\pm 0.003){\\rm Pa\\cdot s}\\qquad {\\rm P=0.6826}\\end{align}$" 53 | ], 54 | "text/plain": [ 55 | "" 56 | ] 57 | }, 58 | "execution_count": 1, 59 | "metadata": {}, 60 | "output_type": "execute_result" 61 | } 62 | ], 63 | "source": [ 64 | "################################################################################\n", 65 | "###############################函数体部分(开始)###############################\n", 66 | "def 液体粘滞系数(d1, d2, d3, d4, d5, D, L, H, t):\n", 67 | " from analyticlab import ut1e, LaTeX, NumItem, LSym, LSymItem, ins, Uncertainty, Measure\n", 68 | "\n", 69 | " #设置环境参数\n", 70 | " Uncertainty.process = True\n", 71 | " Measure.AMethod = 'CollegePhysics'\n", 72 | " LSymItem.sepSymCalc = True\n", 73 | "\n", 74 | " #定义实验常量\n", 75 | " rhop = LSym(r\"\\rho'\", 7.90)\n", 76 | " rho = LSym(r\"\\rho\", 0.958)\n", 77 | " g = LSym('g', 9.7964)\n", 78 | "\n", 79 | " #导入实验数据\n", 80 | " lx = LaTeX(r'\\bbox[gainsboro, 2px]{\\textbf{【液体粘滞系数的计算】}}')\n", 81 | " ds = [None]*5\n", 82 | " ds[0] = NumItem(d1, sym='d_{1}', unit='mm') #小球1直径d1(mm)\n", 83 | " ds[1] = NumItem(d2, sym='d_{2}', unit='mm') #小球2直径d2(mm)\n", 84 | " ds[2] = NumItem(d3, sym='d_{3}', unit='mm') #小球3直径d3(mm)\n", 85 | " ds[3] = NumItem(d4, sym='d_{4}', unit='mm') #小球4直径d4(mm)\n", 86 | " ds[4] = NumItem(d5, sym='d_{5}', unit='mm') #小球5直径d5(mm)\n", 87 | " meanDI = [di.mean(process=True, needValue=True) for di in ds]\n", 88 | " lx.add([mi[1] for mi in meanDI])\n", 89 | " d = LSymItem('d', [mi[0] for mi in meanDI]) #5个小球各自的平均直径(mm)\n", 90 | " D = LSymItem('D', D) #圆筒直径D(m)\n", 91 | " L = LSymItem('L', L) #下落距离L(m)\n", 92 | " H = LSymItem('H', H) #蓖麻油深度H(m)\n", 93 | " t = LSymItem('t', t) #下落时间t(s)\n", 94 | "\n", 95 | " #液体粘滞系数的计算\n", 96 | " eta = (((rhop-rho)*ut1e(3)*g*(d*ut1e(-3))**2*t)/(18*L)) * (1/((1+2.4*((d*ut1e(-3))/D))*(1+1.6*((d*ut1e(-3))/H))))\n", 97 | " lx.addLSymItem(eta, r'\\eta', r'Pa\\cdot s', headExpr='根据液体粘滞系数的计算公式$%s$,得')\n", 98 | "\n", 99 | " #计算不确定度\n", 100 | " lx.add(r'\\bbox[gainsboro, 2px]{\\textbf{【计算不确定度】}}')\n", 101 | " d = Measure(ds, ins.一级千分尺_毫米_3, sym='d', description='小球直径$d_{i}$')\n", 102 | " D = Measure(D, ins.游标卡尺_米_5, description='圆筒内径$D$')\n", 103 | " L = Measure(L, ins.米尺_米_4, description='下落距离$L$')\n", 104 | " H = Measure(H, ins.米尺_米_4, description='蓖麻油深度$H$')\n", 105 | " t = Measure(t, unit='s', description='下落时间$t$')\n", 106 | " u_eta = (((rhop-rho)*ut1e(3)*g*(d*ut1e(-3))**2*t)/(18*L)) * (1/((1+2.4*((d*ut1e(-3))/D))*(1+1.6*((d*ut1e(-3))/H))))\n", 107 | " lx.addUnc(u_eta, NumItem(eta).mean(), r'\\eta', r'Pa\\cdot s', resDescription='故粘滞系数为')\n", 108 | " return lx\n", 109 | "###############################函数体部分(结束)###############################\n", 110 | "################################################################################\n", 111 | "\n", 112 | "#######################————在这里输入实验数据————#######################\n", 113 | "液体粘滞系数(\n", 114 | " d1 = '3.007 3.010 3.008', #小球1直径d1(mm)\n", 115 | " d2 = '3.005 3.009 3.007', #小球2直径d2(mm)\n", 116 | " d3 = '2.996 3.001 2.999', #小球3直径d3(mm)\n", 117 | " d4 = '3.001 2.998 2.997', #小球4直径d4(mm)\n", 118 | " d5 = '3.006 3.009 3.011', #小球5直径d5(mm)\n", 119 | " D = '0.11996 0.12001 0.12014 0.11967 0.11992', #圆筒直径D(m)\n", 120 | " L = '0.5069 0.5081 0.5066 0.5075 0.5073', #下落距离L(m)\n", 121 | " H = '0.6271 0.6266 0.6259 0.6273 0.6268', #蓖麻油深度H(m)\n", 122 | " t = '24.98 25.02 25.00 25.06 25.01' #下落时间t(s)\n", 123 | ")" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [] 132 | } 133 | ], 134 | "metadata": { 135 | "kernelspec": { 136 | "display_name": "Python 3", 137 | "language": "python", 138 | "name": "python3" 139 | }, 140 | "language_info": { 141 | "codemirror_mode": { 142 | "name": "ipython", 143 | "version": 3 144 | }, 145 | "file_extension": ".py", 146 | "mimetype": "text/x-python", 147 | "name": "python", 148 | "nbconvert_exporter": "python", 149 | "pygments_lexer": "ipython3", 150 | "version": "3.6.3" 151 | } 152 | }, 153 | "nbformat": 4, 154 | "nbformat_minor": 2 155 | } 156 | -------------------------------------------------------------------------------- /demo/性能测试与数值运算案例.py: -------------------------------------------------------------------------------- 1 | from time import clock 2 | from analyticlab import NumItem, LSymItem, ACategory, BCategory, ins, Measure, cov, corrCoef, sigDifference, outlier 3 | 4 | Al_real = '10.70' 5 | Al = NumItem('10.69 10.26 10.71 10.72', Al_real, sym='m_{1}', unit='g') 6 | Al_1 = NumItem('10.69 10.67 10.74 10.72', Al_real, sym='m_{2}', unit='g') 7 | Al_2 = NumItem('10.76 10.68 10.73 10.74', Al_real, sym='m_{3}', unit='g') 8 | Al_3 = NumItem("3.13 3.49 4.01 4.48 4.61 4.76 4.98 5.25 5.32 5.39 5.42 5.57 5.59 5.59 5.63 5.63 5.65 5.66 5.67 5.69 5.71 6.00 6.03 6.12 6.76", unit='g') 9 | Al_4 = NumItem('13.52 13.56 13.55 13.54 13.12 13.54', sym = 'm_{4}', unit='g') 10 | Al_5 = NumItem('13.52 13.56 13.55 13.54 13.12 13.54 13.58 13.53 13.74 13.56 13.51', unit='g') 11 | 12 | ttotal = 0 13 | 14 | def bUnc(process=False): 15 | d = LSymItem('d', '1.63 1.68 1.66 1.62') 16 | md = Measure(d, ins.刻度尺_毫米_1) 17 | return md.unc(process) 18 | 19 | def test(func, expr): 20 | start = clock() 21 | for i in range(1000): 22 | p = func() 23 | end = clock() 24 | global ttotal 25 | ttotal += (end-start); 26 | print('%.6fms' % (end-start), expr, p) 27 | 28 | test(lambda: Al.absErr(), '绝对误差') 29 | test(lambda: Al.relErr(), '相对误差') 30 | test(lambda: Al.isum(), '求和') 31 | test(lambda: Al.mean(), '平均值') 32 | test(lambda: Al.devi(), '偏差') 33 | test(lambda: Al.staDevi(), '标准偏差') 34 | test(lambda: Al.relStaDevi(), '相对标准偏差') 35 | test(lambda: Al.relDevi(), '相对偏差') 36 | test(lambda: Al.avgDevi(), '平均偏差') 37 | test(lambda: Al.relAvgDevi(), '相对平均偏差') 38 | test(lambda: Al.samConfIntv(), '双侧置信区间') 39 | test(lambda: Al.samConfIntv(confLevel=0.6826, side='left'), '左侧置信区间') 40 | test(lambda: Al.samConfIntv(confLevel=0.6826, side='right'), '右侧置信区间') 41 | test(lambda: Al.tTest(), 't检验') 42 | test(lambda: cov(Al_1, Al_2), '协方差') 43 | test(lambda: corrCoef(Al_1, Al_2), '相关系数') 44 | test(lambda: sigDifference(Al_1, Al_2), '两组数据的显著性差异') 45 | test(lambda: outlier.Nair(Al_3, 0.65, side='up'), 'Nair检验') 46 | test(lambda: outlier.Grubbs(Al_4), 'Grubbs检验') 47 | test(lambda: outlier.Dixon(Al_5), 'Dixon检验') 48 | test(lambda: outlier.SkewKuri(Al_5), '偏度-峰度检验') 49 | test(lambda: ACategory.Bessel(Al), 'A类不确定度-Bessel法') 50 | test(lambda: ACategory.Range(Al), 'A类不确定度-极差法') 51 | test(lambda: ACategory.CollegePhysics(Al), 'A类不确定度-大学物理实验') 52 | test(lambda: ACategory.CombSamples([Al,Al_1,Al_2,Al_4], sym='m', unit='g', method='CollegePhysics'), 'A类不确定度-组合不确定度') 53 | test(bUnc, 'B类不确定度') 54 | 55 | print('总计用时:%.6fms' % ttotal) 56 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Jan 24 19:15:17 2018 4 | 5 | @author: xingrongtech 6 | """ 7 | 8 | import setuptools 9 | 10 | with open("README.md", "r") as fh: 11 | long_description = fh.read() 12 | 13 | setuptools.setup( 14 | name="analyticlab", 15 | version="0.3.1-dev2", 16 | author="xingrongtech", 17 | author_email="wzv100@163.com", 18 | description="A library for experimental data calculation, treatment and display, which can be used for College Physics Experiment, Analytical Chemistry, etc.", 19 | long_description=long_description, 20 | long_description_content_type="text/markdown", 21 | url="https://github.com/xingrongtech/analyticlab", 22 | install_requires = ['numpy', 'scipy', 'sympy', 'quantities>=0.12.1'], 23 | keywords=['calculation', 'analysis', 'measure', 'uncertainty', 'display'], 24 | packages=['analyticlab', 'analyticlab.lookup', 25 | 'analyticlab.system', 'analyticlab.measure'], 26 | license='MIT License', 27 | classifiers=( 28 | 'Development Status :: 4 - Beta', 29 | 'Operating System :: OS Independent', 30 | 'Intended Audience :: Developers', 31 | 'License :: OSI Approved :: BSD License', 32 | 'Programming Language :: Python', 33 | 'Programming Language :: Python :: Implementation', 34 | 'Programming Language :: Python :: 3.4', 35 | 'Programming Language :: Python :: 3.5', 36 | 'Programming Language :: Python :: 3.6', 37 | 'Topic :: Software Development :: Libraries' 38 | ), 39 | ) 40 | -------------------------------------------------------------------------------- /tutorials/第3章 离群值处理.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 第三章 离群值处理" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "根据现行国家标准**GBT 4883-2008 数据的统计处理和解释正态样本离群值的判断和处理**,**离群值**是指样本中的一个或几个观测值,它们离开其他观测值较远,暗示他们可能来自不同的总体。**离群值**包括**统计离群值**和**歧离值**,其中**统计离群值**是指在剔除水平下统计检验为显著的离群值;**歧离值**是指在检出水平(通常取$\\alpha=0.05$)下显著,但在剔除水平(通常取$\\alpha^*=0.01$)下不显著的离群值。有关离群值、统计离群值、歧离值、检出水平、剔除水平的详细解释,请[点击这里](https://wenku.baidu.com/view/f701e484c8d376eeaeaa31a8.html)阅读标准文件。" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "离群值处理部分位于`analyticlab.outlier`函数模块内,可以实现**已知标准差**、**未知标准差**情形下,限定检出离群值个数**不超过1**和**大于1**时的离群值处理。" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "#### 本章涉及的模块:\n", 29 | "* outlier函数模块 - 离群值处理" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "## 1.导入outlier模块" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": {}, 42 | "source": [ 43 | "通过如下指令实现analyticlab.outlier模块的导入:" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 1, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "from analyticlab import outlier" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "## 2.函数列表" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "**outlier模块支持Nair检验Grubbs检验Dixon检验偏度-峰度检验4种检验法,相应函数如下**:" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "* 已知标准差$\\sigma$情形离群值的判断:\n", 74 | " * `def Nair(item, sigma, detLevel=0.05, delLevel=0.01, side='double', process=False, needValue=False)` - Nair检验法\n", 75 | "* 未知标准差$\\sigma$情形离群值的判断:\n", 76 | " * 限定检出离群值个数不超过1时:\n", 77 | " * `def Grubbs(item, detLevel=0.05, delLevel=0.01, side='double', process=False, needValue=False)` - Grubbs检验法\n", 78 | " * `def Dixon(item, detLevel=0.05, delLevel=0.01, side='double', process=False, needValue=False)` - Dixon检验法\n", 79 | " * 限定检出离群值个数大于1时:\n", 80 | " * `def Dixon(item, detLevel=0.05, delLevel=0.01, side='double', process=False, needValue=False)` - Dixon检验法\n", 81 | " * `def SkewKuri(item, detLevel=0.05, delLevel=0.01, side='double', process=False, needValue=False)` - 偏度-峰度检验法" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "## 3.最简单的调用" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "参数`item`为待检验样本的NumItem数组。对于未知标准差的离群值检验,可以只给出参数`item`;对于已知标准差的离群值检验,可以只给出参数`item`和`simga`(sigma即为标准差$\\sigma$):" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 2, 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [ 104 | "from analyticlab import NumItem" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 3, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "data": { 114 | "text/plain": [ 115 | "({'statOutliers': [13.12], 'stragglers': []},\n", 116 | " [13.52, 13.56, 13.55, 13.54, 13.54])" 117 | ] 118 | }, 119 | "execution_count": 3, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "sample = NumItem('13.52 13.56 13.55 13.54 13.12 13.54')\n", 126 | "outlier.Grubbs(sample) #Grubbs检验" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 4, 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "data": { 136 | "text/plain": [ 137 | "({'statOutliers': [], 'stragglers': [3.13]},\n", 138 | " [3.49, 4.01, 4.48, 4.61, 4.76, 4.98, 5.25, 5.32, 5.39, 5.42, 5.57, 5.59, 5.59, 5.63, 5.63, 5.65, 5.66, 5.67, 5.69, 5.71, 6.00, 6.03, 6.12, 6.76])" 139 | ] 140 | }, 141 | "execution_count": 4, 142 | "metadata": {}, 143 | "output_type": "execute_result" 144 | } 145 | ], 146 | "source": [ 147 | "sample = NumItem(\"3.13 3.49 4.01 4.48 4.61 4.76 4.98 5.25 5.32 5.39 5.42 5.57 5.59 5.59 5.63 5.63 5.65 5.66 5.67 5.69 5.71 6.00 6.03 6.12 6.76\")\n", 148 | "outlier.Nair(sample, 0.65) #Nair检验示例" 149 | ] 150 | }, 151 | { 152 | "cell_type": "markdown", 153 | "metadata": {}, 154 | "source": [ 155 | "离群值检验函数的返回值为**离群值**和**正常值**组成的元组,其中**离群值**为由**统计离群值(statOutliers)**和**歧离值(stragglers)**组成的字典。**统计离群值**、**歧离值**、**正常值**均以`list`给出。" 156 | ] 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "metadata": {}, 161 | "source": [ 162 | "## 4.对哪侧进行检验" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": {}, 168 | "source": [ 169 | "离群值检验分为**双侧情形**、**上侧情形**和**下侧情形**,通过参数`side`决定是哪侧检验。默认`side='double'`,即默认为双侧情形。当需要进行检验的是下侧情形时,可以设置参数side:" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 5, 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "data": { 179 | "text/plain": [ 180 | "({'statOutliers': [3.13], 'stragglers': [3.49]},\n", 181 | " [4.01, 4.48, 4.61, 4.76, 4.98, 5.25, 5.32, 5.39, 5.42, 5.57, 5.59, 5.59, 5.63, 5.63, 5.65, 5.66, 5.67, 5.69, 5.71, 6.00, 6.03, 6.12, 6.76])" 182 | ] 183 | }, 184 | "execution_count": 5, 185 | "metadata": {}, 186 | "output_type": "execute_result" 187 | } 188 | ], 189 | "source": [ 190 | "sample = NumItem(\"3.13 3.49 4.01 4.48 4.61 4.76 4.98 5.25 5.32 5.39 5.42 5.57 5.59 5.59 5.63 5.63 5.65 5.66 5.67 5.69 5.71 6.00 6.03 6.12 6.76\")\n", 191 | "outlier.Nair(sample, 0.65, side='down') #Nair下侧检验" 192 | ] 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "metadata": {}, 197 | "source": [ 198 | "## 5.生成并展示计算过程" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": {}, 204 | "source": [ 205 | "通过附加参数`process=True`,并调用生成LaTeX公式集的`show()`方法,可以展示出离群值处理的分析过程:" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 6, 211 | "metadata": {}, 212 | "outputs": [ 213 | { 214 | "data": { 215 | "text/latex": [ 216 | "$\\begin{align}&\\text{将测量数据由小到大排序:}[13.12, 13.52, 13.54, 13.54, 13.55, 13.56]\\\\ \n", 217 | "&\\overline{{{x}}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {{x}}_{i}=\\frac{1}{6}\\left(13.12+13.52+13.54+13.54+13.55+13.56\\right)=13.47\\\\ \n", 218 | "&s_{{{x}}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({{x}}_{i}-\\overline{{{x}}}\\right)^{2}}=\\sqrt{\\frac{1}{5}\\left[\\left(-0.35\\right)^{2}+0.05^{2}+0.07^{2}+0.07^{2}+0.08^{2}+0.09^{2}\\right]}=0.170\\\\ \n", 219 | "&G_{6}=\\frac{{x}_{(6)}-\\overline{{x}}}{s}=\\frac{13.56-13.47}{0.170}=0.511\\\\ \n", 220 | "&G_{6}'=\\frac{\\overline{{x}}-{x}_{(1)}}{s}=\\frac{13.47-13.12}{0.170}=2.035\\\\ \n", 221 | "&\\text{确定检验水平}\\alpha=0.05\\text{,查表得临界值}G_{1-\\alpha/2}(n)=G_{0.975}(6)=1.887\\\\ \n", 222 | "&\\text{因}G_{6}'> G_{6}\\text{且}G_{6}'>G_{0.975}(6)\\text{,故判定}{x}_{(1)}\\text{为离群值}\\\\ \n", 223 | "&\\text{对于检出的离群值}{x}_{(1)}\\text{,确定剔除水平}\\alpha^{*}=0.01\\text{,查表得临界值}G_{1-\\alpha^{*}/2}(n)=G_{0.975}(6)=1.973\\\\ \n", 224 | "&\\text{因}G_{6}'>G_{0.975}(6)\\text{,故判定}{x}_{(1)}=13.12\\text{为统计离群值}\\end{align}$" 225 | ], 226 | "text/plain": [ 227 | "" 228 | ] 229 | }, 230 | "metadata": {}, 231 | "output_type": "display_data" 232 | } 233 | ], 234 | "source": [ 235 | "sample = NumItem('13.52 13.56 13.55 13.54 13.12 13.54')\n", 236 | "outlier.Grubbs(sample, process=True).show()" 237 | ] 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": {}, 242 | "source": [ 243 | "当既需要处理结果又需要相应的分析过程时,可以在`process=True`的基础上,附加参数`needValue=True`。" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "## \\*6.更改检出水平和剔除水平" 251 | ] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "metadata": {}, 256 | "source": [ 257 | "根据现行国家标准**GBT 4883-2008**,除非根据本标准达成协议的各方另有约定外,$\\alpha$值(检出水平)应为0.05,$\\alpha^*$值(剔除水平)应为0.01。如果确有需要更改检出水平和剔除水平,可以设置参数`detLevel`(检出水平)和`delLevel`(剔除水平)。默认`detLevel=0.05`,`delLevel=0.01`。" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": null, 263 | "metadata": {}, 264 | "outputs": [], 265 | "source": [] 266 | } 267 | ], 268 | "metadata": { 269 | "kernelspec": { 270 | "display_name": "Python 3", 271 | "language": "python", 272 | "name": "python3" 273 | }, 274 | "language_info": { 275 | "codemirror_mode": { 276 | "name": "ipython", 277 | "version": 3 278 | }, 279 | "file_extension": ".py", 280 | "mimetype": "text/x-python", 281 | "name": "python", 282 | "nbconvert_exporter": "python", 283 | "pygments_lexer": "ipython3", 284 | "version": "3.6.5" 285 | } 286 | }, 287 | "nbformat": 4, 288 | "nbformat_minor": 2 289 | } 290 | -------------------------------------------------------------------------------- /tutorials/第6章 LaTeX公式集类.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 第六章 LaTeX公式集类" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "在前面的章节中,提及到当设定`process=True`时,会生成计算过程的LaTeX公式集。本章会详细说明**LaTeX公式集**的使用。通过充分利用LaTeX公式集,能够生成一篇完整的LaTeX格式的**实验报告**。" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "#### 本章涉及的类:\n", 22 | "* 库函数 - 计算过程生成与展示\n", 23 | "* LaTeX类 - 公式集" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "## 1.现有库函数的使用" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "#### 在前面的章节中,提到了下列用于生成计算过程的库函数:\n", 38 | "* `def dispLSym(lSym, resSym=None, resUnit=None)` - 展示LaTeX符号\n", 39 | "* `def dispLSymItem(lSymItem, resSym=None, resUnit=None, headExpr='根据公式$%s$,得', showMean=True, meanExpr=None)` - 展示LaTeX符号组\n", 40 | "* `def dispUnc(resUnc, resValue, resSym=None, resUnit=None, resDescription=None)` - 展示不确定度\n", 41 | "\n", 42 | "#### 还有2个库函数没有提及,它们是:\n", 43 | "* `def dispTable(table)` - 展示表格\n", 44 | "* `def dispRelErr(num, mu, sym=None, muSym=None, ESym='E_r')` - 展示相对误差\n", 45 | "\n", 46 | "下面讲依次介绍这两个函数的使用。" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "### 1.1 展示表格" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "通过`dispTable`函数展示一个表格,该函数如下:" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "`def dispTable(table)`" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "其中参数`table`为表格的内容组成的二维列表,列表中的每一个元素,即为表格中的一个单元格,每个单元格中的内容可以以str类型给出,也可以以非字符串的数据类型给出,如Num、int、float:" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 2, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "data": { 84 | "text/latex": [ 85 | "$\\begin{align}&\\begin{array}{c|c|c|c}\\hline a & 1 & 2.032 & 3\\\\\\hline b & 4.0 & 5 & 6\\\\\\hline\\end{array}\\end{align}$" 86 | ], 87 | "text/plain": [ 88 | "" 89 | ] 90 | }, 91 | "metadata": {}, 92 | "output_type": "display_data" 93 | } 94 | ], 95 | "source": [ 96 | "from analyticlab import dispTable, Num\n", 97 | "dispTable([\n", 98 | " ['a', 1, 2.032, 3],\n", 99 | " ['b', Num('4.0'), 5, 6]\n", 100 | "])" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "当单元格中的内容是以NumItem类型给出时,情况会比较特殊,由于NumItem是由一系列数值组成的数组,而不是单独一个值,因此它会被自动扩展成多个单元格,数组中有几个数值,就会扩展成多少个单元格:" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 3, 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/latex": [ 118 | "$\\begin{align}&\\begin{array}{c|c}\\hline \\mathbf{x} & 7.61 & 7.75 & 7.72 & 7.67 & 7.66\\\\\\hline \\mathbf{y} & 13.01 & 12.96 & 12.99 & 13.05 & 13.00\\\\\\hline\\end{array}\\end{align}$" 119 | ], 120 | "text/plain": [ 121 | "" 122 | ] 123 | }, 124 | "metadata": {}, 125 | "output_type": "display_data" 126 | } 127 | ], 128 | "source": [ 129 | "from analyticlab import NumItem\n", 130 | "x_data = NumItem('7.61 7.75 7.72 7.67 7.66')\n", 131 | "y_data = NumItem('13.01 12.96 12.99 13.05 13.00')\n", 132 | "lp.dispTable([\n", 133 | " [r'\\mathbf{x}', x_data],\n", 134 | " [r'\\mathbf{y}', y_data]\n", 135 | "]).show() # 这里x_data和y_data看似只会占1个单元格,但其实它们各占5个单元格" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": {}, 141 | "source": [ 142 | "注意:`dispTable`作为一个展示表格的函数,其适用范围是很有限的,仅适用于m行n列的表格,若表格中有合并的单元格,则该函数将不再使用,这时需要参见[Mathjax](https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference)中array的语法,并将根据array语法得到的LaTeX语句通过`add`方法添加到LaTeX公式集中。" 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "### 1.2 展示相对误差" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "通过`dispRelErr`函数展示相对误差的计算过程,该函数如下:" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "`def dispRelErr(num, mu, sym=None, muSym=None, ESym='E_r')`" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "其中参数`num`和`mu`分别为测量值和真值,可以以数值的字符串形式给出,也可以以Num、LSym类型给出。\n", 171 | "\n", 172 | "`sym`和`muSym`为测量值、真值的符号,当num或mu为LSym类型时,由于LaTeX符号中同时包含数值和符号,因此不必再给出LSym类型的num对应的sym,或LSym类型的mu对应的muSym。除此之外,必须给出sym、muSym。\n", 173 | "\n", 174 | "`ESym`为相对误差输出的表达式中的首项,即相对误差符号那一项,例如$E_r=\\cfrac{\\left\\lvert x-\\mu\\right\\rvert}{\\mu}=\\cfrac{\\left\\lvert 8.66-8.79\\right\\rvert}{8.79}=1.5\\%$中的$E_r$。当不需要这一项时,可设定`ESym=None`,这是会得到$\\cfrac{\\left\\lvert x-\\mu\\right\\rvert}{\\mu}=\\cfrac{\\left\\lvert 8.66-8.79\\right\\rvert}{8.79}=1.5\\%$。" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "下面举例说明`dispRelErr`函数的使用:" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 6, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "data": { 191 | "text/latex": [ 192 | "$\\begin{align}&E_r=\\cfrac{\\left\\lvert m_测-m_真 \\right\\rvert}{m_真}\\times 100\\%=\\cfrac{\\left\\lvert 13.15-13.40 \\right\\rvert}{13.40}\\times 100\\%=1.9\\%\\end{align}$" 193 | ], 194 | "text/plain": [ 195 | "" 196 | ] 197 | }, 198 | "execution_count": 6, 199 | "metadata": {}, 200 | "output_type": "execute_result" 201 | } 202 | ], 203 | "source": [ 204 | "from analyticlab import dispRelErr\n", 205 | "dispRelErr('13.15', '13.40', 'm_测', 'm_真') #这里num和mu都是字符串形式,所以需要给出sym和muSym" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 7, 211 | "metadata": {}, 212 | "outputs": [ 213 | { 214 | "data": { 215 | "text/latex": [ 216 | "$\\begin{align}&\\cfrac{\\left\\lvert {m_测}-m_真 \\right\\rvert}{m_真}\\times 100\\%=\\cfrac{\\left\\lvert 13.15-13.40 \\right\\rvert}{13.40}\\times 100\\%=1.9\\%\\end{align}$" 217 | ], 218 | "text/plain": [ 219 | "" 220 | ] 221 | }, 222 | "execution_count": 7, 223 | "metadata": {}, 224 | "output_type": "execute_result" 225 | } 226 | ], 227 | "source": [ 228 | "from analyticlab import LSym\n", 229 | "m_测 = LSym('m_测', '13.15')\n", 230 | "dispRelErr(m_测, '13.40', muSym='m_真', ESym=None) #这里num是LSym类型,不需要给出sym;不想要相对误差符号那一项,故ESym=None" 231 | ] 232 | }, 233 | { 234 | "cell_type": "markdown", 235 | "metadata": {}, 236 | "source": [ 237 | "## 2.LaTeX类的使用" 238 | ] 239 | }, 240 | { 241 | "cell_type": "markdown", 242 | "metadata": {}, 243 | "source": [ 244 | "### 2.1 导入LaTeX类" 245 | ] 246 | }, 247 | { 248 | "cell_type": "markdown", 249 | "metadata": {}, 250 | "source": [ 251 | "通过以下指令实现导入LaTeX类:" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": null, 257 | "metadata": {}, 258 | "outputs": [], 259 | "source": [ 260 | "from analyticlab import LaTeX" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": {}, 266 | "source": [ 267 | "### 2.2 创建一个LaTeX公式集" 268 | ] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "LaTeX类的构造方法如下:" 275 | ] 276 | }, 277 | { 278 | "cell_type": "markdown", 279 | "metadata": {}, 280 | "source": [ 281 | "`LaTeX(line=None)`" 282 | ] 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "metadata": {}, 287 | "source": [ 288 | "其中参数`line`为初始化LaTeX公式集时,要加入的公式。`line`可以不给出,可以是一行公式的字符串,也可以是多行公式的字符串组成的列表:" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": 2, 294 | "metadata": {}, 295 | "outputs": [], 296 | "source": [ 297 | "lx1 = LaTeX() #创建一个空的公式集lx1\n", 298 | "lx2 = LaTeX('V=abc') #创建一个公式集lx2,其中含有一行公式\n", 299 | "lx3 = LaTeX(['\\text{根据公式}\\theta=\\arcsin{{k}}\\text{,得}',\n", 300 | " '{\\theta}_1=\\arcsin{0.656}=41.0{\\rm ^{\\circ}}', \n", 301 | " '{\\theta}_2=\\arcsin{0.687}=43.4{\\rm ^{\\circ}}',\n", 302 | " '{\\theta}_3=\\arcsin{0.669}=42.0{\\rm ^{\\circ}}']) #创建一个公式集,其中含有四行公式" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "metadata": {}, 308 | "source": [ 309 | "### 2.3 向现有的公式集中添加新的公式行" 310 | ] 311 | }, 312 | { 313 | "cell_type": "markdown", 314 | "metadata": {}, 315 | "source": [ 316 | "对于通过构造方法或者通过附加参数`process=True`得到的LaTeX公式集,可以通过`add`方法,可以向当前公式集中,添加**新的公式行**或**其他公式集的公式行**。`add`方法如下:" 317 | ] 318 | }, 319 | { 320 | "cell_type": "markdown", 321 | "metadata": {}, 322 | "source": [ 323 | "`def add(line)`" 324 | ] 325 | }, 326 | { 327 | "cell_type": "markdown", 328 | "metadata": {}, 329 | "source": [ 330 | "其中参数`line`为要添加的公式。公式可以是字符串形式,单个字符串用于添加一行公式,多个字符串组成的列表用于添加多行公式;公式也可以源于别的公式集,单个LaTeX对象用于添加一个公式集,多个LaTeX对象组成的列表用于添加多个公式集:" 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": 3, 336 | "metadata": {}, 337 | "outputs": [], 338 | "source": [ 339 | "from analyticlab.numitem import NumItem\n", 340 | "Cl = NumItem('0.0365 0.0361 0.0359 0.0363', sym='Cl^-', unit='mol/L')\n", 341 | "lx1 = LaTeX()\n", 342 | "lx1.add(r'Cl^-\\text{浓度均值:}') #添加一个字符串\n", 343 | "lx1.add(Cl.mean(process=True)) #添加一个LaTeX公式集\n", 344 | "lx1.add(r'Cl^-\\text{浓度标准偏差:}') #添加一个字符串\n", 345 | "lx1.add(Cl.staDevi(process=True, processWithMean=False, remainOneMoreDigit=True)) #添加一个LaTeX公式集" 346 | ] 347 | }, 348 | { 349 | "cell_type": "markdown", 350 | "metadata": {}, 351 | "source": [ 352 | "### 2.4 disp系列函数在公式集中的应用" 353 | ] 354 | }, 355 | { 356 | "cell_type": "markdown", 357 | "metadata": {}, 358 | "source": [ 359 | "在上面的章节中,依次介绍到了`dispLSym`、`dispLSymItem`、`dispUnc`、`dispTable`、`dispRelErr`5个生成计算过程的disp系列库函数。要将这些库函数生成的计算过程添加到LaTeX公式集中,最容易想到的是诸如`lx.add(dispUnc(...))`这样将disp...函数的返回值添加到公式集中的方法。其实,每个`disp...函数`都在LaTeX公式集中,对应一个`add...方法`。对应关系如下:\n", 360 | "* 函数`dispLSym` → 方法`addLSym`\n", 361 | "* 函数`dispLSymItem` → 方法`addLSymItem`\n", 362 | "* 函数`dispUnc` → 方法`addUnc`\n", 363 | "* 函数`dispTable` → 方法`addTable`\n", 364 | "* 函数`dispRelErr` → 方法`addRelErr`" 365 | ] 366 | }, 367 | { 368 | "cell_type": "markdown", 369 | "metadata": {}, 370 | "source": [ 371 | "其中每个库函数对应的类方法的参数都没有变化,只是生成的计算过程直接添加到了LaTeX公式集中,比如`lx.add(dispLSym(uV, 'uV'))`可以写成`lx.addLSym(uV, 'uV')`。" 372 | ] 373 | }, 374 | { 375 | "cell_type": "markdown", 376 | "metadata": {}, 377 | "source": [ 378 | "### 2.5 展示公式集" 379 | ] 380 | }, 381 | { 382 | "cell_type": "markdown", 383 | "metadata": {}, 384 | "source": [ 385 | "通过调用`show()`方法,展示公式集:" 386 | ] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": 4, 391 | "metadata": {}, 392 | "outputs": [ 393 | { 394 | "data": { 395 | "text/latex": [ 396 | "$\\begin{align}&Cl^-\\text{浓度均值:}\\\\ \n", 397 | "&\\overline{{Cl^-}}=\\frac{1}{n}\\sum\\limits_{i=1}^n {Cl^-}_{i}=\\frac{1}{4}\\left(0.0365+0.0361+0.0359+0.0363\\right)=0.0362{\\rm mol/L}\\\\ \n", 398 | "&Cl^-\\text{浓度标准偏差:}\\\\ \n", 399 | "&s_{{Cl^-}}=\\sqrt{\\frac{1}{n-1}\\sum\\limits_{i=1}^n\\left({Cl^-}_{i}-\\overline{{Cl^-}}\\right)^{2}}=\\sqrt{\\frac{1}{3}\\left[0.0003^{2}+\\left(-0.0001\\right)^{2}+\\left(-0.0003\\right)^{2}+0.0001^{2}\\right]}=3.0\\times 10^{-4}{\\rm mol/L}\\end{align}$" 400 | ], 401 | "text/plain": [ 402 | "" 403 | ] 404 | }, 405 | "metadata": {}, 406 | "output_type": "display_data" 407 | } 408 | ], 409 | "source": [ 410 | "lx1.show()" 411 | ] 412 | }, 413 | { 414 | "cell_type": "markdown", 415 | "metadata": {}, 416 | "source": [ 417 | "## 6.综合性应用案例" 418 | ] 419 | }, 420 | { 421 | "cell_type": "markdown", 422 | "metadata": {}, 423 | "source": [ 424 | "在github上analyticlab项目中的`analyticlab/demo`文件夹下,有`实例-拉伸法杨氏模量的测量.ipynb`、`实例-液体粘滞系数.ipynb`这两个展示了更完整的通过LaTeX公式集生成实验报告的案例。" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": null, 430 | "metadata": {}, 431 | "outputs": [], 432 | "source": [] 433 | } 434 | ], 435 | "metadata": { 436 | "kernelspec": { 437 | "display_name": "Python 3", 438 | "language": "python", 439 | "name": "python3" 440 | }, 441 | "language_info": { 442 | "codemirror_mode": { 443 | "name": "ipython", 444 | "version": 3 445 | }, 446 | "file_extension": ".py", 447 | "mimetype": "text/x-python", 448 | "name": "python", 449 | "nbconvert_exporter": "python", 450 | "pygments_lexer": "ipython3", 451 | "version": "3.6.4" 452 | } 453 | }, 454 | "nbformat": 4, 455 | "nbformat_minor": 2 456 | } 457 | -------------------------------------------------------------------------------- /tutorials/第7章 Const常数类.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 第七章 Const常数类" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "在前面的章节中,提及到Num、NumItem、LSym、LSymItem、Measure、Uncertainty可以与int、float类型的纯数字进行数学运算。那么,对于类似于“$\\pi$”、“$e$”、“用于单位换算的科学记数法”这样的常数,尽管它们与Num、NumItem进行运算时,可以用纯数字或math库的常数pi、e表示,但当它们与LSym、LSymItem、Measure、Uncertainty进行数学运算,且涉及到计算过程的生成及展示时,这些常数会在计算过程中直接展开成近似数字,而不是常数本身,例如“$\\pi r^2$”会被展示为“$3.141592653589793 r^2$”。为了实现这些常数的展示,analyticlab引入了Const类,使得“$\\pi r^2$”能够被正常展示,且在代数表达式、数值表达式中都展示为“$\\pi$”。类似于int和float,Const常数可以参与与Num、NumItem、LSym、LSymItem、Measure、Uncertainty的**数值运算**和**符号展示**。" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "#### 本章涉及的类与模块:\n", 22 | "* Const类 - 常数\n", 23 | "* 库函数与库常数 - 常用的Const常数\n", 24 | "* (部分了解)库函数 - 数学运算" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "## 1.const模块:使用现成的常数" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "### 1.1 使用$\\pi$、$e$和$100\\%$" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "#### `const`模块有两个现成的常数:\n", 46 | "* const.PI - $\\pi$\n", 47 | "* const.E - $e$\n", 48 | "* const.hPercent - $100\\%$" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "以上三个常数可直接引用,例如:" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 1, 61 | "metadata": {}, 62 | "outputs": [ 63 | { 64 | "data": { 65 | "text/latex": [ 66 | "$\\begin{align}&S=\\pi {r}^{2}=\\pi \\times {0.751}^{2}=1.77{\\rm cm^2}\\end{align}$" 67 | ], 68 | "text/plain": [ 69 | "" 70 | ] 71 | }, 72 | "execution_count": 1, 73 | "metadata": {}, 74 | "output_type": "execute_result" 75 | } 76 | ], 77 | "source": [ 78 | "from analyticlab import PI, LSym, dispLSym\n", 79 | "r = LSym('r', '0.751')\n", 80 | "S = PI * r**2\n", 81 | "dispLSym(S, 'S', 'cm^2')" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "\\* 其中`hPercent`除了作为一个常数,可以用于符号表达式合成之外,还具有能够自动将一般数值转换为相对比值的功能,当dispRelErr函数生成的相对误差计算过程不能满足需要时,可以使用`hPercent`常数自行定义相对误差计算式。当一个Num、NumItem或者LSym、LSymItem与const.hPercent**相乘**时,其对应的数值会被转换为相对比值。下面举例说明:" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 2, 94 | "metadata": {}, 95 | "outputs": [ 96 | { 97 | "data": { 98 | "text/latex": [ 99 | "$\\begin{align}&E_r=\\cfrac{\\left\\lvert 2{\\Delta x_1}-{\\Delta x_0} \\right\\rvert}{{\\Delta x_0}} \\times 100\\%=\\cfrac{\\left\\lvert 2 \\times {4.33}-{8.79} \\right\\rvert}{{8.79}} \\times 100\\%=1.5\\%{\\rm }\\end{align}$" 100 | ], 101 | "text/plain": [ 102 | "" 103 | ] 104 | }, 105 | "execution_count": 2, 106 | "metadata": {}, 107 | "output_type": "execute_result" 108 | } 109 | ], 110 | "source": [ 111 | "from analyticlab import hPercent\n", 112 | "delta_x1 = LSym(r'\\Delta x_1', '4.33')\n", 113 | "delta_x0 = LSym(r'\\Delta x_0', '8.79')\n", 114 | "Er = abs(2*delta_x1 - delta_x0) / delta_x0 * hPercent\n", 115 | "dispLSym(Er, 'E_r')" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 3, 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "data": { 125 | "text/plain": [ 126 | "1.5%" 127 | ] 128 | }, 129 | "execution_count": 3, 130 | "metadata": {}, 131 | "output_type": "execute_result" 132 | } 133 | ], 134 | "source": [ 135 | "Er.num()" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": {}, 141 | "source": [ 142 | "### 1.3 科学记数法" 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "#### 在`const`模块中,有两个生成科学记数法常数的函数:\n", 150 | "* def t1e(n)\n", 151 | "* def ut1e(n)\n", 152 | "\n", 153 | "在两个函数中,n均表示科学记数法指数,返回值均为科学记数法的Const常数。区别在于`ut1e`比较特殊,其生成的科学记数法是专门用于单位换算的,在LSym和LSymItem中,`ut1e`生成的科学记数法不会在代数表达式中出现,而只会在数值表达式中,作为测量值的换算因子出现;相比之下,`t1e`只是一个一般的科学记数法函数。" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "假设1.2的例子中,希望得到的面积S使用国际单位制,通过下例说明两个函数的使用:" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 4, 166 | "metadata": {}, 167 | "outputs": [ 168 | { 169 | "data": { 170 | "text/latex": [ 171 | "$\\begin{align}&S=\\pi {r}^{2}=\\pi \\times \\left({0.751} \\times 10^{-2}\\right)^{2}=1.77\\times 10^{-4}{\\rm m^2}\\end{align}$" 172 | ], 173 | "text/plain": [ 174 | "" 175 | ] 176 | }, 177 | "execution_count": 4, 178 | "metadata": {}, 179 | "output_type": "execute_result" 180 | } 181 | ], 182 | "source": [ 183 | "from analyticlab import ut1e\n", 184 | "S = PI * (r*ut1e(-2))**2 #使用ut1e函数\n", 185 | "dispLSym(S, 'S', 'm^2')" 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": 5, 191 | "metadata": {}, 192 | "outputs": [ 193 | { 194 | "data": { 195 | "text/latex": [ 196 | "$\\begin{align}&S=\\pi \\left({r} \\cdot 10^{-2}\\right)^{2}=\\pi \\times \\left({0.751} \\times 10^{-2}\\right)^{2}=1.77\\times 10^{-4}{\\rm m^2}\\end{align}$" 197 | ], 198 | "text/plain": [ 199 | "" 200 | ] 201 | }, 202 | "execution_count": 5, 203 | "metadata": {}, 204 | "output_type": "execute_result" 205 | } 206 | ], 207 | "source": [ 208 | "from analyticlab import t1e\n", 209 | "S = PI * (r*t1e(-2))**2 #使用t1e函数\n", 210 | "dispLSym(S, 'S', 'm^2')" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "由于这里科学记数法是用来单位换算的,因此`ut1e`函数更符合需要的效果。" 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "## 2.Const类:创建新的常数" 225 | ] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": {}, 230 | "source": [ 231 | "### 2.1 导入Const类" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "metadata": {}, 237 | "source": [ 238 | "通过以下指令实现导入Const类:" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 6, 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [ 247 | "from analyticlab import Const" 248 | ] 249 | }, 250 | { 251 | "cell_type": "markdown", 252 | "metadata": {}, 253 | "source": [ 254 | "### 2.2 创建一个Const常数" 255 | ] 256 | }, 257 | { 258 | "cell_type": "markdown", 259 | "metadata": {}, 260 | "source": [ 261 | "Const类的构造方法如下:" 262 | ] 263 | }, 264 | { 265 | "cell_type": "markdown", 266 | "metadata": {}, 267 | "source": [ 268 | "`Const(sym, value)`" 269 | ] 270 | }, 271 | { 272 | "cell_type": "markdown", 273 | "metadata": {}, 274 | "source": [ 275 | "其中参数`sym`为常数的符号,以字符串形式给出,例如'\\eta'、'k';`value`为常量的数值,以纯数字形式给出:" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": 7, 281 | "metadata": {}, 282 | "outputs": [], 283 | "source": [ 284 | "eta = Const(r'\\eta', 8.973e-7)" 285 | ] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "metadata": {}, 290 | "source": [ 291 | "## 3.常数间的计算" 292 | ] 293 | }, 294 | { 295 | "cell_type": "markdown", 296 | "metadata": {}, 297 | "source": [ 298 | "Const常数之间可以通过`+`、`-`、`*`、`/`、`//`、`**`(“/”与“//”的区别也在于与LSym、LSymItem生成计算表达式时,除号的形式不同)进行运算,或者使用`amath`库中的函数,以生成新的Const常数,参与Num、LSym等的运算,例如:" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": 8, 304 | "metadata": {}, 305 | "outputs": [ 306 | { 307 | "data": { 308 | "text/latex": [ 309 | "$\\begin{align}&y=4 \\left(1+\\cfrac{\\pi }{2}\\right){x}^{2}=4 \\left(1+\\cfrac{\\pi }{2}\\right) \\times {0.894}^{2}=8.22{\\rm cm^2}\\end{align}$" 310 | ], 311 | "text/plain": [ 312 | "" 313 | ] 314 | }, 315 | "execution_count": 8, 316 | "metadata": {}, 317 | "output_type": "execute_result" 318 | } 319 | ], 320 | "source": [ 321 | "k = 4*(1+PI/2)\n", 322 | "x = LSym('x', '0.894')\n", 323 | "y = k * x**2\n", 324 | "dispLSym(y, 'y', 'cm^2')" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": null, 330 | "metadata": {}, 331 | "outputs": [], 332 | "source": [] 333 | } 334 | ], 335 | "metadata": { 336 | "kernelspec": { 337 | "display_name": "Python 3", 338 | "language": "python", 339 | "name": "python3" 340 | }, 341 | "language_info": { 342 | "codemirror_mode": { 343 | "name": "ipython", 344 | "version": 3 345 | }, 346 | "file_extension": ".py", 347 | "mimetype": "text/x-python", 348 | "name": "python", 349 | "nbconvert_exporter": "python", 350 | "pygments_lexer": "ipython3", 351 | "version": "3.6.4" 352 | } 353 | }, 354 | "nbformat": 4, 355 | "nbformat_minor": 2 356 | } 357 | --------------------------------------------------------------------------------