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