mm_maths 1.2.0 → 1.2.2

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.
Files changed (305) hide show
  1. package/README.md +28 -12
  2. package/index.js +5 -410
  3. package/lib/maths.js +429 -0
  4. package/package.json +42 -33
  5. package/tool/math/index.js +83 -0
  6. package/tool/math/tool.json +60 -0
  7. package/.gitattributes +0 -5
  8. package/convert/format_multiply/index.js +0 -40
  9. package/convert/format_multiply/test.js +0 -5
  10. package/convert/format_plus/index.js +0 -42
  11. package/convert/format_plus/test.js +0 -4
  12. package/convert/four_run/convert.json +0 -8
  13. package/convert/four_run/index.js +0 -20
  14. package/convert/four_run/test.js +0 -7
  15. package/convert/multiply_divide/index.js +0 -197
  16. package/convert/multiply_divide/test.js +0 -14
  17. package/convert/plus_subtract/index.js +0 -160
  18. package/convert/plus_subtract/test.js +0 -15
  19. package/convert/readme.md +0 -2
  20. package/doc/mm_math.doc +0 -0
  21. 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
  22. package/express/README.md +0 -2
  23. package/identities/chemistry/readme.md +0 -1
  24. package/identities/equation/readme.md +0 -2
  25. package/identities/geometry/readme.md +0 -1
  26. package/identities/geometry/rectangle/readme.md +0 -1
  27. package/identities/geometry/string/readme.md +0 -1
  28. package/identities/physics/readme.md +0 -1
  29. package/identities/readme.md +0 -2
  30. package/math/array/average/index.js +0 -10
  31. package/math/array/average/math.json +0 -8
  32. package/math/array/average/test.js +0 -3
  33. package/math/array/col/index.js +0 -17
  34. package/math/array/col/math.json +0 -8
  35. package/math/array/col/test.js +0 -4
  36. package/math/array/composite/index.js +0 -35
  37. package/math/array/composite/math.json +0 -8
  38. package/math/array/composite/test.js +0 -4
  39. package/math/array/deviations/index.js +0 -15
  40. package/math/array/deviations/math.json +0 -8
  41. package/math/array/deviations/test.js +0 -3
  42. package/math/array/difference/index.js +0 -9
  43. package/math/array/difference/math.json +0 -8
  44. package/math/array/difference/test.js +0 -7
  45. package/math/array/distinct/index.js +0 -20
  46. package/math/array/distinct/math.json +0 -8
  47. package/math/array/distinct/test.js +0 -8
  48. package/math/array/distributive/index.js +0 -24
  49. package/math/array/distributive/math.json +0 -8
  50. package/math/array/distributive/test.js +0 -3
  51. package/math/array/divisor/index.js +0 -17
  52. package/math/array/divisor/math.json +0 -8
  53. package/math/array/divisor/test.js +0 -6
  54. package/math/array/intersect/index.js +0 -9
  55. package/math/array/intersect/math.json +0 -8
  56. package/math/array/intersect/test.js +0 -7
  57. package/math/array/max/index.js +0 -17
  58. package/math/array/max/math.json +0 -8
  59. package/math/array/max/test.js +0 -8
  60. package/math/array/median/index.js +0 -34
  61. package/math/array/median/math.json +0 -8
  62. package/math/array/median/test.js +0 -4
  63. package/math/array/min/index.js +0 -16
  64. package/math/array/min/math.json +0 -8
  65. package/math/array/min/test.js +0 -8
  66. package/math/array/multiple/index.js +0 -17
  67. package/math/array/multiple/math.json +0 -8
  68. package/math/array/multiple/test.js +0 -7
  69. package/math/array/natural/index.js +0 -17
  70. package/math/array/natural/math.json +0 -8
  71. package/math/array/natural/test.js +0 -9
  72. package/math/array/pos/index.js +0 -21
  73. package/math/array/pos/math.json +0 -8
  74. package/math/array/pos/test.js +0 -41
  75. package/math/array/prime/index.js +0 -35
  76. package/math/array/prime/math.json +0 -8
  77. package/math/array/prime/test.js +0 -4
  78. package/math/array/readme.md +0 -5
  79. package/math/array/reduction/index.js +0 -57
  80. package/math/array/reduction/math.json +0 -8
  81. package/math/array/reduction/test.js +0 -42
  82. package/math/array/rising/index.js +0 -8
  83. package/math/array/rising/math.json +0 -8
  84. package/math/array/rising/test.js +0 -3
  85. package/math/array/row/index.js +0 -11
  86. package/math/array/row/math.json +0 -8
  87. package/math/array/row/test.js +0 -4
  88. package/math/array/square/index.js +0 -12
  89. package/math/array/square/math.json +0 -8
  90. package/math/array/square/test.js +0 -3
  91. package/math/array/stddev/index.js +0 -17
  92. package/math/array/stddev/math.json +0 -8
  93. package/math/array/stddev/test.js +0 -4
  94. package/math/array/sum/index.js +0 -13
  95. package/math/array/sum/math.json +0 -8
  96. package/math/array/sum/test.js +0 -3
  97. package/math/array/sumproduct/index.js +0 -13
  98. package/math/array/sumproduct/math.json +0 -8
  99. package/math/array/sumproduct/test.js +0 -3
  100. package/math/array/union/index.js +0 -11
  101. package/math/array/union/math.json +0 -8
  102. package/math/array/union/test.js +0 -7
  103. package/math/base/abs/index.js +0 -8
  104. package/math/base/abs/math.json +0 -8
  105. package/math/base/abs/test.js +0 -3
  106. package/math/base/ceil/index.js +0 -8
  107. package/math/base/ceil/math.json +0 -8
  108. package/math/base/ceil/test.js +0 -3
  109. package/math/base/denominator/index.js +0 -17
  110. package/math/base/denominator/math.json +0 -9
  111. package/math/base/denominator/test.js +0 -3
  112. package/math/base/diff/index.js +0 -9
  113. package/math/base/diff/math.json +0 -8
  114. package/math/base/diff/test.js +0 -6
  115. package/math/base/exp/index.js +0 -8
  116. package/math/base/exp/math.json +0 -8
  117. package/math/base/exp/test.js +0 -6
  118. package/math/base/floor/index.js +0 -8
  119. package/math/base/floor/math.json +0 -8
  120. package/math/base/floor/test.js +0 -3
  121. package/math/base/int/index.js +0 -8
  122. package/math/base/int/math.json +0 -8
  123. package/math/base/int/test.js +0 -2
  124. package/math/base/log/index.js +0 -8
  125. package/math/base/log/math.json +0 -8
  126. package/math/base/log/test.js +0 -4
  127. package/math/base/max_divisor/index.js +0 -10
  128. package/math/base/max_divisor/math.json +0 -9
  129. package/math/base/max_divisor/test.js +0 -3
  130. package/math/base/min_multiple/index.js +0 -11
  131. package/math/base/min_multiple/math.json +0 -9
  132. package/math/base/min_multiple/test.js +0 -3
  133. package/math/base/mod/index.js +0 -8
  134. package/math/base/mod/math.json +0 -8
  135. package/math/base/mod/test.js +0 -3
  136. package/math/base/pow/index.js +0 -9
  137. package/math/base/pow/math.json +0 -8
  138. package/math/base/pow/test.js +0 -3
  139. package/math/base/rand/index.js +0 -21
  140. package/math/base/rand/math.json +0 -8
  141. package/math/base/rand/test.js +0 -3
  142. package/math/base/random/index.js +0 -12
  143. package/math/base/random/math.json +0 -8
  144. package/math/base/random/test.js +0 -4
  145. package/math/base/readme.md +0 -1
  146. package/math/base/round/index.js +0 -14
  147. package/math/base/round/math.json +0 -8
  148. package/math/base/round/test.js +0 -4
  149. package/math/base/sqrt/index.js +0 -8
  150. package/math/base/sqrt/math.json +0 -8
  151. package/math/base/sqrt/test.js +0 -5
  152. package/math/readme.md +0 -1
  153. package/math/stock/AMOUNT/index.js +0 -7
  154. package/math/stock/AMOUNT/math.json +0 -8
  155. package/math/stock/AMOUNT/test.js +0 -4
  156. package/math/stock/AP/index.js +0 -20
  157. package/math/stock/AP/math.json +0 -8
  158. package/math/stock/AP/test.js +0 -5
  159. package/math/stock/AROON/index.js +0 -14
  160. package/math/stock/ARR/index.js +0 -10
  161. package/math/stock/ARR/math.json +0 -8
  162. package/math/stock/ARR/test.js +0 -45
  163. package/math/stock/ASTAR/index.js +0 -20
  164. package/math/stock/ASTAR/math.json +0 -8
  165. package/math/stock/ASTAR/test.js +0 -4
  166. package/math/stock/AVERAGE/index.js +0 -14
  167. package/math/stock/AVERAGE/math.json +0 -8
  168. package/math/stock/AVERAGE/test.js +0 -5
  169. package/math/stock/AYANG/index.js +0 -18
  170. package/math/stock/AYANG/math.json +0 -8
  171. package/math/stock/AYANG/test.js +0 -4
  172. package/math/stock/AYIN/index.js +0 -18
  173. package/math/stock/AYIN/math.json +0 -8
  174. package/math/stock/AYIN/test.js +0 -4
  175. package/math/stock/BBANDS/index.js +0 -0
  176. package/math/stock/BETWEEN/index.js +0 -24
  177. package/math/stock/BETWEEN/math.json +0 -8
  178. package/math/stock/BETWEEN/test.js +0 -4
  179. package/math/stock/CLOSE/c.js +0 -0
  180. package/math/stock/CLOSE/index.js +0 -11
  181. package/math/stock/CLOSE/math.json +0 -8
  182. package/math/stock/CLOSE/test.js +0 -45
  183. package/math/stock/CP/index.js +0 -35
  184. package/math/stock/CP/math.json +0 -8
  185. package/math/stock/CP/test.js +0 -5
  186. package/math/stock/CROSS/index.js +0 -29
  187. package/math/stock/CROSS/math.json +0 -8
  188. package/math/stock/CROSS/test.js +0 -6
  189. package/math/stock/DATE/index.js +0 -14
  190. package/math/stock/DATE/math.json +0 -8
  191. package/math/stock/DATE/test.js +0 -45
  192. package/math/stock/DOWN/index.js +0 -19
  193. package/math/stock/DOWN/math.json +0 -8
  194. package/math/stock/DOWN/test.js +0 -8
  195. package/math/stock/DOWNNDAY/index.js +0 -27
  196. package/math/stock/DOWNNDAY/math.json +0 -8
  197. package/math/stock/DOWNNDAY/test.js +0 -10
  198. package/math/stock/EMA/index.js +0 -30
  199. package/math/stock/EMA/math.json +0 -8
  200. package/math/stock/EMA/test.js +0 -4
  201. package/math/stock/HHV/index.js +0 -16
  202. package/math/stock/HHV/math.json +0 -8
  203. package/math/stock/HHV/test.js +0 -4
  204. package/math/stock/HIGH/h.js +0 -0
  205. package/math/stock/HIGH/index.js +0 -11
  206. package/math/stock/HIGH/math.json +0 -8
  207. package/math/stock/HIGH/test.js +0 -45
  208. package/math/stock/LEFT/index.js +0 -13
  209. package/math/stock/LEFT/math.json +0 -8
  210. package/math/stock/LEFT/test.js +0 -4
  211. package/math/stock/LLV/index.js +0 -16
  212. package/math/stock/LLV/math.json +0 -8
  213. package/math/stock/LLV/test.js +0 -4
  214. package/math/stock/LOW/index.js +0 -11
  215. package/math/stock/LOW/l.js +0 -0
  216. package/math/stock/LOW/math.json +0 -8
  217. package/math/stock/LOW/test.js +0 -47
  218. package/math/stock/MA/index.js +0 -30
  219. package/math/stock/MA/math.json +0 -8
  220. package/math/stock/MA/test.js +0 -5
  221. package/math/stock/MACD/index.js +0 -29
  222. package/math/stock/MACD/math.json +0 -8
  223. package/math/stock/MACD/test.js +0 -4
  224. package/math/stock/MFI/index.js +0 -33
  225. package/math/stock/NMA/index.js +0 -23
  226. package/math/stock/NMA/math.json +0 -8
  227. package/math/stock/NMA/test.js +0 -5
  228. package/math/stock/NOT/index.js +0 -7
  229. package/math/stock/NOT/math.json +0 -8
  230. package/math/stock/NOT/test.js +0 -10
  231. package/math/stock/OPEN/index.js +0 -11
  232. package/math/stock/OPEN/math.json +0 -8
  233. package/math/stock/OPEN/o.js +0 -0
  234. package/math/stock/OPEN/test.js +0 -45
  235. package/math/stock/PERCPRICEOSC/index.js +0 -19
  236. package/math/stock/REF/index.js +0 -15
  237. package/math/stock/REF/math.json +0 -8
  238. package/math/stock/REF/test.js +0 -5
  239. package/math/stock/RIGHT/index.js +0 -13
  240. package/math/stock/RIGHT/math.json +0 -8
  241. package/math/stock/RIGHT/test.js +0 -4
  242. package/math/stock/RSI/index.js +0 -36
  243. package/math/stock/SMA/index.js +0 -12
  244. package/math/stock/STAR/index.js +0 -19
  245. package/math/stock/STAR/math.json +0 -8
  246. package/math/stock/STAR/test.js +0 -4
  247. package/math/stock/STOCHRSI/index.js +0 -19
  248. package/math/stock/SUM/index.js +0 -19
  249. package/math/stock/SUM/math.json +0 -8
  250. package/math/stock/SUM/test.js +0 -7
  251. package/math/stock/TIME/index.js +0 -14
  252. package/math/stock/TIME/math.json +0 -8
  253. package/math/stock/TIME/test.js +0 -45
  254. package/math/stock/TRUERANGE/index.js +0 -19
  255. package/math/stock/UP/index.js +0 -19
  256. package/math/stock/UP/math.json +0 -8
  257. package/math/stock/UP/test.js +0 -9
  258. package/math/stock/UPNDAY/index.js +0 -28
  259. package/math/stock/UPNDAY/math.json +0 -8
  260. package/math/stock/UPNDAY/test.js +0 -10
  261. package/math/stock/VMA/index.js +0 -41
  262. package/math/stock/VMA/math.json +0 -8
  263. package/math/stock/VMA/test.js +0 -7
  264. package/math/stock/VOL/index.js +0 -11
  265. package/math/stock/VOL/math.json +0 -8
  266. package/math/stock/VOL/test.js +0 -45
  267. package/math/stock/WILLR/index.js +0 -22
  268. package/math/stock/YANG/index.js +0 -16
  269. package/math/stock/YANG/math.json +0 -8
  270. package/math/stock/YANG/test.js +0 -4
  271. package/math/stock/YIN/index.js +0 -16
  272. package/math/stock/YIN/math.json +0 -8
  273. package/math/stock/YIN/test.js +0 -4
  274. package/math/stock/readme.md +0 -294
  275. package/math/trigonometric/cos/index.js +0 -8
  276. package/math/trigonometric/cos/math.json +0 -20
  277. package/math/trigonometric/cot/index.js +0 -8
  278. package/math/trigonometric/cot/math.json +0 -17
  279. package/math/trigonometric/csc/index.js +0 -8
  280. package/math/trigonometric/csc/math.json +0 -20
  281. package/math/trigonometric/readme.md +0 -2
  282. package/math/trigonometric/sec/index.js +0 -8
  283. package/math/trigonometric/sec/math.json +0 -20
  284. package/math/trigonometric/sin/index.js +0 -8
  285. package/math/trigonometric/sin/math.json +0 -20
  286. package/math/trigonometric/tan/index.js +0 -8
  287. package/math/trigonometric/tan/math.json +0 -18
  288. package/symbol/brackets/index.js +0 -46
  289. package/symbol/brackets/symbol.json +0 -8
  290. package/symbol/brackets/test.js +0 -12
  291. package/symbol/decimal/index.js +0 -41
  292. package/symbol/decimal/symbol.json +0 -8
  293. package/symbol/decimal/test.js +0 -2
  294. package/symbol/percentage/index.js +0 -48
  295. package/symbol/percentage/symbol.json +0 -8
  296. package/symbol/percentage/test.js +0 -2
  297. package/symbol/pi/index.js +0 -20
  298. package/symbol/pi/symbol.json +0 -9
  299. package/symbol/pi/test.js +0 -2
  300. package/symbol/power/index.js +0 -111
  301. package/symbol/power/symbol.json +0 -8
  302. package/symbol/power/test.js +0 -10
  303. package/symbol/readme.md +0 -2
  304. package/test.js +0 -32
  305. package/test2.js +0 -47
