UNPKG

10 kBMarkdownView Raw
1rlab -- A JavaScript Scientific Library like R
2
3## Introduction
4
5The rlab is a A JavaScript Scientific Library like R.
6
7It's based on `lodash.js , jStat.js and numeric.js`
8
9## Install
10
11```
12npm install rlab
13```
14
15## Use rlab in console mode
16
17file : probabilityEx.js
18
19```javascript
20var R = require("rlab");
21var dice = R.steps(1,6);
22log("sample(1:6, 10)", R.samples(dice, 10));
23log("runif(10,0,1)=", R.runif(10, 0, 1).str());
24log("rnorm(10,5,1)=", R.rnorm(10, 5, 1).str());
25log("dnorm(5,5,1)=", R.dnorm(5, 5, 1));
26log("pnorm(5,5,1)=", R.pnorm(5, 5, 1));
27log("qnorm(0.5,5,1)=", R.qnorm(0.5, 5, 1));
28log("rbinom(10, 5, 0.5)=", R.rbinom(10,5,0.5));
29log("dbinom(4, 5, 0.5)=", R.dbinom(4,5,0.5));
30log("dbinom(5, 5, 0.5)=", R.dbinom(5,5,0.5));
31log("pbinom(4, 5, 0.5)=", R.pbinom(4,5,0.5));
32log("qbinom(0.9, 5, 0.5)=", R.qbinom(0.9,5,0.5));
33
34```
35
36run :
37
38```
39$ node probabilityEx.js
40sample(1:6, 10) [ 3, 5, 3, 2, 3, 3, 1, 2, 4, 3 ]
41runif(10,0,1)= [0.9119,0.5899,0.6839,0.1350,0.6894,0.9512,0.8186,0.5826,0.4279,0
42.5125]
43rnorm(10,5,1)= [5.8961,5.4312,6.0002,5.3623,5.5281,4.4413,6.2144,5.7173,5.3111,1
44.3146]
45dnorm(5,5,1)= 0.3989422804014327
46pnorm(5,5,1)= 0.5
47qnorm(0.5,5,1)= 5
48rbinom(10, 5, 0.5)= [ 2, 1, 2, 2, 4, 4, 1, 4, 3, 2 ]
49dbinom(4, 5, 0.5)= 0.15625
50dbinom(5, 5, 0.5)= 0.03125
51pbinom(4, 5, 0.5)= 0.96875
52qbinom(0.9, 5, 0.5)= 4
53```
54
55file : statisticsEx.js
56
57```javascript
58var R = require("rlab");
59var v = [1,3,5];
60log("v.max()=", v.max());
61log("v.min()=", v.min());
62log("v.sum()=", v.sum());
63log("v.normalize()=", v.normalize());
64log("v.normalize().sum()=", v.normalize().sum());
65log("v.product()=", v.product());
66log("v.mean()=", v.mean());
67log("v.range()=", v.range());
68log("v.median()=", v.median());
69log("v.variance()=", v.variance());
70log("v.sd()=", v.sd(), " sd^2=", v.sd()*v.sd());
71log("v.cov(v)=", v.cov(v), "v.cor(v)=", v.cor(v));
72log("factorial(5)=", R.factorial(5));
73```
74
75run :
76
77```
78$ node statisticsEx.js
79v.max()= 5
80v.min()= 1
81v.sum()= 9
82v.normalize()= [ 0.1111111111111111, 0.3333333333333333, 0.5555555555555556 ]
83v.normalize().sum()= 1
84v.product()= 15
85v.mean()= 1
86v.range()= 4
87v.median()= 3
88v.variance()= 2.6666666666666665
89v.sd()= 1.632993161855452 sd^2= 2.6666666666666665
90v.cov(v)= 4 v.cor(v)= 1
91factorial(5)= 120
92```
93
94file : testEx.js
95
96```javascript
97var R = require("../rlab");
98var v = [1,3,5];
99
100var x = R.rnorm(10, 0, 0.1);
101print("x=", x.str());
102
103var t1=R.ttest({x:x, mu:0});
104R.report(t1);
105```
106
107run :
108
109```
110$ node testEx.js
111x= [-0.1117,-0.1211,0.0382,-0.1902,0.0124,0.0949,0.2326,0.1315,-0.0261,0.0874]
112
113=========== report ==========
114name : ttest(X)
115h : H0:mu=0
116alpha : 0.0500
117op : =
118pvalue : 0.7129
119ci : [-0.0733,0.1028]
120df : 9.0000
121mean : 0.0148
122sd : 0.1231
123```
124
125file : matrixEx.js
126
127```javascript
128var M = require("rlab").M;
129var v = [1,2,3];
130log("v.sin()=", v.sin());
131log("v.norm2()=", v.norm2());
132log("v.norm2Squared()=", v.norm2Squared());
133
134var A = [[1,2,3],[4,5,6],[7,3,9]];
135var AiA = A.inv().dot(A);
136log("AiA=\n", AiA.strM());
137log("AiA.tr()=\n", AiA.tr().strM());
138log("A=\n", A.str());
139log("A.mul(0.1)=\n", A.mul(0.1).strM());
140log("A.row(1)=", A.row(1));
141log("A.col(1)=", A.col(1));
142log("A.sumM()=", A.sumM());
143log("A.rowSum()=", A.rowSum());
144log("A.colSum()=", A.colSum());
145log("A.mean(row)=", A.rowMean().str());
146log("A.mean(col)=", A.colMean().str());
147
148var D = M.diag(v);
149log("D=", D);
150
151var Eλ = M.eigR(A);
152var E = Eλ.E, λ=Eλ.lambda;
153log("E*[λ]*E-1=", E.dot(λ.diag()).dot(E.inv()).strM());
154```
155
156run :
157
158```
159$ node matrixEx.js
160v.sin()= [ 0.8414709848078965, 0.9092974268256817, 0.1411200080598672 ]
161v.norm2()= 3.7416573867739413
162v.norm2Squared()= 14
163AiA=
164 [[ 1, 1.11e-16, -1.11e-16],
165 [ 0, 1, 4.441e-16],
166 [ -3.331e-16, -3.331e-16, 1]]
167AiA.tr()=
168 [[ 1, 0, -3.331e-16],
169 [ 1.11e-16, 1, -3.331e-16],
170 [ -1.11e-16, 4.441e-16, 1]]
171A=
172 [[1.0000,2.0000,3.0000],[4.0000,5.0000,6.0000],[7.0000,3.0000,9.0000]]
173A.mul(0.1)=
174 [[ 0.1, 0.2, 0.3],
175 [ 0.4, 0.5, 0.6],
176 [ 0.7, 0.3, 0.9]]
177A.row(1)= [ 4, 5, 6 ]
178A.col(1)= [ 2, 5, 3 ]
179A.sumM()= 40
180A.rowSum(2)= [ 6, 15, 19 ]
181A.colSum(2)= [ 12, 10, 18 ]
182A.mean(row)= [2.0000,5.0000,6.3333]
183A.mean(col)= [4.0000,3.3333,6.0000]
184D= [ [ 1, 0, 0 ], [ 0, 2, 0 ], [ 0, 0, 3 ] ]
185E*[λ]*E-1= [[ 1, 2, 3],
186 [ 4, 5, 6],
187 [ 7, 3, 9]]
188```
189
190file : differentialEx.js
191
192```javascript
193var R = require("rlab");
194
195var d = R.D.d, i=R.D.i, sin=R.sin, PI = R.PI, x2=(x)=>x*x;
196
197log('d(x^2,2)=', d(x2, 2));
198log('d(sin(x/4),pi/4)=', d(sin, PI/4));
199log('i(x^2,0,1)=', i(x2,0,1));
200log('i(sin(x),0,pi/2)=', i(sin,0,PI/2));
201
202```
203
204run :
205
206```
207D:\Dropbox\github\rlab\example>node differentialEx.js
208d(x^2,2)= 4.000999999999699
209d(sin(x/4),pi/4)= 0.7067531099743674
210i(x^2,0,1)= 0.33283350000000095
211i(sin(x),0,pi/2)= 0.9997035898637557
212```
213
214file: symbolEx.js
215
216```javascript
217var R = require("../rlab");
218var S = R.Symbol;
219
220print('x+x=', S.run('x + x')) // => 2 x"
221
222print('10!=', S.factor('10!').toString()); // => "2^8 3^4 5^2 7"
223
224print('integral(x^2)=', S.eval('integral(x^2)').toString()); // => "1/3 x^3"
225
226// composing...
227print('integral(x)=', S.integral(S.eval('x')).toString()); // => "1/2 x^2"
228
229var questions=[
230'13579/99999 + 13580/100000',
231'numerator(1/a+1/b)',
232'denominator(1/(x-1)/(x-2))',
233'rationalize(a/b+b/a)',
234'A=1+i;B=sqrt(2)*exp(i*pi/4);A-B',
235'simplify(cos(x)^2 + sin(x)^2)',
236'simplify(a*b+a*c)',
237'simplify(n!/(n+1)!)',
238'(x-1)(x-2)^3',
239'subst( u, exp(x), 2*exp(x) )',
240'roots(3 x + 12 + y = 24)',
241'roots(a*x^2+b*x+c)',
242'roots(x^4 + x^3 + x^2 + x + 1)',
243'roots(m*x^9 + n)',
244'roots((x^4+x^3)*(x^4*x^2))',
245'nroots(x^4+1)',
246'velocity=17000*"mile"/"hr";time=8*"min"/(60*"min"/"hr");velocity/time',
247'A=((a,b),(c,d));inv(A);adj(A);det(A);inv(A)-adj(A)/det(A)',
248'd(x^2);r=sqrt(x^2+y^2);d(r,(x,y))',
249'F=(x+2y,3x+4y);d(F,(x,y))',
250'integral(x^2)',
251'integral(x*y,x,y)',
252// 'defint(x^2,y,0,sqrt(1-x^2),x,-1,1)', // very slow, why ?
253// 'f=sin(t)^4-2*cos(t/2)^3*sin(t);f=circexp(f);defint(f,t,0,2*pi)', // very slow, why ?
254];
255
256print("=========== Q&A =============");
257
258for (var i in questions) {
259 var q = questions[i];
260 print(q, "=", S.run(q.replace(/;/g, '\n')));
261}
262```
263
264run :
265
266```
267D:\js\rlab\example>node symbolEx.js
268x+x= 2 x
26910!= 2^8 3^4 5^2 7
270integral(x^2)= 1/3 x^3
271integral(x)= 1/2 x^2
272=========== Q&A =============
27313579/99999 + 13580/100000 = 135794321/499995000
274numerator(1/a+1/b) = a + b
275denominator(1/(x-1)/(x-2)) = x^2 - 3 x + 2
276rationalize(a/b+b/a) = (a^2 + b^2) / (a b)
277A=1+i;B=sqrt(2)*exp(i*pi/4);A-B = 1 + i - 2^(1/2) exp(1/4 i pi)
278simplify(cos(x)^2 + sin(x)^2) = 1
279simplify(a*b+a*c) = a (b + c)
280simplify(n!/(n+1)!) = 1 / (1 + n)
281(x-1)(x-2)^3 = x^4 - 7 x^3 + 18 x^2 - 20 x + 8
282subst( u, exp(x), 2*exp(x) ) = 2 u
283roots(3 x + 12 + y = 24) = -1/3 y + 4
284roots(a*x^2+b*x+c) = (-b / (2 a) - (-4 a c + b^2)^(1/2) / (2 a),-b / (2 a) + (-4
285 a c + b^2)^(1/2) / (2 a))
286roots(x^4 + x^3 + x^2 + x + 1) = Stop: roots: the polynomial is not factorable,
287try nroots
288roots(m*x^9 + n) = Stop: roots: the polynomial is not factorable, try nroots
289roots((x^4+x^3)*(x^4*x^2)) = (-1,0)
290nroots(x^4+1) = (-0.707107 - 0.707107 i,-0.707107 + 0.707107 i,0.707107 + 0.7071
29107 i,0.707107 - 0.707107 i)
292velocity=17000*"mile"/"hr";time=8*"min"/(60*"min"/"hr");velocity/time = 127500 "
293mile" / ("hr"^2)
294A=((a,b),(c,d));inv(A);adj(A);det(A);inv(A)-adj(A)/det(A) = ((d / (a d - b c),-b
295 / (a d - b c)),(-c / (a d - b c),a / (a d - b c)))
296((d,-b),(-c,a))
297a d - b c
298((0,0),(0,0))
299d(x^2);r=sqrt(x^2+y^2);d(r,(x,y)) = 2 x
300(x / ((x^2 + y^2)^(1/2)),y / ((x^2 + y^2)^(1/2)))
301F=(x+2y,3x+4y);d(F,(x,y)) = ((1,2),(3,4))
302integral(x^2) = 1/3 x^3
303integral(x*y,x,y) = 1/4 x^2 y^2
304```
305
306## Run rlab on Web
307
308Fdbserver is a used in the server.js of rlab. You have to install fdbserver before start the server.js
309
310```
311$ git clone https://github.com/ccckmit/rlab
312...
313$ cd rlab
314...
315$ npm install fdbserver
316rlab@0.7.1 D:\Dropbox\github\rlab
317+-- chai@3.5.0 extraneous
318+-- express@4.13.4 extraneous
319`-- fdbserver@1.6.6 extraneous
320
321$ node server.js
322```
323
324A demo for rlab is on my web site <http://ccc.nqu.edu.tw/rlab/rlab.html> .
325
326The following figure is Screen Shot for Rlab GUI.
327
328![Run rlab on Web](img/RlabIDE.png)
329
330If you visit the site in https protocol, there will be a Button for 'Save File', and menus for 'Login' and 'Logout'. These function can only be used in the https mode, not in the http mode.
331
332You have to login to save code. The default code path is in 'file/' directory with the file name you choose to upload.
333
334## Rebuild Web Version
335
336You have to wrap rlab for web by browserify in the following command.
337
338```
339$npm run-script build-web
340
341> rlab@0.5.4 build-web D:\Dropbox\github\rlab
342> browserify web/_rlab.js -o web/rlab.js
343```
344
345When you modify the rlab source, make sure to wrap it again.
346
347
348## Author
349
350Author: ccckmit
351
352Email : ccckmit@gmail.com
353
354## License
355
356The rlab project is licensed in MIT license.
357
358Copyright (c) 2013 rlab
359
360Permission is hereby granted, free of charge, to any person obtaining a copy
361of this software and associated documentation files (the "Software"), to deal
362in the Software without restriction, including without limitation the rights
363to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
364copies of the Software, and to permit persons to whom the Software is
365furnished to do so, subject to the following conditions:
366
367The above copyright notice and this permission notice shall be included in
368all copies or substantial portions of the Software.
369
370THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
371IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
372FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
373AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
374LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
375OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
376THE SOFTWARE.
377
378
379