@latex-math/core 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +151 -0
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sora1123
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# @latex-math/core
|
|
2
|
+
|
|
3
|
+
A robust, lightweight TypeScript library that parses LaTeX mathematical expressions into a strictly typed Abstract Syntax Tree (AST), evaluates mathematical values, simplifies expressions, and supports symbolic calculus.
|
|
4
|
+
|
|
5
|
+
Zero dependencies for the core runtime. Built without unsafe dynamic code execution (`eval` or `Function()`).
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@latex-math/core)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- 📐 **Typed Mathematical AST**: Parses mathematical LaTeX into a well-defined `Expression` AST union (fractions, roots, powers, matrices, sums, integrals, and more).
|
|
15
|
+
- ⚡ **Safe & Deterministic**: Hand-rolled recursive descent parser and tokenizer. Never relies on `eval()` or `new Function()`.
|
|
16
|
+
- 🧠 **Evaluation Engine**: Evaluates arithmetic, trigonometric functions, logarithms, binomial coefficients, summations, matrices, and complex numbers with variable scoping.
|
|
17
|
+
- 🔄 **TeX Macro Normalization**: Automatically resolves and normalizes unbraced TeX macros (such as `\sqrt 1+2` $\to$ `\sqrt{1} + 2`, `\frac12` $\to$ `\frac{1}{2}`).
|
|
18
|
+
- 📈 **Calculus Support**: Symbolic differentiation (`\frac{d}{dx}`) and numerical integration with Simpson's rule.
|
|
19
|
+
- 🖨️ **Printer & Simplifier**: Print AST back into clean LaTeX string representation, and perform algebraic simplifications.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @latex-math/core
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
yarn add @latex-math/core
|
|
31
|
+
# or
|
|
32
|
+
pnpm add @latex-math/core
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Quick Start
|
|
38
|
+
|
|
39
|
+
### 1. Parsing to AST
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { parseLatex } from '@latex-math/core';
|
|
43
|
+
|
|
44
|
+
// Parse a LaTeX expression into a typed AST
|
|
45
|
+
const ast = parseLatex('\\frac{2x + 4}{2}');
|
|
46
|
+
console.log(ast);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### 2. Evaluating Expressions
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { parseLatex, evaluateLatex } from '@latex-math/core';
|
|
53
|
+
|
|
54
|
+
// Evaluate numeric expressions
|
|
55
|
+
const result = evaluateLatex('\\sqrt{16} + 2^3');
|
|
56
|
+
console.log(result); // 12
|
|
57
|
+
|
|
58
|
+
// Evaluate with variables
|
|
59
|
+
const ast = parseLatex('3x^2 - 4x + 1');
|
|
60
|
+
const evaluated = evaluateLatex(ast, {
|
|
61
|
+
variables: { x: 5 }
|
|
62
|
+
});
|
|
63
|
+
console.log(evaluated); // 56
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### 3. Symbolic Differentiation & Calculus
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import { parseLatex, differentiate, printLatex } from '@latex-math/core';
|
|
70
|
+
|
|
71
|
+
// Differentiate an expression symbolically with respect to 'x'
|
|
72
|
+
const ast = parseLatex('x^3 + 4x^2 - 7x + 2');
|
|
73
|
+
const derivativeAst = differentiate(ast, 'x');
|
|
74
|
+
|
|
75
|
+
// Print back to LaTeX
|
|
76
|
+
console.log(printLatex(derivativeAst)); // 3x^2 + 8x - 7
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### 4. Expression Simplification
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { parseLatex, simplifyExpression, printLatex } from '@latex-math/core';
|
|
83
|
+
|
|
84
|
+
const expr = parseLatex('0 + 1 \\cdot x + 4 - 2');
|
|
85
|
+
const simplified = simplifyExpression(expr);
|
|
86
|
+
|
|
87
|
+
console.log(printLatex(simplified)); // x + 2
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### 5. LaTeX Argument Normalization
|
|
91
|
+
|
|
92
|
+
Converts ambiguous or unbraced LaTeX syntax into explicit, braced TeX:
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import { normalizeLatex } from '@latex-math/core';
|
|
96
|
+
|
|
97
|
+
normalizeLatex('\\sqrt 1+2'); // "\\sqrt{1} + 2"
|
|
98
|
+
normalizeLatex('\\frac12'); // "\\frac{1}{2}"
|
|
99
|
+
normalizeLatex('\\binom42'); // "\\binom{4}{2}"
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
## Supported LaTeX Syntax
|
|
105
|
+
|
|
106
|
+
| Category | Examples |
|
|
107
|
+
|---|---|
|
|
108
|
+
| **Arithmetic** | `+`, `-`, `\cdot`, `\times`, `/`, `\div`, implicit multiplication (`2x`, `3\pi`) |
|
|
109
|
+
| **Fractions** | `\frac{a}{b}`, `\frac12` |
|
|
110
|
+
| **Powers & Roots** | `x^2`, `x^{2+y}`, `\sqrt{x}`, `\sqrt[3]{8}` |
|
|
111
|
+
| **Trigonometry** | `\sin`, `\cos`, `\tan`, `\arcsin`, `\arccos`, `\arctan`, `\csc`, `\sec`, `\cot` |
|
|
112
|
+
| **Logarithms** | `\ln(x)`, `\log(x)`, `\log_{10}(x)` |
|
|
113
|
+
| **Calculus** | `\frac{d}{dx} f(x)`, `\int_{0}^{1} x^2 \, dx` |
|
|
114
|
+
| **Summations** | `\sum_{n=1}^{10} n^2` |
|
|
115
|
+
| **Matrices** | `\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}`, `\begin{bmatrix} ... \end{bmatrix}` |
|
|
116
|
+
| **Combinatorics** | `\binom{n}{k}`, `n!` |
|
|
117
|
+
| **Special Constants** | `\pi`, `e`, `i` (imaginary unit) |
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## API Reference
|
|
122
|
+
|
|
123
|
+
### `parseLatex(input: string): Expression`
|
|
124
|
+
Parses a LaTeX math string into a typed AST. Automatically runs macro normalization.
|
|
125
|
+
|
|
126
|
+
### `evaluateLatex(expr: Expression | string, scope?: EvaluationScope): number | Matrix | Complex | boolean`
|
|
127
|
+
Evaluates an AST or LaTeX string. Accepts optional variable scopes:
|
|
128
|
+
```typescript
|
|
129
|
+
interface EvaluationScope {
|
|
130
|
+
variables?: Record<string, number | Complex | Matrix>;
|
|
131
|
+
functions?: Record<string, (...args: number[]) => number>;
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### `differentiate(expr: Expression, variable?: string): Expression`
|
|
136
|
+
Calculates the symbolic derivative of an expression with respect to the specified variable (defaults to `'x'`).
|
|
137
|
+
|
|
138
|
+
### `simplifyExpression(expr: Expression): Expression`
|
|
139
|
+
Simplifies an expression using algebraic identity rules (constant folding, zero multiplication, identity element removal).
|
|
140
|
+
|
|
141
|
+
### `printLatex(expr: Expression): string`
|
|
142
|
+
Serializes an `Expression` AST back into a standard LaTeX string.
|
|
143
|
+
|
|
144
|
+
### `normalizeLatex(input: string): string`
|
|
145
|
+
Normalizes unbraced TeX macros (`\sqrt`, `\frac`, `\binom`) by wrapping implicit single tokens in braces.
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
MIT © [Sora1123](https://github.com/Sora1123)
|