package/README.md CHANGED
@@ -13,7 +13,7 @@ npm install mm_maths
13
13
  ### 1. 初始化模块
14
14
 
15
15
  ```javascript
16
- const Maths = require('mm_maths');
16
+ const { Maths } = require('mm_maths');
17
17
 
18
18
  // 创建实例并指定模块路径
19
19
  const maths = new Maths(__dirname);
@@ -26,10 +26,10 @@ const maths = new Maths(__dirname);
26
26
  maths.update(__dirname, "base");
27
27
 
28
28
  // 也可以分别载入不同类型的公式
29
- maths.load_identities(__dirname, "base"); // 载入恒等式
30
- maths.load_convert(__dirname, "base"); // 载入转换函数
31
- maths.load_symbol(__dirname, "base"); // 载入符号处理函数
32
- maths.load_math(__dirname, "base"); // 载入计算函数
29
+ maths.loadIdentities(__dirname, "base"); // 载入恒等式
30
+ maths.loadConvert(__dirname, "base"); // 载入转换函数
31
+ maths.loadSymbol(__dirname, "base"); // 载入符号处理函数
32
+ maths.loadMath(__dirname, "base"); // 载入计算函数
33
33
  ```
34
34
 
35
35
  ### 3. 设置常量和变量
@@ -40,14 +40,14 @@ const consts = {
40
40
  a: 10,
41
41
  b: 3
42
42
  };
