Skip to content

Commit 5210458

Browse files
committed
adding examples to methods and to the overview.
1 parent cbcd452 commit 5210458

File tree

2 files changed

+147
-67
lines changed

2 files changed

+147
-67
lines changed

doc/md/core.md

Lines changed: 107 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,136 @@
1-
## Core Functionality
1+
## Core Instance Functionality
22

33
### jStat( [array][start, stop, count[, func]] )
44

55
Create a new jStat object from either an existing array, or pass in values to generate a sequence.
6+
For example, create a new jStat matrix by doing the following:
7+
8+
var matrix = jStat([[1,2,3],[4,5,6],[7,8,9]]);
9+
10+
Or create a vector from a sequence:
11+
12+
var vector = jStat( 0, 1, 5 );
13+
// vector === [[ 0, 0.25, 0.5, 0.75, 1 ]]
14+
15+
By passing a function the sequence value can be manipulated:
16+
17+
var vector = jStat( 0, 1, 5, function( x ) {
18+
return x * 2;
19+
});
20+
// vector === [[ 0, 0.5, 1, 1.5, 2 ]];
621

722
### rows()
823

924
Returns the number of rows in the jStat object.
1025

26+
jStat([[1,2,3],[4,5,6]]).rows() === 2;
27+
1128
### cols()
1229

1330
Returns the number of columns in the jStat object.
1431

32+
jStat([[1,2,3],[4,5,6]]).cols() === 3;
33+
1534
### dimensions()
1635

1736
Returns and object with the dimensions of the jStat object.
1837

38+
jStat([[1,2,3],[4,5,6]]).dimensions() === { cols: 3, rows: 2 };
39+
1940
### row( index )
2041

2142
Returns a specified row as a vector.
2243

44+
jStat([[1,2,3],[4,5,6]]).row( 0 ) === [[1,2,3]];
45+
2346
### col( index )
2447

25-
Returns the specified column as a vector.
48+
Returns the specified column as a column vector.
49+
50+
jStat([1,2,3],[4,5,6]]).col( 0 ) === [[1],[4]];
2651

2752
### diag()
2853

2954
Returns the diagonal of the matrix.
3055

56+
jStat([[1,2,3],[4,5,6],[7,8,9]]).diag() === [[1],[5],[9]];
57+
3158
### antidiag()
3259

3360
Returns the anti-diagonal of the matrix.
3461

35-
### transpose( arr )
62+
jStat([[1,2,3],[4,5,6],[7,8,9]]).antidiag() === [[3],[5],[7]];
3663

37-
Transpose a Vector or Matrix.
64+
### transpose( [callback] )
65+
66+
Transpose a matrix.
67+
68+
jStat([[1,2],[3,4]]).transpose() === [[1,3],[2,4]];
3869

3970
### map( func )
4071

4172
Map a function to all values and return a new object.
4273

74+
var matrix = jStat([[1,2],[3,4]]),
75+
mapped = matrix.map( function( x ) {
76+
return x * 2;
77+
});
78+
// matrix === [[1,2],[3,4]]
79+
// mapped === [[2,4],[6,8]]
80+
4381
### alter( func )
4482

4583
Destructively alter an object.
4684

47-
### add( arg [,callback] )
85+
var matrix = jStat([[1,2],[3,4]]);
86+
matrix.alter( function( x ) {
87+
return x * 2;
88+
});
89+
// matrix === [[2,4],[6,8]]
90+
91+
### add( arg )
4892

4993
Add value to all entries.
5094

51-
### divide( arg [,callback] )
95+
jStat([[1,2,3]]).add( 2 ) === [[3,4,5]];
96+
97+
### subtract( arg )
98+
99+
Subtract all entries by value.
100+
101+
jStat([[4,5,6]]).subtract( 2 ) === [[2,3,4]];
102+
103+
### divide( arg )
52104

53105
Divide all entries by value.
54106

55-
### multiply( arg [,callback] )
107+
jStat([[2,4,6]]).divide( 2 ) === [[1,2,3]];
56108

57-
Multiply all entries by value.
109+
### multiply( arg )
58110

59-
### subtract( arg [,callback] )
111+
Multiply all entries by value.
60112

61-
Subtract all entries by value.
113+
jStat([[1,2,3]]).multiply( 2 ) === [[2,4,6]];
62114

63-
### dot( arg [,callback] )
115+
### dot( arg )
64116

65117
Take dot product.
66118

67-
### pow( arg [,callback] )
119+
### pow( arg )
68120

