diff --git a/lib/node_modules/@stdlib/stats/strided/midrange/README.md b/lib/node_modules/@stdlib/stats/strided/midrange/README.md new file mode 100644 index 000000000000..8dc11f54c793 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/midrange/README.md @@ -0,0 +1,170 @@ + + +# midrange + +> Calculate the [mid-range][mid-range] of a strided array. + +
+ +The [**mid-range**][mid-range], or **mid-extreme**, is the arithmetic mean of the maximum and minimum values in a data set. The measure is the midpoint of the range and a measure of central tendency. + +
+ + + +
+ +## Usage + +```javascript +var midrange = require( '@stdlib/stats/strided/midrange' ); +``` + +#### midrange( N, x, strideX ) + +Computes the [mid-range][mid-range] of a strided array. + +```javascript +var x = [ 1.0, -2.0, 2.0 ]; + +var v = midrange( x.length, x, 1 ); +// returns 0.0 +``` + +The function has the following parameters: + +- **N**: number of indexed elements. +- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. +- **strideX**: stride length for `x`. + +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [mid-range][mid-range] of every other element in `x`, + +```javascript +var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ]; + +var v = midrange( 4, x, 2 ); +// returns 1.0 +``` + +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + +var v = midrange( 4, x1, 2 ); +// returns 1.0 +``` + +#### midrange.ndarray( N, x, strideX, offsetX ) + +Computes the [mid-range][mid-range] of a strided array using alternative indexing semantics. + +```javascript +var x = [ 1.0, -2.0, 2.0 ]; + +var v = midrange.ndarray( x.length, x, 1, 0 ); +// returns 0.0 +``` + +The function has the following additional parameters: + +- **offsetX**: starting index for `x`. + +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the [mid-range][mid-range] for every other element in `x` starting from the second element + +```javascript +var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; + +var v = midrange.ndarray( 4, x, 2, 1 ); +// returns 1.0 +``` + +
+ + + +
+ +## Notes + +- If `N <= 0`, both functions return `NaN`. +- Depending on the environment, the typed versions ([`dmidrange`][@stdlib/stats/strided/dmidrange], [`smidrange`][@stdlib/stats/strided/smidrange], etc.) are likely to be significantly more performant. +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var midrange = require( '@stdlib/stats/strided/midrange' ); + +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +console.log( x ); + +var v = midrange( x.length, x, 1 ); +console.log( v ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/strided/midrange/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/strided/midrange/benchmark/benchmark.js new file mode 100644 index 000000000000..22fc8fac0e6b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/midrange/benchmark/benchmark.js @@ -0,0 +1,97 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var midrange = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -10, 10, options ); + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = midrange( x.length, x, 1 ); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/strided/midrange/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/strided/midrange/benchmark/benchmark.ndarray.js new file mode 100644 index 000000000000..e4f0c579c7e8 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/midrange/benchmark/benchmark.ndarray.js @@ -0,0 +1,97 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var midrange = require( './../lib/ndarray.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -10, 10, options ); + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = midrange( x.length, x, 1, 0 ); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:ndarray:len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/strided/midrange/docs/repl.txt b/lib/node_modules/@stdlib/stats/strided/midrange/docs/repl.txt new file mode 100644 index 000000000000..f0deb8eaf31d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/midrange/docs/repl.txt @@ -0,0 +1,89 @@ + +{{alias}}( N, x, strideX ) + Computes the mid-range of a strided array. + + The `N` and stride parameters determine which elements in the strided + array are accessed at runtime. + + Indexing is relative to the first index. To introduce an offset, use a + typed array view. + + If `N <= 0`, the function returns `NaN`. + + Parameters + ---------- + N: integer + Number of indexed elements. + + x: Array|TypedArray + Input array. + + strideX: integer + Stride length. + + Returns + ------- + out: number + Mid-range. + + Examples + -------- + // Standard Usage: + > var x = [ 1.0, -2.0, 2.0 ]; + > {{alias}}( x.length, x, 1 ) + 0.0 + + // Using `N` and stride parameters: + > x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ]; + > {{alias}}( 3, x, 2 ) + 0.0 + + // Using view offsets: + > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] ); + > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); + > {{alias}}( 3, x1, 2 ) + 0.0 + + +{{alias}}.ndarray( N, x, strideX, offsetX ) + Computes the mid-range of a strided array using alternative indexing + semantics. + + While typed array views mandate a view offset based on the underlying + buffer, the offset parameter supports indexing semantics based on a + starting index. + + Parameters + ---------- + N: integer + Number of indexed elements. + + x: Array|TypedArray + Input array. + + strideX: integer + Stride length. + + offsetX: integer + Starting index. + + Returns + ------- + out: number + Mid-range. + + Examples + -------- + // Standard Usage: + > var x = [ 1.0, -2.0, 2.0 ]; + > {{alias}}.ndarray( x.length, x, 1, 0 ) + 0.0 + + // Using offset parameter: + > var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ]; + > {{alias}}.ndarray( 3, x, 2, 1 ) + 0.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/strided/midrange/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/strided/midrange/docs/types/index.d.ts new file mode 100644 index 000000000000..d9c9be69f24e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/midrange/docs/types/index.d.ts @@ -0,0 +1,93 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; + +/** +* Interface describing `midrange`. +*/ +interface Routine { + /** + * Computes the mid-range of a strided array. + * + * @param N - number of indexed elements + * @param x - input array + * @param strideX - stride length + * @returns mid-range + * + * @example + * var x = [ 1.0, -2.0, 2.0 ]; + * + * var v = midrange( x.length, x, 1 ); + * // returns 0.0 + */ + ( N: number, x: InputArray, strideX: number ): number; + + /** + * Computes the mid-range of a strided array using alternative indexing semantics. + * + * @param N - number of indexed elements + * @param x - input array + * @param strideX - stride length + * @param offsetX - starting index + * @returns mid-range + * + * @example + * var x = [ 1.0, -2.0, 2.0 ]; + * + * var v = midrange.ndarray( x.length, x, 1, 0 ); + * // returns 0.0 + */ + ndarray( N: number, x: InputArray, strideX: number, offsetX: number ): number; +} + +/** +* Computes the mid-range of a strided array. +* +* @param N - number of indexed elements +* @param x - input array +* @param strideX - stride length +* @returns mid-range +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = midrange( x.length, x, 1 ); +* // returns 0.0 +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = midrange.ndarray( x.length, x, 1, 0 ); +* // returns 0.0 +*/ +declare var midrange: Routine; + + +// EXPORTS // + +export = midrange; diff --git a/lib/node_modules/@stdlib/stats/strided/midrange/docs/types/test.ts b/lib/node_modules/@stdlib/stats/strided/midrange/docs/types/test.ts new file mode 100644 index 000000000000..e59d56a2927e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/midrange/docs/types/test.ts @@ -0,0 +1,156 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import AccessorArray = require( '@stdlib/array/base/accessor' ); +import midrange = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const x = new Float64Array( 10 ); + + midrange( x.length, x, 1 ); // $ExpectType number + midrange( x.length, new AccessorArray( x ), 1 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not a number... +{ + const x = new Float64Array( 10 ); + + midrange( '10', x, 1 ); // $ExpectError + midrange( true, x, 1 ); // $ExpectError + midrange( false, x, 1 ); // $ExpectError + midrange( null, x, 1 ); // $ExpectError + midrange( undefined, x, 1 ); // $ExpectError + midrange( [], x, 1 ); // $ExpectError + midrange( {}, x, 1 ); // $ExpectError + midrange( ( x: number ): number => x, x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a numeric array... +{ + const x = new Float64Array( 10 ); + + midrange( x.length, 10, 1 ); // $ExpectError + midrange( x.length, true, 1 ); // $ExpectError + midrange( x.length, false, 1 ); // $ExpectError + midrange( x.length, null, 1 ); // $ExpectError + midrange( x.length, undefined, 1 ); // $ExpectError + midrange( x.length, {}, 1 ); // $ExpectError + midrange( x.length, ( x: number ): number => x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a number... +{ + const x = new Float64Array( 10 ); + + midrange( x.length, x, '10' ); // $ExpectError + midrange( x.length, x, true ); // $ExpectError + midrange( x.length, x, false ); // $ExpectError + midrange( x.length, x, null ); // $ExpectError + midrange( x.length, x, undefined ); // $ExpectError + midrange( x.length, x, [] ); // $ExpectError + midrange( x.length, x, {} ); // $ExpectError + midrange( x.length, x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + + midrange(); // $ExpectError + midrange( x.length ); // $ExpectError + midrange( x.length, x ); // $ExpectError + midrange( x.length, x, 1, 10 ); // $ExpectError +} + +// Attached to main export is an `ndarray` method which returns a number... +{ + const x = new Float64Array( 10 ); + + midrange.ndarray( x.length, x, 1, 0 ); // $ExpectType number + midrange.ndarray( x.length, new AccessorArray( x ), 1, 0 ); // $ExpectType number +} + +// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... +{ + const x = new Float64Array( 10 ); + + midrange.ndarray( '10', x, 1, 0 ); // $ExpectError + midrange.ndarray( true, x, 1, 0 ); // $ExpectError + midrange.ndarray( false, x, 1, 0 ); // $ExpectError + midrange.ndarray( null, x, 1, 0 ); // $ExpectError + midrange.ndarray( undefined, x, 1, 0 ); // $ExpectError + midrange.ndarray( [], x, 1, 0 ); // $ExpectError + midrange.ndarray( {}, x, 1, 0 ); // $ExpectError + midrange.ndarray( ( x: number ): number => x, x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a second argument which is not a numeric array... +{ + const x = new Float64Array( 10 ); + + midrange.ndarray( x.length, 10, 1, 0 ); // $ExpectError + midrange.ndarray( x.length, true, 1, 0 ); // $ExpectError + midrange.ndarray( x.length, false, 1, 0 ); // $ExpectError + midrange.ndarray( x.length, null, 1, 0 ); // $ExpectError + midrange.ndarray( x.length, undefined, 1, 0 ); // $ExpectError + midrange.ndarray( x.length, {}, 1, 0 ); // $ExpectError + midrange.ndarray( x.length, ( x: number ): number => x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a third argument which is not a number... +{ + const x = new Float64Array( 10 ); + + midrange.ndarray( x.length, x, '10', 0 ); // $ExpectError + midrange.ndarray( x.length, x, true, 0 ); // $ExpectError + midrange.ndarray( x.length, x, false, 0 ); // $ExpectError + midrange.ndarray( x.length, x, null, 0 ); // $ExpectError + midrange.ndarray( x.length, x, undefined, 0 ); // $ExpectError + midrange.ndarray( x.length, x, [], 0 ); // $ExpectError + midrange.ndarray( x.length, x, {}, 0 ); // $ExpectError + midrange.ndarray( x.length, x, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fourth argument which is not a number... +{ + const x = new Float64Array( 10 ); + + midrange.ndarray( x.length, x, 1, '10' ); // $ExpectError + midrange.ndarray( x.length, x, 1, true ); // $ExpectError + midrange.ndarray( x.length, x, 1, false ); // $ExpectError + midrange.ndarray( x.length, x, 1, null ); // $ExpectError + midrange.ndarray( x.length, x, 1, undefined ); // $ExpectError + midrange.ndarray( x.length, x, 1, [] ); // $ExpectError + midrange.ndarray( x.length, x, 1, {} ); // $ExpectError + midrange.ndarray( x.length, x, 1, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + + midrange.ndarray(); // $ExpectError + midrange.ndarray( x.length ); // $ExpectError + midrange.ndarray( x.length, x ); // $ExpectError + midrange.ndarray( x.length, x, 1 ); // $ExpectError + midrange.ndarray( x.length, x, 1, 0, 10 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/strided/midrange/examples/index.js b/lib/node_modules/@stdlib/stats/strided/midrange/examples/index.js new file mode 100644 index 000000000000..6dd451af0d91 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/midrange/examples/index.js @@ -0,0 +1,30 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var midrange = require( './../lib' ); + +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +console.log( x ); + +var v = midrange( x.length, x, 1 ); +console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/strided/midrange/lib/accessors.js b/lib/node_modules/@stdlib/stats/strided/midrange/lib/accessors.js new file mode 100644 index 000000000000..bd0661e56631 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/midrange/lib/accessors.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); + + +// MAIN // + +/** +* Computes the mid-range of a strided array. +* +* @private +* @param {PositiveInteger} N - number of indexed elements +* @param {Object} x - input array object +* @param {Collection} x.data - input array data +* @param {Array} x.accessors - array element accessors +* @param {integer} strideX - strideX length +* @param {NonNegativeInteger} offsetX - starting index +* @returns {number} mid-range +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); +* +* var v = midrange( 4, arraylike2object( x ), 2, 1 ); +* // returns 1.0 +*/ +function midrange( N, x, strideX, offsetX ) { + var xbuf; + var get; + var max; + var min; + var ix; + var v; + var i; + + // Cache reference to array data: + xbuf = x.data; + + // Cache a reference to the element accessor: + get = x.accessors[ 0 ]; + + if ( N === 1 || strideX === 0 ) { + v = get( xbuf, offsetX ); + if ( isnan( v ) ) { + return NaN; + } + return v; + } + ix = offsetX; + min = get( xbuf, ix ); + max = min; + for ( i = 1; i < N; i++ ) { + ix += strideX; + v = get( xbuf, ix ); + if ( isnan( v ) ) { + return v; + } + if ( v < min || ( v === min && isNegativeZero( v ) ) ) { + min = v; + } else if ( v > max || ( v === max && isPositiveZero( v ) ) ) { + max = v; + } + } + return ( max + min ) / 2.0; +} + + +// EXPORTS // + +module.exports = midrange; diff --git a/lib/node_modules/@stdlib/stats/strided/midrange/lib/index.js b/lib/node_modules/@stdlib/stats/strided/midrange/lib/index.js new file mode 100644 index 000000000000..e675e9ff4929 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/midrange/lib/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Compute the mid-range of a strided array. +* +* @module @stdlib/stats/strided/midrange +* +* @example +* var midrange = require( '@stdlib/stats/strided/midrange' ); +* +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = midrange( x.length, x, 1 ); +* // returns 0.0 +* +* @example +* var midrange = require( '@stdlib/stats/strided/midrange' ); +* +* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; +* +* var v = midrange.ndarray( 4, x, 2, 1 ); +* // returns 1.0 +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +setReadOnly( main, 'ndarray', ndarray ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "ndarray": "main.ndarray" } diff --git a/lib/node_modules/@stdlib/stats/strided/midrange/lib/main.js b/lib/node_modules/@stdlib/stats/strided/midrange/lib/main.js new file mode 100644 index 000000000000..34596d242f5a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/midrange/lib/main.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +/** +* Computes the mid-range of a strided array. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {NumericArray} x - input array +* @param {integer} strideX - stride length +* @returns {number} mid-range +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = midrange( x.length, x, 1 ); +* // returns 0.0 +*/ +function midrange( N, x, strideX ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ) ); +} + + +// EXPORTS // + +module.exports = midrange; diff --git a/lib/node_modules/@stdlib/stats/strided/midrange/lib/ndarray.js b/lib/node_modules/@stdlib/stats/strided/midrange/lib/ndarray.js new file mode 100644 index 000000000000..baf6e1eba996 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/midrange/lib/ndarray.js @@ -0,0 +1,90 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); + + +// MAIN // + +/** +* Computes the mid-range of a strided array. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {NumericArray} x - input array +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index +* @returns {number} mid-range +* +* @example +* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; +* +* var v = midrange( 4, x, 2, 1 ); +* // returns 1.0 +*/ +function midrange( N, x, strideX, offsetX ) { + var max; + var min; + var ix; + var o; + var v; + var i; + + if ( N <= 0 ) { + return NaN; + } + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, o, strideX, offsetX ); + } + if ( N === 1 || strideX === 0 ) { + v = x[ offsetX ]; + if ( isnan( v ) ) { + return NaN; + } + return v; + } + ix = offsetX; + min = x[ ix ]; + max = min; + for ( i = 1; i < N; i++ ) { + ix += strideX; + v = x[ ix ]; + if ( isnan( v ) ) { + return v; + } + if ( v < min || ( v === min && isNegativeZero( v ) ) ) { + min = v; + } else if ( v > max || ( v === max && isPositiveZero( v ) ) ) { + max = v; + } + } + return ( max + min ) / 2.0; +} + + +// EXPORTS // + +module.exports = midrange; diff --git a/lib/node_modules/@stdlib/stats/strided/midrange/package.json b/lib/node_modules/@stdlib/stats/strided/midrange/package.json new file mode 100644 index 000000000000..1c7ffdebeee9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/midrange/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/stats/strided/midrange", + "version": "0.0.0", + "description": "Calculate the mid-range of a strided array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "minimum", + "min", + "maximum", + "max", + "midrange", + "extremes", + "dispersion", + "domain", + "extent", + "strided", + "strided array", + "array" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/strided/midrange/test/test.js b/lib/node_modules/@stdlib/stats/strided/midrange/test/test.js new file mode 100644 index 000000000000..c6846e556807 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/midrange/test/test.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var midrange = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof midrange, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method providing an ndarray interface', function test( t ) { + t.strictEqual( typeof midrange.ndarray, 'function', 'method is a function' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/strided/midrange/test/test.midrange.js b/lib/node_modules/@stdlib/stats/strided/midrange/test/test.midrange.js new file mode 100644 index 000000000000..f41540c84927 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/midrange/test/test.midrange.js @@ -0,0 +1,337 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var Float64Array = require( '@stdlib/array/float64' ); +var midrange = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof midrange, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 3', function test( t ) { + t.strictEqual( midrange.length, 3, 'has expected arity' ); + t.end(); +}); + +tape( 'the function calculates the mid-range of a strided array', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = midrange( x.length, x, 1 ); + t.strictEqual( v, 0.5, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = midrange( x.length, x, 1 ); + t.strictEqual( v, -4.5, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = midrange( x.length, x, 1 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ -0.0, -0.0 ]; + v = midrange( x.length, x, 1 ); + t.strictEqual( isNegativeZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = midrange( x.length, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = midrange( x.length, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the mid-range of a strided array (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = midrange( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( v, 0.5, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = midrange( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( v, -4.5, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = midrange( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ -0.0, -0.0 ]; + v = midrange( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isNegativeZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = midrange( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = midrange( x.length, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { + var x; + var v; + + x = [ 1.0, 2.0, 3.0 ]; + + v = midrange( 0, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = midrange( -1, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, 2.0, 3.0 ]; + + v = midrange( 0, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = midrange( -1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `1`, the function returns the first element', function test( t ) { + var x; + var v; + + x = [ 1.0, 2.0, 3.0 ]; + + v = midrange( 1, x, 1 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + x = [ NaN, 2.0, 3.0 ]; + + v = midrange( 1, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `1`, the function returns the first element (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, 2.0, 3.0 ]; + + v = midrange( 1, toAccessorArray( x ), 1 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + x = [ NaN, 2.0, 3.0 ]; + + v = midrange( 1, toAccessorArray( x ), 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a `stride` parameter', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + + v = midrange( 4, x, 2 ); + + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `stride` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + + v = midrange( 4, toAccessorArray( x ), 2 ); + + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative `stride` parameter', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + v = midrange( 4, x, -2 ); + + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + v = midrange( 4, toAccessorArray( x ), -2 ); + + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = midrange( x.length, x, 0 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + x = [ NaN, -2.0, -4.0, 5.0, 3.0 ]; + + v = midrange( x.length, x, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = midrange( x.length, toAccessorArray( x ), 0 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + x = [ NaN, -2.0, -4.0, 5.0, 3.0 ]; + + v = midrange( x.length, toAccessorArray( x ), 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports view offsets', function test( t ) { + var x0; + var x1; + var v; + + x0 = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0, // 3 + 6.0 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + + v = midrange( 4, x1, 2 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports view offsets (accessors)', function test( t ) { + var x0; + var x1; + var v; + + x0 = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0, // 3 + 6.0 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + + v = midrange( 4, toAccessorArray( x1 ), 2 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/strided/midrange/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/strided/midrange/test/test.ndarray.js new file mode 100644 index 000000000000..133d99164592 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/midrange/test/test.ndarray.js @@ -0,0 +1,328 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); +var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var midrange = require( './../lib/ndarray.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof midrange, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 4', function test( t ) { + t.strictEqual( midrange.length, 4, 'has expected arity' ); + t.end(); +}); + +tape( 'the function calculates the mid-range of a strided array', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = midrange( x.length, x, 1, 0 ); + t.strictEqual( v, 0.5, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = midrange( x.length, x, 1, 0 ); + t.strictEqual( v, -4.5, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = midrange( x.length, x, 1, 0 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ -0.0, -0.0 ]; + v = midrange( x.length, x, 1, 0 ); + t.strictEqual( isNegativeZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = midrange( x.length, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = midrange( x.length, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the mid-range of a strided array (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + v = midrange( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 0.5, 'returns expected value' ); + + x = [ -4.0, -5.0 ]; + v = midrange( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, -4.5, 'returns expected value' ); + + x = [ -0.0, 0.0, -0.0 ]; + v = midrange( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isPositiveZero( v ), true, 'returns expected value' ); + + x = [ -0.0, -0.0 ]; + v = midrange( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isNegativeZero( v ), true, 'returns expected value' ); + + x = [ NaN ]; + v = midrange( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + x = [ NaN, NaN ]; + v = midrange( x.length, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = midrange( 0, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = midrange( -1, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = midrange( 0, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = midrange( -1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `1`, the function returns the first element', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = midrange( 1, x, 1, 0 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + x = [ NaN, -2.0, -4.0, 5.0, 3.0 ]; + + v = midrange( 1, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `1`, the function returns the first element (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = midrange( 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + x = [ NaN, -2.0, -4.0, 5.0, 3.0 ]; + + v = midrange( 1, toAccessorArray( x ), 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a `stride` parameter', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + + v = midrange( 4, x, 2, 0 ); + + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `stride` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + + v = midrange( 4, toAccessorArray( x ), 2, 0 ); + + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative `stride` parameter', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + v = midrange( 4, x, -2, x.length-2 ); + + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + v = midrange( 4, toAccessorArray( x ), -2, x.length-2 ); + + t.strictEqual( v, 1.0, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = midrange( x.length, x, 0, 0 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + x = [ NaN, -2.0, -4.0, 5.0, 3.0 ]; + + v = midrange( x.length, x, 0, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element (accessors)', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = midrange( x.length, toAccessorArray( x ), 0, 0 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + x = [ NaN, -2.0, -4.0, 5.0, 3.0 ]; + + v = midrange( x.length, toAccessorArray( x ), 0, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an `offset` parameter', function test( t ) { + var x; + var v; + + x = [ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]; + + v = midrange( 4, x, 2, 1 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an `offset` parameter (accessors)', function test( t ) { + var x; + var v; + + x = [ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]; + + v = midrange( 4, toAccessorArray( x ), 2, 1 ); + t.strictEqual( v, 1.0, 'returns expected value' ); + + t.end(); +});