Skip to content

Commit a97976b

Browse files
committed
added the lib
1 parent 3e1ae00 commit a97976b

File tree

3 files changed

+289
-83
lines changed

3 files changed

+289
-83
lines changed

lib/node_modules/@stdlib/stats/incr/mmin/lib/main.js

Lines changed: 78 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -58,94 +58,89 @@ var format = require( '@stdlib/string/format' );
5858
* m = accumulator();
5959
* // returns -5.0
6060
*/
61-
function incrmmin( W ) {
62-
var buf;
63-
var min;
64-
var N;
65-
var i;
66-
if ( !isPositiveInteger( W ) ) {
67-
throw new TypeError( format( 'invalid argument. Must provide a positive integer. Value: `%s`.', W ) );
68-
}
69-
buf = new Float64Array( W );
70-
min = PINF;
71-
i = -1;
72-
N = 0;
61+
function incrnanmmin( W ) {
62+
var buf;
63+
var min;
64+
var N;
65+
var i;
66+
if ( !isPositiveInteger( W ) ) {
67+
throw new TypeError( format( 'invalid argument. Must provide a positive integer. Value: `%s`.', W ) );
68+
}
69+
buf = new Float64Array( W );
70+
min = PINF;
71+
i = -1;
72+
N = 0;
7373

74-
return accumulator;
74+
return accumulator;
7575

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-
function accumulator( x ) {
84-
var v;
85-
var k;
86-
if ( arguments.length === 0 ) {
87-
if ( N === 0 ) {
88-
return null;
89-
}
90-
return min;
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+
function accumulator( x ) {
84+
var v;
85+
var k;
86+
if ( arguments.length === 0 ) {
87+
if ( N === 0 ) {
88+
return null;
89+
}
90+
return min;
91+
}
92+
if ( isnan( x ) ) {
93+
return min; // Ignore NaN values and return current minimum
94+
}
9495

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-
else if ( 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-
else if ( ( 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-
else if ( buf[ i ] === min && x === min && x === 0.0 ) {
128-
if ( isNegativeZero( x ) ) {
129-
min = x;
130-
} else if ( 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;
14398

144-
buf[ i ] = x;
145-
return min;
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+
else if ( 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...
111+
else if ( buf[ i ] === min && x > min ) {
112+
min = x;
113+
for ( k = 0; k < W; k++ ) {
114+
if ( k !== i ) {
115+
v = buf[ k ];
116+
if ( isnan( v ) ) {
117+
continue; // Ignore NaN values
118+
}
119+
if ( v < min || ( v === min && isNegativeZero( v ) ) ) {
120+
min = v;
121+
}
122+
}
123+
}
124+
}
125+
// Case: handling signed zero differences...
126+
else if ( buf[ i ] === min && x === min && x === 0.0 ) {
127+
if ( isNegativeZero( x ) ) {
128+
min = x;
129+
} else if ( isNegativeZero( buf[ i ] ) ) {
130+
min = x;
131+
for ( k = 0; k < W; k++ ) {
132+
if ( k !== i && isNegativeZero( buf[ k ] ) ) {
133+
min = buf[ k ];
134+
break;
135+
}
136+
}
137+
}
138+
}
148139

140+
buf[ i ] = x;
141+
return min;
142+
}
143+
}
149144

150145
// EXPORTS //
151146

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Compute a moving minimum incrementally. ignoring `NaN` values.
23+
*
24+
* @module @stdlib/stats/incr/nanmmin
25+
*
26+
* @example
27+
* var incrnanmmin = require( '@stdlib/stats/incr/nanmmin' );
28+
*
29+
* var accumulator = incrnanmmin( 3 );
30+
*
31+
* var m = accumulator();
32+
* // returns null
33+
*
34+
* m = accumulator( 2.0 );
35+
* // returns 2.0
36+
*
37+
* m = accumulator( NaN );
38+
* // returns 2.0
39+
*
40+
* m = accumulator( -5.0 );
41+
* // returns -5.0
42+
*
43+
* m = accumulator( 3.0 );
44+
* // returns -5.0
45+
*
46+
* m = accumulator( 5.0 );
47+
* // returns -5.0
48+
*
49+
* m = accumulator();
50+
* // returns -5.0
51+
*/
52+
53+
// MODULES //
54+
55+
var main = require( './main.js' );
56+
57+
58+
// EXPORTS //
59+
60+
module.exports = main;
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' );
26+
var PINF = require( '@stdlib/constants/float64/pinf' );
27+
var Float64Array = require( '@stdlib/array/float64' );
28+
var format = require( '@stdlib/string/format' );
29+
30+
31+
// MAIN //
32+
33+
/**
34+
* 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+
function incrnanmmin( W ) {
65+
var buf;
66+
var min;
67+
var N;
68+
var i;
69+
if ( !isPositiveInteger( W ) ) {
70+
throw new TypeError( format( 'invalid argument. Must provide a positive integer. Value: `%s`.', W ) );
71+
}
72+
buf = new Float64Array( W );
73+
min = PINF;
74+
i = -1;
75+
N = 0;
76+
77+
return accumulator;
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+
function accumulator( x ) {
87+
var v;
88+
var k;
89+
if ( arguments.length === 0 ) {
90+
if ( N === 0 ) {
91+
return null;
92+
}
93+
return min;
94+
}
95+
if ( isnan( x ) ) {
96+
return min; // 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+
else if ( 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...
114+
else if ( buf[ i ] === min && x > min ) {
115+
min = x;
116+
for ( k = 0; k < W; k++ ) {
117+
if ( k !== i ) {
118+
v = buf[ k ];
119+
if ( isnan( v ) ) {
120+
continue; // Ignore NaN values
121+
}
122+
if ( v < min || ( v === min && isNegativeZero( v ) ) ) {
123+
min = v;
124+
}
125+
}
126+
}
127+
}
128+
// Case: handling signed zero differences...
129+
else if ( buf[ i ] === min && x === min && x === 0.0 ) {
130+
if ( isNegativeZero( x ) ) {
131+
min = x;
132+
} else if ( isNegativeZero( buf[ i ] ) ) {
133+
min = x;
134+
for ( k = 0; k < W; k++ ) {
135+
if ( k !== i && isNegativeZero( buf[ k ] ) ) {
136+
min = buf[ k ];
137+
break;
138+
}
139+
}
140+
}
141+
}
142+
143+
buf[ i ] = x;
144+
return min;
145+
}
146+
}
147+
148+
149+
// EXPORTS //
150+
151+
module.exports = incrnanmmin;

0 commit comments

Comments
 (0)