69121
Raise all entries by value.
70122

123+
jStat([[1,2,3]]).pow( 2 ) === [[1,4,9]];
124+
71125
### abs()
72126

73127
Return the absolute values of all entries.
74128

129+
jStat([[1,-2,-3]]).abs() === [[1,2,3]];
130+
75131
### clear()
76132

77-
Set all values to 0.
133+
Set all values to 0. This is destructive.
78134

79135
### norm()
80136

@@ -88,65 +144,69 @@ Compute the angle between two vectors.
88144

89145
Tests if a matrix is symmetric.
90146

91-
### sum( [bool,][callback] )
147+
jStat([[1,2],[2,1]]).symmetric() === true
148+
149+
### sum( [[bool][,callback]][callback] )
92150

93-
Return the sum of a Vector, or of Matrix columns.
94-
If pass boolean true as first argument, then return sum of entire Matrix.
151+
Return the sum of a vector, or of matrix columns.
152+
If pass boolean true as first argument, then return sum of entire object.
95153

96-
### min( [bool,][callback] )
154+
### min( [[bool][,callback]][callback] )
97155

98-
Return the minimum value of a Vector, or of Matrix columns.
99-
If pass boolean true as first argument, then return min of entire Matrix.
156+
Return the minimum value of a vector, or of matrix columns.
157+
If pass boolean true as first argument, then return min of entire object.
100158

101-
### max( [bool,][callback] )
159+
### max( [[bool][,callback]][callback] )
102160

103-
Return the maximum value of a Vector, or of Matrix columns.
104-
If pass boolean true as first argument, then return max of entire Matrix.
161+
Return the maximum value of a vector, or of matrix columns.
162+
If pass boolean true as first argument, then return max of entire object.
105163

106-
### mean( [bool,][callback] )
164+
### mean( [[bool][,callback]][callback] )
107165

108-
Return the mean value of a Vector, or of Matrix columns.
109-
If pass boolean true as first argument, then return mean of entire Matrix.
166+
Return the mean value of a vector, or of matrix columns.
167+
If pass boolean true as first argument, then return mean of entire object.
110168

111-
### median( [bool,][callback] )
169+
### median( [[bool][,callback]][callback] )
112170

113-
Return the median value of a Vector, or of Matrix columns.
114-
If pass boolean true as first argument, then return median of entire Matrix.
171+
Return the median value of a vector, or of matrix columns.
172+
If pass boolean true as first argument, then return median of entire object.
115173

116-
### mode( [bool,][callback] )
174+
### mode( [[bool][,callback]][callback] )
117175

118-
Return the mode of a Vector, or of Matrix columns.
119-
If pass boolean true as first argument, then return mode of entire Matrix.
176+
Return the mode of a vector, or of matrix columns.
177+
If pass boolean true as first argument, then return mode of entire object.
120178

121-
### range( [bool,][callback] )
179+
### range( [[bool][,callback]][callback] )
122180

123-
Return the mode of a Vector, or of Matrix columns.
124-
If pass boolean true as first argument, then return range of entire Matrix.
181+
Return the mode of a vector, or of matrix columns.
182+
If pass boolean true as first argument, then return range of entire object.
125183

126-
### variance( [bool,][callback] )
184+
### variance( [[bool][,callback]][callback] )
127185

128-
Return the variance of a Vector, or of Matrix columns.
129-
If pass boolean true as first argument, then return variance of entire Matrix.
186+
Return the variance of a vector, or of matrix columns.
187+
If pass boolean true as first argument, then return variance of entire object.
130188

131-
### stdev( [bool,][callback] )
189+
### stdev( [[bool][,callback]][callback] )
132190

133-
Return the standard deviation of a Vector, or of Matrix columns.
134-
If pass boolean true as first argument, then return standard deviation of entire Matrix.
191+
Return the standard deviation of a vector, or of matrix columns.
192+
If pass boolean true as first argument, then return standard deviation of entire object.
135193

136194
### meandev( [callback] )
137195

138-
Return the mean deviation of a Vector, or of Matrix columns.
139-
If pass boolean true as first argument, then return mean deviation of entire Matrix.
196+
Return the mean deviation of a vector, or of matrix columns.
197+
If pass boolean true as first argument, then return mean deviation of entire object.
140198

141199
### meddev( [callback] )
142200

