UNPKG

4.82 kBJavaScriptView Raw
1var _ = require("lodash");
2var R = require("./lib/statistics");
3var M = require("./lib/matrix");
4var Symbol = require("./lib/symbolic");
5var D = require("./lib/calculus");
6R.NN = require("./plugin/neural");
7R.NN.RBM = require("./plugin/neural/rbm");
8
9R.Matrix = R.M = M;
10R._ = _;
11R.S = R.Sym = R.Symbol = Symbol;
12
13R.PI = Math.PI;
14R.E = Math.E;
15
16// space 沒有加上機率參數 , 不能指定機率
17R.samples = function(space, size, arg) {
18 var arg = _.defaults(arg, {replace:true});
19 if (arg.replace)
20 return _.times(size, ()=>_.sample(space));
21 else
22 return _.sampleSize(space, size);
23}
24
25// Graph
26
27R.G = G = {}
28
29G.curve=function(f, from=-10, to=10, step=0.1) {
30 var x=R.steps(from, to, step);
31 var y=x.map(f);
32 return { type:"curve", x:x, y:y };
33}
34
35G.hist=function(a, from, to, step=1) {
36 from = from||a.min();
37 to = to||a.max();
38 var n = Math.ceil((to-from+R.EPSILON)/step);
39 var xc = R.steps(from+step/2.0, to, step);
40 var bins = R.M.newV(n, 0);
41 for (var i in a) {
42 var slot=Math.floor((a[i]-from)/step);
43 if (slot>=0 && slot < n)
44 bins[slot]++;
45 }
46 return { type:'histogram', xc:xc, bins:bins, from:from, to:to, step:step};
47}
48
49G.ihist=function(a) {
50 return G.hist(a, a.min()-0.5, a.max()+0.5, 1);
51}
52
53// Global
54debug = function() {
55 var arg = _.slice(arguments);
56 console.debug.apply(console, arg);
57}
58
59print = function() {
60 var arg = _.slice(arguments);
61 console.log.apply(console, arg);
62}
63
64R.debug = debug;
65R.print = print;
66
67// ==== Copy functions to R ======
68R.copyFunctions(R, D, "differential,integral".split(","));
69R.copyFunctions(R, Math, "abs,acos,asin,atan,ceil,cos,exp,floor,log,pow,random,round,sin,sqrt,tan".split(","));
70
71R.copyFunctions(R, M, "solveLP,solveMP,ODE,minimize,complex,spline,linspace".split(","));
72
73
74// not include : bench, xxxeq, ccsXXX, cgrid,
75R.mixThis(Array.prototype, M, [
76"cLU",
77"cdelsq",
78"clone",
79"rows",
80"cols",
81"row",
82"col",
83"tr",
84// "str",
85"not",
86"bnot",
87"neg",
88"abs",
89"sin",
90"cos",
91"tan",
92"asin",
93"acos",
94"atan",
95"atan2",
96"inv",
97"all",
98"any",
99"same",
100"isFinite",
101"isNaN",
102"sqrt",
103"ceil",
104"floor",
105"round",
106"log",
107"exp",
108"pow",
109"mapreduce",
110"lshifteq",
111"rshifteq",
112"add",
113"sub",
114"mul",
115"div",
116"mod",
117"and",
118"or",
119"xor",
120"band",
121"bor",
122"bxor",
123"eq",
124"neq",
125"geq",
126"leq",
127"lt",
128"gt",
129"complex",
130"det",
131"norm2",
132"norm2Squared",
133"norm2inf",
134"dot",
135"dim",
136"eig",
137"LU",
138"svd",
139"sum",
140"rowSum",
141"colSum",
142"rowMean",
143"colMean",
144"addMV",
145"mapM",
146"mapMM",
147"flatM",
148"fillVM",
149"fillMM",
150"getBlock",
151"setBlock",
152"getDiag",
153"diag",
154"parseFloat",
155"parseDate",
156"parseCSV",
157"toCSV",
158"strM",
159"sumM",
160]);
161
162// not include : bench, xxxeq, ccsXXX, cgrid,
163R.mixThis(Array.prototype, G, [
164"hist",
165"ihist",
166]);
167
168R.mixThis(Array.prototype, R, [
169// statistics
170"max",
171"min",
172// "sum",
173"product",
174"mean",
175"range",
176"median",
177"variance",
178"deviation",
179"sd",
180"cov",
181"cor",
182"normalize",
183]);
184
185R.mixThis(Array.prototype, _, [
186// lodash
187"chunk",
188"compact",
189// concat:_.concat
190"difference",
191"differenceBy",
192"differenceWith",
193"drop",
194"dropRight",
195"dropRightWhile",
196"dropWhile",
197// fill:_.fill,
198// findIndex:_.findIndex,
199"findLastIndex",
200"flatten",
201"flattenDeep",
202"flattenDepth",
203"fromPairs",
204"head",
205// indexOf:_.indexOf,
206"initial",
207"intersection",
208"intersectionBy",
209"intersectionWith",
210// _.join
211"last",
212// _.lastIndexOf
213"nth",
214"pull",
215"pullAll",
216"pullAllBy",
217"pullAllWith",
218"pullAt",
219"remove",
220// _.reverse
221// _.slice
222"sortedIndex",
223"sortedIndexBy",
224"sortedIndexOf",
225"sortedLastIndex",
226"sortedLastIndexBy",
227"sortedLastIndexOf",
228"sortedUniq",
229"sortedUniqBy",
230"tail",
231"take",
232"takeRight",
233"takeRightWhile",
234"takeWhile",
235"union",
236"unionBy",
237"unionWith",
238"uniq",
239"uniqBy",
240"uniqWith",
241"unzip",
242"unzipWith",
243"without",
244// _.xor
245// _.xorBy
246// _.xorWith
247"zip",
248"zipObject",
249"zipObjectDeep",
250"zipWith",
251// Collection
252"countBy",
253// _.each → forEach
254// _.eachRight → forEachRight
255// every:_.every
256// filter:_.filter
257// find:_.find
258"findLast",
259"flatMap",
260"flatMapDeep",
261"flatMapDepth",
262// _.forEach
263"forEachRight",
264"groupBy",
265// includes:_.includes
266"invokeMap",
267"keyBy",
268// _.map
269"orderBy",
270"partition",
271// _.reduce
272// reduceRight:_.reduceRight,
273"reject",
274"sample",
275"sampleSize",
276"shuffle",
277"size",
278// some:_.some
279"sortBy",
280]);
281
282// R.mixThis(Array.prototype, {str:R.astr}, ['str']);
283R.mixThisMap(Array.prototype, R, {astr:'str',print:'print'});
284R.mixThisMap(Number.prototype, R, {nstr:'str',print:'print'});
285R.mixThisMap(String.prototype, R, {sstr:'str',print:'print'});
286R.mixThisMap(Object.prototype, R, {ostr:'str',print:'print'});
287R.mixThisMap(Object.prototype, M, {strM:'strM'});
288
289module.exports = R;
290
291