You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/node_modules/@stdlib/stats/incr/mmin/lib/main.js
+78-83Lines changed: 78 additions & 83 deletions
Original file line number
Diff line number
Diff line change
@@ -58,94 +58,89 @@ var format = require( '@stdlib/string/format' );
58
58
* m = accumulator();
59
59
* // returns -5.0
60
60
*/
61
-
functionincrmmin(W){
62
-
varbuf;
63
-
varmin;
64
-
varN;
65
-
vari;
66
-
if(!isPositiveInteger(W)){
67
-
thrownewTypeError(format('invalid argument. Must provide a positive integer. Value: `%s`.',W));
68
-
}
69
-
buf=newFloat64Array(W);
70
-
min=PINF;
71
-
i=-1;
72
-
N=0;
61
+
functionincrnanmmin(W){
62
+
varbuf;
63
+
varmin;
64
+
varN;
65
+
vari;
66
+
if(!isPositiveInteger(W)){
67
+
thrownewTypeError(format('invalid argument. Must provide a positive integer. Value: `%s`.',W));
68
+
}
69
+
buf=newFloat64Array(W);
70
+
min=PINF;
71
+
i=-1;
72
+
N=0;
73
73
74
-
returnaccumulator;
74
+
returnaccumulator;
75
75
76
-
/**
77
-
* If provided a value, the accumulator function returns an updated minimum. If not provided a value, the accumulator function returns the current minimum.
78
-
*
79
-
* @private
80
-
* @param {number} [x] - input value
81
-
* @returns {(number|null)} minimum value or null
82
-
*/
83
-
functionaccumulator(x){
84
-
varv;
85
-
vark;
86
-
if(arguments.length===0){
87
-
if(N===0){
88
-
returnnull;
89
-
}
90
-
returnmin;
91
-
}
92
-
// Update the index for managing the circular buffer:
93
-
i=(i+1)%W;
76
+
/**
77
+
* If provided a value, the accumulator function returns an updated minimum, ignoring NaN values. If not provided a value, the accumulator function returns the current minimum.
78
+
*
79
+
* @private
80
+
* @param {number} [x] - input value
81
+
* @returns {(number|null)} minimum value or null
82
+
*/
83
+
functionaccumulator(x){
84
+
varv;
85
+
vark;
86
+
if(arguments.length===0){
87
+
if(N===0){
88
+
returnnull;
89
+
}
90
+
returnmin;
91
+
}
92
+
if(isnan(x)){
93
+
returnmin;// Ignore NaN values and return current minimum
94
+
}
94
95
95
-
// Case: update initial window...
96
-
if(N<W){
97
-
N+=1;
98
-
if(
99
-
isnan(x)||
100
-
x<min||
101
-
(x===min&&isNegativeZero(x))
102
-
){
103
-
min=x;
104
-
}
105
-
}
106
-
// Case: incoming value is NaN or less than current minimum value...
107
-
elseif(isnan(x)||x<min){
108
-
min=x;
109
-
}
110
-
// Case: outgoing value is the current minimum and the new value is greater than the minimum, and, thus, we need to find a new minimum among the current values...
111
-
elseif((buf[i]===min&&x>min)||isnan(buf[i])){
112
-
min=x;
113
-
for(k=0;k<W;k++){
114
-
if(k!==i){
115
-
v=buf[k];
116
-
if(isnan(v)){
117
-
min=v;
118
-
break;// no need to continue searching
119
-
}
120
-
if(v<min||(v===min&&isNegativeZero(v))){
121
-
min=v;
122
-
}
123
-
}
124
-
}
125
-
}
126
-
// Case: outgoing value is the current minimum, which is zero, and the new value is also zero, and, thus, we need to correctly handle signed zeros...
127
-
elseif(buf[i]===min&&x===min&&x===0.0){
128
-
if(isNegativeZero(x)){
129
-
min=x;
130
-
}elseif(isNegativeZero(buf[i])){
131
-
// Because the outgoing and incoming are different signs (-,+), we need to search the buffer to see if it contains a negative zero. If so, the minimum value remains negative zero; otherwise, the minimum value is incoming value...
132
-
min=x;
133
-
for(k=0;k<W;k++){
134
-
if(k!==i&&isNegativeZero(buf[k])){
135
-
min=buf[k];
136
-
break;
137
-
}
138
-
}
139
-
}
140
-
// Case: the outgoing and incoming values are both positive zero, so nothing changes
141
-
}
142
-
// Case: updating existing window; however, the minimum value does not change so nothing to do but update our buffer...
96
+
// Update the index for managing the circular buffer:
97
+
i=(i+1)%W;
143
98
144
-
buf[i]=x;
145
-
returnmin;
146
-
}
147
-
}
99
+
// Case: update initial window...
100
+
if(N<W){
101
+
N+=1;
102
+
if(x<min||(x===min&&isNegativeZero(x))){
103
+
min=x;
104
+
}
105
+
}
106
+
// Case: incoming value is less than current minimum value...
107
+
elseif(x<min){
108
+
min=x;
109
+
}
110
+
// Case: outgoing value is the current minimum and the new value is greater than the minimum, requiring a new minimum search...
* Returns an accumulator function which incrementally computes a moving minimum value. ignoring `NaN` values.
35
+
*
36
+
* @param {PositiveInteger} W - window size
37
+
* @throws {TypeError} must provide a positive integer
38
+
* @returns {Function} accumulator function
39
+
*
40
+
* @example
41
+
* var accumulator = incrmmin( 3 );
42
+
*
43
+
* var m = accumulator();
44
+
* // returns null
45
+
*
46
+
* m = accumulator( 2.0 );
47
+
* // returns 2.0
48
+
*
49
+
* m = accumulator( NaN );
50
+
* // returns 2.0
51
+
*
52
+
* m = accumulator( -5.0 );
53
+
* // returns -5.0
54
+
*
55
+
* m = accumulator( 3.0 );
56
+
* // returns -5.0
57
+
*
58
+
* m = accumulator( 5.0 );
59
+
* // returns -5.0
60
+
*
61
+
* m = accumulator();
62
+
* // returns -5.0
63
+
*/
64
+
functionincrnanmmin(W){
65
+
varbuf;
66
+
varmin;
67
+
varN;
68
+
vari;
69
+
if(!isPositiveInteger(W)){
70
+
thrownewTypeError(format('invalid argument. Must provide a positive integer. Value: `%s`.',W));
71
+
}
72
+
buf=newFloat64Array(W);
73
+
min=PINF;
74
+
i=-1;
75
+
N=0;
76
+
77
+
returnaccumulator;
78
+
79
+
/**
80
+
* If provided a value, the accumulator function returns an updated minimum, ignoring NaN values. If not provided a value, the accumulator function returns the current minimum.
81
+
*
82
+
* @private
83
+
* @param {number} [x] - input value
84
+
* @returns {(number|null)} minimum value or null
85
+
*/
86
+
functionaccumulator(x){
87
+
varv;
88
+
vark;
89
+
if(arguments.length===0){
90
+
if(N===0){
91
+
returnnull;
92
+
}
93
+
returnmin;
94
+
}
95
+
if(isnan(x)){
96
+
returnmin;// Ignore NaN values and return current minimum
97
+
}
98
+
99
+
// Update the index for managing the circular buffer:
100
+
i=(i+1)%W;
101
+
102
+
// Case: update initial window...
103
+
if(N<W){
104
+
N+=1;
105
+
if(x<min||(x===min&&isNegativeZero(x))){
106
+
min=x;
107
+
}
108
+
}
109
+
// Case: incoming value is less than current minimum value...
110
+
elseif(x<min){
111
+
min=x;
112
+
}
113
+
// Case: outgoing value is the current minimum and the new value is greater than the minimum, requiring a new minimum search...
0 commit comments