143-
Return the median deviation of a Vector, or of Matrix columns.
144-
If pass boolean true as first argument, then return median deviation of entire Matrix.
201+
Return the median deviation of a vector, or of matrix columns.
202+
If pass boolean true as first argument, then return median deviation of entire object.
145203

146204
### quartiles( [callback] )
147205

148-
Return the quartiles of a Vector, or of Matrix columns.
149-
If pass boolean true as first argument, then return quartiles of entire Matrix.
206+
Return the quartiles of a vector, or of matrix columns.
207+
If pass boolean true as first argument, then return quartiles of entire object.
208+
209+
## Core Static Functionality
150210

151211
### jStat.create( rows, cols, func )
152212

@@ -170,7 +230,7 @@ Generate an identity matrix of size row x cols.
170230

171231
### jStat.seq( min, max, length [,func] )
172232

173-
Generate a vector sequence.
233+
Generate an array sequence.
174234

175235
### jStat.transpose( arr )
176236

doc/md/overview.md

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,49 @@ jStat is a statistical library written in JavaScript that allows you to perform
66

77
### Architecture
88

9-
All functionality breaks into one of either static or instance methods. Static methods have been written to be as quick an simple as possible, which means they can be quite inflexible. They also work with native JavaScript data types (e.g. Array's). Static methods were written with other developers in mind. It provides as low-level access to the calculations engine as possible. This way developers can use jStat in their programs without causing a significant amount of overhead.
9+
All methods that execute from the jStat prototype (instance methods) simply pass their parameters to the static method counterpart to do the actual calculation.
10+
Here is a pseudo example of what is happening in `core.js`:
1011

11-
Instance methods work from first creating a jStat Object, then doing all calculations on that object. While the instance methods cannot perform the calculations as quickly as the static methods, the added flexibility allows for for easy statistical analysis on one or many data sets. They were created for those who will be using jStat for statistical analysis.
12+
jStat.min = function( arr ) {
13+
return Math.min.apply( null, arr );
14+
}
1215

13-
Here is a simple example on the difference in usage between the static and instance methods:
14-
15-
var myVect = [2,6,4,7,2,7,4],
16-
jObj = jStat( myVect );
16+
jStat.prototype.min = function() {
17+
var i = 0,
18+
newval = [];
19+
while( newval.push( jStat.min( this[i] )), ++i < this.length );
20+
if ( newval.length === 1 ) newval = newval[0];
21+
return newval;
22+
}
1723

18-
// calculate the sum of the the vector
19-
jStat.sum( myVect );
20-
jObj.sum();
24+
`jStat.min` does the actual calculation on the array, while `jStat.prototype.min` is a wrapper to help work with the jStat object.
25+
The reason for this approach is to allow for maxium flexibility to other developers who want to extend jStat, while allowing for easy creation of wrappers.
26+
This way extending jStat requires minimal performance overhead and allows for more unique wrappers to be created.
2127

22-
Now say we want to do several operations on the vector (e.g. sum, min, max, and standard deviation). This can be accomplished using the static methods, but each will need to be called separately. By using the jStat object we can pass callback functions and chain the execution of each operation:
28+
**Remember: Static methods always return native JavaScript types. Instance methods almost always return a jStat object.**
2329

24-
jObj.sum( function( val ) {
25-
// val === sum
26-
}).min( function( val ) {
27-
// val === min
28-
}).max( function( val ) {
29-
// val === max
30-
}).stdev( function( val ) {
31-
// val === st. dev.
32-
});
30+
Here is a simple example on the difference in usage between the static and instance methods:
3331

34-
This method sets each calculation to be executed in an asynchronous queue. Very useful method of preventing blocking when working with large data sets.
32+
var myVect = [2,6,4,7,2,7,4],
33+
jObj = jStat( myVect );
34+
35+
// calculate the sum of the the vector
36+
jStat.sum( myVect ) === 32;
37+
jObj.sum() === 32;
38+
39+
Now say we want to do several operations on the vector (e.g. sum, min, max, and standard deviation).
40+
This can be accomplished using the static methods, but each will need to be called separately.
41+
By using the jStat object we can pass callback functions and chain the execution of each operation:
42+
43+
jObj.sum( function( val ) {
44+
// val === sum
45+
}).min( function( val ) {
46+
// val === min
47+
}).max( function( val ) {
48+
// val === max
49+
}).stdev( function( val ) {
50+
// val === st. dev.
51+
});
52+
53+
This method sets each calculation to be executed in an asynchronous queue.
54+
Very useful method of preventing blocking when working with large data sets.

0 commit comments

Comments
 (0)