Skip to content

@sciexpr/math

数学专用包 — 符号库、希腊字母、矩阵运算工具。

安装

bash
npm install @sciexpr/math

符号库

typescript
import {
  GREEK_LOWERCASE, GREEK_UPPERCASE,
  OPERATORS, RELATIONS, ARROWS, MISC_SYMBOLS,
  ALL_SYMBOLS, searchSymbols, getSymbolsByCategory,
} from '@sciexpr/math'

// 22 个希腊小写字母
GREEK_LOWERCASE[0]  // { latex:'\\alpha', unicode:'α', name:'alpha' }

// 11 个大写字母
GREEK_UPPERCASE[0]  // { latex:'\\Gamma', unicode:'Γ', name:'Gamma' }

// 7 个运算符 — +, −, ×, ÷, ·, ±, ∓
// 19 个关系符 — <, >, ≤, ≥, ≠, ≈, ≡, ∈, ⊆, ...
// 12 个箭头 — →, ←, ⇒, ⇐, ⇔, ↦, ↑, ↓, ...
// 17 个杂项 — ∞, ∂, ∇, ∀, ∃, ∅, ℏ, ...

// 按名称搜索
searchSymbols('alpha')
// → [{ latex:'\\alpha', unicode:'α', name:'alpha', ... }]

// 按分类获取
getSymbolsByCategory('greek-lower')

SymbolEntry 结构

typescript
interface SymbolEntry {
  latex: string        // '\\alpha'
  unicode: string      // 'α'
  name: string         // 'alpha'
  category: string     // 'greek-lower' | 'greek-upper' | 'operator' | 'relation' | 'arrow' | 'misc'
  description?: string
}

矩阵工具

typescript
import { MatrixUtils, zeroMatrix, identityMatrix, latexMatrixTemplate } from '@sciexpr/math'

const utils = new MatrixUtils()

基础操作

typescript
// 验证维度
utils.validateDimensions([[1,2],[3,4]])  // true
utils.validateDimensions([[1,2],[3,4,5]]) // false

// 获取维度
utils.getDimensions([[1,2],[3,4],[5,6]]) // [3, 2]

// 是否为方阵
utils.isSquare([[1,2],[3,4]])  // true

// 转置
utils.transpose([[1,2],[3,4],[5,6]])
// → [[1,3,5],[2,4,6]]

矩阵运算

typescript
// 加法
utils.add([[1,2],[3,4]], [[5,6],[7,8]])
// → [[6,8],[10,12]]

// 乘法
utils.multiply([[1,2],[3,4]], [[5,6],[7,8]])
// → [[19,22],[43,50]]

// 标量乘法
utils.scalarMultiply([[1,2],[3,4]], 2)
// → [[2,4],[6,8]]

行列式

typescript
// 2×2
utils.determinant2x2([[3,8],[4,6]])  // -14

// 3×3
utils.determinant3x3([[1,2,3],[4,5,6],[7,8,9]])  // 0

LaTeX 生成

typescript
// 生成矩阵 LaTeX
utils.toLaTeX([['a','b'],['c','d']])
// → '\begin{pmatrix}\na & b \\\\\nc & d\n\\end{pmatrix}'

// 生成行列式 LaTeX
utils.toDeterminantLaTeX([['a','b'],['c','d']])
// → '\begin{vmatrix}\na & b \\\\\nc & d\n\\end{vmatrix}'

// 指定矩阵类型
utils.toLaTeX(data, { matrixType: 'bmatrix' })  // [ ]
utils.toLaTeX(data, { matrixType: 'vmatrix' })  // | |
utils.toLaTeX(data, { matrixType: 'Bmatrix' })  // { }

辅助函数

typescript
// n×n 单位矩阵
identityMatrix(3)  // [[1,0,0],[0,1,0],[0,0,1]]

// 零矩阵
zeroMatrix(2, 3)   // [[0,0,0],[0,0,0]]

// LaTeX 模板
latexMatrixTemplate(2, 3)
// → '\begin{pmatrix}\n & &  \\\\\n & & \n\\end{pmatrix}'

基于 MIT 协议发布