MathZetta
几乎以任何方式解析数学——纯文本、LaTeX、unicode、
JavaScript, Python(数学,numpy或SymPy), SQL, GLSL或电子表格公式 标准的后缀,
然后添加所需的输出:LaTeX、MathML、英语口语、JavaScript、
Python、SQL、Excel/Sheets、GLSL、纯文本以及精确或数值计算。它读取
它编写的每个代码格式(test/unit/roundtrip.test.js),所以代码可以运行
出去又回来; mathzetta-convert 是那一页。
npm install mathzetta
import { parse } from 'mathzetta';
import { toLaTeX } from 'mathzetta/latex';
import { toSpeech } from 'mathzetta/speech';
import { calculate } from 'mathzetta/calculate';
parse('3x^2 + \\sin x') // ['3', 'x', '2', '^', '*', 'x', 'sin', '+']
parse('Math.pow(x, 2) / 2') // ['x', '2', '^', '2', '/']
parse('x² ≤ √y') // ['x', '2', '^', 'y', 'sqrt', '≤']
parse('d/dx (x^2 + 1)') // ['x', '2', '^', '1', '+', 'x', 'der']
toLaTeX('3x^2 + sin(x)') // '3x^2 + \\sin\\left(x\\right)'
toSpeech('(x+1)^2') // 'the quantity x plus 1, squared'
calculate('sqrt(8)').text // '2*sqrt(2)'
parse('3+4={{3+4}}') // ['3', '4', '+', '7', '='] — once mathzetta/calculate is loaded
在CommonJS中,使用相同的入口点 require: require('mathzetta/latex') 加载核心
和乳胶(build/cjs/),而 require('mathzetta') 一切都是同时发生的吗 mathzetta/all。
没有运行时依赖;ES模块用于浏览器和Node 18+。一个
<script src="build/mathzetta.min.js"> 标签将所有内容公开为
window.MathZetta. 数学 显示 是一个独立的图书馆,
内联数学,它绘制这些树,每个部分都是可寻址的。
来自CDN
每个npm版本都是在jsdeliver(和unpkg)上发布的,不需要安装或构建。
<!-- a script tag: everything on window.MathZetta -->
<script src="https://cdn.jsdelivr.net/npm/mathzetta@1/build/mathzetta.min.js"></script>
<!-- a module -->
<script type="module">
import { parse, toLaTeX } from 'https://cdn.jsdelivr.net/npm/mathzetta@1/build/mathzetta.esm.min.js';
</script>
<!-- an import map: code written for npm (import … from 'mathzetta', 'mathzetta/latex') runs unchanged,
since the bundle holds every entry point -->
<script type="importmap">
{ "imports": { "mathzetta": "https://cdn.jsdelivr.net/npm/mathzetta@1/build/mathzetta.esm.min.js",
"mathzetta/latex": "https://cdn.jsdelivr.net/npm/mathzetta@1/build/mathzetta.esm.min.js" } }
</script>
比什么都少——选择网站的 CDN页面,其中写道 代码片段和总计大小:
<script>标签有不同的味道 (tools/flavours.js;build/mathzetta.<flavour>.min.js,每个 设置window.MathZetta):core(解析,树的工具,操作符表;22 KB压缩); 核心加上一个输出latex,javascript,python,sql,excel,glsl(这 带来了formulas);formulas(与JavaScript, SQL和Excel输出它是);display(LaTeX, MathML, text, speech; 30kb);maths(求值,计算;31 KB)和maths-display,maths-javascript,maths-python,maths-sql,maths-excel,maths-glsl;code(每个代码输出;50 KB);和mathzetta.min.js,所有内容(65 KB -) 56 KB,带有CDN使用的brotli)。npm run cdn列出他们的地址。- 平原 -没有密码拼写阅读器也一样(
src/parse/clean/code.js: numpy, SymPy, SQL,电子表格和GLSL拼写输入),大约5 KB的压缩少,对于页面读取 仅限数学和LaTeX:mathzetta.plain.min.js(17 KB)和plain-latex,plain-display,plain-maths,plain-maths-display;作为模块mathzetta.plain.esm.min.js(import … from 'mathzetta/plain';require('mathzetta/plain')负载mathzetta.plain.cjs),build/esm-plain/. 的代码 输出保持完整的读取器:输出代码和读取代码属于一起。 - ES模块,任意组合:
build/esm/<entry>.js每个入口点有一个文件吗 (esm/latex.js,esm/speech.js……),都是分享esm/chunks/core.js(22 KB),因此页面导入 它想要的输出和核心加载一次- LaTeX多2kb,语音4.5 KB, GLSL 9 KB。一个 导入地图("mathzetta"→esm/core.js,"mathzetta/latex"→esm/latex.js)让代码 为NPM编写的,运行不变。build/sizes.json拥有每个文件的大小;npm run build写它。
@1 遵循最新的1.x; @1.0.0 固定一个版本,它永远不会改变(永远缓存它,添加
一个 integrity 散列, npm run cdn 输出带有中版本的散列的代码片段
包中。json, npm run cdn -- --check 确认CDN的副本与本地构建相匹配)。 src/
也在CDN上(…/mathzetta@1/src/latex.js),用于只需要一个输出而不需要任何输出的页面
别的。发布 npm publish (prepublishOnly 运行测试和构建)。
入口点
核心是解析器;每个输出都是可选的。每个条目接受一个 表达式字符串、标准后缀或树。
| 进口 | 给了 |
|---|---|
mathzetta | parse, parseTree, parseSteps, clean; fromPostfix, toPostfix, walk, treeOf;运算符表(operator, operatorNames, needsParens,……);读树助手输出共享(needsBrackets, isImplicitProduct, GREEK,……) |
mathzetta/latex | toLaTeX(x, { wrap }) - - - - - - wrap(node, latex) 可以标记每个子表达式吗 |
mathzetta/mathml | toMathML(x) -每个节点的元素有 id="mz-<index>" |
mathzetta/text | toText(x) -可读文本: √(x), x ∈ {1, 2} |
mathzetta/speech | toSpeech(x, { words, names, say }) ——像老师一样大声朗读,每一个字都是多变的; toSpeechSegments(x) -同样是用节点id标记的片段,用于遵循数学的标题 |
mathzetta/javascript | toJavaScript(x) -代码使用 Math.* |
mathzetta/python | toPython(x, { numpy, sympy }) -代码使用 math.*, numpy元素,或SymPy(精确; diff, integrate, limit, summation); toPythonFunction(f, { numpy, sympy, errors }) -选中 def (对于sympy, d/dx的x是一个Symbol,参数x被替换)(1/0引发,或 errors: 'nan'); fillPythonTemplate(program, formulas) / preparePythonTemplate (每个槽的结果为一个表单)-一个Python程序 # mathzetta: growth(P, r, K) = … 槽,包括数据集列上的公式(keep(data: columns) = HR >= 30); mathzetta-python chart.py x='H / AB' --run out.png 填充和运行一个。 examples/python/logistic.py 是模特; pyslots 将模板制作成网页(pyoide或Flask),在棒球数据上使用matplotlib图表 |
mathzetta/excel | toExcel(x, { cells, ranges }) - Excel /谷歌表格公式(=IF(x>0, SQRT(x), 0)),包含电子表格自身的规则;再回头看看(=… 读取为电子表格公式); forXlsxFile 增加了 _xlfn. 用于.xlsx文件 |
mathzetta/sql | toSQL(x, { dialect, quoteIdentifiers, stringQuote }) —默认为PostgreSQL,或 dialect: 'sqlite' |
mathzetta/glsl | toGLSL(x, { version }) -一个着色器表达式,浮动贯穿; GLSL_HELPERS; filterShader(spec) -在图像像素上的整个片段着色器(过滤器格式和WebGL运行器在其上) qblur) |
mathzetta/evaluate | evaluate(x, scope) -一个双精度值,不运行生成的代码 |
mathzetta/calculate | calculate(x, scope, { decimal, places }) → { value, text, exact } -尽可能精确(分数,2¹⁰⁰, 2*sqrt(2), π,标准角三角,精确对数),数值导数,定积分,极限和无穷求和(∑ 1/n² → pi^2/6). 抛出 MathError 当没有答案时, UnknownError 当它无法解决的时候。 打开加载该条目的开关 {{…}} 块 在每个输入中: {{1/3+1/6}} → 1/2, {{~3 pi}} → 3.142 |
mathzetta/formulas | strToFunction(header, formula), createRowEvaluator(js), strToSQL(header, formula, selections, { dialect }) -表列上的公式 |
mathzetta/all | 以上所有,加上 toUnified(x) (每一种格式一次)和 convert(x, format) |
parse(text, options): names 列出多字母变量以保持完整
(['cost', 'rate']); values 永远不是函数的名称; blocks
集如何 {{…}} 这个电话是空的。null 离开他们作为一个平原
集团), scope 给出其变量的值; keepIntermediateSteps (或
parseSteps)返回每个阶段-清理的文本,标记,原始后缀,
树,后缀,错误。
试一试
mathzetta.com 实时尝试每个输出 操场上 显示您键入的每个阶段和格式, 运营商 列出每个人读和写的内容, 这个 API页面 有每个功能和选项与一个活生生的例子, 这个 CDN页面 为您的输出构建脚本标记或导入映射 挑,然后 后缀测验 教算法。
npm test # ~6,400 checks: unit tests and every table in test/cases
npm run build # the bundles in build/ (what npm and the CDN ship)
npm run coverage # what the test tables don't cover yet
后缀格式
文档/ postfix.md 列出每个标记。简而言之:操作数
操作人员按波兰颠倒顺序操作;固定数操作符是空的
(x sin, a b ^),可变值跟随计数(a b c 3 max),一元
-是 neg. 它是每个兄弟库使用的格式:内联数学
绘制它,计算它,重写它,语音片段被它标记。
它是如何工作的
文档/ how-it-works.md 走过管道—— 清理→标记化→解析→树→呈现-并有食谱 添加拼写、操作符和测试,以及已知的解析间隙。
布局
src/
index.js the core: parser, operator table, tree tools
all.js everything, plus toUnified — what build/ bundles
latex.js mathml.js text.js speech.js javascript.js python.js sql.js excel.js glsl.js
one entry per outputter (each a few lines over render/)
evaluate.js doubles calculate.js exact + numeric calculus
formulas.js formulas over table columns
operators.js the operator table: precedence, arity, ...
tree.js tree ⇄ postfix common.js how a tree reads when written
parse/ clean/ → tokenize → parser (Pratt) → build
render/ tree → string, one file per format
types.d.ts every declaration; index.d.ts and <entry>.d.ts pick from it
test/
run.js npm test
unit/ focused tests
cases/ table tests (TSV); checked by test/checks.js
known-failures.json checks that fail today, with reasons
tools/ the build's flavour list, the CDN addresses and sizes, test coverage, mathzetta-python
docs/