mm_maths 1.1.8 → 1.2.0

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 ADDED
@@ -0,0 +1,140 @@
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.load_identities(__dirname, "base"); // 载入恒等式
30
+ maths.load_convert(__dirname, "base"); // 载入转换函数
31
+ maths.load_symbol(__dirname, "base"); // 载入符号处理函数
32
+ maths.load_math(__dirname, "base"); // 载入计算函数
33
+ ```
34
+
35
+ ### 3. 设置常量和变量
36
+
37
+ ```javascript
38
+ // 设置常量
39
+ const consts = {
40
+ a: 10,
41
+ b: 3
42
+ };
43
+ maths.set_const(consts);
44
+
45
+ // 设置变量
46
+ const vars = {
47
+ ma5: "MA(CLOSE, 5)", // 可以设置公式作为变量
48
+ price: null
49
+ };
50
+ maths.set_var(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.run_code("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
+ #### set_const(constant)
87
+
88
+ 设置常量。
89
+
90
+ - `constant` {Object} 常量集合,如 `{ a: 10, b: 15 }`
91
+
92
+ #### set_var(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
+ #### run_code(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
package/index.js CHANGED
@@ -26,7 +26,21 @@ class Maths {
26
26
  * 常量(集合)
27
27
  */
28
28
  this.const = {};
29
-
29
+
30
+ /**
31
+ * 变量(集合)用于替换代码中的文字,赋予值。等同于读取缓存值
32
+ */
33
+ this.vars = {
34
+ // "ma5": "MA(CLOSE, 5)"
35
+ };
36
+
37
+ /**
38
+ * 变量值(集合)用于替换代码中的文字,赋予值。等同于读取缓存值
39
+ */
40
+ this.vals = {
41
+ // "ma5": []
42
+ };
43
+
30
44
  /**
31
45
  * 声明(集合)
32
46
  */
@@ -228,6 +242,25 @@ Maths.prototype.set_const = function(constant) {
228
242
  $.push(this.const, constant, true);
229
243
  };
230
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
+
231
264
  /**
232
265
  * 下定义
233
266
  * @param {Object} def 定义集合, 例如:
@@ -248,17 +281,20 @@ Maths.prototype.set_know = function(know) {
248
281
  * 设置函数头
249
282
  */
250
283
  Maths.prototype.head = function() {
251
- var head = "";
252
- for (var k in this.math) {
253
- if (!this.const[k]) {
254
- head += "var " + k + " = math." + k + ";\r\n"
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
+ }
255
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;
256
296
  }
257
- for (var k in this.const) {
258
- var value = this.const[k];
259
- head += "var " + k + " = constant." + k + ";\r\n"
260
- }
261
- return head;
297
+ return this.head_value;
262
298
  }
263
299
 
264
300
  /**
@@ -268,7 +304,6 @@ Maths.prototype.head = function() {
268
304
  */
269
305
  Maths.prototype.convertTo = function(express){
270
306
  var express = this.convert_equal(express);
271
-
272
307
  // 先进行符号转换
273
308
  var dt = this.symbol;
274
309
  for(var k in dt){
@@ -278,7 +313,6 @@ Maths.prototype.convertTo = function(express){
278
313
  express = func(express);
279
314
  }
280
315
  }
281
-
282
316
  // 再进行式子转换
283
317
  dt = this.convert;
284
318
  for(var k in dt){
@@ -323,12 +357,23 @@ Maths.prototype.derivation = function(express) {
323
357
  // 符号转换
324
358
  };
325
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
+
326
371
  /**
327
372
  * 运行代码
328
373
  * @param {String} express 表达式
329
374
  * @return {Object} 执行结果
330
375
  */
331
- Maths.prototype.run_code = function(express) {
376
+ Maths.prototype.first_run_code = function(express) {
332
377
  var head = this.head();
333
378
  var exp = head + express;
334
379
  let func = new Function('constant', 'math', exp);
@@ -336,6 +381,25 @@ Maths.prototype.run_code = function(express) {
336
381
  var ret;
337
382
  try {
338
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);
339
403
  } catch (e) {
340
404
  console.log(e);
341
405
  //TODO handle the exception
package/package.json CHANGED
@@ -1,33 +1,33 @@
1
- {
2
- "name": "mm_maths",
3
- "version": "1.1.8",
4
- "description": "这是一个可以将数学公式推导变形,并转成js和计算的模块。",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "node test.js"
8
- },
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://github.com/qiuwenwu/mm_maths.git"
12
- },
13
- "keywords": [
14
- "math",
15
- "array",
16
- "number",
17
- "identities",
18
- "expression",
19
- "set",
20
- "convert",
21
- "symbol",
22
- "$"
23
- ],
24
- "author": "邱文武",
25
- "license": "ISC",
26
- "bugs": {
27
- "url": "https://github.com/qiuwenwu/mm_maths/issues"
28
- },
29
- "homepage": "https://github.com/qiuwenwu/mm_maths#readme",
30
- "dependencies": {
31
- "mm_expand": "^1.4.7"
32
- }
33
- }
1
+ {
2
+ "name": "mm_maths",
3
+ "version": "1.2.0",
4
+ "description": "这是一个可以将数学公式推导变形,并转成js和计算的模块。",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node test.js"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://gitee.com/qiuwenwu91/mm_maths.git"
12
+ },
13
+ "keywords": [
14
+ "math",
15
+ "array",
16
+ "number",
17
+ "identities",
18
+ "expression",
19
+ "set",
20
+ "convert",
21
+ "symbol",
22
+ "$"
23
+ ],
24
+ "author": "邱文武",
25
+ "license": "ISC",
26
+ "bugs": {
27
+ "url": "https://gitee.com/qiuwenwu91/mm_maths/issues"
28
+ },
29
+ "homepage": "https://gitee.com/qiuwenwu91/mm_maths#readme",
30
+ "dependencies": {
31
+ "mm_expand": "^1.8.3"
32
+ }
33
+ }
package/test.js CHANGED
@@ -12,7 +12,7 @@ var consts = {
12
12
  };
13
13
  maths.set_const(consts);
14
14
 
15
- // // 第三步,转换公式
15
+ // 第三步,转换公式
16
16
  var expression = "y = abc * ab * 3c * 2";
17
17
  var express = maths.convertTo(expression);
18
18
  console.log('打印公式: ', express);
package/test2.js CHANGED
@@ -1,3 +1,36 @@
1
+ var Maths = require('./index');
2
+
3
+ var maths = new Maths(__dirname);
4
+
5
+ // 第一步,载入公式及相关函数
6
+ maths.update(__dirname, "stock");
7
+
8
+ maths.const = {
9
+ CLOSE: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
10
+ }
11
+
12
+ maths.set_var({
13
+ ma5: "MA(CLOSE, 5)"
14
+ });
15
+
16
+ console.log(new Date());
17
+ var ret = maths.run_code(`
18
+ var a = MA(CLOSE, 5);
19
+ return a;
20
+ `);
21
+
22
+ console.log(new Date());
23
+ var ret = maths.run_code(`
24
+ var b = MA(CLOSE, 5);
25
+ return REF(b, 0);
26
+ `);
27
+
28
+ console.log(new Date());
29
+ console.log(ret);
30
+
31
+
32
+
33
+
1
34
 
2
35
  // console.log(Math.cos(0));
3
36
  // var fd = (2*Math.PI / 360) * 30;