mm_maths 1.2.0 → 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 +28 -12
- 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/lib/maths.js
ADDED
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileOverview 数学公式变形、转换成js 并计算模块
|
|
3
|
+
* @author <a href="http://qww.elins.cn">邱文武</a>
|
|
4
|
+
* @version 1.0
|
|
5
|
+
*/
|
|
6
|
+
const helper = require('mm_helper');
|
|
7
|
+
let { getAllFile } = require('mm_files');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 数学公式计算函数
|
|
11
|
+
* @property {Object} identities 恒等式(注释列表)
|
|
12
|
+
* @property {Object} math 计算函数(注释列表)
|
|
13
|
+
* @property {Object} convert 变换函数(注释列表)
|
|
14
|
+
* @property {Object} symbol 符号替换函数(注释列表)
|
|
15
|
+
*/
|
|
16
|
+
class Maths {
|
|
17
|
+
/**
|
|
18
|
+
* @param {String} dir 路径
|
|
19
|
+
*/
|
|
20
|
+
constructor(dir) {
|
|
21
|
+
/**
|
|
22
|
+
* 检索文件路径
|
|
23
|
+
*/
|
|
24
|
+
this.dir = dir ? dir : __dirname;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 常量(集合)
|
|
28
|
+
*/
|
|
29
|
+
this.const = {};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 变量(集合)用于替换代码中的文字,赋予值。等同于读取缓存值
|
|
33
|
+
*/
|
|
34
|
+
this.vars = {
|
|
35
|
+
// "ma5": "MA(CLOSE, 5)"
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 变量值(集合)用于替换代码中的文字,赋予值。等同于读取缓存值
|
|
40
|
+
*/
|
|
41
|
+
this.vals = {
|
|
42
|
+
// "ma5": []
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 声明(集合)
|
|
47
|
+
*/
|
|
48
|
+
this.define = {};
|
|
49
|
+
this.def = this.define;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 已知条件
|
|
53
|
+
*/
|
|
54
|
+
this.knownConditions = {};
|
|
55
|
+
this.known = this.knownConditions;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 恒等式替换函数(集合)
|
|
59
|
+
*/
|
|
60
|
+
this.identities = {};
|
|
61
|
+
this.eq = this.identities;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 计算函数(集合)
|
|
65
|
+
*/
|
|
66
|
+
this.math = {};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 变换函数(集合)
|
|
70
|
+
*/
|
|
71
|
+
this.convert = {};
|
|
72
|
+
this.ct = this.convert;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 符号替换函数(集合)
|
|
76
|
+
*/
|
|
77
|
+
this.symbol = {};
|
|
78
|
+
this.sl = this.symbol;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 学科方向,mathematical数学、physical物理、chemistry化学
|
|
82
|
+
*/
|
|
83
|
+
this.subject = "mathematical";
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 类型,base基础数学、geometry几何数学、stock股票
|
|
87
|
+
*/
|
|
88
|
+
this.type = "base";
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 初始化
|
|
94
|
+
*/
|
|
95
|
+
Maths.prototype.init = function() {};
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 载入公式
|
|
99
|
+
* @param {String} path 检索的路径
|
|
100
|
+
* @param {String} type 类型
|
|
101
|
+
* @param {String} expand 拓展类型, 例如: identities恒等式、convert变换函数、symbol符号、math计算函数
|
|
102
|
+
* @return {String} 成功返回true,失败返回错误类型;
|
|
103
|
+
*/
|
|
104
|
+
Maths.prototype.loadSub = function(path, type, expand) {
|
|
105
|
+
if (!path) {
|
|
106
|
+
path = __dirname
|
|
107
|
+
}
|
|
108
|
+
if (!type) {
|
|
109
|
+
type = "math";
|
|
110
|
+
}
|
|
111
|
+
if (!expand) {
|
|
112
|
+
expand = "base";
|
|
113
|
+
}
|
|
114
|
+
let dict = this[expand];
|
|
115
|
+
if (!dict) {
|
|
116
|
+
console.debug("不存在的类型", expand);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
let searchFile = `*${expand}.json`;
|
|
120
|
+
let files = getAllFile(path, searchFile);
|
|
121
|
+
|
|
122
|
+
files.map(o => {
|
|
123
|
+
let cg = o.loadJson();
|
|
124
|
+
if (cg && cg.name) {
|
|
125
|
+
if (!cg.type || cg.type.has(type)) {
|
|
126
|
+
if (cg.func_file && cg.key) {
|
|
127
|
+
dict[cg.key] = cg;
|
|
128
|
+
let f = cg.func_file.fullname(o.dirname());
|
|
129
|
+
if (f.hasFile()) {
|
|
130
|
+
let func = require(f);
|
|
131
|
+
if (func) {
|
|
132
|
+
this[cg.key] = func;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 更新
|
|
143
|
+
* @param {String} path 路径
|
|
144
|
+
* @param {String} type 类型
|
|
145
|
+
*/
|
|
146
|
+
Maths.prototype.clear = function(path, type) {
|
|
147
|
+
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 更新
|
|
152
|
+
* @param {String} path 路径
|
|
153
|
+
* @param {String} type 类型
|
|
154
|
+
*/
|
|
155
|
+
Maths.prototype.update = function(path, type) {
|
|
156
|
+
this.clear(type);
|
|
157
|
+
this.load(path, type);
|
|
158
|
+
this.sort();
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 载入恒等式变形函数
|
|
163
|
+
* @param {String} path 检索的路径
|
|
164
|
+
* @param {String} type 类型, 例如:default 默认,base 基础数学
|
|
165
|
+
* @return {String} 成功返回true,失败返回错误类型;
|
|
166
|
+
*/
|
|
167
|
+
Maths.prototype.loadIdentities = function(path, type) {
|
|
168
|
+
this.loadSub(path, type, "identities");
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 载入转换函数
|
|
173
|
+
* @param {String} path 检索的路径
|
|
174
|
+
* @param {String} type 类型, 例如:default 默认,base 基础数学
|
|
175
|
+
* @return {String} 成功返回true,失败返回错误类型;
|
|
176
|
+
*/
|
|
177
|
+
Maths.prototype.loadConvert = function(path, type) {
|
|
178
|
+
this.loadSub(path, type, "convert");
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* 载入符号处理函数
|
|
183
|
+
* @param {String} path 检索的路径
|
|
184
|
+
* @param {String} type 类型, 例如:default 默认,base 基础数学
|
|
185
|
+
* @return {String} 成功返回true,失败返回错误类型;
|
|
186
|
+
*/
|
|
187
|
+
Maths.prototype.loadSymbol = function(path, type) {
|
|
188
|
+
this.loadSub(path, type, "symbol");
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 载入计算函数
|
|
193
|
+
* @param {String} path 检索的路径
|
|
194
|
+
* @param {String} type 类型, 例如:default 默认,base 基础数学
|
|
195
|
+
* @return {String} 成功返回true,失败返回错误类型;
|
|
196
|
+
*/
|
|
197
|
+
Maths.prototype.loadMath = function(path, type) {
|
|
198
|
+
this.loadSub(path, type, "math");
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 载入相关函数
|
|
203
|
+
* @param {String} path 检索的路径
|
|
204
|
+
* @param {String} type 类型, 例如:default 默认,base 基础数学
|
|
205
|
+
* @return {String} 成功返回true,失败返回错误类型;
|
|
206
|
+
*/
|
|
207
|
+
Maths.prototype.load = function(path, type) {
|
|
208
|
+
this.loadIdentities(path, type);
|
|
209
|
+
this.loadConvert(path, type);
|
|
210
|
+
this.loadSymbol(path, type);
|
|
211
|
+
this.loadMath(path, type);
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 函数字典排序
|
|
216
|
+
*/
|
|
217
|
+
Maths.prototype.sort = function() {
|
|
218
|
+
this.convert = this.sortSub(this.convert);
|
|
219
|
+
this.symbol = this.sortSub(this.symbol);
|
|
220
|
+
this.identities = this.sortSub(this.identities);
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* 函数排序
|
|
225
|
+
* @param {Object} dict 要排序的字典
|
|
226
|
+
*/
|
|
227
|
+
Maths.prototype.sortSub = function(dict) {
|
|
228
|
+
let list = [];
|
|
229
|
+
for(let k in dict){
|
|
230
|
+
list.push(dict[k]);
|
|
231
|
+
}
|
|
232
|
+
list.sortBy('asc', 'sort');
|
|
233
|
+
let dt = {};
|
|
234
|
+
list.map(o => dt[o.key] = o);
|
|
235
|
+
return dt;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 设置常量
|
|
240
|
+
* @param {Object} constant 常量集合,采用键值对的方式 例如:{ a: 10, b: 15, d: [1,-1] }
|
|
241
|
+
*/
|
|
242
|
+
Maths.prototype.setConst = function(constant) {
|
|
243
|
+
helper.object.push(this.const, constant, true);
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* 重置变量
|
|
249
|
+
*/
|
|
250
|
+
Maths.prototype.resetVal = function(){
|
|
251
|
+
for(let k in this.vars){
|
|
252
|
+
this.vals[k] = this.firstRunCode("return " + this.vars[k]);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* 设置变量
|
|
258
|
+
* @param {Object} vars 变量集合,采用键值对的方式 例如:{ a: null, b: 15, d: [1,-1] }
|
|
259
|
+
*/
|
|
260
|
+
Maths.prototype.setVar = function(vars) {
|
|
261
|
+
helper.object.push(this.vars, vars, true);
|
|
262
|
+
this.resetVal();
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* 下定义
|
|
267
|
+
* @param {Object} def 定义集合, 例如:
|
|
268
|
+
*/
|
|
269
|
+
Maths.prototype.setDef = function(def) {
|
|
270
|
+
helper.object.push(this.def, def, true);
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* 设置已知条件
|
|
275
|
+
* @param {Object} know 已知条件 { c: "a + b", e: "ab" }
|
|
276
|
+
*/
|
|
277
|
+
Maths.prototype.setKnow = function(know) {
|
|
278
|
+
helper.object.push(this.know, know, true);
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* 设置函数头
|
|
283
|
+
*/
|
|
284
|
+
Maths.prototype.head = function() {
|
|
285
|
+
if(!this.headValue){
|
|
286
|
+
let head = "";
|
|
287
|
+
for (let k in this.math) {
|
|
288
|
+
if (!this.const[k]) {
|
|
289
|
+
head += "var " + k + " = math." + k + ";\r\n"
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
for (let k in this.const) {
|
|
293
|
+
let value = this.const[k];
|
|
294
|
+
head += "var " + k + " = constant." + k + ";\r\n"
|
|
295
|
+
}
|
|
296
|
+
this.headValue = head;
|
|
297
|
+
}
|
|
298
|
+
return this.headValue;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* 转换公式
|
|
303
|
+
* @param {String} express 表达式
|
|
304
|
+
* @return {String} 返回推导结果
|
|
305
|
+
*/
|
|
306
|
+
Maths.prototype.convertTo = function(express){
|
|
307
|
+
let expr = this.convertEqual(express);
|
|
308
|
+
let parts = expr.split('=');
|
|
309
|
+
let left = parts[0];
|
|
310
|
+
// 等号右侧(通常是 0)保持原样,避免被四则运算吞掉
|
|
311
|
+
let right = parts.length > 1 ? parts.slice(1).join('=') : null;
|
|
312
|
+
left = this.convertExpress(left);
|
|
313
|
+
if (right !== null) {
|
|
314
|
+
right = this.convertExpress(right);
|
|
315
|
+
return left + ' = ' + right.trim();
|
|
316
|
+
}
|
|
317
|
+
return left;
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* 转换表达式
|
|
322
|
+
* @param {String} express 表达式
|
|
323
|
+
* @return {String} 返回推导结果
|
|
324
|
+
*/
|
|
325
|
+
Maths.prototype.convertExpress = function(express){
|
|
326
|
+
// 先进行符号转换
|
|
327
|
+
let dt = this.symbol;
|
|
328
|
+
for(let k in dt){
|
|
329
|
+
let func = this[k];
|
|
330
|
+
if(func)
|
|
331
|
+
{
|
|
332
|
+
express = func(express);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
// 再进行式子转换
|
|
336
|
+
dt = this.convert;
|
|
337
|
+
for(let k in dt){
|
|
338
|
+
let func = this[k];
|
|
339
|
+
if(func)
|
|
340
|
+
{
|
|
341
|
+
express = func(express);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return express;
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* 等号转换
|
|
349
|
+
* @param {Object} express
|
|
350
|
+
* @return {String} 返回公式
|
|
351
|
+
*/
|
|
352
|
+
Maths.prototype.convertEqual = function(express){
|
|
353
|
+
let left = express.left('=', true);
|
|
354
|
+
let right = express.right('=');
|
|
355
|
+
|
|
356
|
+
if(right){
|
|
357
|
+
if(/\+|\-|\*|\//.test(right)) {
|
|
358
|
+
return left + " - (" + right.trim() + ") = 0";
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
return left + " - " + right.trim() + " = 0";
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
return left;
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* 公式推导
|
|
372
|
+
* @param {String} express 表达式
|
|
373
|
+
* @return {String} 返回推导结果
|
|
374
|
+
*/
|
|
375
|
+
Maths.prototype.derivation = function(express) {
|
|
376
|
+
// 符号转换
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* 更新标签
|
|
381
|
+
* @param {String} express 表达式
|
|
382
|
+
*/
|
|
383
|
+
Maths.prototype.updateTag = function(express) {
|
|
384
|
+
for(let k in this.vars){
|
|
385
|
+
express = express.replaceAll(this.vars[k], "vals." + k);
|
|
386
|
+
}
|
|
387
|
+
return express;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* 运行代码
|
|
392
|
+
* @param {String} express 表达式
|
|
393
|
+
* @return {Object} 执行结果
|
|
394
|
+
*/
|
|
395
|
+
Maths.prototype.firstRunCode = function(express) {
|
|
396
|
+
let head = this.head();
|
|
397
|
+
let exp = head + express;
|
|
398
|
+
let func = new Function('constant', 'math', exp);
|
|
399
|
+
|
|
400
|
+
let ret;
|
|
401
|
+
try {
|
|
402
|
+
ret = func(this.const, this);
|
|
403
|
+
} catch (e) {
|
|
404
|
+
console.debug(e);
|
|
405
|
+
}
|
|
406
|
+
return ret;
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* 运行代码
|
|
411
|
+
* @param {String} express 表达式
|
|
412
|
+
* @return {Object} 执行结果
|
|
413
|
+
*/
|
|
414
|
+
Maths.prototype.runCode = function(express) {
|
|
415
|
+
let head = this.head();
|
|
416
|
+
let exp = head + this.updateTag(express);
|
|
417
|
+
let func = new Function('constant', 'math', 'vals', exp);
|
|
418
|
+
|
|
419
|
+
let ret;
|
|
420
|
+
try {
|
|
421
|
+
ret = func(this.const, this, this.vals);
|
|
422
|
+
} catch (e) {
|
|
423
|
+
console.debug(e);
|
|
424
|
+
//TODO handle the exception
|
|
425
|
+
}
|
|
426
|
+
return ret;
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
module.exports = Maths;
|
package/package.json
CHANGED
|
@@ -1,33 +1,42 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "mm_maths",
|
|
3
|
+
"version": "1.2.1",
|
|
4
|
+
"description": "这是一个可以将数学公式推导变形,并转成js和计算的模块。",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"index.js",
|
|
8
|
+
"lib",
|
|
9
|
+
"tool"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "node test.js"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://gitee.com/qiuwenwu91/mm_maths.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"mm_mod",
|
|
20
|
+
"mm_common",
|
|
21
|
+
"math",
|
|
22
|
+
"array",
|
|
23
|
+
"number",
|
|
24
|
+
"identities",
|
|
25
|
+
"expression",
|
|
26
|
+
"set",
|
|
27
|
+
"convert",
|
|
28
|
+
"symbol"
|
|
29
|
+
],
|
|
30
|
+
"author": "邱文武",
|
|
31
|
+
"license": "ISC",
|
|
32
|
+
"layer": "util",
|
|
33
|
+
"group": "no_io",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://gitee.com/qiuwenwu91/mm_maths/issues"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://gitee.com/qiuwenwu91/mm_maths#readme",
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"mm_files": "^1.4.0",
|
|
40
|
+
"mm_helper": "^1.2.0"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* math —— 数学公式求值与等式归一工具
|
|
4
|
+
* 复用模块 Maths 类:常量绑定、函数头构建与求值都由模块维护。
|
|
5
|
+
* 说明:模块实例会缓存函数头(head),故每次调用新建实例,避免不同常量互相影响。
|
|
6
|
+
*/
|
|
7
|
+
const { Maths } = require('../../index.js');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 新建公式实例
|
|
11
|
+
* @returns {object} Maths 实例
|
|
12
|
+
*/
|
|
13
|
+
function _new() {
|
|
14
|
+
let inst = new Maths();
|
|
15
|
+
if (typeof inst.init === 'function') {
|
|
16
|
+
try { inst.init(); } catch (_e) { void _e; }
|
|
17
|
+
}
|
|
18
|
+
return inst;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 归一数学写法为可计算的 JS 表达式
|
|
23
|
+
* @param {string} express 数学表达式
|
|
24
|
+
* @returns {string} JS 表达式
|
|
25
|
+
*/
|
|
26
|
+
function _toJs(express) {
|
|
27
|
+
return String(express)
|
|
28
|
+
.replace(/[×·]/g, '*')
|
|
29
|
+
.replace(/÷/g, '/')
|
|
30
|
+
.replace(/[−–—]/g, '-')
|
|
31
|
+
.replace(/(/g, '(')
|
|
32
|
+
.replace(/)/g, ')')
|
|
33
|
+
.replace(/π/g, 'Math.PI')
|
|
34
|
+
.replace(/\^/g, '**');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 执行数学公式求值或等式归一。
|
|
39
|
+
* @param {Object} params - 工具入参,含 action、formula、consts 等字段
|
|
40
|
+
* @param {Object} ctx - 工具上下文,结果写入 ctx.output
|
|
41
|
+
* @param {Object} [options] - 保留参数,当前未使用
|
|
42
|
+
* @returns {Promise<Object>} 含 code、msg 及结果字段的响应对象
|
|
43
|
+
*/
|
|
44
|
+
async function main(params, ctx, options) {
|
|
45
|
+
void options;
|
|
46
|
+
let p = params || {};
|
|
47
|
+
let action = p.action || 'run';
|
|
48
|
+
/**
|
|
49
|
+
* 写入输出并原样返回响应对象。
|
|
50
|
+
* @param {Object} data - 要写入的响应对象
|
|
51
|
+
* @returns {Object} 传入的响应对象
|
|
52
|
+
*/
|
|
53
|
+
const done = (data) => { ctx.output = data; return data; };
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
if (!p.formula) throw new TypeError('formula 不能为空');
|
|
57
|
+
let inst = _new();
|
|
58
|
+
|
|
59
|
+
if (action === 'run') {
|
|
60
|
+
let consts = p.consts && typeof p.consts === 'object' ? p.consts : {};
|
|
61
|
+
if (Object.keys(consts).length) inst.setConst(consts);
|
|
62
|
+
let expr = _toJs(p.formula).trim();
|
|
63
|
+
let body = /^return\b/.test(expr) ? expr : 'return (' + expr + ')';
|
|
64
|
+
let result = inst.runCode(body);
|
|
65
|
+
if (result === undefined) {
|
|
66
|
+
return done({ code: 10002, msg: '求值失败:表达式无法解析或引用了未声明的常量(可通过 consts 传值)', formula: p.formula });
|
|
67
|
+
}
|
|
68
|
+
return done({ code: 0, msg: '计算完成', formula: p.formula, result: result });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (action === 'normalize') {
|
|
72
|
+
let expression = inst.convertEqual(p.formula);
|
|
73
|
+
if (!expression) return done({ code: 10002, msg: '归一失败:未能识别等式结构', formula: p.formula });
|
|
74
|
+
return done({ code: 0, msg: '归一完成', formula: p.formula, expression: String(expression) });
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return done({ code: 10001, msg: '不支持的操作: ' + action });
|
|
78
|
+
} catch (error) {
|
|
79
|
+
return done({ code: 10000, msg: '执行失败:' + error.message, error: { name: error.name, message: error.message } });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
module.exports = { main };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "math",
|
|
3
|
+
"title": "数学公式求值与等式归一",
|
|
4
|
+
"description": "基于 mm_maths 的公式能力:对数学表达式求值(常量经 consts 绑定),或把等式归一为标准形式 X - (...) = 0。适用场景:算复合公式、核对公式口径、把业务等式整理成可比较形式。工作流提示:formula 用数学写法(如 3*x^2+1、a+b),常量名通过 consts 传入;模块的符号/恒等式函数库当前不可用,故不提供符号推导。",
|
|
5
|
+
"category": "数学计算",
|
|
6
|
+
"group": "algorithm",
|
|
7
|
+
"type": "function",
|
|
8
|
+
"main": "./index.js",
|
|
9
|
+
"params": {
|
|
10
|
+
"type": "object",
|
|
11
|
+
"properties": {
|
|
12
|
+
"action": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"description": "操作类型: run(公式求值)/normalize(等式归一)",
|
|
15
|
+
"enum": [
|
|
16
|
+
"run",
|
|
17
|
+
"normalize"
|
|
18
|
+
],
|
|
19
|
+
"default": "run"
|
|
20
|
+
},
|
|
21
|
+
"formula": {
|
|
22
|
+
"type": "string",
|
|
23
|
+
"description": "数学表达式或等式(run/normalize 必填)"
|
|
24
|
+
},
|
|
25
|
+
"consts": {
|
|
26
|
+
"type": "object",
|
|
27
|
+
"description": "常量取值(run 可选),如 { a: 10, b: 15 }"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"required": [
|
|
31
|
+
"action",
|
|
32
|
+
"formula"
|
|
33
|
+
]
|
|
34
|
+
},
|
|
35
|
+
"returns": {
|
|
36
|
+
"type": "object",
|
|
37
|
+
"properties": {
|
|
38
|
+
"code": {
|
|
39
|
+
"type": "number",
|
|
40
|
+
"description": "0 表示成功,1xxxx 表示业务/执行异常"
|
|
41
|
+
},
|
|
42
|
+
"msg": {
|
|
43
|
+
"type": "string",
|
|
44
|
+
"description": "结果描述"
|
|
45
|
+
},
|
|
46
|
+
"result": {
|
|
47
|
+
"description": "计算结果(run)"
|
|
48
|
+
},
|
|
49
|
+
"expression": {
|
|
50
|
+
"type": "string",
|
|
51
|
+
"description": "归一后的表达式(normalize)"
|
|
52
|
+
},
|
|
53
|
+
"error": {
|
|
54
|
+
"type": "object",
|
|
55
|
+
"description": "异常对象 { name, message }"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
"handler": "manager"
|
|
60
|
+
}
|
package/.gitattributes
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 公式的乘法格式化
|
|
3
|
-
* @param {String} expression
|
|
4
|
-
* @return {String} 返回格式化后的公式
|
|
5
|
-
*/
|
|
6
|
-
module.exports = function format_multiply(expression) {
|
|
7
|
-
var exp = expression.replace(/\^\-/g, '^@');
|
|
8
|
-
exp = exp.replace(/\*/g, ' *').replace(/\//g, ' /');
|
|
9
|
-
exp = exp.replace(/\^@/g, '^-');
|
|
10
|
-
if (/[a-zA-Z]/.test(exp)) {
|
|
11
|
-
var left = "";
|
|
12
|
-
var between = "";
|
|
13
|
-
var right = "";
|
|
14
|
-
var value = "";
|
|
15
|
-
var vals = exp.split(' ');
|
|
16
|
-
var len = vals.length;
|
|
17
|
-
for (var i = 0; i < len; i++) {
|
|
18
|
-
var val = vals[i];
|
|
19
|
-
if (/[a-zA-Z]/.test(val)) {
|
|
20
|
-
if (val.indexOf('/') === -1) {
|
|
21
|
-
left += ' ' + val;
|
|
22
|
-
} else {
|
|
23
|
-
between += ' ' + val;
|
|
24
|
-
}
|
|
25
|
-
} else {
|
|
26
|
-
var k = val.replace(/\+/g, '').replace(/\-/g, '').replace(/\*/g, '');
|
|
27
|
-
if (/^[0-9.]+$/.test(k)) {
|
|
28
|
-
value += ' ' + val;
|
|
29
|
-
} else {
|
|
30
|
-
right += ' ' + val;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
exp = left + ' *' + between + ' *' + right;
|
|
35
|
-
if (value) {
|
|
36
|
-
exp += ' *' + eval((1 + '*' + value).replace('* *', '*').replace('* /', '/'));
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return exp.replace('* *', '*').replace('* *', '*').replace('* /', '/').replace('* /', '/');
|
|
40
|
-
};
|