mm_maths 1.1.9 → 1.2.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/README.md +156 -0
- package/index.js +6 -410
- package/lib/maths.js +429 -0
- package/package.json +42 -33
- package/tool/math/index.js +83 -0
- package/tool/math/tool.json +60 -0
- package/.gitattributes +0 -5
- package/convert/format_multiply/index.js +0 -40
- package/convert/format_multiply/test.js +0 -5
- package/convert/format_plus/index.js +0 -42
- package/convert/format_plus/test.js +0 -4
- package/convert/four_run/convert.json +0 -8
- package/convert/four_run/index.js +0 -20
- package/convert/four_run/test.js +0 -7
- package/convert/multiply_divide/index.js +0 -197
- package/convert/multiply_divide/test.js +0 -14
- package/convert/plus_subtract/index.js +0 -160
- package/convert/plus_subtract/test.js +0 -15
- package/convert/readme.md +0 -2
- package/doc/mm_math.doc +0 -0
- package/doc//345/207/275/346/225/260/347/254/246/345/217/267/344/270/200/350/247/210/350/241/250.xls +0 -0
- package/express/README.md +0 -2
- package/identities/chemistry/readme.md +0 -1
- package/identities/equation/readme.md +0 -2
- package/identities/geometry/readme.md +0 -1
- package/identities/geometry/rectangle/readme.md +0 -1
- package/identities/geometry/string/readme.md +0 -1
- package/identities/physics/readme.md +0 -1
- package/identities/readme.md +0 -2
- package/math/array/average/index.js +0 -10
- package/math/array/average/math.json +0 -8
- package/math/array/average/test.js +0 -3
- package/math/array/col/index.js +0 -17
- package/math/array/col/math.json +0 -8
- package/math/array/col/test.js +0 -4
- package/math/array/composite/index.js +0 -35
- package/math/array/composite/math.json +0 -8
- package/math/array/composite/test.js +0 -4
- package/math/array/deviations/index.js +0 -15
- package/math/array/deviations/math.json +0 -8
- package/math/array/deviations/test.js +0 -3
- package/math/array/difference/index.js +0 -9
- package/math/array/difference/math.json +0 -8
- package/math/array/difference/test.js +0 -7
- package/math/array/distinct/index.js +0 -20
- package/math/array/distinct/math.json +0 -8
- package/math/array/distinct/test.js +0 -8
- package/math/array/distributive/index.js +0 -24
- package/math/array/distributive/math.json +0 -8
- package/math/array/distributive/test.js +0 -3
- package/math/array/divisor/index.js +0 -17
- package/math/array/divisor/math.json +0 -8
- package/math/array/divisor/test.js +0 -6
- package/math/array/intersect/index.js +0 -9
- package/math/array/intersect/math.json +0 -8
- package/math/array/intersect/test.js +0 -7
- package/math/array/max/index.js +0 -17
- package/math/array/max/math.json +0 -8
- package/math/array/max/test.js +0 -8
- package/math/array/median/index.js +0 -34
- package/math/array/median/math.json +0 -8
- package/math/array/median/test.js +0 -4
- package/math/array/min/index.js +0 -16
- package/math/array/min/math.json +0 -8
- package/math/array/min/test.js +0 -8
- package/math/array/multiple/index.js +0 -17
- package/math/array/multiple/math.json +0 -8
- package/math/array/multiple/test.js +0 -7
- package/math/array/natural/index.js +0 -17
- package/math/array/natural/math.json +0 -8
- package/math/array/natural/test.js +0 -9
- package/math/array/pos/index.js +0 -21
- package/math/array/pos/math.json +0 -8
- package/math/array/pos/test.js +0 -41
- package/math/array/prime/index.js +0 -35
- package/math/array/prime/math.json +0 -8
- package/math/array/prime/test.js +0 -4
- package/math/array/readme.md +0 -5
- package/math/array/reduction/index.js +0 -57
- package/math/array/reduction/math.json +0 -8
- package/math/array/reduction/test.js +0 -42
- package/math/array/rising/index.js +0 -8
- package/math/array/rising/math.json +0 -8
- package/math/array/rising/test.js +0 -3
- package/math/array/row/index.js +0 -11
- package/math/array/row/math.json +0 -8
- package/math/array/row/test.js +0 -4
- package/math/array/square/index.js +0 -12
- package/math/array/square/math.json +0 -8
- package/math/array/square/test.js +0 -3
- package/math/array/stddev/index.js +0 -17
- package/math/array/stddev/math.json +0 -8
- package/math/array/stddev/test.js +0 -4
- package/math/array/sum/index.js +0 -13
- package/math/array/sum/math.json +0 -8
- package/math/array/sum/test.js +0 -3
- package/math/array/sumproduct/index.js +0 -13
- package/math/array/sumproduct/math.json +0 -8
- package/math/array/sumproduct/test.js +0 -3
- package/math/array/union/index.js +0 -11
- package/math/array/union/math.json +0 -8
- package/math/array/union/test.js +0 -7
- package/math/base/abs/index.js +0 -8
- package/math/base/abs/math.json +0 -8
- package/math/base/abs/test.js +0 -3
- package/math/base/ceil/index.js +0 -8
- package/math/base/ceil/math.json +0 -8
- package/math/base/ceil/test.js +0 -3
- package/math/base/denominator/index.js +0 -17
- package/math/base/denominator/math.json +0 -9
- package/math/base/denominator/test.js +0 -3
- package/math/base/diff/index.js +0 -9
- package/math/base/diff/math.json +0 -8
- package/math/base/diff/test.js +0 -6
- package/math/base/exp/index.js +0 -8
- package/math/base/exp/math.json +0 -8
- package/math/base/exp/test.js +0 -6
- package/math/base/floor/index.js +0 -8
- package/math/base/floor/math.json +0 -8
- package/math/base/floor/test.js +0 -3
- package/math/base/int/index.js +0 -8
- package/math/base/int/math.json +0 -8
- package/math/base/int/test.js +0 -2
- package/math/base/log/index.js +0 -8
- package/math/base/log/math.json +0 -8
- package/math/base/log/test.js +0 -4
- package/math/base/max_divisor/index.js +0 -10
- package/math/base/max_divisor/math.json +0 -9
- package/math/base/max_divisor/test.js +0 -3
- package/math/base/min_multiple/index.js +0 -11
- package/math/base/min_multiple/math.json +0 -9
- package/math/base/min_multiple/test.js +0 -3
- package/math/base/mod/index.js +0 -8
- package/math/base/mod/math.json +0 -8
- package/math/base/mod/test.js +0 -3
- package/math/base/pow/index.js +0 -9
- package/math/base/pow/math.json +0 -8
- package/math/base/pow/test.js +0 -3
- package/math/base/rand/index.js +0 -21
- package/math/base/rand/math.json +0 -8
- package/math/base/rand/test.js +0 -3
- package/math/base/random/index.js +0 -12
- package/math/base/random/math.json +0 -8
- package/math/base/random/test.js +0 -4
- package/math/base/readme.md +0 -1
- package/math/base/round/index.js +0 -14
- package/math/base/round/math.json +0 -8
- package/math/base/round/test.js +0 -4
- package/math/base/sqrt/index.js +0 -8
- package/math/base/sqrt/math.json +0 -8
- package/math/base/sqrt/test.js +0 -5
- package/math/readme.md +0 -1
- package/math/stock/AMOUNT/index.js +0 -7
- package/math/stock/AMOUNT/math.json +0 -8
- package/math/stock/AMOUNT/test.js +0 -4
- package/math/stock/AP/index.js +0 -20
- package/math/stock/AP/math.json +0 -8
- package/math/stock/AP/test.js +0 -5
- package/math/stock/AROON/index.js +0 -14
- package/math/stock/ARR/index.js +0 -10
- package/math/stock/ARR/math.json +0 -8
- package/math/stock/ARR/test.js +0 -45
- package/math/stock/ASTAR/index.js +0 -20
- package/math/stock/ASTAR/math.json +0 -8
- package/math/stock/ASTAR/test.js +0 -4
- package/math/stock/AVERAGE/index.js +0 -14
- package/math/stock/AVERAGE/math.json +0 -8
- package/math/stock/AVERAGE/test.js +0 -5
- package/math/stock/AYANG/index.js +0 -18
- package/math/stock/AYANG/math.json +0 -8
- package/math/stock/AYANG/test.js +0 -4
- package/math/stock/AYIN/index.js +0 -18
- package/math/stock/AYIN/math.json +0 -8
- package/math/stock/AYIN/test.js +0 -4
- package/math/stock/BBANDS/index.js +0 -0
- package/math/stock/BETWEEN/index.js +0 -24
- package/math/stock/BETWEEN/math.json +0 -8
- package/math/stock/BETWEEN/test.js +0 -4
- package/math/stock/CLOSE/c.js +0 -0
- package/math/stock/CLOSE/index.js +0 -11
- package/math/stock/CLOSE/math.json +0 -8
- package/math/stock/CLOSE/test.js +0 -45
- package/math/stock/CP/index.js +0 -35
- package/math/stock/CP/math.json +0 -8
- package/math/stock/CP/test.js +0 -5
- package/math/stock/CROSS/index.js +0 -29
- package/math/stock/CROSS/math.json +0 -8
- package/math/stock/CROSS/test.js +0 -6
- package/math/stock/DATE/index.js +0 -14
- package/math/stock/DATE/math.json +0 -8
- package/math/stock/DATE/test.js +0 -45
- package/math/stock/DOWN/index.js +0 -19
- package/math/stock/DOWN/math.json +0 -8
- package/math/stock/DOWN/test.js +0 -8
- package/math/stock/DOWNNDAY/index.js +0 -27
- package/math/stock/DOWNNDAY/math.json +0 -8
- package/math/stock/DOWNNDAY/test.js +0 -10
- package/math/stock/EMA/index.js +0 -30
- package/math/stock/EMA/math.json +0 -8
- package/math/stock/EMA/test.js +0 -4
- package/math/stock/HHV/index.js +0 -16
- package/math/stock/HHV/math.json +0 -8
- package/math/stock/HHV/test.js +0 -4
- package/math/stock/HIGH/h.js +0 -0
- package/math/stock/HIGH/index.js +0 -11
- package/math/stock/HIGH/math.json +0 -8
- package/math/stock/HIGH/test.js +0 -45
- package/math/stock/LEFT/index.js +0 -13
- package/math/stock/LEFT/math.json +0 -8
- package/math/stock/LEFT/test.js +0 -4
- package/math/stock/LLV/index.js +0 -16
- package/math/stock/LLV/math.json +0 -8
- package/math/stock/LLV/test.js +0 -4
- package/math/stock/LOW/index.js +0 -11
- package/math/stock/LOW/l.js +0 -0
- package/math/stock/LOW/math.json +0 -8
- package/math/stock/LOW/test.js +0 -47
- package/math/stock/MA/index.js +0 -30
- package/math/stock/MA/math.json +0 -8
- package/math/stock/MA/test.js +0 -5
- package/math/stock/MACD/index.js +0 -29
- package/math/stock/MACD/math.json +0 -8
- package/math/stock/MACD/test.js +0 -4
- package/math/stock/MFI/index.js +0 -33
- package/math/stock/NMA/index.js +0 -23
- package/math/stock/NMA/math.json +0 -8
- package/math/stock/NMA/test.js +0 -5
- package/math/stock/NOT/index.js +0 -7
- package/math/stock/NOT/math.json +0 -8
- package/math/stock/NOT/test.js +0 -10
- package/math/stock/OPEN/index.js +0 -11
- package/math/stock/OPEN/math.json +0 -8
- package/math/stock/OPEN/o.js +0 -0
- package/math/stock/OPEN/test.js +0 -45
- package/math/stock/PERCPRICEOSC/index.js +0 -19
- package/math/stock/REF/index.js +0 -15
- package/math/stock/REF/math.json +0 -8
- package/math/stock/REF/test.js +0 -5
- package/math/stock/RIGHT/index.js +0 -13
- package/math/stock/RIGHT/math.json +0 -8
- package/math/stock/RIGHT/test.js +0 -4
- package/math/stock/RSI/index.js +0 -36
- package/math/stock/SMA/index.js +0 -12
- package/math/stock/STAR/index.js +0 -19
- package/math/stock/STAR/math.json +0 -8
- package/math/stock/STAR/test.js +0 -4
- package/math/stock/STOCHRSI/index.js +0 -19
- package/math/stock/SUM/index.js +0 -19
- package/math/stock/SUM/math.json +0 -8
- package/math/stock/SUM/test.js +0 -7
- package/math/stock/TIME/index.js +0 -14
- package/math/stock/TIME/math.json +0 -8
- package/math/stock/TIME/test.js +0 -45
- package/math/stock/TRUERANGE/index.js +0 -19
- package/math/stock/UP/index.js +0 -19
- package/math/stock/UP/math.json +0 -8
- package/math/stock/UP/test.js +0 -9
- package/math/stock/UPNDAY/index.js +0 -28
- package/math/stock/UPNDAY/math.json +0 -8
- package/math/stock/UPNDAY/test.js +0 -10
- package/math/stock/VMA/index.js +0 -41
- package/math/stock/VMA/math.json +0 -8
- package/math/stock/VMA/test.js +0 -7
- package/math/stock/VOL/index.js +0 -11
- package/math/stock/VOL/math.json +0 -8
- package/math/stock/VOL/test.js +0 -45
- package/math/stock/WILLR/index.js +0 -22
- package/math/stock/YANG/index.js +0 -16
- package/math/stock/YANG/math.json +0 -8
- package/math/stock/YANG/test.js +0 -4
- package/math/stock/YIN/index.js +0 -16
- package/math/stock/YIN/math.json +0 -8
- package/math/stock/YIN/test.js +0 -4
- package/math/stock/readme.md +0 -294
- package/math/trigonometric/cos/index.js +0 -8
- package/math/trigonometric/cos/math.json +0 -20
- package/math/trigonometric/cot/index.js +0 -8
- package/math/trigonometric/cot/math.json +0 -17
- package/math/trigonometric/csc/index.js +0 -8
- package/math/trigonometric/csc/math.json +0 -20
- package/math/trigonometric/readme.md +0 -2
- package/math/trigonometric/sec/index.js +0 -8
- package/math/trigonometric/sec/math.json +0 -20
- package/math/trigonometric/sin/index.js +0 -8
- package/math/trigonometric/sin/math.json +0 -20
- package/math/trigonometric/tan/index.js +0 -8
- package/math/trigonometric/tan/math.json +0 -18
- package/symbol/brackets/index.js +0 -46
- package/symbol/brackets/symbol.json +0 -8
- package/symbol/brackets/test.js +0 -12
- package/symbol/decimal/index.js +0 -41
- package/symbol/decimal/symbol.json +0 -8
- package/symbol/decimal/test.js +0 -2
- package/symbol/percentage/index.js +0 -48
- package/symbol/percentage/symbol.json +0 -8
- package/symbol/percentage/test.js +0 -2
- package/symbol/pi/index.js +0 -20
- package/symbol/pi/symbol.json +0 -9
- package/symbol/pi/test.js +0 -2
- package/symbol/power/index.js +0 -111
- package/symbol/power/symbol.json +0 -8
- package/symbol/power/test.js +0 -10
- package/symbol/readme.md +0 -2
- package/test.js +0 -32
- package/test2.js +0 -47
package/README.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# mm_maths
|
|
2
|
+
|
|
3
|
+
这是一个可以将数学公式推导变形,并转成js和计算的模块。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install mm_maths
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 基本使用
|
|
12
|
+
|
|
13
|
+
### 1. 初始化模块
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
const { Maths } = require('mm_maths');
|
|
17
|
+
|
|
18
|
+
// 创建实例并指定模块路径
|
|
19
|
+
const maths = new Maths(__dirname);
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### 2. 载入公式
|
|
23
|
+
|
|
24
|
+
```javascript
|
|
25
|
+
// 载入基础数学公式
|
|
26
|
+
maths.update(__dirname, "base");
|
|
27
|
+
|
|
28
|
+
// 也可以分别载入不同类型的公式
|
|
29
|
+
maths.loadIdentities(__dirname, "base"); // 载入恒等式
|
|
30
|
+
maths.loadConvert(__dirname, "base"); // 载入转换函数
|
|
31
|
+
maths.loadSymbol(__dirname, "base"); // 载入符号处理函数
|
|
32
|
+
maths.loadMath(__dirname, "base"); // 载入计算函数
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 3. 设置常量和变量
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
// 设置常量
|
|
39
|
+
const consts = {
|
|
40
|
+
a: 10,
|
|
41
|
+
b: 3
|
|
42
|
+
};
|
|
43
|
+
maths.setConst(consts);
|
|
44
|
+
|
|
45
|
+
// 设置变量
|
|
46
|
+
const vars = {
|
|
47
|
+
ma5: "MA(CLOSE, 5)", // 可以设置公式作为变量
|
|
48
|
+
price: null
|
|
49
|
+
};
|
|
50
|
+
maths.setVar(vars);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 4. 公式转换和计算
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
// 转换公式
|
|
57
|
+
const expression = "y = abc * ab * 3c * 2";
|
|
58
|
+
const express = maths.convertTo(expression);
|
|
59
|
+
console.log('转换后的公式:', express);
|
|
60
|
+
|
|
61
|
+
// 运行代码
|
|
62
|
+
const result = maths.runCode("return " + express);
|
|
63
|
+
console.log('计算结果:', result);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## API 说明
|
|
67
|
+
|
|
68
|
+
### 主要类
|
|
69
|
+
|
|
70
|
+
#### Maths
|
|
71
|
+
|
|
72
|
+
数学公式计算的主类,提供公式转换、计算等功能。
|
|
73
|
+
|
|
74
|
+
**构造函数参数:**
|
|
75
|
+
- `dir` {String} 模块路径,默认为 __dirname
|
|
76
|
+
|
|
77
|
+
### 主要方法
|
|
78
|
+
|
|
79
|
+
#### update(path, type)
|
|
80
|
+
|
|
81
|
+
更新并载入指定类型的所有公式。
|
|
82
|
+
|
|
83
|
+
- `path` {String} 检索路径
|
|
84
|
+
- `type` {String} 公式类型,如 "base"(基础数学)、"stock"(股票)
|
|
85
|
+
|
|
86
|
+
#### setConst(constant)
|
|
87
|
+
|
|
88
|
+
设置常量。
|
|
89
|
+
|
|
90
|
+
- `constant` {Object} 常量集合,如 `{ a: 10, b: 15 }`
|
|
91
|
+
|
|
92
|
+
#### setVar(vars)
|
|
93
|
+
|
|
94
|
+
设置变量。
|
|
95
|
+
|
|
96
|
+
- `vars` {Object} 变量集合,如 `{ ma5: "MA(CLOSE, 5)", price: null }`
|
|
97
|
+
|
|
98
|
+
#### convertTo(express)
|
|
99
|
+
|
|
100
|
+
转换公式。
|
|
101
|
+
|
|
102
|
+
- `express` {String} 要转换的表达式
|
|
103
|
+
- 返回 {String} 转换后的表达式
|
|
104
|
+
|
|
105
|
+
#### runCode(express)
|
|
106
|
+
|
|
107
|
+
运行代码。
|
|
108
|
+
|
|
109
|
+
- `express` {String} 要执行的表达式
|
|
110
|
+
- 返回 {Object} 执行结果
|
|
111
|
+
|
|
112
|
+
## 支持的公式类型
|
|
113
|
+
|
|
114
|
+
- 基础数学 (base)
|
|
115
|
+
- 四则运算
|
|
116
|
+
- 指数运算
|
|
117
|
+
- 对数运算
|
|
118
|
+
- 三角函数
|
|
119
|
+
|
|
120
|
+
- 数组运算 (array)
|
|
121
|
+
- 求和
|
|
122
|
+
- 平均值
|
|
123
|
+
- 最大/最小值
|
|
124
|
+
- 中位数
|
|
125
|
+
|
|
126
|
+
- 股票技术指标 (stock)
|
|
127
|
+
- MA (移动平均线)
|
|
128
|
+
- MACD
|
|
129
|
+
- RSI
|
|
130
|
+
- BBANDS (布林带)
|
|
131
|
+
|
|
132
|
+
## 注意事项
|
|
133
|
+
|
|
134
|
+
1. 在使用前请确保已正确载入所需的公式类型
|
|
135
|
+
2. 设置常量和变量时注意避免名称冲突
|
|
136
|
+
3. 公式转换时注意检查语法正确性
|
|
137
|
+
|
|
138
|
+
## 许可证
|
|
139
|
+
|
|
140
|
+
ISC License
|
|
141
|
+
|
|
142
|
+
## 工具契约(LLM 可调用)
|
|
143
|
+
|
|
144
|
+
本模块 `tool/` 下登记以下工具契约,实现位于 `lib/tools/<name>/`,`tool/<name>/index.js` 仅为薄适配层。
|
|
145
|
+
其它模块可直接 `require('<pkg>').tools.<name>` 复用实现,无需经过工具注册面。
|
|
146
|
+
|
|
147
|
+
| 工具名 | 标题 | 说明 |
|
|
148
|
+
| --- | --- | --- |
|
|
149
|
+
| `math` | 数学公式求值与等式归一 | 公式求值与等式归一 |
|
|
150
|
+
|
|
151
|
+
## 更新日志
|
|
152
|
+
|
|
153
|
+
### v1.2.1(2026-11-20)
|
|
154
|
+
|
|
155
|
+
- 根入口改为纯聚合导出,`Maths` 类移入 `lib/maths.js` 直接导出,模块搜索逻辑移入 `lib/admin.js`。
|
|
156
|
+
- 清除 `var`(含公式模块与测试)、规范 `let`/`const` 用法,补全 JSDoc,修正 README 导入示例。
|
package/index.js
CHANGED
|
@@ -1,410 +1,6 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 数学公式计算函数
|
|
10
|
-
* @property {Object} identities 恒等式(注释列表)
|
|
11
|
-
* @property {Object} math 计算函数(注释列表)
|
|
12
|
-
* @property {Object} convert 变换函数(注释列表)
|
|
13
|
-
* @property {Object} symbol 符号替换函数(注释列表)
|
|
14
|
-
*/
|
|
15
|
-
class Maths {
|
|
16
|
-
/**
|
|
17
|
-
* @param {String} dir 路径
|
|
18
|
-
*/
|
|
19
|
-
constructor(dir) {
|
|
20
|
-
/**
|
|
21
|
-
* 检索文件路径
|
|
22
|
-
*/
|
|
23
|
-
this.dir = dir ? dir : __dirname;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* 常量(集合)
|
|
27
|
-
*/
|
|
28
|
-
this.const = {};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 变量(集合)用于替换代码中的文字,赋予值。等同于读取缓存值
|
|
32
|
-
*/
|
|
33
|
-
this.vars = {
|
|
34
|
-
// "ma5": "MA(CLOSE, 5)"
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* 变量值(集合)用于替换代码中的文字,赋予值。等同于读取缓存值
|
|
39
|
-
*/
|
|
40
|
-
this.vals = {
|
|
41
|
-
// "ma5": []
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* 声明(集合)
|
|
46
|
-
*/
|
|
47
|
-
this.define = {};
|
|
48
|
-
this.def = this.define;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* 已知条件
|
|
52
|
-
*/
|
|
53
|
-
this.known_conditions = {};
|
|
54
|
-
this.known = this.known_conditions;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 恒等式替换函数(集合)
|
|
58
|
-
*/
|
|
59
|
-
this.identities = {};
|
|
60
|
-
this.eq = this.identities;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* 计算函数(集合)
|
|
64
|
-
*/
|
|
65
|
-
this.math = {};
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* 变换函数(集合)
|
|
69
|
-
*/
|
|
70
|
-
this.convert = {};
|
|
71
|
-
this.ct = this.convert;
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* 符号替换函数(集合)
|
|
75
|
-
*/
|
|
76
|
-
this.symbol = {};
|
|
77
|
-
this.sl = this.symbol;
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* 学科方向,mathematical数学、physical物理、chemistry化学
|
|
81
|
-
*/
|
|
82
|
-
this.subject = "mathematical";
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* 类型,base基础数学、geometry几何数学、stock股票
|
|
86
|
-
*/
|
|
87
|
-
this.type = "base";
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* 初始化
|
|
93
|
-
*/
|
|
94
|
-
Maths.prototype.init = function() {};
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* 载入公式
|
|
98
|
-
* @param {String} path 检索的路径
|
|
99
|
-
* @param {String} type 类型
|
|
100
|
-
* @param {String} expand 拓展类型, 例如: identities恒等式、convert变换函数、symbol符号、math计算函数
|
|
101
|
-
* @return {String} 成功返回true,失败返回错误类型;
|
|
102
|
-
*/
|
|
103
|
-
Maths.prototype.load_sub = function(path, type, expand) {
|
|
104
|
-
if (!path) {
|
|
105
|
-
path = __dirname
|
|
106
|
-
}
|
|
107
|
-
if (!type) {
|
|
108
|
-
type = "math";
|
|
109
|
-
}
|
|
110
|
-
if (!expand) {
|
|
111
|
-
expand = "base";
|
|
112
|
-
}
|
|
113
|
-
var dict = this[expand];
|
|
114
|
-
if (!dict) {
|
|
115
|
-
console.log("不存在的类型", expand);
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
var searchFile = `*${expand}.json`;
|
|
119
|
-
var files = $.file.getAll(path, searchFile);
|
|
120
|
-
|
|
121
|
-
files.map(o => {
|
|
122
|
-
var cg = o.loadJson();
|
|
123
|
-
if (cg && cg.name) {
|
|
124
|
-
if (!cg.type || cg.type.has(type)) {
|
|
125
|
-
if (cg.func_file && cg.key) {
|
|
126
|
-
dict[cg.key] = cg;
|
|
127
|
-
var f = cg.func_file.fullname(o.dirname());
|
|
128
|
-
if (f.hasFile()) {
|
|
129
|
-
var func = require(f);
|
|
130
|
-
if (func) {
|
|
131
|
-
this[cg.key] = func;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* 更新
|
|
142
|
-
* @param {String} path 路径
|
|
143
|
-
* @param {String} type 类型
|
|
144
|
-
*/
|
|
145
|
-
Maths.prototype.clear = function(path, type) {
|
|
146
|
-
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* 更新
|
|
151
|
-
* @param {String} path 路径
|
|
152
|
-
* @param {String} type 类型
|
|
153
|
-
*/
|
|
154
|
-
Maths.prototype.update = function(path, type) {
|
|
155
|
-
this.clear(type);
|
|
156
|
-
this.load(path, type);
|
|
157
|
-
this.sort();
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* 载入恒等式变形函数
|
|
162
|
-
* @param {String} path 检索的路径
|
|
163
|
-
* @param {String} type 类型, 例如:default 默认,base 基础数学
|
|
164
|
-
* @return {String} 成功返回true,失败返回错误类型;
|
|
165
|
-
*/
|
|
166
|
-
Maths.prototype.load_identities = function(path, type) {
|
|
167
|
-
this.load_sub(path, type, "identities");
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* 载入转换函数
|
|
172
|
-
* @param {String} path 检索的路径
|
|
173
|
-
* @param {String} type 类型, 例如:default 默认,base 基础数学
|
|
174
|
-
* @return {String} 成功返回true,失败返回错误类型;
|
|
175
|
-
*/
|
|
176
|
-
Maths.prototype.load_convert = function(path, type) {
|
|
177
|
-
this.load_sub(path, type, "convert");
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* 载入符号处理函数
|
|
182
|
-
* @param {String} path 检索的路径
|
|
183
|
-
* @param {String} type 类型, 例如:default 默认,base 基础数学
|
|
184
|
-
* @return {String} 成功返回true,失败返回错误类型;
|
|
185
|
-
*/
|
|
186
|
-
Maths.prototype.load_symbol = function(path, type) {
|
|
187
|
-
this.load_sub(path, type, "symbol");
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* 载入计算函数
|
|
192
|
-
* @param {String} path 检索的路径
|
|
193
|
-
* @param {String} type 类型, 例如:default 默认,base 基础数学
|
|
194
|
-
* @return {String} 成功返回true,失败返回错误类型;
|
|
195
|
-
*/
|
|
196
|
-
Maths.prototype.load_math = function(path, type) {
|
|
197
|
-
this.load_sub(path, type, "math");
|
|
198
|
-
};
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* 载入相关函数
|
|
202
|
-
* @param {String} path 检索的路径
|
|
203
|
-
* @param {String} type 类型, 例如:default 默认,base 基础数学
|
|
204
|
-
* @return {String} 成功返回true,失败返回错误类型;
|
|
205
|
-
*/
|
|
206
|
-
Maths.prototype.load = function(path, type) {
|
|
207
|
-
this.load_identities(path, type);
|
|
208
|
-
this.load_convert(path, type);
|
|
209
|
-
this.load_symbol(path, type);
|
|
210
|
-
this.load_math(path, type);
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* 函数字典排序
|
|
215
|
-
*/
|
|
216
|
-
Maths.prototype.sort = function() {
|
|
217
|
-
this.convert = this.sort_sub(this.convert);
|
|
218
|
-
this.symbol = this.sort_sub(this.symbol);
|
|
219
|
-
this.identities = this.sort_sub(this.identities);
|
|
220
|
-
};
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* 函数排序
|
|
224
|
-
* @param {Object} dict 要排序的字典
|
|
225
|
-
*/
|
|
226
|
-
Maths.prototype.sort_sub = function(dict) {
|
|
227
|
-
var list = [];
|
|
228
|
-
for(var k in dict){
|
|
229
|
-
list.push(dict[k]);
|
|
230
|
-
}
|
|
231
|
-
list.sortBy('asc', 'sort');
|
|
232
|
-
var dt = {};
|
|
233
|
-
list.map(o => dt[o.key] = o);
|
|
234
|
-
return dt;
|
|
235
|
-
};
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* 设置常量
|
|
239
|
-
* @param {Object} constant 常量集合,采用键值对的方式 例如:{ a: 10, b: 15, d: [1,-1] }
|
|
240
|
-
*/
|
|
241
|
-
Maths.prototype.set_const = function(constant) {
|
|
242
|
-
$.push(this.const, constant, true);
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* 重置变量
|
|
248
|
-
*/
|
|
249
|
-
Maths.prototype.reset_val = function(){
|
|
250
|
-
for(var k in this.vars){
|
|
251
|
-
this.vals[k] = this.first_run_code("return " + this.vars[k]);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* 设置变量
|
|
257
|
-
* @param {Object} vars 变量集合,采用键值对的方式 例如:{ a: null, b: 15, d: [1,-1] }
|
|
258
|
-
*/
|
|
259
|
-
Maths.prototype.set_var = function(vars) {
|
|
260
|
-
$.push(this.vars, vars, true);
|
|
261
|
-
this.reset_val();
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* 下定义
|
|
266
|
-
* @param {Object} def 定义集合, 例如:
|
|
267
|
-
*/
|
|
268
|
-
Maths.prototype.set_def = function(def) {
|
|
269
|
-
$.push(this.def, def, true);
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* 设置已知条件
|
|
274
|
-
* @param {Object} know 已知条件 { c: "a + b", e: "ab" }
|
|
275
|
-
*/
|
|
276
|
-
Maths.prototype.set_know = function(know) {
|
|
277
|
-
$.push(this.know, know, true);
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* 设置函数头
|
|
282
|
-
*/
|
|
283
|
-
Maths.prototype.head = function() {
|
|
284
|
-
if(!this.head_value){
|
|
285
|
-
var head = "";
|
|
286
|
-
for (var k in this.math) {
|
|
287
|
-
if (!this.const[k]) {
|
|
288
|
-
head += "var " + k + " = math." + k + ";\r\n"
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
for (var k in this.const) {
|
|
292
|
-
var value = this.const[k];
|
|
293
|
-
head += "var " + k + " = constant." + k + ";\r\n"
|
|
294
|
-
}
|
|
295
|
-
this.head_value = head;
|
|
296
|
-
}
|
|
297
|
-
return this.head_value;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* 转换公式
|
|
302
|
-
* @param {String} express 表达式
|
|
303
|
-
* @return {String} 返回推导结果
|
|
304
|
-
*/
|
|
305
|
-
Maths.prototype.convertTo = function(express){
|
|
306
|
-
var express = this.convert_equal(express);
|
|
307
|
-
// 先进行符号转换
|
|
308
|
-
var dt = this.symbol;
|
|
309
|
-
for(var k in dt){
|
|
310
|
-
var func = this[k];
|
|
311
|
-
if(func)
|
|
312
|
-
{
|
|
313
|
-
express = func(express);
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
// 再进行式子转换
|
|
317
|
-
dt = this.convert;
|
|
318
|
-
for(var k in dt){
|
|
319
|
-
var func = this[k];
|
|
320
|
-
if(func)
|
|
321
|
-
{
|
|
322
|
-
express = func(express);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
return express;
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* 等号转换
|
|
330
|
-
* @param {Object} express
|
|
331
|
-
* @return {String} 返回公式
|
|
332
|
-
*/
|
|
333
|
-
Maths.prototype.convert_equal = function(express){
|
|
334
|
-
var left = express.left('=', true);
|
|
335
|
-
var right = express.right('=');
|
|
336
|
-
|
|
337
|
-
if(right){
|
|
338
|
-
if(/\+|\-|\*|\//.test(right)) {
|
|
339
|
-
return left + " - (" + right.trim() + ") = 0";
|
|
340
|
-
}
|
|
341
|
-
else {
|
|
342
|
-
return left + " - " + right.trim() + " = 0";
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
else {
|
|
346
|
-
return left;
|
|
347
|
-
}
|
|
348
|
-
};
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
* 公式推导
|
|
353
|
-
* @param {String} express 表达式
|
|
354
|
-
* @return {String} 返回推导结果
|
|
355
|
-
*/
|
|
356
|
-
Maths.prototype.derivation = function(express) {
|
|
357
|
-
// 符号转换
|
|
358
|
-
};
|
|
359
|
-
|
|
360
|
-
/**
|
|
361
|
-
* 更新标签
|
|
362
|
-
* @param {String} express 表达式
|
|
363
|
-
*/
|
|
364
|
-
Maths.prototype.update_tag = function(express) {
|
|
365
|
-
for(var k in this.vars){
|
|
366
|
-
express = express.replaceAll(this.vars[k], "vals." + k);
|
|
367
|
-
}
|
|
368
|
-
return express;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* 运行代码
|
|
373
|
-
* @param {String} express 表达式
|
|
374
|
-
* @return {Object} 执行结果
|
|
375
|
-
*/
|
|
376
|
-
Maths.prototype.first_run_code = function(express) {
|
|
377
|
-
var head = this.head();
|
|
378
|
-
var exp = head + express;
|
|
379
|
-
let func = new Function('constant', 'math', exp);
|
|
380
|
-
|
|
381
|
-
var ret;
|
|
382
|
-
try {
|
|
383
|
-
ret = func(this.const, this);
|
|
384
|
-
} catch (e) {
|
|
385
|
-
console.log(e);
|
|
386
|
-
}
|
|
387
|
-
return ret;
|
|
388
|
-
};
|
|
389
|
-
|
|
390
|
-
/**
|
|
391
|
-
* 运行代码
|
|
392
|
-
* @param {String} express 表达式
|
|
393
|
-
* @return {Object} 执行结果
|
|
394
|
-
*/
|
|
395
|
-
Maths.prototype.run_code = function(express) {
|
|
396
|
-
var head = this.head();
|
|
397
|
-
var exp = head + this.update_tag(express);
|
|
398
|
-
let func = new Function('constant', 'math', 'vals', exp);
|
|
399
|
-
|
|
400
|
-
var ret;
|
|
401
|
-
try {
|
|
402
|
-
ret = func(this.const, this, this.vals);
|
|
403
|
-
} catch (e) {
|
|
404
|
-
console.log(e);
|
|
405
|
-
//TODO handle the exception
|
|
406
|
-
}
|
|
407
|
-
return ret;
|
|
408
|
-
};
|
|
409
|
-
|
|
410
|
-
module.exports = Maths;
|
|
1
|
+
/**
|
|
2
|
+
* 数学公式变形、转换成 js 并计算模块入口
|
|
3
|
+
*/
|
|
4
|
+
const Maths = require('./lib/maths');
|
|
5
|
+
|
|
6
|
+
module.exports = { Maths };
|