@@ -9,9 +9,6 @@ this.j$ = this.jStat = (function( Math, undefined ) {
9
9
var slice = Array . prototype . slice ,
10
10
toString = Object . prototype . toString ,
11
11
12
- // for code compression
13
- length = 'length' ,
14
-
15
12
// ascending/descending functions for sort
16
13
ascNum = function ( a , b ) { return a - b ; } ,
17
14
descNum = function ( a , b ) { return b - a ; } ,
@@ -44,7 +41,7 @@ jStat.fn = jStat.prototype = {
44
41
// if first argument is an array, must be vector or matrix
45
42
if ( isArray ( args [ 0 ] ) ) {
46
43
if ( isArray ( args [ 0 ] [ 0 ] ) ) {
47
- for ( var i = 0 ; i < args [ 0 ] [ length ] ; i ++ ) {
44
+ for ( var i = 0 ; i < args [ 0 ] . length ; i ++ ) {
48
45
this . push ( args [ 0 ] [ i ] ) ;
49
46
}
50
47
} else {
@@ -77,46 +74,46 @@ jStat.fn.init.prototype = jStat.fn;
77
74
jStat . extend = function ( obj ) {
78
75
var args = slice . call ( arguments ) ,
79
76
i = 1 , j ;
80
- if ( args [ length ] === 1 ) {
77
+ if ( args . length === 1 ) {
81
78
for ( i in obj ) {
82
79
jStat [ i ] = obj [ i ] ;
83
80
}
84
81
return this ;
85
82
}
86
- for ( ; i < args [ length ] ; i ++ ) {
83
+ for ( ; i < args . length ; i ++ ) {
87
84
for ( j in args [ i ] ) obj [ j ] = args [ i ] [ j ] ;
88
85
}
89
86
return obj ;
90
87
} ;
91
88
92
89
// extend jStat.fn with methods which don't require arguments and work on columns
93
90
( function ( funcs ) {
94
- for ( var i = 0 ; i < funcs [ length ] ; i ++ ) ( function ( passfunc ) {
91
+ for ( var i = 0 ; i < funcs . length ; i ++ ) ( function ( passfunc ) {
95
92
96
93
// if a matrix is passed, automatically assume operation should be done on the columns
97
94
jStat . fn [ passfunc ] = function ( fullbool ) {
98
95
var arr = [ ] ,
99
96
i = 0 ,
100
97
tmpthis ;
101
98
102
- if ( this [ length ] > 1 ) {
99
+ if ( this . length > 1 ) {
103
100
tmpthis = this . transpose ( ) ;
104
- for ( ; i < tmpthis [ length ] ; i ++ ) {
101
+ for ( ; i < tmpthis . length ; i ++ ) {
105
102
arr [ i ] = jStat [ passfunc ] ( tmpthis [ i ] ) ;
106
103
}
107
104
if ( fullbool === true ) {
108
105
arr = jStat [ passfunc ] ( arr ) ;
109
106
}
110
107
}
111
108
112
- return arr [ length ] > 1 ? jStat ( arr ) : jStat [ passfunc ] ( this [ 0 ] ) ;
109
+ return arr . length > 1 ? jStat ( arr ) : jStat [ passfunc ] ( this [ 0 ] ) ;
113
110
} ;
114
111
} ) ( funcs [ i ] ) ;
115
112
} ) ( 'sum min max mean median mode range variance stdev meandev meddev quartiles' . split ( ' ' ) ) ;
116
113
117
114
// extend jStat.fn with methods that have no argument
118
115
( function ( funcs ) {
119
- for ( var i = 0 ; i < funcs [ length ] ; i ++ ) ( function ( passfunc ) {
116
+ for ( var i = 0 ; i < funcs . length ; i ++ ) ( function ( passfunc ) {
120
117
jStat . fn [ passfunc ] = function ( ) {
121
118
var results = jStat [ passfunc ] ( this ) ;
122
119
return isArray ( results ) ? jStat ( results ) : results ;
@@ -126,7 +123,7 @@ jStat.extend = function( obj ) {
126
123
127
124
// extend jStat.fn with methods that require one argument
128
125
( function ( funcs ) {
129
- for ( var i = 0 ; i < funcs [ length ] ; i ++ ) ( function ( passfunc ) {
126
+ for ( var i = 0 ; i < funcs . length ; i ++ ) ( function ( passfunc ) {
130
127
jStat . fn [ passfunc ] = function ( arg ) {
131
128
return jStat ( jStat [ passfunc ] ( this , arg ) ) ;
132
129
} ;
@@ -138,12 +135,12 @@ jStat.extend( jStat.fn, {
138
135
139
136
// Returns the number of rows in the matrix
140
137
rows : function ( ) {
141
- return this [ length ] || 1 ;
138
+ return this . length || 1 ;
142
139
} ,
143
140
144
141
// Returns the number of columns in the matrix
145
142
cols : function ( ) {
146
- return this [ 0 ] [ length ] || 1 ;
143
+ return this [ 0 ] . length || 1 ;
147
144
} ,
148
145
149
146
// Returns the dimensions of the object { rows: i, cols: j }
@@ -163,7 +160,7 @@ jStat.extend( jStat.fn, {
163
160
col : function ( index ) {
164
161
var column = [ ] ,
165
162
i = 0 ;
166
- for ( ; i < this [ length ] ; i ++ ) {
163
+ for ( ; i < this . length ; i ++ ) {
167
164
column [ i ] = [ this [ i ] [ index ] ] ;
168
165
}
169
166
return jStat ( column ) ;
@@ -211,8 +208,8 @@ jStat.extend({
211
208
// transpose a matrix or array
212
209
transpose : function ( arr ) {
213
210
arr = isArray ( arr [ 0 ] ) ? arr : [ arr ] ;
214
- var rows = arr [ length ] ,
215
- cols = arr [ 0 ] [ length ] ,
211
+ var rows = arr . length ,
212
+ cols = arr [ 0 ] . length ,
216
213
obj = [ ] ,
217
214
i = 0 , j ;
218
215
for ( ; i < cols ; i ++ ) {
@@ -228,8 +225,8 @@ jStat.extend({
228
225
map : function ( arr , func , toAlter ) {
229
226
arr = isArray ( arr [ 0 ] ) ? arr : [ arr ] ;
230
227
var row = 0 ,
231
- nrow = arr [ length ] ,
232
- ncol = arr [ 0 ] [ length ] ,
228
+ nrow = arr . length ,
229
+ ncol = arr [ 0 ] . length ,
233
230
res = toAlter ? arr : [ ] ,
234
231
col ;
235
232
for ( ; row < nrow ; row ++ ) {
@@ -311,9 +308,9 @@ jStat.extend({
311
308
// matrix multiplication
312
309
multiply : function ( arr , arg ) {
313
310
var row , col , nrescols , sum ,
314
- nrow = arr [ length ] ,
315
- ncol = arr [ 0 ] [ length ] ,
316
- res = jStat . zeros ( nrow , nrescols = ( isNaN ( arg ) ) ? arg [ 0 ] [ length ] : ncol ) ,
311
+ nrow = arr . length ,
312
+ ncol = arr [ 0 ] . length ,
313
+ res = jStat . zeros ( nrow , nrescols = ( isNaN ( arg ) ) ? arg [ 0 ] . length : ncol ) ,
317
314
rescols = 0 ;
318
315
if ( isNaN ( arg ) ) {
319
316
for ( ; rescols < nrescols ; rescols ++ ) {
@@ -343,12 +340,12 @@ jStat.extend({
343
340
arg = isArray ( arg [ 0 ] ) ? arg : [ arg ] ;
344
341
345
342
// convert column to row vector
346
- var left = ( arr [ 0 ] [ length ] === 1 && arr [ length ] !== 1 ) ? jStat . transpose ( arr ) : arr ,
347
- right = ( arg [ 0 ] [ length ] === 1 && arg [ length ] !== 1 ) ? jStat . transpose ( arg ) : arg ,
343
+ var left = ( arr [ 0 ] . length === 1 && arr [ length ] !== 1 ) ? jStat . transpose ( arr ) : arr ,
344
+ right = ( arg [ 0 ] . length === 1 && arg [ length ] !== 1 ) ? jStat . transpose ( arg ) : arg ,
348
345
res = [ ] ,
349
346
row = 0 ,
350
- nrow = left [ length ] ,
351
- ncol = left [ 0 ] [ length ] ,
347
+ nrow = left . length ,
348
+ ncol = left [ 0 ] . length ,
352
349
sum , col ;
353
350
for ( ; row < nrow ; row ++ ) {
354
351
res [ row ] = [ ] ;
@@ -358,7 +355,7 @@ jStat.extend({
358
355
}
359
356
res [ row ] = sum ;
360
357
}
361
- return ( res [ length ] === 1 ) ? res [ 0 ] : res ;
358
+ return ( res . length === 1 ) ? res [ 0 ] : res ;
362
359
} ,
363
360
364
361
// raise every element by a scalar or vector
@@ -380,7 +377,7 @@ jStat.extend({
380
377
// computes the norm of the vector
381
378
norm : function ( arr ) {
382
379
arr = isArray ( arr [ 0 ] ) ? arr : [ arr ] ;
383
- if ( arr [ length ] > 1 && arr [ 0 ] [ length ] > 1 ) {
380
+ if ( arr . length > 1 && arr [ 0 ] [ length ] > 1 ) {
384
381
// matrix norm
385
382
} else {
386
383
// vector norm
@@ -398,8 +395,8 @@ jStat.extend({
398
395
symmetric : function ( arr ) {
399
396
var issymmetric = true ,
400
397
row = 0 ,
401
- size = arr [ length ] , col ;
402
- if ( arr [ length ] !== arr [ 0 ] [ length ] ) return false ;
398
+ size = arr . length , col ;
399
+ if ( arr . length !== arr [ 0 ] [ length ] ) return false ;
403
400
for ( ; row < size ; row ++ ) {
404
401
for ( col = 0 ; col < size ; col ++ ) {
405
402
if ( arr [ col ] [ row ] !== arr [ row ] [ col ] ) return false ;
@@ -413,7 +410,7 @@ jStat.extend({
413
410
// sum of an array
414
411
sum : function ( arr ) {
415
412
var sum = 0 ,
416
- i = arr [ length ] ;
413
+ i = arr . length ;
417
414
while ( -- i >= 0 ) {
418
415
sum += arr [ i ] ;
419
416
}
@@ -432,12 +429,12 @@ jStat.extend({
432
429
433
430
// mean value of an array
434
431
mean : function ( arr ) {
435
- return jStat . sum ( arr ) / arr [ length ] ;
432
+ return jStat . sum ( arr ) / arr . length ;
436
433
} ,
437
434
438
435
// median of an array
439
436
median : function ( arr ) {
440
- var arrlen = arr [ length ] ,
437
+ var arrlen = arr . length ,
441
438
_arr = arr . slice ( ) . sort ( ascNum ) ;
442
439
443
440
// check if array is even or odd, then return the appropriate
@@ -446,7 +443,7 @@ jStat.extend({
446
443
447
444
// mode of an array
448
445
mode : function ( arr ) {
449
- var arrLen = arr [ length ] ,
446
+ var arrLen = arr . length ,
450
447
_arr = arr . slice ( ) . sort ( ascNum ) ,
451
448
count = 1 ,
452
449
maxCount = 0 ,
@@ -482,18 +479,18 @@ jStat.extend({
482
479
// range of an array
483
480
range : function ( arr ) {
484
481
var _arr = arr . slice ( ) . sort ( ascNum ) ;
485
- return _arr [ _arr [ length ] - 1 ] - _arr [ 0 ] ;
482
+ return _arr [ _arr . length - 1 ] - _arr [ 0 ] ;
486
483
} ,
487
484
488
485
// variance of an array
489
486
variance : function ( arr ) {
490
487
var mean = jStat . mean ( arr ) ,
491
488
stSum = 0 ,
492
- i = arr [ length ] - 1 ;
489
+ i = arr . length - 1 ;
493
490
for ( ; i >= 0 ; i -- ) {
494
491
stSum += Math . pow ( ( arr [ i ] - mean ) , 2 ) ;
495
492
}
496
- return stSum / ( arr [ length ] - 1 ) ;
493
+ return stSum / ( arr . length - 1 ) ;
497
494
} ,
498
495
499
496
// standard deviation of an array
@@ -505,27 +502,27 @@ jStat.extend({
505
502
meandev : function ( arr ) {
506
503
var devSum = 0 ,
507
504
mean = jStat . mean ( arr ) ,
508
- i = arr [ length ] - 1 ;
505
+ i = arr . length - 1 ;
509
506
for ( ; i >= 0 ; i -- ) {
510
507
devSum += Math . abs ( arr [ i ] - mean ) ;
511
508
}
512
- return devSum / arr [ length ] ;
509
+ return devSum / arr . length ;
513
510
} ,
514
511
515
512
// median deviation (median absolute deviation) of an array
516
513
meddev : function ( arr ) {
517
514
var devSum = 0 ,
518
515
median = jStat . median ( arr ) ,
519
- i = arr [ length ] - 1 ;
516
+ i = arr . length - 1 ;
520
517
for ( ; i >= 0 ; i -- ) {
521
518
devSum += Math . abs ( arr [ i ] - median ) ;
522
519
}
523
- return devSum / arr [ length ] ;
520
+ return devSum / arr . length ;
524
521
} ,
525
522
526
523
// quartiles of an array
527
524
quartiles : function ( arr ) {
528
- var arrlen = arr [ length ] ,
525
+ var arrlen = arr . length ,
529
526
_arr = arr . slice ( ) . sort ( ascNum ) ;
530
527
return [ _arr [ Math . round ( ( arrlen ) / 4 ) - 1 ] , _arr [ Math . round ( ( arrlen ) / 2 ) - 1 ] , _arr [ Math . round ( ( arrlen ) * 3 / 4 ) - 1 ] ] ;
531
528
} ,
@@ -535,7 +532,7 @@ jStat.extend({
535
532
var u = jStat . mean ( arr1 ) ,
536
533
v = jStat . mean ( arr2 ) ,
537
534
sq_dev = [ ] ,
538
- arr1Len = arr1 [ length ] ,
535
+ arr1Len = arr1 . length ,
539
536
i = 0 ;
540
537
for ( ; i < arr1Len ; i ++ ) {
541
538
sq_dev [ i ] = ( arr1 [ i ] - u ) * ( arr2 [ i ] - v ) ;
0 commit comments