43
- maths.set_const(consts);
43
+ maths.setConst(consts);
44
44
 
45
45
  // 设置变量
46
46
  const vars = {
47
47
  ma5: "MA(CLOSE, 5)", // 可以设置公式作为变量
48
48
  price: null
49
49
  };
50
- maths.set_var(vars);
50
+ maths.setVar(vars);
51
51
  ```
52
52
 
53
53
  ### 4. 公式转换和计算
@@ -59,7 +59,7 @@ const express = maths.convertTo(expression);
59
59
  console.log('转换后的公式:', express);
60
60
 
61
61
  // 运行代码
62
- const result = maths.run_code("return " + express);
62
+ const result = maths.runCode("return " + express);
63
63
  console.log('计算结果:', result);
64
64
  ```
65
65
 
@@ -83,13 +83,13 @@ console.log('计算结果:', result);
83
83
  - `path` {String} 检索路径
84
84
  - `type` {String} 公式类型,如 "base"(基础数学)、"stock"(股票)
85
85
 
86
- #### set_const(constant)
86
+ #### setConst(constant)
87
87
 
88
88
  设置常量。
89
89
 
90
90
  - `constant` {Object} 常量集合,如 `{ a: 10, b: 15 }`
91
91
 
92
- #### set_var(vars)
92
+ #### setVar(vars)
93
93
 
94
94
  设置变量。
95
95
 
@@ -102,7 +102,7 @@ console.log('计算结果:', result);
102
102
  - `express` {String} 要转换的表达式
103
103
  - 返回 {String} 转换后的表达式
104
104
 
105
- #### run_code(express)
105
+ #### runCode(express)
106
106
 
107
107
  运行代码。
108
108
 
@@ -137,4 +137,20 @@ console.log('计算结果:', result);
137
137
 
138
138
  ## 许可证
139
139
 
140
- ISC License
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,5 @@
1
- /**
2
- * @fileOverview 数学公式变形、转换成js 并计算模块
3
- * @author <a href="http://qww.elins.cn">邱文武</a>
4
- * @version 1.0
5
- */
6
- require('mm_expand');
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
+ const Maths = require('./lib/maths.js');
2
+
3
+ module.exports = {
4
+ Maths
5
+ };