diff --git a/lib/node_modules/@stdlib/_tools/doctest/compare-values/README.md b/lib/node_modules/@stdlib/_tools/doctest/compare-values/README.md index 5e1166a387e1..39a8f5b354f2 100644 --- a/lib/node_modules/@stdlib/_tools/doctest/compare-values/README.md +++ b/lib/node_modules/@stdlib/_tools/doctest/compare-values/README.md @@ -32,7 +32,7 @@ var compareValues = require( '@stdlib/_tools/doctest/compare-values' ); #### compareValues( actual, expected ) -Checks whether a return value is equal to the value of a return annotation. In case of a mismatch, the function returns an error message; otherwise, it returns `null`. +Checks whether a return value is equal to the value of a return annotation. ```javascript var out = compareValues( 4.126, '~4.13' ); @@ -42,12 +42,14 @@ out = compareValues( 4.126e-13, '~4.13e-13' ); // returns null out = compareValues( NaN, 'null' ); -// returns 'Displayed return value is `null`, but function returns `NaN` instead' +// returns 'Displayed return value is `null`, but expected `NaN` instead' out = compareValues( 'Hello World', '"Hello World"' ); -// returns '`Hello World` should be wrapped in single quotes' +// returns '`"Hello World"` should be wrapped in single quotes' ``` +When an actual value does not match the expected value as described by a return annotation, the function returns an error message; otherwise, the function returns `null`. + @@ -68,10 +70,10 @@ out = compareValues( 'Hello World', '"Hello World"' ); var compareValues = require( '@stdlib/_tools/doctest/compare-values' ); console.log( compareValues( true, 'false' ) ); -// => 'Displayed return value is `false`, but function returns `true` instead' +// => 'Displayed return value is `false`, but expected `true` instead' console.log( compareValues( 0.25, '0.26' ) ); -// => 'Displayed return value is `0.26`, but function returns `0.25` instead' +// => 'Displayed return value is `0.26`, but expected `0.25` instead' console.log( compareValues( 23, '23' ) ); // => null diff --git a/lib/node_modules/@stdlib/_tools/doctest/compare-values/lib/complex_ctors.js b/lib/node_modules/@stdlib/_tools/doctest/compare-values/lib/complex_ctors.js new file mode 100644 index 000000000000..a147ccdb7fb3 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/doctest/compare-values/lib/complex_ctors.js @@ -0,0 +1,37 @@ +/** +* @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 Complex128 = require( '@stdlib/complex/float64/ctor' ); +var Complex64 = require( '@stdlib/complex/float32/ctor' ); + + +// MAIN // + +var ctors = { + 'Complex128': Complex128, + 'Complex64': Complex64 +}; + + +// EXPORTS // + +module.exports = ctors; diff --git a/lib/node_modules/@stdlib/_tools/doctest/compare-values/lib/main.js b/lib/node_modules/@stdlib/_tools/doctest/compare-values/lib/main.js index 997119c98666..dd8f37d264dd 100644 --- a/lib/node_modules/@stdlib/_tools/doctest/compare-values/lib/main.js +++ b/lib/node_modules/@stdlib/_tools/doctest/compare-values/lib/main.js @@ -22,7 +22,7 @@ var roundn = require( '@stdlib/math/base/special/roundn' ); var epsdiff = require( '@stdlib/math/base/utils/float64-epsilon-difference' ); -var indexOf = require( '@stdlib/utils/index-of' ); +var indexOf = require( '@stdlib/array/base/index-of' ); var typeOf = require( '@stdlib/utils/type-of' ); var deepEqual = require( '@stdlib/assert/deep-equal' ); @@ -53,6 +53,7 @@ var isInt8Array = require( '@stdlib/assert/is-int8array' ); var isInt16Array = require( '@stdlib/assert/is-int16array' ); var isInt32Array = require( '@stdlib/assert/is-int32array' ); var isMultiSlice = require( '@stdlib/assert/is-multi-slice' ); +var isnan = require( '@stdlib/assert/is-nan' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var isNull = require( '@stdlib/assert/is-null' ); var isNumber = require( '@stdlib/assert/is-number' ); @@ -76,13 +77,14 @@ var isUint32Array = require( '@stdlib/assert/is-uint32array' ); var isUndefined = require( '@stdlib/assert/is-undefined' ); var isURIError = require( '@stdlib/assert/is-uri-error' ); -var startsWith = require( '@stdlib/string/starts-with' ); var contains = require( '@stdlib/assert/contains' ); +var startsWith = require( '@stdlib/string/starts-with' ); var endsWith = require( '@stdlib/string/ends-with' ); var removeFirst = require( '@stdlib/string/remove-first' ); var removeLast = require( '@stdlib/string/remove-last' ); var replace = require( '@stdlib/string/replace' ); var trim = require( '@stdlib/string/trim' ); +var constructorName = require( '@stdlib/utils/constructor-name' ); var PINF = require( '@stdlib/constants/float64/pinf' ); var NINF = require( '@stdlib/constants/float64/ninf' ); var real = require( '@stdlib/complex/float64/real' ); @@ -91,7 +93,11 @@ var realf = require( '@stdlib/complex/float32/real' ); var imagf = require( '@stdlib/complex/float32/imag' ); var reinterpretComplex = require( '@stdlib/strided/base/reinterpret-complex' ); var copyArray = require( '@stdlib/array/base/copy' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); +var ndims = require( '@stdlib/ndarray/ndims' ); var createAnnotationValue = require( '@stdlib/_tools/doctest/create-annotation-value' ); +var format = require( '@stdlib/string/format' ); var parse = require( './parse.js' ); @@ -99,6 +105,8 @@ var parse = require( './parse.js' ); var RE_INSTANCE_ANNOTATION = /<([^>]+)>(\[?[^\]]*\]?)/; var RE_ARRAY = /(\[[\s\S]+\])/; +var RE_NDARRAY = /<([^>]+)>(\[[\s\S]+\])/; + var RE_UNESCAPED_QUOTE = /[^\\]'/; var BOXED_TYPE_ANNOTATIONS = [ '', @@ -106,12 +114,13 @@ var BOXED_TYPE_ANNOTATIONS = [ '', '' ]; -var PRIMITIVE_TYPE_ANNOTATIONS = [ +var PRIMITIVE_TYPE_ANNOTATIONS = [ // eslint-disable-line id-length '', '', '', '' ]; + var OPTS = { 'numel': 1e308 }; @@ -123,9 +132,9 @@ var OPTS = { * Checks whether a primitive value matches a return annotation. * * @private -* @param {(string|boolean|number|null|undefined)} actual - primitive value +* @param {(string|boolean|number|null|void)} actual - primitive value * @param {string} expected - return value annotation -* @returns {boolean} boolean indicating whether value matches return annotation +* @returns {boolean} boolean indicating whether a value matches a return annotation */ function checkPrimitive( actual, expected ) { var dgts; @@ -142,15 +151,15 @@ function checkPrimitive( actual, expected ) { return actual === NINF; } if ( expected === 'NaN' ) { - return isNaN( actual ); + return isnan( actual ); } if ( startsWith( expected, '~' ) ) { if ( contains( expected, 'e' ) ) { - dgts = indexOf( expected, 'e' ) - indexOf( expected, '.' ); + dgts = indexOf( expected, 'e', 0 ) - indexOf( expected, '.', 0 ); a = actual.toPrecision( dgts ); b = removeFirst( expected ); } else { - dgts = expected.length - indexOf( expected, '.' ) - 1; + dgts = expected.length - indexOf( expected, '.', 0 ) - 1; a = roundn( actual, -dgts ); b = roundn( parseFloat( removeFirst( expected ) ), -dgts ); } @@ -186,7 +195,7 @@ function checkPrimitive( actual, expected ) { * @private * @param {*} actual - actual return value * @param {string} expected - return value annotation -* @returns {boolean} boolean indicating whether type annotation matches the actual output +* @returns {boolean} boolean indicating whether a type annotation matches the actual output */ function checkForPlaceholders( actual, expected ) { // Note: keep in alphabetical order... @@ -297,11 +306,11 @@ function checkForPlaceholders( actual, expected ) { function compareEntries( actual, expected ) { var i; if ( actual.length !== expected.length ) { - return 'Expected entries ['+expected+'], but observed ['+actual+']'; + return format( 'Expected entries [%s], but observed [%s]', expected, actual ); } for ( i = 0; i < expected.length; i++ ) { if ( !checkPrimitive( actual[ i ], String( expected[ i ] ) ) ) { - return 'Expected entries ['+expected+'], but observed ['+actual+']'; + return format( 'Expected entries [%s], but observed [%s]', expected, actual ); } } return null; @@ -324,8 +333,8 @@ function checkComplex( actual, expected ) { if ( match ) { type = match[ 1 ]; entries = match[ 2 ]; - if ( actual.constructor.name !== type ) { - return 'Expected instance type <'+type+'>, but observed <'+actual.constructor.name+'>'; + if ( constructorName( actual ) !== type ) { + return format( 'Expected instance type <%s>, but observed <%s>', type, constructorName( actual ) ); } if ( entries ) { if ( isComplex128( actual ) ) { @@ -353,7 +362,7 @@ function checkComplex( actual, expected ) { * @param {string} expected - return value annotation * @returns {(string|null)} error message or null */ -function checkTypedArrays( actual, expected ) { +function checkTypedArray( actual, expected ) { var entries; var match; var type; @@ -362,8 +371,8 @@ function checkTypedArrays( actual, expected ) { if ( match ) { type = match[ 1 ]; entries = match[ 2 ]; - if ( actual.constructor.name !== type ) { - return 'Expected instance type <'+type+'>, but observed <'+actual.constructor.name+'>'; + if ( constructorName( actual ) !== type ) { + return format( 'Expected instance type <%s>, but observed <%s>', type, constructorName( actual ) ); } if ( entries ) { if ( isComplexTypedArray( actual ) ) { @@ -405,10 +414,7 @@ function checkArray( actual, expected ) { if ( b === '...' ) { break; } - if ( - startsWith( String( b ), '<' ) && - endsWith( String( b ), '>' ) - ) { + if ( startsWith( String( b ), '<' ) && endsWith( String( b ), '>' ) ) { if ( !checkForPlaceholders( a, String( b ) ) ) { return false; } @@ -423,20 +429,22 @@ function checkArray( actual, expected ) { ) { return false; } - } - else if ( isPrimitive( a ) ) { + } else if ( isComplex64( a ) || isComplex128( a ) ) { + b = createAnnotationValue( b, OPTS ); + if ( checkComplex( a, b ) ) { + return false; + } + } else if ( isPrimitive( a ) ) { b = String( b ); if ( !checkPrimitive( a, b ) ) { return false; } - } - else if ( isArray( a ) ) { + } else if ( isArray( a ) ) { b = createAnnotationValue( b, OPTS ); if ( !checkArray( a, b ) ) { return false; } - } - else if ( isRegExp( a ) ) { + } else if ( isRegExp( a ) ) { if ( a.toString() !== b.toString() ) { return false; } @@ -447,6 +455,47 @@ function checkArray( actual, expected ) { return false; } +/** +* Checks whether an ndarray matches the expected return annotation. +* +* ## Notes +* +* - If the return annotation asserts deep instance equality, check whether it corresponds to the actual value. +* - Otherwise, check whether the return annotation signals the correct type. +* +* @private +* @param {*} actual - actual return value +* @param {string} expected - return value annotation +* @returns {(string|null)} error message or null +*/ +function checkNDArray( actual, expected ) { + var entries; + var match; + var type; + + match = RE_NDARRAY.exec( expected ); + if ( match ) { + type = match[ 1 ]; + entries = match[ 2 ]; + if ( constructorName( actual ) !== type ) { + return format( 'Expected instance type <%s>, but observed <%s>', type, constructorName( actual ) ); + } + if ( entries ) { + // Check for a zero-dimensional ndarray... + if ( ndims( actual ) === 0 ) { + actual = [ ndarraylike2scalar( actual ) ]; + } else { + actual = ndarray2array( actual ); + } + if ( !checkArray( actual, entries ) ) { + return format( 'Expected entries %s, but observed %s', entries, createAnnotationValue( actual ) ); + } + } + return null; + } + return 'ndarrays should be documented using instance annotation'; +} + // MAIN // @@ -472,6 +521,7 @@ function compareValues( actual, expected ) { var a; var b; + // Case: compareValues( true, 'true || false' ) if ( contains( expected, '||' ) ) { parts = expected.split( '||' ); a = trim( parts[ 0 ] ); @@ -479,87 +529,103 @@ function compareValues( actual, expected ) { msg1 = compareValues( actual, a ); msg2 = compareValues( actual, b ); if ( msg1 && msg2 ) { - return 'Actual value is `'+actual+'`, but expected `'+a+'` or `'+b+'`'; + return format( 'Actual value is `%s`, but expected `%s` or `%s`', actual, a, b ); } return null; } + // Case: compareValues( ???, '' ) if ( isEmptyString( expected ) ) { return 'Return annotation is empty'; } + // Case: compareValues( ???, 'e.g., returns 5' ) if ( contains( expected, 'e.g.' ) ) { // Early return since we cannot compare actual value to return annotation value: return null; } + // Case: compareValues( [], '' ) if ( startsWith( expected, '<' ) && endsWith( expected, '>' ) ) { if ( !checkForPlaceholders( actual, expected ) ) { - return 'Expected a '+expected+', but received: `'+actual+'`'; + return format( 'Expected a %s, but received: `%s`', expected, actual ); } + // Case: compareValues( new Number( 5 ), '' ) if ( contains( BOXED_TYPE_ANNOTATIONS, expected ) && isPrimitive( actual ) ) { - return 'Expected a '+expected+', but received an unboxed primitive: `'+actual+'`'; + return format( 'Expected a %s, but received an unboxed primitive: `%s`', expected, actual ); } + // Case: compareValues( 5, '' ) if ( contains( PRIMITIVE_TYPE_ANNOTATIONS, expected ) && isBoxedPrimitive( actual ) ) { - return 'Expected a '+expected+', but received a boxed primitive: `'+actual+'`'; + return format( 'Expected a %s, but received a boxed primitive: `%s`', expected, actual ); } return null; } + // Case: compareValues( 5, '5' ) if ( isPrimitive( actual ) ) { if ( isString( actual) ) { if ( !RE_UNESCAPED_QUOTE.test( actual ) && ( !startsWith( expected, '\'' ) || !endsWith( expected, '\'' ) ) ) { - return '`'+expected+'` should be wrapped in single quotes'; + return format( '`%s` should be wrapped in single quotes', expected ); } expected = removeFirst( removeLast( expected ) ); } if ( !checkPrimitive( actual, expected ) ) { - return 'Displayed return value is `'+expected+'`, but expected `'+actual+'` instead'; + return format( 'Displayed return value is `%s`, but expected `%s` instead', expected, actual ); } return null; } - if ( isTypedArray( actual ) || isComplexTypedArray( actual ) || isBooleanArray( actual ) ) { - return checkTypedArrays( actual, expected ); + // Case: compareValues( new Float64Array( [ 1.0, 2.0 ] ), '[ 1.0, 2.0 ]' ) + if ( isTypedArray( actual ) || isComplexTypedArray( actual ) || isBooleanArray( actual ) ) { // eslint-disable-line max-len + return checkTypedArray( actual, expected ); } + // Case: compareValues( [ 1.0, 2.0 ], '[ 1.0, 2.0 ]' ) if ( isArray( actual ) ) { if ( !checkArray( actual, expected ) ) { - return 'Displayed return value is `'+expected+'`, but expected `'+createAnnotationValue( actual )+'` instead'; + return format( 'Displayed return value is `%s`, but expected `%s` instead', expected, createAnnotationValue( actual ) ); } return null; } + // Case: compareValues( new Complex64( 1.0, 2.0 ), '[ 1.0, 2.0 ]' ) if ( isComplex64( actual ) || isComplex128( actual ) ) { return checkComplex( actual, expected ); } + // Case: compareValues( array( [ 1.0, 2.0 ] ), '[ 1.0, 2.0 ]' ) + if ( isndarrayLike( actual ) ) { + return checkNDArray( actual, expected ); + } + // Case: compareValues( /foo/, '/foo/' ) if ( isRegExp( actual ) ) { actual = actual.toString(); if ( actual !== expected ) { - return 'Displayed return value is `'+expected+'`, but expected `'+actual+'` instead'; + return format( 'Displayed return value is `%s`, but expected `%s` instead', expected, actual ); } return null; } + // Case: compareValues( function noop() {}, 'function noop() {}' ) if ( isFunction( actual ) ) { actual = actual.toString(); if ( actual !== expected ) { - return 'Displayed return value is `'+expected+'`, but expected `'+actual+'` instead'; + return format( 'Displayed return value is `%s`, but expected `%s` instead', expected, actual ); } return null; } + // Case: compareValues( {}, '{}' ) if ( isObjectLike( actual ) ) { if ( !contains( expected, '...' ) ) { try { expectedValue = parse( expected ); } catch ( err ) { - return 'Malformed return annotation. Encountered error while parsing: '+err.message; + return format( 'Malformed return annotation. Encountered error while parsing: %s', err.message ); } actualString = createAnnotationValue( actual, OPTS ); actualValue = parse( actualString ); if ( !deepEqual( expectedValue, actualValue ) ) { - return 'Displayed return value is `'+expected+'`, but expected `'+actualString+'` instead'; + return format( 'Displayed return value is `%s`, but expected `%s` instead', expected, actualString ); } } return null; diff --git a/lib/node_modules/@stdlib/_tools/doctest/compare-values/lib/parse.js b/lib/node_modules/@stdlib/_tools/doctest/compare-values/lib/parse.js index 88b42c477bf7..71258d3e43ae 100644 --- a/lib/node_modules/@stdlib/_tools/doctest/compare-values/lib/parse.js +++ b/lib/node_modules/@stdlib/_tools/doctest/compare-values/lib/parse.js @@ -27,20 +27,21 @@ // MODULES // -var startsWith = require( '@stdlib/string/starts-with' ); -var reFromString = require( '@stdlib/utils/regexp-from-string' ); var isNumberArray = require( '@stdlib/assert/is-number-array' ); -var isFinite = require( '@stdlib/assert/is-finite' ); // eslint-disable-line stdlib/no-redeclare +var isfinite = require( '@stdlib/assert/is-finite' ); var isString = require( '@stdlib/assert/is-string' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var startsWith = require( '@stdlib/string/starts-with' ); +var reFromString = require( '@stdlib/utils/regexp-from-string' ); var NINF = require( '@stdlib/constants/float64/ninf' ); var PINF = require( '@stdlib/constants/float64/pinf' ); -var ctors = require( './typed_array_ctors.js' ); +var format = require( '@stdlib/string/format' ); +var array2ctor = require( './typed_array_ctors.js' ); +var complex2ctor = require( './complex_ctors.js' ); // VARIABLES // -var at; // The index of the current character -var ch; // The current character var escapee = { '\'': '\'', '\\': '\\', @@ -52,15 +53,28 @@ var escapee = { 't': '\t' }; var text; +var at; // index of the current character +var ch; // current character // FUNCTIONS // /** -* Parses a value. It could be an object, an array, a string, a number, a regular expression, a placeholder sequence (`...`), a type annotation, or a word. +* Throws an error when something went wrong during parsing. +* +* @private +* @param {string} m - error message +* @throws {SyntaxError} must provide valid annotation value +*/ +function error( m ) { + throw new SyntaxError( m ); +} + +/** +* Parses a value. * * @private -* @returns {*} parsed vlaue +* @returns {*} parsed value */ function value() { white(); @@ -88,17 +102,6 @@ function value() { } } -/** -* Throws an error when something went wrong during parsing. -* -* @private -* @param {string} m - error message -* @throws {SyntaxError} must provide valid annotation value -*/ -function error( m ) { - throw new SyntaxError( m ); -} - /** * Parses a regular expression. * @@ -132,7 +135,7 @@ function regexp() { * Parses placeholder dots. * * @private -* @returns {(string|undefined)} `...` or undefined +* @returns {(string|void)} result */ function dots() { if ( ch === '.' ) { @@ -151,47 +154,47 @@ function dots() { * - The function parses `NaN` as a string in order for deep equality checks in the main function to pass for `NaN`. * * @private -* @returns {(boolean|null|string|undefined)} parseed value +* @returns {(boolean|null|string|void)} parsed value */ function word() { switch ( ch ) { case 't': - goNext('t'); - goNext('r'); - goNext('u'); - goNext('e'); + goNext( 't' ); + goNext( 'r' ); + goNext( 'u' ); + goNext( 'e' ); return true; case 'f': - goNext('f'); - goNext('a'); - goNext('l'); - goNext('s'); - goNext('e'); + goNext( 'f' ); + goNext( 'a' ); + goNext( 'l' ); + goNext( 's' ); + goNext( 'e' ); return false; case 'n': - goNext('n'); - goNext('u'); - goNext('l'); - goNext('l'); + goNext( 'n' ); + goNext( 'u' ); + goNext( 'l' ); + goNext( 'l' ); return null; case 'N': - goNext('N'); - goNext('a'); - goNext('N'); + goNext( 'N' ); + goNext( 'a' ); + goNext( 'N' ); return 'NaN'; case 'u': - goNext('u'); - goNext('n'); - goNext('d'); - goNext('e'); - goNext('f'); - goNext('i'); - goNext('n'); - goNext('e'); - goNext('d'); + goNext( 'u' ); + goNext( 'n' ); + goNext( 'd' ); + goNext( 'e' ); + goNext( 'f' ); + goNext( 'i' ); + goNext( 'n' ); + goNext( 'e' ); + goNext( 'd' ); return void 0; default: - error( 'Unexpected `' + ch + '`' ); + error( format( 'Unexpected `%s`', ch ) ); } } @@ -202,9 +205,11 @@ function word() { * @returns {string} type identifier wrapped in quotes */ function type() { - var str = ''; + var ctor; + var str; var arr; + str = ''; if ( ch === '<' ) { goNext( '<' ); if ( ch === '>' ) { @@ -217,14 +222,21 @@ function type() { } goNext( '>' ); if ( ch === '[' ) { - // Encountered a deep instance equality type annotation: + // Encountered a deep instance equality type annotation... arr = array(); if ( isNumberArray( arr ) ) { - return new ctors[ str ]( arr ); + ctor = array2ctor[ str ]; + if ( ctor ) { + return new ctor( arr ); + } + ctor = complex2ctor[ str ]; + if ( ctor ) { + return new ctor( arr[ 0 ], arr[ 1 ] ); + } } return arr; } - return '<'+str+'>'; + return format( '<%s>', str ); } } @@ -304,8 +316,8 @@ function object() { key = objectKey(); white(); goNext( ':' ); - if ( Object.hasOwnProperty.call( obj, key ) ) { - error( 'Duplicate key `' + key + '`' ); + if ( hasOwnProp( obj, key ) ) { + error( format( 'Duplicate key `%s`', key ) ); } obj[ key ] = value(); white(); @@ -334,7 +346,7 @@ function string() { value = ''; - // When parsing for string values, we must look for `'` and `\` characters. + // When parsing for string values, we must look for `'` and `\` characters... if ( ch === '\'' || ch === '"' ) { while ( goNext() ) { if ( ch === '\'' || ch === '"' ) { @@ -347,12 +359,10 @@ function string() { uffff = 0; for ( i = 0; i < 4; i += 1 ) { hex = parseInt( goNext(), 16 ); - - // eslint-disable-next-line max-depth - if ( !isFinite( hex ) ) { + if ( !isfinite( hex ) ) { // eslint-disable-line max-depth break; } - uffff = ( uffff * 16 ) + hex; + uffff = ( uffff*16 ) + hex; } value += String.fromCharCode( uffff ); } else if ( isString( escapee[ch] ) ) { @@ -376,7 +386,6 @@ function string() { */ function objectKey() { var value = ''; - if ( ch === '\'' || ch === '"' ) { return string(); } @@ -390,10 +399,11 @@ function objectKey() { } /** -* Returns the next character and assigns it to the `ch` variable. When there are no more characters, the function returns the empty string. +* Returns the next character and assigns it to the `ch` variable. * * ## Notes * +* - When there are no more characters, the function returns an empty string. * - If a `c` parameter is provided, the function verifies that it matches the current character. * * @private @@ -402,7 +412,7 @@ function objectKey() { */ function goNext( c ) { if ( c && c !== ch ) { - error( 'Expected `' + c + '` instead of `' + ch + '`' ); + error( format( 'Expected `%s` instead of `%s`', c, ch ) ); } ch = text.charAt( at ); at += 1; @@ -468,7 +478,7 @@ function number() { return string; } value = +string; - if ( !isFinite( value ) ) { + if ( !isfinite( value ) ) { return error( 'Bad number' ); } return value; @@ -478,7 +488,7 @@ function number() { // MAIN // /** -* Parses a return annotation value to a JavaScript object. +* Parses a return annotation value and returns a JavaScript object. * * @param {string} source - return annotation value * @returns {*} parsed value @@ -489,6 +499,7 @@ function parse( source ) { text = source; at = 0; ch = ' '; + result = value(); white(); if ( ch ) { diff --git a/lib/node_modules/@stdlib/_tools/doctest/compare-values/test/test.js b/lib/node_modules/@stdlib/_tools/doctest/compare-values/test/test.js index c43cf8c857a1..5a004ff6869f 100644 --- a/lib/node_modules/@stdlib/_tools/doctest/compare-values/test/test.js +++ b/lib/node_modules/@stdlib/_tools/doctest/compare-values/test/test.js @@ -31,6 +31,8 @@ var Complex64Array = require( '@stdlib/array/complex64' ); var Complex128Array = require( '@stdlib/array/complex128' ); var BooleanArray = require( '@stdlib/array/bool' ); var ArrayBuffer = require( '@stdlib/array/buffer' ); +var array = require( '@stdlib/ndarray/array' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var compareValues = require( './../lib' ); @@ -146,7 +148,7 @@ tape( 'the function compares an array and a corresponding return annotation', fu t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' ); actual = [ 0, 2, 3 ]; - expected = '[ , , ]'; + expected = '[ , , ]'; t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' ); actual = [ 0, 2, 3 ]; @@ -329,6 +331,63 @@ tape( 'the function compares a typed array and a corresponding return annotation t.end(); }); +tape( 'the function compares an ndarray and a corresponding return annotation', function test( t ) { + var expected; + var actual; + var msg; + + // Zero-dimensional ndarrays... + actual = scalar2ndarray( 3 ); + expected = '[ 3 ]'; + t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' ); + + actual = scalar2ndarray( 3 ); + expected = '[ 2 ]'; + msg = 'Expected entries [ 2 ], but observed [ 3 ]'; + t.strictEqual( compareValues( actual, expected ), msg, 'returns expected value' ); + + // One-dimensional ndarrays... + actual = array( [ 0, 2, 3 ] ); + expected = '[ 0, 2, 3 ]'; + t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' ); + + actual = array( [ 0, 2, 3 ] ); + expected = '[ 0, 2, 2 ]'; + msg = 'Expected entries [ 0, 2, 2 ], but observed [ 0, 2, 3 ]'; + t.strictEqual( compareValues( actual, expected ), msg, 'returns expected value' ); + + actual = array( new Complex128Array( [ 1.0, 2.0, 3.0, 4.0 ] ) ); + expected = '[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]'; + t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' ); + + actual = array( new Complex128Array( [ 1.0, 2.0, 3.0, 4.0 ] ) ); + expected = '[ [ 1.0, 2.0 ], [ 1.0, 2.0 ] ]'; + msg = 'Expected entries [ [ 1.0, 2.0 ], [ 1.0, 2.0 ] ], but observed [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]'; + t.strictEqual( compareValues( actual, expected ), msg, 'returns expected value' ); + + // Two-dimensional ndarrays... + actual = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ); + expected = '[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]'; + t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' ); + + actual = array( [ [ 1, 2, 3 ], [ 1, 2, 3 ] ] ); + expected = '[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]'; + msg = 'Expected entries [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], but observed [ [ 1, 2, 3 ], [ 1, 2, 3 ] ]'; + t.strictEqual( compareValues( actual, expected ), msg, 'returns expected value' ); + + // Three-dimensional ndarrays... + actual = array( [ [ [ 1, 2, 3 ] ], [ [ 4, 5, 6 ] ] ] ); + expected = '[ [ [ 1, 2, 3 ] ], [ [ 4, 5, 6 ] ] ]'; + t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' ); + + actual = array( [ [ [ 1, 2, 3 ] ], [ [ 1, 2, 3 ] ] ] ); + expected = '[ [ [ 1, 2, 3 ] ], [ [ 4, 5, 6 ] ] ]'; + msg = 'Expected entries [ [ [ 1, 2, 3 ] ], [ [ 4, 5, 6 ] ] ], but observed [ [ [ 1, 2, 3 ] ], [ [ 1, 2, 3 ] ] ]'; + t.strictEqual( compareValues( actual, expected ), msg, 'returns expected value' ); + + t.end(); +}); + tape( 'the function compares a complex number and a corresponding return annotation', function test( t ) { var expected; var actual; @@ -552,5 +611,3 @@ tape( 'the function compares a function and a corresponding return annotation', return x; } }); - -// TODO: Add tests diff --git a/lib/node_modules/@stdlib/_tools/doctest/compare-values/test/test.parse.js b/lib/node_modules/@stdlib/_tools/doctest/compare-values/test/test.parse.js index edb47be77f54..0488901b9cfd 100644 --- a/lib/node_modules/@stdlib/_tools/doctest/compare-values/test/test.parse.js +++ b/lib/node_modules/@stdlib/_tools/doctest/compare-values/test/test.parse.js @@ -30,6 +30,7 @@ var parse = require( './../lib/parse.js' ); // TESTS // tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); t.strictEqual( typeof parse, 'function', 'main export is a function' ); t.end(); }); @@ -115,37 +116,37 @@ tape( 'the function parses a return annotation value for an array', function tes val = '[ 1, 2, 3 ]'; expected = [ 1, 2, 3 ]; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); val = '[ 1, , 3 ]'; expected = [ 1, void 0, 3 ]; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); val = '[ [ 1, , 3 ], [1,,3] ]'; expected = [ [ 1, void 0, 3 ], [ 1, void 0, 3 ] ]; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); val = '[ 1, , , 4, 5 ]'; expected = [ 1, void 0, void 0, 4, 5 ]; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); val = '[ 0, ~0.333, ~0.667, 1 ]'; expected = [ 0, '~0.333', '~0.667', 1 ]; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); val = '[ 1, 2, 3, ... ]'; expected = [ 1, 2, 3, '...' ]; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); val = '[ [ 1, 2, 3, ... ], [ 7, 8, 9, ... ] ]'; expected = [ [ 1, 2, 3, '...' ], [ 7, 8, 9, '...' ] ]; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); val = '[ [ 1, 2, 3, ... ], [ 7, 8, 9, ... ], ... ]'; expected = [ @@ -153,11 +154,11 @@ tape( 'the function parses a return annotation value for an array', function tes [ 7, 8, 9, '...' ], '...' ]; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); val = '[ 1, 2, ... ]'; expected = [ 1, 2, '...' ]; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); t.end(); }); @@ -168,23 +169,23 @@ tape( 'the function parses a return annotation value for a regular expression', val = '/beep/'; expected = /beep/; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); val = '/beep/g'; expected = /beep/g; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); val = '/beep/gi'; expected = /beep/gi; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); val = '/beep/gim'; expected = /beep/gim; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); val = '/[0-9]/m'; expected = /[0-9]/m; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); t.end(); }); @@ -195,22 +196,22 @@ tape( 'the function parses a return annotation value for type annotations', func val = ''; expected = ''; - t.strictEqual( parse( val ), expected, true, 'returns expected value' ); + t.strictEqual( parse( val ), expected, 'returns expected value' ); val = ''; expected = ''; - t.strictEqual( parse( val ), expected, true, 'returns expected value' ); + t.strictEqual( parse( val ), expected, 'returns expected value' ); val = '[ , ]'; expected = [ '', '' ]; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); val = '{ \'a\': , \'b\': }'; expected = { 'a': '', 'b': '' }; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); t.end(); }); @@ -225,19 +226,19 @@ tape( 'the function parses a return annotation value for a plain object', functi 'a': 'beep', 'b': 123 }; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); // Keys with double quotes: val = '{ "a": "beep", "b": 123 }'; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); // Keys with mixed quotes: val = '{ \'a\': "beep", "b": 123 }'; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); // Keys without quotes: val = '{ a: "beep", b: 123 }'; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); t.end(); }); @@ -252,7 +253,7 @@ tape( 'the function parses placeholder annotations', function test( t ) { val = '[...]'; expected = [ '...' ]; - t.deepEqual( parse( val ), expected, true, 'returns expected value' ); + t.deepEqual( parse( val ), expected, 'returns expected value' ); t.end(); }); @@ -265,12 +266,12 @@ tape( 'the function parses a deep instance equality type annotation', function t val = '[ 0, 1, 2, 3 ]'; expected = new Int8Array( [ 0, 1, 2, 3 ] ); actual = parse( val ); - t.deepEqual( actual, expected, true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); val = '[ 1, ..., 3 ]'; expected = [ 1, '...', 3 ]; actual = parse( val ); - t.deepEqual( actual, expected, true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); t.end(); }); diff --git a/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/README.md b/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/README.md index f647f76db214..392cddbdcf28 100644 --- a/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/README.md +++ b/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/README.md @@ -35,14 +35,14 @@ var createAnnotationValue = require( '@stdlib/_tools/doctest/create-annotation-v Creates a doctest return annotation value. ```javascript -var out = createAnnotationValue( 1/3 ); +var out = createAnnotationValue( 1.0/3.0 ); // returns '~0.333' ``` -The function accepts the following `options`: +The function accepts the following options: -- **decimal**: `boolean` indicating whether to always include a decimal point even for integer values. Default: `false`. -- **type**: `boolean` indicating whether to only specify type in created annotation. Default: `false`. +- **decimal**: boolean indicating whether to always include a decimal point even for integer values. Default: `false`. +- **type**: boolean indicating whether to only specify type in created annotation. Default: `false`. - **numel**: number of array elements to display in return annotation values before skipping elements via `...` notation. Default: `4`. - **precision**: number of decimal digits for real-valued numbers. Default: `3`. diff --git a/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/lib/main.js b/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/lib/main.js index 50d00828fab5..f3047117d58c 100644 --- a/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/lib/main.js +++ b/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/lib/main.js @@ -25,6 +25,7 @@ var isArray = require( '@stdlib/assert/is-array' ); var isBoolean = require( '@stdlib/assert/is-boolean' ); var isBooleanArray = require( '@stdlib/assert/is-booleanarray' ); var isBoxedPrimitive = require( '@stdlib/assert/is-boxed-primitive' ); +var isComplex = require( '@stdlib/assert/is-complex' ); var isComplexTypedArray = require( '@stdlib/assert/is-complex-typed-array' ); var isError = require( '@stdlib/assert/is-error' ); var isInfinite = require( '@stdlib/assert/is-infinite' ); @@ -42,9 +43,12 @@ var isUndefined = require( '@stdlib/assert/is-undefined' ); var capitalize = require( '@stdlib/string/capitalize' ); var roundn = require( '@stdlib/math/base/special/roundn' ); var floor = require( '@stdlib/math/base/special/floor' ); +var real = require( '@stdlib/complex/float64/real' ); +var imag = require( '@stdlib/complex/float64/imag' ); var constructorName = require( '@stdlib/utils/constructor-name' ); var objectKeys = require( '@stdlib/utils/keys' ); var typeOf = require( '@stdlib/utils/type-of' ); +var objectAssign = require( '@stdlib/object/assign' ); var reinterpretComplex = require( '@stdlib/strided/base/reinterpret-complex' ); var copyArray = require( '@stdlib/array/base/copy' ); var validate = require( './validate.js' ); @@ -125,6 +129,29 @@ function primitiveAnnotation( actual, opts ) { } } +/** +* Creates a return annotation for a complex number. +* +* @private +* @param {*} actual - actual return value +* @param {Object} opts - function options +* @returns {string} return annotation for complex number +*/ +function complexAnnotation( actual, opts ) { + var copts; + var out; + + copts = objectAssign( {}, opts ); + copts.decimal = true; + + out = '<'+actual.constructor.name+'>'; + out += '[ '+numberAnnotation( real( actual ), copts ); + out += ', '+numberAnnotation( imag( actual ), copts ); + out += ' ]'; + + return out; +} + /** * Creates a return annotation for a JavaScript value. * @@ -143,6 +170,9 @@ function genericAnnotation( actual, opts ) { if ( isPrimitive( actual ) ) { return primitiveAnnotation( actual, opts ); } + if ( isComplex( actual ) ) { + return complexAnnotation( actual, opts ); + } if ( isTypedArray( actual ) || isComplexTypedArray( actual ) || isBooleanArray( actual ) ) { out = '<'+actual.constructor.name+'>'; if ( actual.length === 0 ) { diff --git a/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/test/test.js b/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/test/test.js index 0030afdb4f45..2bfda55c823f 100644 --- a/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/test/test.js +++ b/lib/node_modules/@stdlib/_tools/doctest/create-annotation-value/test/test.js @@ -326,6 +326,21 @@ tape( 'the function creates a deep instance equality annotation value for typed t.end(); }); +tape( 'the function creates a deep instance equality annotation value for complex numbers', function test( t ) { + var expected; + var val; + + val = new Complex128( 3.0, 5.0 ); + expected = '[ 3.0, 5.0 ]'; + t.strictEqual( createAnnotationValue( val ), expected, 'returns expected value' ); + + val = new Complex64( 3.0, 5.0 ); + expected = '[ 3.0, 5.0 ]'; + t.strictEqual( createAnnotationValue( val ), expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function creates a return annotation value for plain objects', function test( t ) { var expected; var val; diff --git a/lib/node_modules/@stdlib/array/base/cusome-by/benchmark/benchmark.assign.length.js b/lib/node_modules/@stdlib/array/base/cusome-by/benchmark/benchmark.assign.length.js index f69c802a58f8..e31e03ff34cb 100644 --- a/lib/node_modules/@stdlib/array/base/cusome-by/benchmark/benchmark.assign.length.js +++ b/lib/node_modules/@stdlib/array/base/cusome-by/benchmark/benchmark.assign.length.js @@ -25,6 +25,7 @@ var pow = require( '@stdlib/math/base/special/pow' ); var isArray = require( '@stdlib/assert/is-array' ); var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; var filled = require( '@stdlib/array/base/filled' ); +var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; var cusomeBy = require( './../lib' ); @@ -92,7 +93,7 @@ function main() { for ( i = min; i <= max; i++ ) { len = pow( 10, i ); f = createBenchmark( len ); - bench( pkg+':assign:len='+len, f ); + bench( format( '%s:assign:len=%d', pkg, len ), f ); } } diff --git a/lib/node_modules/@stdlib/array/base/cusome-by/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/array/base/cusome-by/benchmark/benchmark.length.js index 4ea48252860f..acfc337ab95d 100644 --- a/lib/node_modules/@stdlib/array/base/cusome-by/benchmark/benchmark.length.js +++ b/lib/node_modules/@stdlib/array/base/cusome-by/benchmark/benchmark.length.js @@ -25,6 +25,7 @@ var pow = require( '@stdlib/math/base/special/pow' ); var isArray = require( '@stdlib/assert/is-array' ); var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ); var filled = require( '@stdlib/array/base/filled' ); +var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; var cusomeBy = require( './../lib' ); @@ -89,7 +90,7 @@ function main() { for ( i = min; i <= max; i++ ) { len = pow( 10, i ); f = createBenchmark( len ); - bench( pkg+':len='+len, f ); + bench( format( '%s:len=%d', pkg, len ), f ); } } diff --git a/lib/node_modules/@stdlib/array/base/fill-by/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/array/base/fill-by/benchmark/benchmark.length.js index 04b8f9381231..4ebd66af2e83 100644 --- a/lib/node_modules/@stdlib/array/base/fill-by/benchmark/benchmark.length.js +++ b/lib/node_modules/@stdlib/array/base/fill-by/benchmark/benchmark.length.js @@ -24,6 +24,7 @@ var bench = require( '@stdlib/bench' ); var pow = require( '@stdlib/math/base/special/pow' ); var isArray = require( '@stdlib/assert/is-array' ); var ones = require( '@stdlib/array/base/ones' ); +var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; var fillBy = require( './../lib' ); @@ -102,7 +103,7 @@ function main() { len = pow( 10, i ); f = createBenchmark( len ); - bench( pkg+':dtype=generic,len='+len, f ); + bench( format( '%s:dtype=generic,len=%d', pkg, len ), f ); } } diff --git a/lib/node_modules/@stdlib/array/base/fill/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/array/base/fill/benchmark/benchmark.length.js index a13928d65ad7..08a26ab4c619 100644 --- a/lib/node_modules/@stdlib/array/base/fill/benchmark/benchmark.length.js +++ b/lib/node_modules/@stdlib/array/base/fill/benchmark/benchmark.length.js @@ -24,6 +24,7 @@ var bench = require( '@stdlib/bench' ); var pow = require( '@stdlib/math/base/special/pow' ); var isArray = require( '@stdlib/assert/is-array' ); var ones = require( '@stdlib/array/base/ones' ); +var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; var fill = require( './../lib' ); @@ -89,7 +90,7 @@ function main() { len = pow( 10, i ); f = createBenchmark( len ); - bench( pkg+':dtype=generic,len='+len, f ); + bench( format( '%s:dtype=generic,len=%d', pkg, len ), f ); } } diff --git a/lib/node_modules/@stdlib/array/base/filled-by/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/array/base/filled-by/benchmark/benchmark.length.js index 8d6189e7ad76..7d3b192fa37d 100644 --- a/lib/node_modules/@stdlib/array/base/filled-by/benchmark/benchmark.length.js +++ b/lib/node_modules/@stdlib/array/base/filled-by/benchmark/benchmark.length.js @@ -24,6 +24,7 @@ var bench = require( '@stdlib/bench' ); var pow = require( '@stdlib/math/base/special/pow' ); var isArray = require( '@stdlib/assert/is-array' ); var constantFunction = require( '@stdlib/utils/constant-function' ); +var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; var filledBy = require( './../lib' ); @@ -91,7 +92,7 @@ function main() { len = pow( 10, i ); f = createBenchmark( len ); - bench( pkg+':len='+len, f ); + bench( format( '%s:len=%d', pkg, len ), f ); } } diff --git a/lib/node_modules/@stdlib/array/base/filled/benchmark/benchmark.length.js b/lib/node_modules/@stdlib/array/base/filled/benchmark/benchmark.length.js index d004af28a925..d6b2a6cbb63a 100644 --- a/lib/node_modules/@stdlib/array/base/filled/benchmark/benchmark.length.js +++ b/lib/node_modules/@stdlib/array/base/filled/benchmark/benchmark.length.js @@ -23,6 +23,7 @@ var bench = require( '@stdlib/bench' ); var pow = require( '@stdlib/math/base/special/pow' ); var isArray = require( '@stdlib/assert/is-array' ); +var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; var filled = require( './../lib' ); @@ -87,7 +88,7 @@ function main() { len = pow( 10, i ); f = createBenchmark( len ); - bench( pkg+':len='+len, f ); + bench( format( '%s:len=%d', pkg, len ), f ); } } diff --git a/lib/node_modules/@stdlib/array/base/filled2d/benchmark/benchmark.size.js b/lib/node_modules/@stdlib/array/base/filled2d/benchmark/benchmark.size.js index bb7c5ab9db9e..963d2caf393d 100644 --- a/lib/node_modules/@stdlib/array/base/filled2d/benchmark/benchmark.size.js +++ b/lib/node_modules/@stdlib/array/base/filled2d/benchmark/benchmark.size.js @@ -25,6 +25,7 @@ var pow = require( '@stdlib/math/base/special/pow' ); var floor = require( '@stdlib/math/base/special/floor' ); var sqrt = require( '@stdlib/math/base/special/sqrt' ); var isArrayArray = require( '@stdlib/assert/is-array-array' ); +var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; var filled2d = require( './../lib' ); @@ -89,7 +90,7 @@ function main() { N = floor( sqrt( pow( 10, i ) ) ); f = createBenchmark( N ); - bench( pkg+'::square_matrix:size='+(N*N), f ); + bench( format( '%s::square_matrix:size=%d', pkg, N*N ), f ); } } diff --git a/lib/node_modules/@stdlib/assert/is-configurable-property-in/benchmark/benchmark.js b/lib/node_modules/@stdlib/assert/is-configurable-property-in/benchmark/benchmark.js index 403eaaea9a83..f604ad721813 100644 --- a/lib/node_modules/@stdlib/assert/is-configurable-property-in/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/assert/is-configurable-property-in/benchmark/benchmark.js @@ -22,6 +22,7 @@ var bench = require( '@stdlib/bench' ); var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var zeros = require( '@stdlib/array/base/zeros' ); var pkg = require( './../package.json' ).name; var isConfigurablePropertyIn = require( './../lib' ); @@ -33,7 +34,7 @@ bench( pkg, function benchmark( b ) { var arr; var i; - arr = new Array( 100 ); + arr = zeros( 100 ); b.tic(); for ( i = 0; i < b.iterations; i++ ) { diff --git a/lib/node_modules/@stdlib/assert/is-data-property-in/examples/index.js b/lib/node_modules/@stdlib/assert/is-data-property-in/examples/index.js index 21e39e825a6a..4aeb59c449c9 100644 --- a/lib/node_modules/@stdlib/assert/is-data-property-in/examples/index.js +++ b/lib/node_modules/@stdlib/assert/is-data-property-in/examples/index.js @@ -16,7 +16,7 @@ * limitations under the License. */ -/* eslint-disable object-curly-newline */ +/* eslint-disable object-curly-newline, stdlib/eol-open-bracket-spacing */ 'use strict'; diff --git a/lib/node_modules/@stdlib/blas/base/caxpy/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/blas/base/caxpy/benchmark/c/benchmark.length.c index 69d5c9c8d9f2..391006a54011 100644 --- a/lib/node_modules/@stdlib/blas/base/caxpy/benchmark/c/benchmark.length.c +++ b/lib/node_modules/@stdlib/blas/base/caxpy/benchmark/c/benchmark.length.c @@ -97,13 +97,15 @@ static float rand_float( void ) { */ static double benchmark1( int iterations, int len ) { stdlib_complex64_t alpha; - float x[ len*2 ]; - float y[ len*2 ]; + float *x; + float *y; double elapsed; double t; int i; alpha = stdlib_complex64( 1.0f, 0.0f ); + x = (float *) malloc( len * 2 * sizeof( float ) ); + y = (float *) malloc( len * 2 * sizeof( float ) ); for ( i = 0; i < len*2; i += 2 ) { x[ i ] = ( rand_float()*2.0f ) - 1.0f; x[ i+1 ] = ( rand_float()*2.0f ) - 1.0f; @@ -122,6 +124,8 @@ static double benchmark1( int iterations, int len ) { if ( y[ 0 ] != y[ 0 ] ) { printf( "should not return NaN\n" ); } + free( x ); + free( y ); return elapsed; } @@ -134,13 +138,15 @@ static double benchmark1( int iterations, int len ) { */ static double benchmark2( int iterations, int len ) { stdlib_complex64_t alpha; - float x[ len*2 ]; - float y[ len*2 ]; + float *x; + float *y; double elapsed; double t; int i; alpha = stdlib_complex64( 1.0f, 0.0f ); + x = (float *) malloc( len * 2 * sizeof( float ) ); + y = (float *) malloc( len * 2 * sizeof( float ) ); for ( i = 0; i < len*2; i += 2 ) { x[ i ] = ( rand_float()*2.0f ) - 1.0f; x[ i+1 ] = ( rand_float()*2.0f ) - 1.0f; @@ -159,6 +165,8 @@ static double benchmark2( int iterations, int len ) { if ( y[ 0 ] != y[ 0 ] ) { printf( "should not return NaN\n" ); } + free( x ); + free( y ); return elapsed; } diff --git a/lib/node_modules/@stdlib/blas/base/cscal/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/blas/base/cscal/benchmark/c/benchmark.length.c index d8304dd48453..ee2163e5b25e 100644 --- a/lib/node_modules/@stdlib/blas/base/cscal/benchmark/c/benchmark.length.c +++ b/lib/node_modules/@stdlib/blas/base/cscal/benchmark/c/benchmark.length.c @@ -97,12 +97,13 @@ static float rand_float( void ) { */ static double benchmark1( int iterations, int len ) { stdlib_complex64_t alpha; - float x[ len*2 ]; + float *x; double elapsed; double t; int i; alpha = stdlib_complex64( 1.0f, 0.0f ); + x = (float *) malloc( len * 2 * sizeof( float ) ); for ( i = 0; i < len*2; i += 2 ) { x[ i ] = ( rand_float()*2.0f ) - 1.0f; x[ i+1 ] = ( rand_float()*2.0f ) - 1.0f; @@ -119,6 +120,7 @@ static double benchmark1( int iterations, int len ) { if ( x[ 0 ] != x[ 0 ] ) { printf( "should not return NaN\n" ); } + free( x ); return elapsed; } @@ -131,12 +133,13 @@ static double benchmark1( int iterations, int len ) { */ static double benchmark2( int iterations, int len ) { stdlib_complex64_t alpha; - float x[ len*2 ]; + float *x; double elapsed; double t; int i; alpha = stdlib_complex64( 1.0f, 0.0f ); + x = (float *) malloc( len * 2 * sizeof( float ) ); for ( i = 0; i < len*2; i += 2 ) { x[ i ] = ( rand_float()*2.0f ) - 1.0f; x[ i+1 ] = ( rand_float()*2.0f ) - 1.0f; @@ -153,6 +156,7 @@ static double benchmark2( int iterations, int len ) { if ( x[ 0 ] != x[ 0 ] ) { printf( "should not return NaN\n" ); } + free( x ); return elapsed; } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusum/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusum/lib/main.js index 24c5dbc3c906..555d2500283b 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusum/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusum/lib/main.js @@ -34,7 +34,7 @@ var strided = require( '@stdlib/blas/ext/base/dcusum' ).ndarray; * Computes the cumulative sum of a one-dimensional double-precision floating-point ndarray. * * @param {ArrayLikeObject} arrays - array-like object containing an input ndarray, an output ndarray, and an ndarray containing the initial sum -* @returns {number} sum +* @returns {Object} output ndarray * * @example * var Float64Array = require( '@stdlib/array/float64' ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/README.md new file mode 100644 index 000000000000..3789090b9237 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/README.md @@ -0,0 +1,146 @@ + + +# dcusumkbn + +> Compute the cumulative sum of a one-dimensional double-precision floating-point ndarray using an improved Kahan–Babuška algorithm. + +
+ +
+ + + +
+ +## Usage + +```javascript +var dcusumkbn = require( '@stdlib/blas/ext/base/ndarray/dcusumkbn' ); +``` + +#### dcusumkbn( arrays ) + +Computes the cumulative sum of a one-dimensional double-precision floating-point ndarray using an improved Kahan–Babuška algorithm. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +var xbuf = new Float64Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +var ybuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); +var y = new ndarray( 'float64', ybuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +var initial = scalar2ndarray( 0.0, 'float64', 'row-major' ); + +var v = dcusumkbn( [ x, y, initial ] ); +// returns + +var bool = ( v === y ); +// returns true + +var arr = ndarray2array( v ); +// returns [ 1.0, 4.0, 8.0, 10.0 ] +``` + +The function has the following parameters: + +- **arrays**: array-like object containing a one-dimensional input ndarray, a one-dimensional output ndarray, and a zero-dimensional ndarray containing the initial sum. + +
+ + + +
+ +## Notes + +- If provided an empty one-dimensional input ndarray, the function returns the output ndarray unchanged. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var zerosLike = require( '@stdlib/ndarray/zeros-like' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dcusumkbn = require( '@stdlib/blas/ext/base/ndarray/dcusumkbn' ); + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var y = zerosLike( x ); +console.log( ndarray2array( y ) ); + +var initial = scalar2ndarray( 100.0, { + 'dtype': 'float64' +}); + +var v = dcusumkbn( [ x, y, initial ] ); +console.log( ndarray2array( v ) ); +``` + +
+ + + +
+ +## References + +- Neumaier, Arnold. 1974. "Rounding Error Analysis of Some Methods for Summing Finite Sums." _Zeitschrift Für Angewandte Mathematik Und Mechanik_ 54 (1): 39–51. doi:[10.1002/zamm.19740540106][@neumaier:1974a]. + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/benchmark/benchmark.js new file mode 100644 index 000000000000..5c3b52bdf98c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/benchmark/benchmark.js @@ -0,0 +1,112 @@ +/** +* @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 zeros = require( '@stdlib/array/zeros' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +var pkg = require( './../package.json' ).name; +var dcusumkbn = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var initial; + var xbuf; + var ybuf; + var x; + var y; + + xbuf = uniform( len, -10.0, 10.0, options ); + x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); + + initial = scalar2ndarray( 0.0, options.dtype, 'row-major' ); + + ybuf = zeros( len, options.dtype ); + y = new ndarray( options.dtype, ybuf, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = dcusumkbn( [ x, y, initial ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( v.get( i%len ) ) ) { + 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( pkg + ':len=' + len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/docs/repl.txt new file mode 100644 index 000000000000..6cdda69cba10 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/docs/repl.txt @@ -0,0 +1,39 @@ + +{{alias}}( arrays ) + Computes the cumulative sum of a one-dimensional double-precision floating- + point ndarray using an improved Kahan–Babuška algorithm. + + If provided an empty input ndarray, the function returns the output ndarray + unchanged. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing a one-dimensional input ndarray, a one- + dimensional output ndarray, and a zero-dimensional ndarray containing + the initial sum. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 2.0 ] ); + > var ybuf = new {{alias:@stdlib/array/float64}}( [ 0.0, 0.0, 0.0 ] ); + > var dt = 'float64'; + > var sh = [ xbuf.length ]; + > var st = [ 1 ]; + > var oo = 0; + > var ord = 'row-major'; + > var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, st, oo, ord ); + > var y = new {{alias:@stdlib/ndarray/ctor}}( dt, ybuf, sh, st, oo, ord ); + > var s = {{alias:@stdlib/ndarray/from-scalar}}( 0.0, { 'dtype': dt } ); + > {{alias}}( [ x, y, s ] ); + > {{alias:@stdlib/ndarray/to-array}}( y ) + [ 1.0, -1.0, 1.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/docs/types/index.d.ts new file mode 100644 index 000000000000..692b9b9b729b --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/docs/types/index.d.ts @@ -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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { float64ndarray } from '@stdlib/types/ndarray'; + +/** +* Computes the cumulative sum of a one-dimensional double-precision floating-point ndarray using an improved Kahan–Babuška algorithm. +* +* @param arrays - array-like object containing an input ndarray, an output ndarray, and an ndarray containing the initial sum +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var xbuf = new Float64Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); +* var y = new ndarray( 'float64', ybuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var initial = scalar2ndarray( 0.0, 'float64', 'row-major' ); +* +* var v = dcusumkbn( [ x, y, initial ] ); +* // returns +* +* var bool = ( v === y ); +* // returns true +* +* var arr = ndarray2array( v ); +* // returns [ 1.0, 4.0, 8.0, 10.0 ] +*/ +declare function dcusumkbn( arrays: [ float64ndarray, float64ndarray, float64ndarray ] ): float64ndarray; + + +// EXPORTS // + +export = dcusumkbn; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/docs/types/test.ts new file mode 100644 index 000000000000..ece274748381 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/docs/types/test.ts @@ -0,0 +1,69 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import dcusumkbn = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float64' + }); + const y = zeros( [ 10 ], { + 'dtype': 'float64' + }); + const initial = zeros( [], { + 'dtype': 'float64' + }); + + dcusumkbn( [ x, y, initial ] ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + dcusumkbn( '10' ); // $ExpectError + dcusumkbn( 10 ); // $ExpectError + dcusumkbn( true ); // $ExpectError + dcusumkbn( false ); // $ExpectError + dcusumkbn( null ); // $ExpectError + dcusumkbn( undefined ); // $ExpectError + dcusumkbn( [] ); // $ExpectError + dcusumkbn( {} ); // $ExpectError + dcusumkbn( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float64' + }); + const y = zeros( [ 10 ], { + 'dtype': 'float64' + }); + const initial = zeros( [], { + 'dtype': 'float64' + }); + + dcusumkbn(); // $ExpectError + dcusumkbn( [ x, y, initial ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/examples/index.js new file mode 100644 index 000000000000..67257a7cedc3 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/examples/index.js @@ -0,0 +1,42 @@ +/** +* @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 ndarray = require( '@stdlib/ndarray/base/ctor' ); +var zerosLike = require( '@stdlib/ndarray/zeros-like' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dcusumkbn = require( './../lib' ); + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var y = zerosLike( x ); +console.log( ndarray2array( y ) ); + +var initial = scalar2ndarray( 100.0, { + 'dtype': 'float64' +}); + +var v = dcusumkbn( [ x, y, initial ] ); +console.log( ndarray2array( v ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/lib/index.js new file mode 100644 index 000000000000..fedfcf64e6a9 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/lib/index.js @@ -0,0 +1,58 @@ +/** +* @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 cumulative sum of a one-dimensional double-precision floating-point ndarray using an improved Kahan–Babuška algorithm. +* +* @module @stdlib/blas/ext/base/ndarray/dcusumkbn +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +* var dcusumkbn = require( '@stdlib/blas/ext/base/ndarray/dcusumkbn' ); +* +* var xbuf = new Float64Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); +* var y = new ndarray( 'float64', ybuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var initial = scalar2ndarray( 0.0, 'float64', 'row-major' ); +* +* var v = dcusumkbn( [ x, y, initial ] ); +* // returns +* +* var bool = ( v === y ); +* // returns true +* +* var arr = ndarray2array( v ); +* // returns [ 1.0, 4.0, 8.0, 10.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/lib/main.js new file mode 100644 index 000000000000..6374ff5dcf38 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/lib/main.js @@ -0,0 +1,73 @@ +/** +* @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 numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); +var strided = require( '@stdlib/blas/ext/base/dcusumkbn' ).ndarray; + + +// MAIN // + +/** +* Computes the cumulative sum of a one-dimensional double-precision floating-point ndarray using an improved Kahan–Babuška algorithm. +* +* @param {ArrayLikeObject} arrays - array-like object containing an input ndarray, an output ndarray, and an ndarray containing the initial sum +* @returns {Object} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +* +* var xbuf = new Float64Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); +* var y = new ndarray( 'float64', ybuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var initial = scalar2ndarray( 0.0, 'float64', 'row-major' ); +* +* var v = dcusumkbn( [ x, y, initial ] ); +* // returns +* +* var bool = ( v === y ); +* // returns true +* +* var arr = ndarray2array( v ); +* // returns [ 1.0, 4.0, 8.0, 10.0 ] +*/ +function dcusumkbn( arrays ) { + var x = arrays[ 0 ]; + var y = arrays[ 1 ]; + var v = ndarraylike2scalar( arrays[ 2 ] ); + strided( numelDimension( x, 0 ), v, getData( x ), getStride( x, 0 ), getOffset( x ), getData( y ), getStride( y, 0 ), getOffset( y ) ); // eslint-disable-line max-len + return y; +} + + +// EXPORTS // + +module.exports = dcusumkbn; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/package.json new file mode 100644 index 000000000000..dd0e343676cd --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/dcusumkbn", + "version": "0.0.0", + "description": "Compute the cumulative sum of a one-dimensional double-precision floating-point ndarray using an improved Kahan–Babuška algorithm.", + "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", + "cumulative", + "accumulate", + "sum", + "kahan", + "babuska", + "kbn", + "total", + "summation", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/test/test.js new file mode 100644 index 000000000000..a99f4c39addf --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn/test/test.js @@ -0,0 +1,311 @@ +/** +* @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 isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var scalar2ndarrayLike = require( '@stdlib/ndarray/base/from-scalar-like' ); +var zerosLike = require( '@stdlib/ndarray/zeros-like' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var dcusumkbn = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Float64Array} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float64', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dcusumkbn, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( dcusumkbn.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function computes the cumulative sum of a one-dimensional ndarray', function test( t ) { + var expected; + var initial; + var xbuf; + var x; + var y; + var v; + + xbuf = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] ); + x = vector( xbuf, 6, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, 0.0 ); + v = dcusumkbn( [ x, y, initial ] ); + + expected = new Float64Array( [ 1.0, -1.0, -5.0, 0.0, 0.0, 3.0 ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float64Array( [ -4.0, -5.0 ] ); + x = vector( xbuf, 2, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, 10.0 ); + v = dcusumkbn( [ x, y, initial ] ); + + expected = new Float64Array( [ 6.0, 1.0 ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float64Array( [ -0.0, 0.0, -0.0 ] ); + x = vector( xbuf, 3, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, -0.0 ); + v = dcusumkbn( [ x, y, initial ] ); + + expected = new Float64Array( [ -0.0, 0.0, 0.0 ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float64Array( [ -0.0, -0.0, -0.0 ] ); + x = vector( xbuf, 3, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, -0.0 ); + v = dcusumkbn( [ x, y, initial ] ); + + expected = new Float64Array( [ -0.0, -0.0, -0.0 ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float64Array( [ -0.0, -0.0, -0.0 ] ); + x = vector( xbuf, 3, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, 0.0 ); + v = dcusumkbn( [ x, y, initial ] ); + + expected = new Float64Array( [ 0.0, 0.0, 0.0 ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float64Array( [ NaN ] ); + x = vector( xbuf, 1, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, 0.0 ); + v = dcusumkbn( [ x, y, initial ] ); + + expected = new Float64Array( [ NaN ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float64Array( [ NaN, NaN ] ); + x = vector( xbuf, 2, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, NaN ); + v = dcusumkbn( [ x, y, initial ] ); + + expected = new Float64Array( [ NaN, NaN ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an empty ndarray, the function returns the output array unchanged', function test( t ) { + var expected; + var initial; + var xbuf; + var x; + var y; + var v; + + xbuf = new Float64Array( [] ); + x = vector( xbuf, 0, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, 100.0 ); + + v = dcusumkbn( [ x, y, initial ] ); + + expected = new Float64Array( [] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-unit strides', function test( t ) { + var expected; + var initial; + var xbuf; + var ybuf; + var x; + var y; + var v; + + xbuf = new Float64Array([ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]); + x = vector( xbuf, 4, 2, 0 ); + + ybuf = new Float64Array([ + 0.0, // 0 + 0.0, + 0.0, // 1 + 0.0, + 0.0, // 2 + 0.0, + 0.0, // 3 + 0.0 + ]); + y = vector( ybuf, 4, 2, 0 ); + + initial = scalar2ndarrayLike( x, 5.0 ); + + v = dcusumkbn( [ x, y, initial ] ); + + expected = new Float64Array([ + 6.0, // 0 + 0.0, + 8.0, // 1 + 0.0, + 6.0, // 2 + 0.0, + 10.0, // 3 + 0.0 + ]); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having negative strides', function test( t ) { + var expected; + var initial; + var xbuf; + var ybuf; + var x; + var y; + var v; + + xbuf = new Float64Array([ + 1.0, // 2 + -2.0, + 3.0, // 1 + 4.0, + -5.0 // 0 + ]); + x = vector( xbuf, 3, -2, 4 ); + + ybuf = new Float64Array([ + 0.0, // 2 + 0.0, // 1 + 0.0, // 0 + 0.0, + 0.0 + ]); + y = vector( ybuf, 3, -1, 2 ); + + initial = scalar2ndarrayLike( x, 0.0 ); + + v = dcusumkbn( [ x, y, initial ] ); + + expected = new Float64Array([ + -1.0, // 2 + -2.0, // 1 + -5.0, // 0 + 0.0, + 0.0 + ]); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-zero offsets', function test( t ) { + var expected; + var initial; + var xbuf; + var ybuf; + var x; + var y; + var v; + + xbuf = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]); + x = vector( xbuf, 4, 2, 1 ); + + ybuf = new Float64Array([ + 0.0, + 0.0, + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0, // 3 + 0.0, + 0.0 + ]); + y = vector( ybuf, 4, 1, 2 ); + + initial = scalar2ndarrayLike( x, 0.0 ); + + v = dcusumkbn( [ x, y, initial ] ); + + expected = new Float64Array([ + 0.0, + 0.0, + 1.0, // 0 + -1.0, // 1 + 1.0, // 2 + 5.0, // 3 + 0.0, + 0.0 + ]); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/README.md new file mode 100644 index 000000000000..b9b21c3d8fb5 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/README.md @@ -0,0 +1,146 @@ + + +# dcusumkbn2 + +> Compute the cumulative sum of a one-dimensional double-precision floating-point ndarray using a second-order iterative Kahan–Babuška algorithm. + +
+ +
+ + + +
+ +## Usage + +```javascript +var dcusumkbn2 = require( '@stdlib/blas/ext/base/ndarray/dcusumkbn2' ); +``` + +#### dcusumkbn2( arrays ) + +Computes the cumulative sum of a one-dimensional double-precision floating-point ndarray using a second-order iterative Kahan–Babuška algorithm. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +var xbuf = new Float64Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +var ybuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); +var y = new ndarray( 'float64', ybuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +var initial = scalar2ndarray( 0.0, 'float64', 'row-major' ); + +var v = dcusumkbn2( [ x, y, initial ] ); +// returns + +var bool = ( v === y ); +// returns true + +var arr = ndarray2array( v ); +// returns [ 1.0, 4.0, 8.0, 10.0 ] +``` + +The function has the following parameters: + +- **arrays**: array-like object containing a one-dimensional input ndarray, a one-dimensional output ndarray, and a zero-dimensional ndarray containing the initial sum. + +
+ + + +
+ +## Notes + +- If provided an empty one-dimensional input ndarray, the function returns the output ndarray unchanged. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var zerosLike = require( '@stdlib/ndarray/zeros-like' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dcusumkbn2 = require( '@stdlib/blas/ext/base/ndarray/dcusumkbn2' ); + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var y = zerosLike( x ); +console.log( ndarray2array( y ) ); + +var initial = scalar2ndarray( 100.0, { + 'dtype': 'float64' +}); + +var v = dcusumkbn2( [ x, y, initial ] ); +console.log( ndarray2array( v ) ); +``` + +
+ + + +
+ +## References + +- Klein, Andreas. 2005. "A Generalized Kahan-Babuška-Summation-Algorithm." _Computing_ 76 (3): 279–93. doi:[10.1007/s00607-005-0139-x][@klein:2005a]. + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/benchmark/benchmark.js new file mode 100644 index 000000000000..f683efd834b8 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/benchmark/benchmark.js @@ -0,0 +1,112 @@ +/** +* @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 zeros = require( '@stdlib/array/zeros' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +var pkg = require( './../package.json' ).name; +var dcusumkbn2 = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var initial; + var xbuf; + var ybuf; + var x; + var y; + + xbuf = uniform( len, -10.0, 10.0, options ); + x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); + + initial = scalar2ndarray( 0.0, options.dtype, 'row-major' ); + + ybuf = zeros( len, options.dtype ); + y = new ndarray( options.dtype, ybuf, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = dcusumkbn2( [ x, y, initial ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( v.get( i % len ) ) ) { + 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( pkg + ':len=' + len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/docs/repl.txt new file mode 100644 index 000000000000..7c015c1b2f35 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/docs/repl.txt @@ -0,0 +1,39 @@ + +{{alias}}( arrays ) + Computes the cumulative sum of a one-dimensional double-precision floating- + point ndarray using a second-order iterative Kahan–Babuška algorithm. + + If provided an empty input ndarray, the function returns the output ndarray + unchanged. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing a one-dimensional input ndarray, a one- + dimensional output ndarray, and a zero-dimensional ndarray containing + the initial sum. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 2.0 ] ); + > var ybuf = new {{alias:@stdlib/array/float64}}( [ 0.0, 0.0, 0.0 ] ); + > var dt = 'float64'; + > var sh = [ xbuf.length ]; + > var st = [ 1 ]; + > var oo = 0; + > var ord = 'row-major'; + > var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, st, oo, ord ); + > var y = new {{alias:@stdlib/ndarray/ctor}}( dt, ybuf, sh, st, oo, ord ); + > var s = {{alias:@stdlib/ndarray/from-scalar}}( 0.0, { 'dtype': dt } ); + > {{alias}}( [ x, y, s ] ); + > {{alias:@stdlib/ndarray/to-array}}( y ) + [ 1.0, -1.0, 1.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/docs/types/index.d.ts new file mode 100644 index 000000000000..4d516c5c7814 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/docs/types/index.d.ts @@ -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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { float64ndarray } from '@stdlib/types/ndarray'; + +/** +* Computes the cumulative sum of a one-dimensional double-precision floating-point ndarray using a second-order iterative Kahan–Babuška algorithm. +* +* @param arrays - array-like object containing an input ndarray, an output ndarray, and an ndarray containing the initial sum +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var xbuf = new Float64Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); +* var y = new ndarray( 'float64', ybuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var initial = scalar2ndarray( 0.0, 'float64', 'row-major' ); +* +* var v = dcusumkbn2( [ x, y, initial ] ); +* // returns +* +* var bool = ( v === y ); +* // returns true +* +* var arr = ndarray2array( v ); +* // returns [ 1.0, 4.0, 8.0, 10.0 ] +*/ +declare function dcusumkbn2( arrays: [ float64ndarray, float64ndarray, float64ndarray ] ): float64ndarray; + + +// EXPORTS // + +export = dcusumkbn2; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/docs/types/test.ts new file mode 100644 index 000000000000..4065ad3398a6 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/docs/types/test.ts @@ -0,0 +1,69 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import dcusumkbn2 = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float64' + }); + const y = zeros( [ 10 ], { + 'dtype': 'float64' + }); + const initial = zeros( [], { + 'dtype': 'float64' + }); + + dcusumkbn2( [ x, y, initial ] ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + dcusumkbn2( '10' ); // $ExpectError + dcusumkbn2( 10 ); // $ExpectError + dcusumkbn2( true ); // $ExpectError + dcusumkbn2( false ); // $ExpectError + dcusumkbn2( null ); // $ExpectError + dcusumkbn2( undefined ); // $ExpectError + dcusumkbn2( [] ); // $ExpectError + dcusumkbn2( {} ); // $ExpectError + dcusumkbn2( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float64' + }); + const y = zeros( [ 10 ], { + 'dtype': 'float64' + }); + const initial = zeros( [], { + 'dtype': 'float64' + }); + + dcusumkbn2(); // $ExpectError + dcusumkbn2( [ x, y, initial ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/examples/index.js new file mode 100644 index 000000000000..c31789b56a53 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/examples/index.js @@ -0,0 +1,42 @@ +/** +* @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 ndarray = require( '@stdlib/ndarray/base/ctor' ); +var zerosLike = require( '@stdlib/ndarray/zeros-like' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dcusumkbn2 = require( './../lib' ); + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var y = zerosLike( x ); +console.log( ndarray2array( y ) ); + +var initial = scalar2ndarray( 100.0, { + 'dtype': 'float64' +}); + +var v = dcusumkbn2( [ x, y, initial ] ); +console.log( ndarray2array( v ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/lib/index.js new file mode 100644 index 000000000000..e18ee2a56694 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/lib/index.js @@ -0,0 +1,58 @@ +/** +* @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 cumulative sum of a one-dimensional double-precision floating-point ndarray using a second-order iterative Kahan–Babuška algorithm. +* +* @module @stdlib/blas/ext/base/ndarray/dcusumkbn2 +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +* var dcusumkbn2 = require( '@stdlib/blas/ext/base/ndarray/dcusumkbn2' ); +* +* var xbuf = new Float64Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); +* var y = new ndarray( 'float64', ybuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var initial = scalar2ndarray( 0.0, 'float64', 'row-major' ); +* +* var v = dcusumkbn2( [ x, y, initial ] ); +* // returns +* +* var bool = ( v === y ); +* // returns true +* +* var arr = ndarray2array( v ); +* // returns [ 1.0, 4.0, 8.0, 10.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/lib/main.js new file mode 100644 index 000000000000..08ed6f1d330b --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/lib/main.js @@ -0,0 +1,73 @@ +/** +* @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 numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); +var strided = require( '@stdlib/blas/ext/base/dcusumkbn2' ).ndarray; + + +// MAIN // + +/** +* Computes the cumulative sum of a one-dimensional double-precision floating-point ndarray using a second-order iterative Kahan–Babuška algorithm. +* +* @param {ArrayLikeObject} arrays - array-like object containing an input ndarray, an output ndarray, and an ndarray containing the initial sum +* @returns {Object} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +* +* var xbuf = new Float64Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); +* var y = new ndarray( 'float64', ybuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var initial = scalar2ndarray( 0.0, 'float64', 'row-major' ); +* +* var v = dcusumkbn2( [ x, y, initial ] ); +* // returns +* +* var bool = ( v === y ); +* // returns true +* +* var arr = ndarray2array( v ); +* // returns [ 1.0, 4.0, 8.0, 10.0 ] +*/ +function dcusumkbn2( arrays ) { + var x = arrays[ 0 ]; + var y = arrays[ 1 ]; + var v = ndarraylike2scalar( arrays[ 2 ] ); + strided( numelDimension( x, 0 ), v, getData( x ), getStride( x, 0 ), getOffset( x ), getData( y ), getStride( y, 0 ), getOffset( y ) ); // eslint-disable-line max-len + return y; +} + + +// EXPORTS // + +module.exports = dcusumkbn2; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/package.json new file mode 100644 index 000000000000..95fcb3d74268 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/dcusumkbn2", + "version": "0.0.0", + "description": "Compute the cumulative sum of a one-dimensional double-precision floating-point ndarray using a second-order iterative Kahan–Babuška algorithm.", + "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", + "cumulative", + "accumulate", + "sum", + "kahan", + "babuska", + "kbn2", + "total", + "summation", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/test/test.js new file mode 100644 index 000000000000..cbdfe360fb55 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcusumkbn2/test/test.js @@ -0,0 +1,311 @@ +/** +* @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 isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var scalar2ndarrayLike = require( '@stdlib/ndarray/base/from-scalar-like' ); +var zerosLike = require( '@stdlib/ndarray/zeros-like' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var dcusumkbn2 = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Float64Array} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float64', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dcusumkbn2, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( dcusumkbn2.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function computes the cumulative sum of a one-dimensional ndarray', function test( t ) { + var expected; + var initial; + var xbuf; + var x; + var y; + var v; + + xbuf = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] ); + x = vector( xbuf, 6, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, 0.0 ); + v = dcusumkbn2( [ x, y, initial ] ); + + expected = new Float64Array( [ 1.0, -1.0, -5.0, 0.0, 0.0, 3.0 ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float64Array( [ -4.0, -5.0 ] ); + x = vector( xbuf, 2, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, 10.0 ); + v = dcusumkbn2( [ x, y, initial ] ); + + expected = new Float64Array( [ 6.0, 1.0 ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float64Array( [ -0.0, 0.0, -0.0 ] ); + x = vector( xbuf, 3, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, -0.0 ); + v = dcusumkbn2( [ x, y, initial ] ); + + expected = new Float64Array( [ -0.0, 0.0, 0.0 ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float64Array( [ -0.0, -0.0, -0.0 ] ); + x = vector( xbuf, 3, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, -0.0 ); + v = dcusumkbn2( [ x, y, initial ] ); + + expected = new Float64Array( [ -0.0, -0.0, -0.0 ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float64Array( [ -0.0, -0.0, -0.0 ] ); + x = vector( xbuf, 3, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, 0.0 ); + v = dcusumkbn2( [ x, y, initial ] ); + + expected = new Float64Array( [ 0.0, 0.0, 0.0 ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float64Array( [ NaN ] ); + x = vector( xbuf, 1, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, 0.0 ); + v = dcusumkbn2( [ x, y, initial ] ); + + expected = new Float64Array( [ NaN ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float64Array( [ NaN, NaN ] ); + x = vector( xbuf, 2, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, NaN ); + v = dcusumkbn2( [ x, y, initial ] ); + + expected = new Float64Array( [ NaN, NaN ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an empty ndarray, the function returns the output array unchanged', function test( t ) { + var expected; + var initial; + var xbuf; + var x; + var y; + var v; + + xbuf = new Float64Array( [] ); + x = vector( xbuf, 0, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, 100.0 ); + + v = dcusumkbn2( [ x, y, initial ] ); + + expected = new Float64Array( [] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-unit strides', function test( t ) { + var expected; + var initial; + var xbuf; + var ybuf; + var x; + var y; + var v; + + xbuf = new Float64Array([ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]); + x = vector( xbuf, 4, 2, 0 ); + + ybuf = new Float64Array([ + 0.0, // 0 + 0.0, + 0.0, // 1 + 0.0, + 0.0, // 2 + 0.0, + 0.0, // 3 + 0.0 + ]); + y = vector( ybuf, 4, 2, 0 ); + + initial = scalar2ndarrayLike( x, 5.0 ); + + v = dcusumkbn2( [ x, y, initial ] ); + + expected = new Float64Array([ + 6.0, // 0 + 0.0, + 8.0, // 1 + 0.0, + 6.0, // 2 + 0.0, + 10.0, // 3 + 0.0 + ]); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having negative strides', function test( t ) { + var expected; + var initial; + var xbuf; + var ybuf; + var x; + var y; + var v; + + xbuf = new Float64Array([ + 1.0, // 2 + -2.0, + 3.0, // 1 + 4.0, + -5.0 // 0 + ]); + x = vector( xbuf, 3, -2, 4 ); + + ybuf = new Float64Array([ + 0.0, // 2 + 0.0, // 1 + 0.0, // 0 + 0.0, + 0.0 + ]); + y = vector( ybuf, 3, -1, 2 ); + + initial = scalar2ndarrayLike( x, 0.0 ); + + v = dcusumkbn2( [ x, y, initial ] ); + + expected = new Float64Array([ + -1.0, // 2 + -2.0, // 1 + -5.0, // 0 + 0.0, + 0.0 + ]); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-zero offsets', function test( t ) { + var expected; + var initial; + var xbuf; + var ybuf; + var x; + var y; + var v; + + xbuf = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]); + x = vector( xbuf, 4, 2, 1 ); + + ybuf = new Float64Array([ + 0.0, + 0.0, + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0, // 3 + 0.0, + 0.0 + ]); + y = vector( ybuf, 4, 1, 2 ); + + initial = scalar2ndarrayLike( x, 0.0 ); + + v = dcusumkbn2( [ x, y, initial ] ); + + expected = new Float64Array([ + 0.0, + 0.0, + 1.0, // 0 + -1.0, // 1 + 1.0, // 2 + 5.0, // 3 + 0.0, + 0.0 + ]); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gcusum/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gcusum/lib/main.js index 5363bff7da7c..2c02811b7fca 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gcusum/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gcusum/lib/main.js @@ -34,7 +34,7 @@ var strided = require( '@stdlib/blas/ext/base/gcusum' ).ndarray; * Computes the cumulative sum of a one-dimensional ndarray. * * @param {ArrayLikeObject} arrays - array-like object containing an input ndarray, an output ndarray, and an ndarray containing the initial sum -* @returns {number} sum +* @returns {Object} output ndarray * * @example * var ndarray2array = require( '@stdlib/ndarray/to-array' ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusum/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusum/lib/main.js index a8387af015e6..13ebe5afd329 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusum/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusum/lib/main.js @@ -34,7 +34,7 @@ var strided = require( '@stdlib/blas/ext/base/scusum' ).ndarray; * Computes the cumulative sum of a one-dimensional single-precision floating-point ndarray. * * @param {ArrayLikeObject} arrays - array-like object containing an input ndarray, an output ndarray, and an ndarray containing the initial sum -* @returns {number} sum +* @returns {Object} output ndarray * * @example * var Float32Array = require( '@stdlib/array/float32' ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/README.md new file mode 100644 index 000000000000..dec99ab7088e --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/README.md @@ -0,0 +1,146 @@ + + +# scusumkbn + +> Compute the cumulative sum of a one-dimensional single-precision floating-point ndarray using an improved Kahan–Babuška algorithm. + +
+ +
+ + + +
+ +## Usage + +```javascript +var scusumkbn = require( '@stdlib/blas/ext/base/ndarray/scusumkbn' ); +``` + +#### scusumkbn( arrays ) + +Computes the cumulative sum of a one-dimensional single-precision floating-point ndarray using an improved Kahan–Babuška algorithm. + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +var xbuf = new Float32Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +var ybuf = new Float32Array( [ 0.0, 0.0, 0.0, 0.0 ] ); +var y = new ndarray( 'float32', ybuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +var initial = scalar2ndarray( 0.0, 'float32', 'row-major' ); + +var v = scusumkbn( [ x, y, initial ] ); +// returns + +var bool = ( v === y ); +// returns true + +var arr = ndarray2array( v ); +// returns [ 1.0, 4.0, 8.0, 10.0 ] +``` + +The function has the following parameters: + +- **arrays**: array-like object containing a one-dimensional input ndarray, a one-dimensional output ndarray, and a zero-dimensional ndarray containing the initial sum. + +
+ + + +
+ +## Notes + +- If provided an empty one-dimensional input ndarray, the function returns the output ndarray unchanged. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var zerosLike = require( '@stdlib/ndarray/zeros-like' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var scusumkbn = require( '@stdlib/blas/ext/base/ndarray/scusumkbn' ); + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'float32' +}); +var x = new ndarray( 'float32', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var y = zerosLike( x ); +console.log( ndarray2array( y ) ); + +var initial = scalar2ndarray( 100.0, { + 'dtype': 'float32' +}); + +var v = scusumkbn( [ x, y, initial ] ); +console.log( ndarray2array( v ) ); +``` + +
+ + + +
+ +## References + +- Neumaier, Arnold. 1974. "Rounding Error Analysis of Some Methods for Summing Finite Sums." _Zeitschrift Für Angewandte Mathematik Und Mechanik_ 54 (1): 39–51. doi:[10.1002/zamm.19740540106][@neumaier:1974a]. + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/benchmark/benchmark.js new file mode 100644 index 000000000000..285817c620c6 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/benchmark/benchmark.js @@ -0,0 +1,112 @@ +/** +* @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 zeros = require( '@stdlib/array/zeros' ); +var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +var pkg = require( './../package.json' ).name; +var scusumkbn = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float32' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var initial; + var xbuf; + var ybuf; + var x; + var y; + + xbuf = uniform( len, -10.0, 10.0, options ); + x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); + + initial = scalar2ndarray( 0.0, options.dtype, 'row-major' ); + + ybuf = zeros( len, options.dtype ); + y = new ndarray( options.dtype, ybuf, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = scusumkbn( [ x, y, initial ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnanf( v.get( i%len ) ) ) { + 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( pkg + ':len=' + len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/docs/repl.txt new file mode 100644 index 000000000000..cebd0b256878 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/docs/repl.txt @@ -0,0 +1,39 @@ + +{{alias}}( arrays ) + Computes the cumulative sum of a one-dimensional single-precision floating- + point ndarray using an improved Kahan–Babuška algorithm. + + If provided an empty input ndarray, the function returns the output ndarray + unchanged. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing a one-dimensional input ndarray, a one- + dimensional output ndarray, and a zero-dimensional ndarray containing + the initial sum. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var xbuf = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, 2.0 ] ); + > var ybuf = new {{alias:@stdlib/array/float32}}( [ 0.0, 0.0, 0.0 ] ); + > var dt = 'float32'; + > var sh = [ xbuf.length ]; + > var st = [ 1 ]; + > var oo = 0; + > var ord = 'row-major'; + > var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, st, oo, ord ); + > var y = new {{alias:@stdlib/ndarray/ctor}}( dt, ybuf, sh, st, oo, ord ); + > var s = {{alias:@stdlib/ndarray/from-scalar}}( 0.0, { 'dtype': dt } ); + > {{alias}}( [ x, y, s ] ); + > {{alias:@stdlib/ndarray/to-array}}( y ) + [ 1.0, -1.0, 1.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/docs/types/index.d.ts new file mode 100644 index 000000000000..1cdf293fc9c4 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/docs/types/index.d.ts @@ -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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { float32ndarray } from '@stdlib/types/ndarray'; + +/** +* Computes the cumulative sum of a one-dimensional single-precision floating-point ndarray using an improved Kahan–Babuška algorithm. +* +* @param arrays - array-like object containing an input ndarray, an output ndarray, and an ndarray containing the initial sum +* @returns output ndarray +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var xbuf = new Float32Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var ybuf = new Float32Array( [ 0.0, 0.0, 0.0, 0.0 ] ); +* var y = new ndarray( 'float32', ybuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var initial = scalar2ndarray( 0.0, 'float32', 'row-major' ); +* +* var v = scusumkbn( [ x, y, initial ] ); +* // returns +* +* var bool = ( v === y ); +* // returns true +* +* var arr = ndarray2array( v ); +* // returns [ 1.0, 4.0, 8.0, 10.0 ] +*/ +declare function scusumkbn( arrays: [ float32ndarray, float32ndarray, float32ndarray ] ): float32ndarray; + + +// EXPORTS // + +export = scusumkbn; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/docs/types/test.ts new file mode 100644 index 000000000000..e0ac06996946 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/docs/types/test.ts @@ -0,0 +1,69 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import scusumkbn = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float32' + }); + const y = zeros( [ 10 ], { + 'dtype': 'float32' + }); + const initial = zeros( [], { + 'dtype': 'float32' + }); + + scusumkbn( [ x, y, initial ] ); // $ExpectType float32ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + scusumkbn( '10' ); // $ExpectError + scusumkbn( 10 ); // $ExpectError + scusumkbn( true ); // $ExpectError + scusumkbn( false ); // $ExpectError + scusumkbn( null ); // $ExpectError + scusumkbn( undefined ); // $ExpectError + scusumkbn( [] ); // $ExpectError + scusumkbn( {} ); // $ExpectError + scusumkbn( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float32' + }); + const y = zeros( [ 10 ], { + 'dtype': 'float32' + }); + const initial = zeros( [], { + 'dtype': 'float32' + }); + + scusumkbn(); // $ExpectError + scusumkbn( [ x, y, initial ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/examples/index.js new file mode 100644 index 000000000000..7367f44e618c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/examples/index.js @@ -0,0 +1,42 @@ +/** +* @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 ndarray = require( '@stdlib/ndarray/base/ctor' ); +var zerosLike = require( '@stdlib/ndarray/zeros-like' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var scusumkbn = require( './../lib' ); + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'float32' +}); +var x = new ndarray( 'float32', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var y = zerosLike( x ); +console.log( ndarray2array( y ) ); + +var initial = scalar2ndarray( 100.0, { + 'dtype': 'float32' +}); + +var v = scusumkbn( [ x, y, initial ] ); +console.log( ndarray2array( v ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/lib/index.js new file mode 100644 index 000000000000..14a862738af1 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/lib/index.js @@ -0,0 +1,58 @@ +/** +* @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 cumulative sum of a one-dimensional single-precision floating-point ndarray using an improved Kahan–Babuška algorithm. +* +* @module @stdlib/blas/ext/base/ndarray/scusumkbn +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +* var scusumkbn = require( '@stdlib/blas/ext/base/ndarray/scusumkbn' ); +* +* var xbuf = new Float32Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var ybuf = new Float32Array( [ 0.0, 0.0, 0.0, 0.0 ] ); +* var y = new ndarray( 'float32', ybuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var initial = scalar2ndarray( 0.0, 'float32', 'row-major' ); +* +* var v = scusumkbn( [ x, y, initial ] ); +* // returns +* +* var bool = ( v === y ); +* // returns true +* +* var arr = ndarray2array( v ); +* // returns [ 1.0, 4.0, 8.0, 10.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/lib/main.js new file mode 100644 index 000000000000..195a7f77d5ff --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/lib/main.js @@ -0,0 +1,73 @@ +/** +* @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 numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); +var strided = require( '@stdlib/blas/ext/base/scusumkbn' ).ndarray; + + +// MAIN // + +/** +* Computes the cumulative sum of a one-dimensional single-precision floating-point ndarray using an improved Kahan–Babuška algorithm. +* +* @param {ArrayLikeObject} arrays - array-like object containing an input ndarray, an output ndarray, and an ndarray containing the initial sum +* @returns {Object} output ndarray +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +* +* var xbuf = new Float32Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var ybuf = new Float32Array( [ 0.0, 0.0, 0.0, 0.0 ] ); +* var y = new ndarray( 'float32', ybuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var initial = scalar2ndarray( 0.0, 'float32', 'row-major' ); +* +* var v = scusumkbn( [ x, y, initial ] ); +* // returns +* +* var bool = ( v === y ); +* // returns true +* +* var arr = ndarray2array( v ); +* // returns [ 1.0, 4.0, 8.0, 10.0 ] +*/ +function scusumkbn( arrays ) { + var x = arrays[ 0 ]; + var y = arrays[ 1 ]; + var v = ndarraylike2scalar( arrays[ 2 ] ); + strided( numelDimension( x, 0 ), v, getData( x ), getStride( x, 0 ), getOffset( x ), getData( y ), getStride( y, 0 ), getOffset( y ) ); // eslint-disable-line max-len + return y; +} + + +// EXPORTS // + +module.exports = scusumkbn; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/package.json new file mode 100644 index 000000000000..dfd767f33144 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/scusumkbn", + "version": "0.0.0", + "description": "Compute the cumulative sum of a one-dimensional single-precision floating-point ndarray using an improved Kahan–Babuška algorithm.", + "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", + "blas", + "extended", + "cumulative", + "accumulate", + "sum", + "total", + "summation", + "compensated", + "kahan", + "kbn", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/test/test.js new file mode 100644 index 000000000000..011092496fea --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scusumkbn/test/test.js @@ -0,0 +1,311 @@ +/** +* @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 isSameFloat32Array = require( '@stdlib/assert/is-same-float32array' ); +var Float32Array = require( '@stdlib/array/float32' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var scalar2ndarrayLike = require( '@stdlib/ndarray/base/from-scalar-like' ); +var zerosLike = require( '@stdlib/ndarray/zeros-like' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var scusumkbn = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Float32Array} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float32', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof scusumkbn, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( scusumkbn.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function computes the cumulative sum of a one-dimensional ndarray', function test( t ) { + var expected; + var initial; + var xbuf; + var x; + var y; + var v; + + xbuf = new Float32Array( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] ); + x = vector( xbuf, 6, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, 0.0 ); + v = scusumkbn( [ x, y, initial ] ); + + expected = new Float32Array( [ 1.0, -1.0, -5.0, 0.0, 0.0, 3.0 ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float32Array( [ -4.0, -5.0 ] ); + x = vector( xbuf, 2, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, 10.0 ); + v = scusumkbn( [ x, y, initial ] ); + + expected = new Float32Array( [ 6.0, 1.0 ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float32Array( [ -0.0, 0.0, -0.0 ] ); + x = vector( xbuf, 3, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, -0.0 ); + v = scusumkbn( [ x, y, initial ] ); + + expected = new Float32Array( [ -0.0, 0.0, 0.0 ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float32Array( [ -0.0, -0.0, -0.0 ] ); + x = vector( xbuf, 3, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, -0.0 ); + v = scusumkbn( [ x, y, initial ] ); + + expected = new Float32Array( [ -0.0, -0.0, -0.0 ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float32Array( [ -0.0, -0.0, -0.0 ] ); + x = vector( xbuf, 3, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, 0.0 ); + v = scusumkbn( [ x, y, initial ] ); + + expected = new Float32Array( [ 0.0, 0.0, 0.0 ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float32Array( [ NaN ] ); + x = vector( xbuf, 1, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, 0.0 ); + v = scusumkbn( [ x, y, initial ] ); + + expected = new Float32Array( [ NaN ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + + xbuf = new Float32Array( [ NaN, NaN ] ); + x = vector( xbuf, 2, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, NaN ); + v = scusumkbn( [ x, y, initial ] ); + + expected = new Float32Array( [ NaN, NaN ] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an empty ndarray, the function returns the output array unchanged', function test( t ) { + var expected; + var initial; + var xbuf; + var x; + var y; + var v; + + xbuf = new Float32Array( [] ); + x = vector( xbuf, 0, 1, 0 ); + y = zerosLike( x ); + initial = scalar2ndarrayLike( x, 100.0 ); + + v = scusumkbn( [ x, y, initial ] ); + + expected = new Float32Array( [] ); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-unit strides', function test( t ) { + var expected; + var initial; + var xbuf; + var ybuf; + var x; + var y; + var v; + + xbuf = new Float32Array([ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]); + x = vector( xbuf, 4, 2, 0 ); + + ybuf = new Float32Array([ + 0.0, // 0 + 0.0, + 0.0, // 1 + 0.0, + 0.0, // 2 + 0.0, + 0.0, // 3 + 0.0 + ]); + y = vector( ybuf, 4, 2, 0 ); + + initial = scalar2ndarrayLike( x, 5.0 ); + + v = scusumkbn( [ x, y, initial ] ); + + expected = new Float32Array([ + 6.0, // 0 + 0.0, + 8.0, // 1 + 0.0, + 6.0, // 2 + 0.0, + 10.0, // 3 + 0.0 + ]); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having negative strides', function test( t ) { + var expected; + var initial; + var xbuf; + var ybuf; + var x; + var y; + var v; + + xbuf = new Float32Array([ + 1.0, // 2 + -2.0, + 3.0, // 1 + 4.0, + -5.0 // 0 + ]); + x = vector( xbuf, 3, -2, 4 ); + + ybuf = new Float32Array([ + 0.0, // 2 + 0.0, // 1 + 0.0, // 0 + 0.0, + 0.0 + ]); + y = vector( ybuf, 3, -1, 2 ); + + initial = scalar2ndarrayLike( x, 0.0 ); + + v = scusumkbn( [ x, y, initial ] ); + + expected = new Float32Array([ + -1.0, // 2 + -2.0, // 1 + -5.0, // 0 + 0.0, + 0.0 + ]); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-zero offsets', function test( t ) { + var expected; + var initial; + var xbuf; + var ybuf; + var x; + var y; + var v; + + xbuf = new Float32Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]); + x = vector( xbuf, 4, 2, 1 ); + + ybuf = new Float32Array([ + 0.0, + 0.0, + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0, // 3 + 0.0, + 0.0 + ]); + y = vector( ybuf, 4, 1, 2 ); + + initial = scalar2ndarrayLike( x, 0.0 ); + + v = scusumkbn( [ x, y, initial ] ); + + expected = new Float32Array([ + 0.0, + 0.0, + 1.0, // 0 + -1.0, // 1 + 1.0, // 2 + 5.0, // 3 + 0.0, + 0.0 + ]); + t.strictEqual( v, y, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/README.md new file mode 100644 index 000000000000..cfe8f4df8330 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/README.md @@ -0,0 +1,127 @@ + + +# zsumkbn + +> Compute the sum of all elements in a one-dimensional double-precision complex floating-point ndarray using an improved Kahan–Babuška algorithm. + +
+ +
+ + + +
+ +## Usage + +```javascript +var zsumkbn = require( '@stdlib/blas/ext/base/ndarray/zsumkbn' ); +``` + +#### zsumkbn( arrays ) + +Computes the sum of all elements in a one-dimensional double-precision complex floating-point ndarray using an improved Kahan–Babuška algorithm. + +```javascript +var Complex128Array = require( '@stdlib/array/complex128' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +var xbuf = new Complex128Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +var x = new ndarray( 'complex128', xbuf, [ 2 ], [ 1 ], 0, 'row-major' ); + +var v = zsumkbn( [ x ] ); +// returns [ 5.0, 5.0 ] +``` + +The function has the following parameters: + +- **arrays**: array-like object containing a one-dimensional input ndarray. + +
+ + + +
+ +## Notes + +- If provided an empty one-dimensional ndarray, the function returns `0.0 + 0.0i`. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var zsumkbn = require( '@stdlib/blas/ext/base/ndarray/zsumkbn' ); + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +xbuf = new Complex128Array( xbuf ); + +var x = new ndarray( 'complex128', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var v = zsumkbn( [ x ] ); +console.log( v ); +``` + +
+ + + +
+ +## References + +- Neumaier, Arnold. 1974. "Rounding Error Analysis of Some Methods for Summing Finite Sums." _Zeitschrift Für Angewandte Mathematik Und Mechanik_ 54 (1): 39–51. doi:[10.1002/zamm.19740540106][@neumaier:1974a]. + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/benchmark/benchmark.js new file mode 100644 index 000000000000..5544c4317e88 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/benchmark/benchmark.js @@ -0,0 +1,107 @@ +/** +* @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 real = require( '@stdlib/complex/float64/real' ); +var imag = require( '@stdlib/complex/float64/imag' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var pkg = require( './../package.json' ).name; +var zsumkbn = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'complex128' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var xbuf; + var x; + + xbuf = uniform( len*2, -10.0, 10.0, { + 'dtype': 'float64' + }); + x = new ndarray( options.dtype, new Complex128Array( xbuf ), [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = zsumkbn( [ x ] ); + if ( isnan( real( v ) ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( imag( 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( pkg+':len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/docs/repl.txt new file mode 100644 index 000000000000..805cd283a867 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( arrays ) + Computes the sum of all elements in a one-dimensional double-precision + complex floating-point ndarray using an improved Kahan–Babuška algorithm. + + If provided an empty ndarray, the function returns `0.0 + 0.0i`. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing a one-dimensional input ndarray. + + Returns + ------- + out: Complex128 + Sum. + + Examples + -------- + > var xbuf = new {{alias:@stdlib/array/complex128}}( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var dt = 'complex128'; + > var sh = [ xbuf.length ]; + > var sx = [ 1 ]; + > var ox = 0; + > var ord = 'row-major'; + > var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord ); + > {{alias}}( [ x ] ) + [ 4.0, 6.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/docs/types/index.d.ts new file mode 100644 index 000000000000..55801ca4a522 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/docs/types/index.d.ts @@ -0,0 +1,47 @@ +/* +* @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 { complex128ndarray } from '@stdlib/types/ndarray'; +import { Complex128 } from '@stdlib/types/complex'; + +/** +* Computes the sum of all elements in a one-dimensional double-precision complex floating-point ndarray using an improved Kahan–Babuška algorithm. +* +* @param arrays - array-like object containing an input ndarray +* @returns sum +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var xbuf = new Complex128Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +* var x = new ndarray( 'complex128', xbuf, [ 2 ], [ 1 ], 0, 'row-major' ); +* +* var v = zsumkbn( [ x ] ); +* // returns [ 5.0, 5.0 ] +*/ +declare function zsumkbn( arrays: [ complex128ndarray ] ): Complex128; + + +// EXPORTS // + +export = zsumkbn; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/docs/types/test.ts new file mode 100644 index 000000000000..a1d55aa2470a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/docs/types/test.ts @@ -0,0 +1,57 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import zsumkbn = require( './index' ); + + +// TESTS // + +// The function returns a complex number... +{ + const x = zeros( [ 10 ], { + 'dtype': 'complex128' + }); + + zsumkbn( [ x ] ); // $ExpectType Complex128 +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + zsumkbn( '10' ); // $ExpectError + zsumkbn( 10 ); // $ExpectError + zsumkbn( true ); // $ExpectError + zsumkbn( false ); // $ExpectError + zsumkbn( null ); // $ExpectError + zsumkbn( undefined ); // $ExpectError + zsumkbn( [] ); // $ExpectError + zsumkbn( {} ); // $ExpectError + zsumkbn( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'complex128' + }); + + zsumkbn(); // $ExpectError + zsumkbn( [ x ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/examples/index.js new file mode 100644 index 000000000000..2cb00d2b4d3e --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/examples/index.js @@ -0,0 +1,36 @@ +/** +* @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 Complex128Array = require( '@stdlib/array/complex128' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var zsumkbn = require( './../lib' ); + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +xbuf = new Complex128Array( xbuf ); + +var x = new ndarray( 'complex128', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var v = zsumkbn( [ x ] ); +console.log( v ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/lib/index.js new file mode 100644 index 000000000000..c25cbefd4bb8 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/lib/index.js @@ -0,0 +1,45 @@ +/** +* @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 sum of all elements in a one-dimensional double-precision complex floating-point ndarray using an improved Kahan–Babuška algorithm. +* +* @module @stdlib/blas/ext/base/ndarray/zsumkbn +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var zsumkbn = require( '@stdlib/blas/ext/base/ndarray/zsumkbn' ); +* +* var xbuf = new Complex128Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +* var x = new ndarray( 'complex128', xbuf, [ 2 ], [ 1 ], 0, 'row-major' ); +* +* var v = zsumkbn( [ x ] ); +* // returns [ 5.0, 5.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/lib/main.js new file mode 100644 index 000000000000..ac8f2bef55d3 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/lib/main.js @@ -0,0 +1,56 @@ +/** +* @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 numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var strided = require( '@stdlib/blas/ext/base/zsumkbn' ).ndarray; + + +// MAIN // + +/** +* Computes the sum of all elements in a one-dimensional double-precision complex floating-point ndarray using an improved Kahan–Babuška algorithm. +* +* @param {ArrayLikeObject} arrays - array-like object containing an input ndarray +* @returns {Complex128} sum +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var xbuf = new Complex128Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +* var x = new ndarray( 'complex128', xbuf, [ 2 ], [ 1 ], 0, 'row-major' ); +* +* var v = zsumkbn( [ x ] ); +* // returns [ 5.0, 5.0 ] +*/ +function zsumkbn( arrays ) { + var x = arrays[ 0 ]; + return strided( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ) ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = zsumkbn; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/package.json new file mode 100644 index 000000000000..f7b0f35d5303 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/zsumkbn", + "version": "0.0.0", + "description": "Compute the sum of all elements in a one-dimensional double-precision complex floating-point ndarray using an improved Kahan–Babuška algorithm.", + "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", + "blas", + "extended", + "sum", + "total", + "summation", + "compensated", + "kahan", + "kbn", + "complex128", + "complex", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/test/test.js new file mode 100644 index 000000000000..f258db26dc24 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/zsumkbn/test/test.js @@ -0,0 +1,199 @@ +/** +* @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 isSameComplex128 = require( '@stdlib/assert/is-same-complex128' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var Complex128 = require( '@stdlib/complex/float64/ctor' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var zsumkbn = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Complex128Array} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'complex128', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof zsumkbn, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( zsumkbn.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function computes the sum of all elements in a one-dimensional ndarray using an improved Kahan–Babuška algorithm', function test( t ) { + var x; + var v; + + x = new Complex128Array( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] ); + v = zsumkbn( [ vector( x, 3, 1, 0 ) ] ); + t.strictEqual( isSameComplex128( v, new Complex128( -3.0, 6.0 ) ), true, 'returns expected value' ); + + x = new Complex128Array( [ -4.0, -5.0, -4.0, -5.0 ] ); + v = zsumkbn( [ vector( x, 2, 1, 0 ) ] ); + t.strictEqual( isSameComplex128( v, new Complex128( -8.0, -10.0 ) ), true, 'returns expected value' ); + + x = new Complex128Array( [ -0.0, 0.0, -0.0, -0.0 ] ); + v = zsumkbn( [ vector( x, 2, 1, 0 ) ] ); + t.strictEqual( isSameComplex128( v, new Complex128( -0.0, 0.0 ) ), true, 'returns expected value' ); + + x = new Complex128Array( [ NaN, NaN ] ); + v = zsumkbn( [ vector( x, 1, 1, 0 ) ] ); + t.strictEqual( isSameComplex128( v, new Complex128( NaN, NaN ) ), true, 'returns expected value' ); + + x = new Complex128Array( [ NaN, NaN, NaN, NaN ] ); + v = zsumkbn( [ vector( x, 2, 1, 0 ) ] ); + t.strictEqual( isSameComplex128( v, new Complex128( NaN, NaN ) ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an empty ndarray, the function returns `0.0 + 0.0i`', function test( t ) { + var x; + var v; + + x = new Complex128Array( [] ); + + v = zsumkbn( [ vector( x, 0, 1, 0 ) ] ); + t.strictEqual( isSameComplex128( v, new Complex128( 0.0, 0.0 ) ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an ndarray containing a single element, the function returns that element', function test( t ) { + var x; + var v; + + x = new Complex128Array( [ 1.0, 2.0 ] ); + + v = zsumkbn( [ vector( x, 1, 1, 0 ) ] ); + t.strictEqual( isSameComplex128( v, new Complex128( 1.0, 2.0 ) ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-unit strides', function test( t ) { + var x; + var v; + + x = new Complex128Array([ + 1.0, // 0 + 1.0, // 0 + 2.0, + 2.0, + 2.0, // 1 + 2.0, // 1 + -7.0, + -7.0, + -2.0, // 2 + -2.0, // 2 + 3.0, + 3.0, + 4.0, // 3 + 4.0, // 3 + 2.0, + 2.0 + ]); + + v = zsumkbn( [ vector( x, 4, 2, 0 ) ] ); + + t.strictEqual( isSameComplex128( v, new Complex128( 5.0, 5.0 ) ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having negative strides', function test( t ) { + var x; + var v; + + x = new Complex128Array([ + 1.0, // 3 + 1.0, // 3 + 2.0, + 2.0, + 2.0, // 2 + 2.0, // 2 + -7.0, + -7.0, + -2.0, // 1 + -2.0, // 1 + 3.0, + 3.0, + 4.0, // 0 + 4.0, // 0 + 2.0, + 2.0 + ]); + + v = zsumkbn( [ vector( x, 4, -2, 6 ) ] ); + + t.strictEqual( isSameComplex128( v, new Complex128( 5.0, 5.0 ) ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-zero offsets', function test( t ) { + var x; + var v; + + x = new Complex128Array([ + -9.0, + -9.0, + 1.0, // 3 + 1.0, // 3 + 2.0, + 2.0, + 2.0, // 2 + 2.0, // 2 + -7.0, + -7.0, + -2.0, // 1 + -2.0, // 1 + 3.0, + 3.0, + 4.0, // 0 + 4.0, // 0 + 2.0, + 2.0 + ]); + + v = zsumkbn( [ vector( x, 4, 2, 1 ) ] ); + t.strictEqual( isSameComplex128( v, new Complex128( 5.0, 5.0 ) ), true, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/README.md b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/README.md new file mode 100644 index 000000000000..16268d27a94f --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/README.md @@ -0,0 +1,139 @@ + + +# FLOAT16_MAX_BASE2_EXPONENT + +> The maximum biased base 2 exponent for a [half-precision floating-point number][ieee754]. + +
+ +## Usage + + + +```javascript +var FLOAT16_MAX_BASE2_EXPONENT = require( '@stdlib/constants/float16/max-base2-exponent' ); +``` + +#### FLOAT16_MAX_BASE2_EXPONENT + +The maximum biased base 2 exponent for a [half-precision floating-point number][ieee754]. + + + +```javascript +var bool = ( FLOAT16_MAX_BASE2_EXPONENT === 15 ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var FLOAT16_MAX_BASE2_EXPONENT = require( '@stdlib/constants/float16/max-base2-exponent' ); + +console.log( FLOAT16_MAX_BASE2_EXPONENT ); +// => 15 +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/constants/float16/max_base2_exponent.h" +``` + +#### STDLIB_CONSTANT_FLOAT16_MAX_BASE2_EXPONENT + +Macro for the maximum biased base 2 exponent for a [half-precision floating-point number][ieee754]. + +
+ + + + + +
+ +
+ + + + + +
+ +
+ + + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/docs/repl.txt b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/docs/repl.txt new file mode 100644 index 000000000000..9d633ea2ba88 --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/docs/repl.txt @@ -0,0 +1,13 @@ + +{{alias}} + The maximum biased base 2 exponent for a half-precision floating-point + number. + + Examples + -------- + > {{alias}} + 15 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/docs/types/index.d.ts b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/docs/types/index.d.ts new file mode 100644 index 000000000000..ee9d81a2eab0 --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/docs/types/index.d.ts @@ -0,0 +1,33 @@ +/* +* @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 + +/** +* The maximum biased base 2 exponent for a half-precision floating-point number. +* +* @example +* var exp = FLOAT16_MAX_BASE2_EXPONENT; +* // returns 15 +*/ +declare const FLOAT16_MAX_BASE2_EXPONENT: number; + + +// EXPORTS // + +export = FLOAT16_MAX_BASE2_EXPONENT; diff --git a/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/docs/types/test.ts b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/docs/types/test.ts new file mode 100644 index 000000000000..9a9350e48956 --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/docs/types/test.ts @@ -0,0 +1,28 @@ +/* +* @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 FLOAT16_MAX_BASE2_EXPONENT = require( './index' ); + + +// TESTS // + +// The export is a number... +{ + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + FLOAT16_MAX_BASE2_EXPONENT; // $ExpectType number +} diff --git a/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/examples/index.js b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/examples/index.js new file mode 100644 index 000000000000..e0d8a80d8641 --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/examples/index.js @@ -0,0 +1,24 @@ +/** +* @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 FLOAT16_MAX_BASE2_EXPONENT = require( './../lib' ); + +console.log( FLOAT16_MAX_BASE2_EXPONENT ); +// => 15 diff --git a/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/include/stdlib/constants/float16/max_base2_exponent.h b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/include/stdlib/constants/float16/max_base2_exponent.h new file mode 100644 index 000000000000..2daefbe564b9 --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/include/stdlib/constants/float16/max_base2_exponent.h @@ -0,0 +1,27 @@ +/** +* @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. +*/ + +#ifndef STDLIB_CONSTANTS_FLOAT16_MAX_BASE2_EXPONENT_H +#define STDLIB_CONSTANTS_FLOAT16_MAX_BASE2_EXPONENT_H + +/** +* Macro for the maximum biased base 2 exponent for a half-precision floating-point number. +*/ +#define STDLIB_CONSTANT_FLOAT16_MAX_BASE2_EXPONENT 15 + +#endif // !STDLIB_CONSTANTS_FLOAT16_MAX_BASE2_EXPONENT_H diff --git a/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/lib/index.js b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/lib/index.js new file mode 100644 index 000000000000..38a46c477e97 --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/lib/index.js @@ -0,0 +1,54 @@ +/** +* @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'; + +/** +* The maximum biased base 2 exponent for a half-precision floating-point number. +* +* @module @stdlib/constants/float16/max-base2-exponent +* @type {integer32} +* +* @example +* var FLOAT16_MAX_BASE2_EXPONENT = require( '@stdlib/constants/float16/max-base2-exponent' ); +* // returns 15 +*/ + + +// MAIN // + +/** +* The maximum biased base 2 exponent for a half-precision floating-point number. +* +* ```text +* 11110 => 30 - BIAS = 15 +* ``` +* +* where `BIAS = 15`. +* +* @constant +* @type {integer32} +* @default 15 +* @see [IEEE 754]{@link https://en.wikipedia.org/wiki/IEEE_754-1985} +*/ +var FLOAT16_MAX_BASE2_EXPONENT = 15|0; // asm type annotation + + +// EXPORTS // + +module.exports = FLOAT16_MAX_BASE2_EXPONENT; diff --git a/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/manifest.json b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/manifest.json new file mode 100644 index 000000000000..844d692f6439 --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/manifest.json @@ -0,0 +1,36 @@ +{ + "options": {}, + "fields": [ + { + "field": "src", + "resolve": true, + "relative": true + }, + { + "field": "include", + "resolve": true, + "relative": true + }, + { + "field": "libraries", + "resolve": false, + "relative": false + }, + { + "field": "libpath", + "resolve": true, + "relative": false + } + ], + "confs": [ + { + "src": [], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [] + } + ] +} diff --git a/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/package.json b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/package.json new file mode 100644 index 000000000000..e44615e35a24 --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/constants/float16/max-base2-exponent", + "version": "0.0.0", + "description": "The maximum biased base 2 exponent for a half-precision floating-point number.", + "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": { + "doc": "./docs", + "example": "./examples", + "include": "./include", + "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", + "constant", + "const", + "mathematics", + "math", + "half", + "flt", + "floating-point", + "float", + "ieee754", + "bias", + "exponent", + "max", + "maximum", + "binary", + "base 2" + ] +} diff --git a/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/test/test.js b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/test/test.js new file mode 100644 index 000000000000..aad464eebea7 --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/max-base2-exponent/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 FLOAT16_MAX_BASE2_EXPONENT = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a number', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof FLOAT16_MAX_BASE2_EXPONENT, 'number', 'main export is a number' ); + t.end(); +}); + +tape( 'the exported value is 15', function test( t ) { + t.strictEqual( FLOAT16_MAX_BASE2_EXPONENT, 15, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/constants/float16/min-ln/README.md b/lib/node_modules/@stdlib/constants/float16/min-ln/README.md new file mode 100644 index 000000000000..2a633cb64dd6 --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/min-ln/README.md @@ -0,0 +1,83 @@ + + +# FLOAT16_MIN_LN + +> [Natural logarithm][natural-logarithm] of the smallest **normalized** [half-precision floating-point number][ieee754]. + +
+ +## Usage + +```javascript +var FLOAT16_MIN_LN = require( '@stdlib/constants/float16/min-ln' ); +``` + +#### FLOAT16_MIN_LN + +[Natural logarithm][natural-logarithm] of the smallest **normalized** [half-precision floating-point number][ieee754]. + +```javascript +var bool = ( FLOAT16_MIN_LN === -9.703125 ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var FLOAT16_MIN_LN = require( '@stdlib/constants/float16/min-ln' ); + +console.log( FLOAT16_MIN_LN ); +// => -9.703125 +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/constants/float16/min-ln/docs/repl.txt b/lib/node_modules/@stdlib/constants/float16/min-ln/docs/repl.txt new file mode 100644 index 000000000000..0b2a98532bc8 --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/min-ln/docs/repl.txt @@ -0,0 +1,13 @@ + +{{alias}} + Natural logarithm of the smallest normalized half-precision floating-point + number. + + Examples + -------- + > {{alias}} + -9.703125 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/constants/float16/min-ln/docs/types/index.d.ts b/lib/node_modules/@stdlib/constants/float16/min-ln/docs/types/index.d.ts new file mode 100644 index 000000000000..65e720ce0a54 --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/min-ln/docs/types/index.d.ts @@ -0,0 +1,33 @@ +/* +* @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 + +/** +* Natural logarithm of the smallest normalized half-precision floating-point number. +* +* @example +* var min = FLOAT16_MIN_LN; +* // returns -9.703125 +*/ +declare const FLOAT16_MIN_LN: number; + + +// EXPORTS // + +export = FLOAT16_MIN_LN; diff --git a/lib/node_modules/@stdlib/constants/float16/min-ln/docs/types/test.ts b/lib/node_modules/@stdlib/constants/float16/min-ln/docs/types/test.ts new file mode 100644 index 000000000000..aafb7e6df96d --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/min-ln/docs/types/test.ts @@ -0,0 +1,28 @@ +/* +* @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 FLOAT16_MIN_LN = require( './index' ); + + +// TESTS // + +// The export is a number... +{ + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + FLOAT16_MIN_LN; // $ExpectType number +} diff --git a/lib/node_modules/@stdlib/constants/float16/min-ln/examples/index.js b/lib/node_modules/@stdlib/constants/float16/min-ln/examples/index.js new file mode 100644 index 000000000000..08a15382579e --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/min-ln/examples/index.js @@ -0,0 +1,24 @@ +/** +* @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 FLOAT16_MIN_LN = require( './../lib' ); + +console.log( FLOAT16_MIN_LN ); +// => -9.703125 diff --git a/lib/node_modules/@stdlib/constants/float16/min-ln/lib/index.js b/lib/node_modules/@stdlib/constants/float16/min-ln/lib/index.js new file mode 100644 index 000000000000..b10154399052 --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/min-ln/lib/index.js @@ -0,0 +1,55 @@ +/** +* @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'; + +/** +* Natural logarithm of the smallest normalized half-precision floating-point number. +* +* @module @stdlib/constants/float16/min-ln +* @type {number} +* +* @example +* var FLOAT16_MIN_LN = require( '@stdlib/constants/float16/min-ln' ); +* // returns -9.703125 +*/ + +// MAIN // + +/** +* Natural logarithm of the smallest normalized half-precision floating-point number. +* +* ## Notes +* +* The number has the value +* +* ```tex +* -\ln \left( 2^{15-1} \right) +* ``` +* +* @constant +* @type {number} +* @default -9.703125 +* @see [IEEE 754]{@link https://en.wikipedia.org/wiki/IEEE_754-1985} +*/ +var FLOAT16_MIN_LN = -9.703125; + + +// EXPORTS // + +module.exports = FLOAT16_MIN_LN; diff --git a/lib/node_modules/@stdlib/constants/float16/min-ln/package.json b/lib/node_modules/@stdlib/constants/float16/min-ln/package.json new file mode 100644 index 000000000000..c3ab6b00df64 --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/min-ln/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/constants/float16/min-ln", + "version": "0.0.0", + "description": "Natural logarithm of the smallest normalized half-precision floating-point number.", + "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": { + "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", + "constant", + "const", + "mathematics", + "math", + "smallest", + "minimum", + "floating-point", + "float16", + "float", + "16bit", + "ieee754", + "flt", + "precision", + "min", + "normalized", + "logarithm", + "log" + ] +} diff --git a/lib/node_modules/@stdlib/constants/float16/min-ln/test/test.js b/lib/node_modules/@stdlib/constants/float16/min-ln/test/test.js new file mode 100644 index 000000000000..1a2eb8253986 --- /dev/null +++ b/lib/node_modules/@stdlib/constants/float16/min-ln/test/test.js @@ -0,0 +1,43 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var ln = require( '@stdlib/math/base/special/ln' ); +var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); +var SMALLEST_NORMAL = require( '@stdlib/constants/float16/smallest-normal' ); +var FLOAT16_MIN_LN = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a number', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof FLOAT16_MIN_LN, 'number', 'main export is a number' ); + t.end(); +}); + +tape( 'export is a half-precision floating-point number equal to the natural logarithm of the smallest normalized half-precision floating-point number', function test( t ) { + t.strictEqual( FLOAT16_MIN_LN, float64ToFloat16( -ln( pow( 2, 14 ) ) ), 'returns expected value' ); + t.strictEqual( FLOAT16_MIN_LN, float64ToFloat16( ln( SMALLEST_NORMAL ) ), 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/constants/float32/catalan/lib/index.js b/lib/node_modules/@stdlib/constants/float32/catalan/lib/index.js index f7ad2ebd6fde..1e041d574f4d 100644 --- a/lib/node_modules/@stdlib/constants/float32/catalan/lib/index.js +++ b/lib/node_modules/@stdlib/constants/float32/catalan/lib/index.js @@ -25,7 +25,7 @@ * @type {number} * * @example -* var FOAL32_CATALAN = require( '@stdlib/constants/float32/catalan' ); +* var FLOAT32_CATALAN = require( '@stdlib/constants/float32/catalan' ); * // returns 0.9159656167030334 */ @@ -45,9 +45,9 @@ var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); * @see [OEIS]{@link http://oeis.org/A006752} * @see [Wikipedia]{@link https://en.wikipedia.org/wiki/Catalan%27s_constant} */ -var FOAL32_CATALAN = float64ToFloat32( 0.915965594177219 ); +var FLOAT32_CATALAN = float64ToFloat32( 0.915965594177219 ); // EXPORTS // -module.exports = FOAL32_CATALAN; +module.exports = FLOAT32_CATALAN; diff --git a/lib/node_modules/@stdlib/number/float64/base/get-high-word/README.md b/lib/node_modules/@stdlib/number/float64/base/get-high-word/README.md index c876d7542e10..639c5ec23f3f 100644 --- a/lib/node_modules/@stdlib/number/float64/base/get-high-word/README.md +++ b/lib/node_modules/@stdlib/number/float64/base/get-high-word/README.md @@ -144,7 +144,7 @@ void stdlib_base_float64_get_high_word( const double x, uint32_t *high ); #include int main( void ) { - double x[] = { 3.14, -3.14, 0.0, 0.0/0.0 }; + const double x[] = { 3.14, -3.14, 0.0, 0.0/0.0 }; uint32_t high; int i; diff --git a/lib/node_modules/@stdlib/number/float64/base/get-high-word/examples/c/example.c b/lib/node_modules/@stdlib/number/float64/base/get-high-word/examples/c/example.c index 85e8b6da8ba0..b4f484ab1d22 100644 --- a/lib/node_modules/@stdlib/number/float64/base/get-high-word/examples/c/example.c +++ b/lib/node_modules/@stdlib/number/float64/base/get-high-word/examples/c/example.c @@ -21,7 +21,7 @@ #include int main( void ) { - double x[] = { 3.14, -3.14, 0.0, 0.0/0.0 }; + const double x[] = { 3.14, -3.14, 0.0, 0.0/0.0 }; uint32_t high; int i; diff --git a/lib/node_modules/@stdlib/stats/max-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/max-by/docs/types/index.d.ts index b9ea039f3329..aeceee438ea9 100644 --- a/lib/node_modules/@stdlib/stats/max-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/max-by/docs/types/index.d.ts @@ -233,7 +233,7 @@ interface Unary { * @example * var array = require( '@stdlib/ndarray/array' ); * -* var x = array( [ -1.0, 2.0, -3.0 ] ) +* var x = array( [ -1.0, 2.0, -3.0 ] ); * * function clbk( value ) { * return value * 2.0; @@ -249,7 +249,7 @@ interface Unary { * var array = require( '@stdlib/ndarray/array' ); * var zeros = require( '@stdlib/ndarray/zeros' ); * -* var x = array( [ -1.0, 2.0, -3.0 ] ) +* var x = array( [ -1.0, 2.0, -3.0 ] ); * var y = zeros( [] ); * * function clbk( value ) { diff --git a/lib/node_modules/@stdlib/stats/max/README.md b/lib/node_modules/@stdlib/stats/max/README.md index b6faa817535f..e33ca8883d84 100644 --- a/lib/node_modules/@stdlib/stats/max/README.md +++ b/lib/node_modules/@stdlib/stats/max/README.md @@ -40,10 +40,7 @@ var array = require( '@stdlib/ndarray/array' ); var x = array( [ -1.0, 2.0, -3.0 ] ); var y = max( x ); -// returns - -var v = y.get(); -// returns 2.0 +// returns [ 2.0 ] ``` The function has the following parameters: @@ -60,81 +57,58 @@ The function accepts the following options: By default, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform a reduction over specific dimensions, provide a `dims` option. ```javascript -var ndarray2array = require( '@stdlib/ndarray/to-array' ); var array = require( '@stdlib/ndarray/array' ); var x = array( [ -1.0, 2.0, -3.0, 4.0 ], { 'shape': [ 2, 2 ], 'order': 'row-major' }); -var v = ndarray2array( x ); -// returns [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] +// returns [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] var y = max( x, { 'dims': [ 0 ] }); -// returns - -v = ndarray2array( y ); -// returns [ -1.0, 4.0 ] +// returns [ -1.0, 4.0 ] y = max( x, { 'dims': [ 1 ] }); -// returns - -v = ndarray2array( y ); -// returns [ 2.0, 4.0 ] +// returns [ 2.0, 4.0 ] y = max( x, { 'dims': [ 0, 1 ] }); -// returns - -v = y.get(); -// returns 4.0 +// returns [ 4.0 ] ``` By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`. ```javascript -var ndarray2array = require( '@stdlib/ndarray/to-array' ); var array = require( '@stdlib/ndarray/array' ); var x = array( [ -1.0, 2.0, -3.0, 4.0 ], { 'shape': [ 2, 2 ], 'order': 'row-major' }); - -var v = ndarray2array( x ); -// returns [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] +// returns [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] var y = max( x, { 'dims': [ 0 ], 'keepdims': true }); -// returns - -v = ndarray2array( y ); -// returns [ [ -1.0, 4.0 ] ] +// returns [ [ -1.0, 4.0 ] ] y = max( x, { 'dims': [ 1 ], 'keepdims': true }); -// returns - -v = ndarray2array( y ); -// returns [ [ 2.0 ], [ 4.0 ] ] +// returns [ [ 2.0 ], [ 4.0 ] ] y = max( x, { 'dims': [ 0, 1 ], 'keepdims': true }); -// returns - -v = ndarray2array( y ); -// returns [ [ 4.0 ] ] +// returns [ [ 4.0 ] ] ``` By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option. @@ -150,7 +124,7 @@ var x = array( [ -1.0, 2.0, -3.0 ], { var y = max( x, { 'dtype': 'float64' }); -// returns +// returns [ 2.0 ] var dt = String( getDType( y ) ); // returns 'float64' @@ -168,10 +142,7 @@ var x = array( [ -1.0, 2.0, -3.0 ] ); var y = zeros( [] ); var out = max.assign( x, y ); -// returns - -var v = out.get(); -// returns 2.0 +// returns [ 2.0 ] var bool = ( out === y ); // returns true diff --git a/lib/node_modules/@stdlib/stats/max/docs/repl.txt b/lib/node_modules/@stdlib/stats/max/docs/repl.txt index 92c8702a5d9b..f79fc65135cb 100644 --- a/lib/node_modules/@stdlib/stats/max/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/max/docs/repl.txt @@ -65,11 +65,9 @@ > var x = {{alias:@stdlib/ndarray/array}}( [ -1.0, 2.0, -3.0, -4.0 ] ); > var out = {{alias:@stdlib/ndarray/zeros}}( [] ); > var y = {{alias}}.assign( x, out ) - + [ 2.0 ] > var bool = ( out === y ) true - > var v = out.get() - 2.0 See Also -------- diff --git a/lib/node_modules/@stdlib/stats/max/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/max/docs/types/index.d.ts index cf9e41151d37..af9fa54e7a52 100644 --- a/lib/node_modules/@stdlib/stats/max/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/max/docs/types/index.d.ts @@ -75,10 +75,7 @@ interface Unary { * var x = array( [ -1.0, 2.0, -3.0 ] ); * * var y = max( x ); - * // returns - * - * var v = y.get(); - * // returns 2.0 + * // returns [ 2.0 ] */ ( x: InputArray, options?: Options ): OutputArray; // NOTE: we lose type specificity here, but retaining specificity would likely be difficult and/or tedious to completely enumerate, as the output ndarray data type is dependent on how `x` interacts with output data type policy and whether that policy has been overridden by `options.dtype`. @@ -98,10 +95,7 @@ interface Unary { * var y = zeros( [] ); * * var out = max.assign( x, y ); - * // returns - * - * var v = out.get(); - * // returns 2.0 + * // returns [ 2.0 ] * * var bool = ( out === y ); * // returns true @@ -119,13 +113,10 @@ interface Unary { * @example * var array = require( '@stdlib/ndarray/array' ); * -* var x = array( [ -1.0, 2.0, -3.0 ] ) +* var x = array( [ -1.0, 2.0, -3.0 ] ); * * var y = max( x ); -* // returns -* -* var v = y.get(); -* // returns 2.0 +* // returns [ 2.0 ] * * @example * var array = require( '@stdlib/ndarray/array' ); @@ -135,10 +126,7 @@ interface Unary { * var y = zeros( [] ); * * var out = max.assign( x, y ); -* // returns -* -* var v = out.get(); -* // returns 2.0 +* // returns [ 2.0 ] * * var bool = ( out === y ); * // returns true diff --git a/lib/node_modules/@stdlib/stats/max/lib/index.js b/lib/node_modules/@stdlib/stats/max/lib/index.js index a1e22b5eb900..906995b14648 100644 --- a/lib/node_modules/@stdlib/stats/max/lib/index.js +++ b/lib/node_modules/@stdlib/stats/max/lib/index.js @@ -45,10 +45,7 @@ * * // Perform reduction: * var out = max( x ); -* // returns -* -* var v = out.get(); -* // returns 11.0 +* // returns [ 11.0 ] */ // MODULES // diff --git a/lib/node_modules/@stdlib/stats/max/lib/main.js b/lib/node_modules/@stdlib/stats/max/lib/main.js index 3abcb22ead7c..17d311dd7c32 100644 --- a/lib/node_modules/@stdlib/stats/max/lib/main.js +++ b/lib/node_modules/@stdlib/stats/max/lib/main.js @@ -88,10 +88,7 @@ var table = { * * // Perform reduction: * var out = max( x ); -* // returns -* -* var v = out.get(); -* // returns 11.0 +* // returns [ 11.0 ] */ var max = factory( table, [ idtypes ], odtypes, policies ); diff --git a/lib/node_modules/@stdlib/stats/meankbn/README.md b/lib/node_modules/@stdlib/stats/meankbn/README.md new file mode 100644 index 000000000000..64fe59e187a1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn/README.md @@ -0,0 +1,297 @@ + + +# meankbn + +> Compute the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using an improved Kahan–Babuška algorithm. + +
+ +The [arithmetic mean][arithmetic-mean] is defined as + + + +```math +\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var meankbn = require( '@stdlib/stats/meankbn' ); +``` + +#### meankbn( x\[, options] ) + +Computes the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using an improved Kahan–Babuška algorithm. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); + +var y = meankbn( x ); +// returns + +var v = y.get(); +// returns 1.25 +``` + +The function has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **options**: function options (_optional_). + +The function accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. + +By default, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform a reduction over specific dimensions, provide a `dims` option. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ], { + 'shape': [ 2, 2 ], + 'order': 'row-major' +}); +var v = ndarray2array( x ); +// returns [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ] + +var y = meankbn( x, { + 'dims': [ 0 ] +}); +// returns + +v = ndarray2array( y ); +// returns [ -0.5, 3.0 ] + +y = meankbn( x, { + 'dims': [ 1 ] +}); +// returns + +v = ndarray2array( y ); +// returns [ 1.5, 1.0 ] + +y = meankbn( x, { + 'dims': [ 0, 1 ] +}); +// returns + +v = y.get(); +// returns 1.25 +``` + +By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ], { + 'shape': [ 2, 2 ], + 'order': 'row-major' +}); + +var v = ndarray2array( x ); +// returns [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ] + +var y = meankbn( x, { + 'dims': [ 0 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ -0.5, 3.0 ] ] + +y = meankbn( x, { + 'dims': [ 1 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 1.5 ], [ 1.0 ] ] + +y = meankbn( x, { + 'dims': [ 0, 1 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 1.25 ] ] +``` + +By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option. + +```javascript +var getDType = require( '@stdlib/ndarray/dtype' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ], { + 'dtype': 'generic' +}); + +var y = meankbn( x, { + 'dtype': 'float64' +}); +// returns + +var dt = String( getDType( y ) ); +// returns 'float64' +``` + +#### meankbn.assign( x, out\[, options] ) + +Computes the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using an improved Kahan–Babuška algorithm and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); +var y = zeros( [] ); + +var out = meankbn.assign( x, y ); +// returns + +var v = out.get(); +// returns 1.25 + +var bool = ( out === y ); +// returns true +``` + +The method has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or generic [data type][@stdlib/ndarray/dtypes]. +- **out**: output [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). + +The method accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. + +
+ + + +
+ +## Notes + +- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor]. +- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes]. + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var meankbn = require( '@stdlib/stats/meankbn' ); + +// Generate an array of random numbers: +var x = uniform( [ 5, 5 ], 0.0, 20.0 ); +console.log( ndarray2array( x ) ); + +// Perform a reduction: +var y = meankbn( x, { + 'dims': [ 0 ] +}); + +// Resolve the output array data type: +var dt = getDType( y ); +console.log( dt ); + +// Print the results: +console.log( ndarray2array( y ) ); +``` + +
+ + + +* * * + +
+ +## References + +- Neumaier, Arnold. 1974. "Rounding Error Analysis of Some Methods for Summing Finite Sums." _Zeitschrift Für Angewandte Mathematik Und Mechanik_ 54 (1): 39–51. doi:[10.1002/zamm.19740540106][@neumaier:1974a]. + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/meankbn/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/stats/meankbn/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..e681245338b7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn/benchmark/benchmark.assign.js @@ -0,0 +1,111 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var zeros = require( '@stdlib/array/zeros' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var meankbn = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out; + var x; + + x = uniform( len, -50.0, 50.0, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + out = new ndarray( options.dtype, zeros( 1, options.dtype ), [], [ 0 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = meankbn.assign( x, out ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + 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( pkg+':assign:dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/meankbn/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/meankbn/benchmark/benchmark.js new file mode 100644 index 000000000000..2a0524593c45 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn/benchmark/benchmark.js @@ -0,0 +1,105 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var meankbn = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -50.0, 50.0, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = meankbn( x ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + 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( pkg+':dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/meankbn/docs/repl.txt b/lib/node_modules/@stdlib/stats/meankbn/docs/repl.txt new file mode 100644 index 000000000000..0494e7e6a558 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn/docs/repl.txt @@ -0,0 +1,79 @@ + +{{alias}}( x[, options] ) + Computes the arithmetic mean along one or more ndarray dimensions using an + improved Kahan–Babuška algorithm. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or "generic" data type. + + options: Object (optional) + Function options. + + options.dtype: string (optional) + Output array data type. Must be a real-valued floating-point or + "generic" data type. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in a provided input + ndarray. + + options.keepdims: boolean (optional) + Boolean indicating whether the reduced dimensions should be included in + the returned ndarray as singleton dimensions. Default: false. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ -1.0, 2.0, -3.0, -4.0 ] ); + > var y = {{alias}}( x ); + > var v = y.get() + -1.5 + + +{{alias}}.assign( x, out[, options] ) + Computes the arithmetic mean along one or more ndarray dimensions using an + improved Kahan–Babuška algorithm and assigns results to a provided output + ndarray. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or "generic" data type. + + out: ndarray + Output array. + + options: Object (optional) + Function options. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in a provided input + ndarray. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ -1.0, 2.0, -3.0, -4.0 ] ); + > var out = {{alias:@stdlib/ndarray/zeros}}( [] ); + > var y = {{alias}}.assign( x, out ) + + > var bool = ( out === y ) + true + > var v = out.get() + -1.5 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/meankbn/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/meankbn/docs/types/index.d.ts new file mode 100644 index 000000000000..28d647e28022 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn/docs/types/index.d.ts @@ -0,0 +1,151 @@ +/* +* @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 { ArrayLike } from '@stdlib/types/array'; +import { RealFloatingPointAndGenericDataType as DataType, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Input array. +*/ +type InputArray = typedndarray; + +/** +* Output array. +*/ +type OutputArray = typedndarray; + +/** +* Interface defining "base" options. +*/ +interface BaseOptions { + /** + * List of dimensions over which to perform a reduction. + */ + dims?: ArrayLike; +} + +/** +* Interface defining options. +*/ +interface Options extends BaseOptions { + /** + * Output array data type. + */ + dtype?: DataType; + + /** + * Boolean indicating whether the reduced dimensions should be included in the returned array as singleton dimensions. Default: `false`. + */ + keepdims?: boolean; +} + +/** +* Interface for performing a reduction on an ndarray. +*/ +interface Unary { + /** + * Computes the arithmetic mean along one or more ndarray dimensions using an improved Kahan–Babuška algorithm. + * + * @param x - input ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * + * var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); + * + * var y = meankbn( x ); + * // returns + * + * var v = y.get(); + * // returns 1.25 + */ + ( x: InputArray, options?: Options ): OutputArray; // NOTE: we lose type specificity here, but retaining specificity would likely be difficult and/or tedious to completely enumerate, as the output ndarray data type is dependent on how `x` interacts with output data type policy and whether that policy has been overridden by `options.dtype`. + + /** + * Computes the arithmetic mean along one or more ndarray dimensions using an improved Kahan–Babuška algorithm and assigns results to a provided output ndarray. + * + * @param x - input ndarray + * @param out - output ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * var zeros = require( '@stdlib/ndarray/zeros' ); + * + * var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); + * var y = zeros( [] ); + * + * var out = meankbn.assign( x, y ); + * // returns + * + * var v = out.get(); + * // returns 1.25 + * + * var bool = ( out === y ); + * // returns true + */ + assign = OutputArray>( x: InputArray, out: U, options?: BaseOptions ): U; +} + +/** +* Computes the arithmetic mean along one or more ndarray dimensions using an improved Kahan–Babuška algorithm. +* +* @param x - input ndarray +* @param options - function options +* @returns output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); +* +* var y = meankbn( x ); +* // returns +* +* var v = y.get(); +* // returns 1.25 +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); +* var y = zeros( [] ); +* +* var out = meankbn.assign( x, y ); +* // returns +* +* var v = out.get(); +* // returns 1.25 +* +* var bool = ( out === y ); +* // returns true +*/ +declare const meankbn: Unary; + + +// EXPORTS // + +export = meankbn; diff --git a/lib/node_modules/@stdlib/stats/meankbn/docs/types/test.ts b/lib/node_modules/@stdlib/stats/meankbn/docs/types/test.ts new file mode 100644 index 000000000000..d2063a833c61 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn/docs/types/test.ts @@ -0,0 +1,225 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +/// + +import zeros = require( '@stdlib/ndarray/zeros' ); +import meankbn = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn( x ); // $ExpectType OutputArray + meankbn( x, {} ); // $ExpectType OutputArray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + meankbn( '5' ); // $ExpectError + meankbn( 5 ); // $ExpectError + meankbn( true ); // $ExpectError + meankbn( false ); // $ExpectError + meankbn( null ); // $ExpectError + meankbn( void 0 ); // $ExpectError + meankbn( {} ); // $ExpectError + meankbn( ( x: number ): number => x ); // $ExpectError + + meankbn( '5', {} ); // $ExpectError + meankbn( 5, {} ); // $ExpectError + meankbn( true, {} ); // $ExpectError + meankbn( false, {} ); // $ExpectError + meankbn( null, {} ); // $ExpectError + meankbn( void 0, {} ); // $ExpectError + meankbn( {}, {} ); // $ExpectError + meankbn( ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn( x, '5' ); // $ExpectError + meankbn( x, true ); // $ExpectError + meankbn( x, false ); // $ExpectError + meankbn( x, null ); // $ExpectError + meankbn( x, [] ); // $ExpectError + meankbn( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dtype` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn( x, { 'dtype': '5' } ); // $ExpectError + meankbn( x, { 'dtype': 5 } ); // $ExpectError + meankbn( x, { 'dtype': true } ); // $ExpectError + meankbn( x, { 'dtype': false } ); // $ExpectError + meankbn( x, { 'dtype': null } ); // $ExpectError + meankbn( x, { 'dtype': [] } ); // $ExpectError + meankbn( x, { 'dtype': {} } ); // $ExpectError + meankbn( x, { 'dtype': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `keepdims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn( x, { 'keepdims': '5' } ); // $ExpectError + meankbn( x, { 'keepdims': 5 } ); // $ExpectError + meankbn( x, { 'keepdims': null } ); // $ExpectError + meankbn( x, { 'keepdims': [] } ); // $ExpectError + meankbn( x, { 'keepdims': {} } ); // $ExpectError + meankbn( x, { 'keepdims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn( x, { 'dims': '5' } ); // $ExpectError + meankbn( x, { 'dims': 5 } ); // $ExpectError + meankbn( x, { 'dims': true } ); // $ExpectError + meankbn( x, { 'dims': false } ); // $ExpectError + meankbn( x, { 'dims': null } ); // $ExpectError + meankbn( x, { 'dims': {} } ); // $ExpectError + meankbn( x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn(); // $ExpectError + meankbn( x, {}, {} ); // $ExpectError +} + +// Attached to the function is an `assign` method which returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn.assign( x, x ); // $ExpectType float64ndarray + meankbn.assign( x, x, {} ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn.assign( '5', x ); // $ExpectError + meankbn.assign( 5, x ); // $ExpectError + meankbn.assign( true, x ); // $ExpectError + meankbn.assign( false, x ); // $ExpectError + meankbn.assign( null, x ); // $ExpectError + meankbn.assign( void 0, x ); // $ExpectError + meankbn.assign( {}, x ); // $ExpectError + meankbn.assign( ( x: number ): number => x, x ); // $ExpectError + + meankbn.assign( '5', x, {} ); // $ExpectError + meankbn.assign( 5, x, {} ); // $ExpectError + meankbn.assign( true, x, {} ); // $ExpectError + meankbn.assign( false, x, {} ); // $ExpectError + meankbn.assign( null, x, {} ); // $ExpectError + meankbn.assign( void 0, x, {} ); // $ExpectError + meankbn.assign( {}, x, {} ); // $ExpectError + meankbn.assign( ( x: number ): number => x, x, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn.assign( x, '5' ); // $ExpectError + meankbn.assign( x, 5 ); // $ExpectError + meankbn.assign( x, true ); // $ExpectError + meankbn.assign( x, false ); // $ExpectError + meankbn.assign( x, null ); // $ExpectError + meankbn.assign( x, void 0 ); // $ExpectError + meankbn.assign( x, ( x: number ): number => x ); // $ExpectError + + meankbn.assign( x, '5', {} ); // $ExpectError + meankbn.assign( x, 5, {} ); // $ExpectError + meankbn.assign( x, true, {} ); // $ExpectError + meankbn.assign( x, false, {} ); // $ExpectError + meankbn.assign( x, null, {} ); // $ExpectError + meankbn.assign( x, void 0, {} ); // $ExpectError + meankbn.assign( x, ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a third argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn.assign( x, x, '5' ); // $ExpectError + meankbn.assign( x, x, true ); // $ExpectError + meankbn.assign( x, x, false ); // $ExpectError + meankbn.assign( x, x, null ); // $ExpectError + meankbn.assign( x, x, [] ); // $ExpectError + meankbn.assign( x, x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn.assign( x, x, { 'dims': '5' } ); // $ExpectError + meankbn.assign( x, x, { 'dims': 5 } ); // $ExpectError + meankbn.assign( x, x, { 'dims': true } ); // $ExpectError + meankbn.assign( x, x, { 'dims': false } ); // $ExpectError + meankbn.assign( x, x, { 'dims': null } ); // $ExpectError + meankbn.assign( x, x, { 'dims': {} } ); // $ExpectError + meankbn.assign( x, x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn.assign(); // $ExpectError + meankbn.assign( x ); // $ExpectError + meankbn.assign( x, x, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/meankbn/examples/index.js b/lib/node_modules/@stdlib/stats/meankbn/examples/index.js new file mode 100644 index 000000000000..9894327b17cc --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn/examples/index.js @@ -0,0 +1,40 @@ +/** +* @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 uniform = require( '@stdlib/random/uniform' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var meankbn = require( './../lib' ); + +// Generate an array of random numbers: +var x = uniform( [ 5, 5 ], 0.0, 20.0 ); +console.log( ndarray2array( x ) ); + +// Perform a reduction: +var y = meankbn( x, { + 'dims': [ 0 ] +}); + +// Resolve the output array data type: +var dt = getDType( y ); +console.log( dt ); + +// Print the results: +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/stats/meankbn/lib/index.js b/lib/node_modules/@stdlib/stats/meankbn/lib/index.js new file mode 100644 index 000000000000..4c57edd9a491 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn/lib/index.js @@ -0,0 +1,63 @@ +/** +* @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 arithmetic mean along one or more ndarray dimensions using an improved Kahan–Babuška algorithm. +* +* @module @stdlib/stats/meankbn +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var meankbn = require( '@stdlib/stats/meankbn' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 0.0, 2.0, 3.0, 0.0, 0.0, 6.0, 7.0, 0.0, 0.0, 10.0, 11.0, 0.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform reduction: +* var out = meankbn( x ); +* // returns +* +* var v = out.get(); +* // returns 6.5 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/stats/meankbn/lib/main.js b/lib/node_modules/@stdlib/stats/meankbn/lib/main.js new file mode 100644 index 000000000000..754e5cd73ae6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn/lib/main.js @@ -0,0 +1,101 @@ +/** +* @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 dtypes = require( '@stdlib/ndarray/dtypes' ); +var gmeankbn = require( '@stdlib/stats/base/ndarray/meankbn' ); +var dmeankbn = require( '@stdlib/stats/base/ndarray/dmeankbn' ); +var smeankbn = require( '@stdlib/stats/base/ndarray/smeankbn' ); +var factory = require( '@stdlib/ndarray/base/unary-reduce-strided1d-dispatch-factory' ); + + +// VARIABLES // + +var idtypes = dtypes( 'real_and_generic' ); +var odtypes = dtypes( 'real_floating_point_and_generic' ); +var policies = { + 'output': 'real_floating_point_and_generic', + 'casting': 'none' +}; +var table = { + 'types': [ + 'float64', // input + 'float32' // input + ], + 'fcns': [ + dmeankbn, + smeankbn + ], + 'default': gmeankbn +}; + + +// MAIN // + +/** +* Computes the arithmetic mean along one or more ndarray dimensions using an improved Kahan–Babuška algorithm. +* +* @name meankbn +* @type {Function} +* @param {ndarray} x - input ndarray +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction +* @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions +* @param {string} [options.dtype] - output ndarray data type +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 0.0, 2.0, 3.0, 0.0, 0.0, 6.0, 7.0, 0.0, 0.0, 10.0, 11.0, 0.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform reduction: +* var out = meankbn( x ); +* // returns +* +* var v = out.get(); +* // returns 6.5 +*/ +var meankbn = factory( table, [ idtypes ], odtypes, policies ); + + +// EXPORTS // + +module.exports = meankbn; diff --git a/lib/node_modules/@stdlib/stats/meankbn/package.json b/lib/node_modules/@stdlib/stats/meankbn/package.json new file mode 100644 index 000000000000..754a59deeefa --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/stats/meankbn", + "version": "0.0.0", + "description": "Compute the arithmetic mean along one or more ndarray dimensions using an improved Kahan–Babuška algorithm.", + "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", + "average", + "avg", + "mean", + "arithmetic mean", + "central tendency", + "extent", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/meankbn/test/test.assign.js b/lib/node_modules/@stdlib/stats/meankbn/test/test.assign.js new file mode 100644 index 000000000000..2110cb41d5ac --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn/test/test.assign.js @@ -0,0 +1,717 @@ +/** +* @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 ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var meankbn = require( './../lib' ).assign; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof meankbn, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( value, out ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( value, out, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( value, out ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (options)', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( value, out, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( x, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not an object', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( x, out, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [ 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (default)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 2, 2 ], + [ 2 ], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + meankbn( x, out ); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (all dimensions)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 2, 2 ], + [ 2 ], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + meankbn( x, out, { + 'dims': [ 0, 1 ] + }); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (some dimensions)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + meankbn( x, out, { + 'dims': [ 0 ] + }); + }; + } +}); + +tape( 'the function performs a reduction on an ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = meankbn( x, out ); + expected = 1.25; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = meankbn( x, out ); + expected = 1.25; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = meankbn( x, out, { + 'dims': [ 0, 1 ] + }); + expected = 1.25; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = meankbn( x, out, { + 'dims': [ 0, 1 ] + }); + expected = 1.25; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2, 2 ] + }); + + actual = meankbn( x, out, { + 'dims': [] + }); + expected = [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2, 2 ] + }); + + actual = meankbn( x, out, { + 'dims': [] + }); + expected = [ [ 1.0, -2.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = meankbn( x, out, { + 'dims': [ 0 ] + }); + expected = [ -0.5, 3.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = meankbn( x, out, { + 'dims': [ 1 ] + }); + expected = [ 1.5, 1.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = meankbn( x, out, { + 'dims': [ 0 ] + }); + expected = [ 1.5, 1.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = meankbn( x, out, { + 'dims': [ 1 ] + }); + expected = [ -0.5, 3.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/meankbn/test/test.js b/lib/node_modules/@stdlib/stats/meankbn/test/test.js new file mode 100644 index 000000000000..3eee01bdf4e0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn/test/test.js @@ -0,0 +1,39 @@ +/** +* @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 isMethod = require( '@stdlib/assert/is-method' ); +var meankbn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof meankbn, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( meankbn, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/meankbn/test/test.main.js b/lib/node_modules/@stdlib/stats/meankbn/test/test.main.js new file mode 100644 index 000000000000..eff031c2df86 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn/test/test.main.js @@ -0,0 +1,753 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var meankbn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof meankbn, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (options)', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dtype` option which is not a supported data type', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + 'foo', + 'bar', + 'beep', + 'boop' + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( x, { + 'dtype': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `keepdims` option which is not a boolean', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( x, { + 'keepdims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 0, 20 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function performs a reduction on an ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn( x ); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn( x ); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn( x, { + 'dims': [ 0, 1 ] + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn( x, { + 'dims': [ 0, 1 ], + 'keepdims': false + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn( x, { + 'dims': [ 0, 1 ], + 'keepdims': true + }); + expected = [ [ 1.25 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn( x, { + 'dims': [ 0, 1 ] + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn( x, { + 'dims': [ 0, 1 ], + 'keepdims': false + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn( x, { + 'dims': [ 0, 1 ], + 'keepdims': true + }); + expected = [ [ 1.25 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn( x, { + 'dims': [], + 'keepdims': false + }); + expected = [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn( x, { + 'dims': [], + 'keepdims': true + }); + expected = [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn( x, { + 'dims': [], + 'keepdims': false + }); + expected = [ [ 1.0, -2.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn( x, { + 'dims': [], + 'keepdims': true + }); + expected = [ [ 1.0, -2.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn( x, { + 'dims': [ 0 ], + 'keepdims': false + }); + expected = [ -0.5, 3.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn( x, { + 'dims': [ 0 ], + 'keepdims': true + }); + expected = [ [ -0.5, 3.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn( x, { + 'dims': [ 1 ], + 'keepdims': false + }); + expected = [ 1.5, 1.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn( x, { + 'dims': [ 1 ], + 'keepdims': true + }); + expected = [ [ 1.5 ], [ 1.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn( x, { + 'dims': [ 0 ], + 'keepdims': false + }); + expected = [ 1.5, 1.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn( x, { + 'dims': [ 0 ], + 'keepdims': true + }); + expected = [ [ 1.5, 1.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn( x, { + 'dims': [ 1 ], + 'keepdims': false + }); + expected = [ -0.5, 3.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn( x, { + 'dims': [ 1 ], + 'keepdims': true + }); + expected = [ [ -0.5 ], [ 3.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the output array data type', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn( x, { + 'dtype': 'float64' + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn( x, { + 'dtype': 'float64' + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/meankbn2/README.md b/lib/node_modules/@stdlib/stats/meankbn2/README.md new file mode 100644 index 000000000000..aae015619a24 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn2/README.md @@ -0,0 +1,297 @@ + + +# meankbn2 + +> Compute the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using a second-order iterative Kahan–Babuška algorithm. + +
+ +The [arithmetic mean][arithmetic-mean] is defined as + + + +```math +\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var meankbn2 = require( '@stdlib/stats/meankbn2' ); +``` + +#### meankbn2( x\[, options] ) + +Computes the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using a second-order iterative Kahan–Babuška algorithm. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); + +var y = meankbn2( x ); +// returns + +var v = y.get(); +// returns 1.25 +``` + +The function has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **options**: function options (_optional_). + +The function accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. + +By default, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform a reduction over specific dimensions, provide a `dims` option. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ], { + 'shape': [ 2, 2 ], + 'order': 'row-major' +}); +var v = ndarray2array( x ); +// returns [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ] + +var y = meankbn2( x, { + 'dims': [ 0 ] +}); +// returns + +v = ndarray2array( y ); +// returns [ -0.5, 3.0 ] + +y = meankbn2( x, { + 'dims': [ 1 ] +}); +// returns + +v = ndarray2array( y ); +// returns [ 1.5, 1.0 ] + +y = meankbn2( x, { + 'dims': [ 0, 1 ] +}); +// returns + +v = y.get(); +// returns 1.25 +``` + +By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ], { + 'shape': [ 2, 2 ], + 'order': 'row-major' +}); + +var v = ndarray2array( x ); +// returns [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ] + +var y = meankbn2( x, { + 'dims': [ 0 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ -0.5, 3.0 ] ] + +y = meankbn2( x, { + 'dims': [ 1 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 1.5 ], [ 1.0 ] ] + +y = meankbn2( x, { + 'dims': [ 0, 1 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 1.25 ] ] +``` + +By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option. + +```javascript +var getDType = require( '@stdlib/ndarray/dtype' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ], { + 'dtype': 'generic' +}); + +var y = meankbn2( x, { + 'dtype': 'float64' +}); +// returns + +var dt = String( getDType( y ) ); +// returns 'float64' +``` + +#### meankbn2.assign( x, out\[, options] ) + +Computes the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using a second-order iterative Kahan–Babuška algorithm and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); +var y = zeros( [] ); + +var out = meankbn2.assign( x, y ); +// returns + +var v = out.get(); +// returns 1.25 + +var bool = ( out === y ); +// returns true +``` + +The method has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or generic [data type][@stdlib/ndarray/dtypes]. +- **out**: output [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). + +The method accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. + +
+ + + +
+ +## Notes + +- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor]. +- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes]. + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var meankbn2 = require( '@stdlib/stats/meankbn2' ); + +// Generate an array of random numbers: +var x = uniform( [ 5, 5 ], 0.0, 20.0 ); +console.log( ndarray2array( x ) ); + +// Perform a reduction: +var y = meankbn2( x, { + 'dims': [ 0 ] +}); + +// Resolve the output array data type: +var dt = getDType( y ); +console.log( dt ); + +// Print the results: +console.log( ndarray2array( y ) ); +``` + +
+ + + +* * * + +
+ +## References + +- Klein, Andreas. 2005. "A Generalized Kahan-Babuška-Summation-Algorithm." _Computing_ 76 (3): 279–93. doi:[10.1007/s00607-005-0139-x][@klein:2005a]. + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/meankbn2/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/stats/meankbn2/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..769aa698e5e1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn2/benchmark/benchmark.assign.js @@ -0,0 +1,111 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var zeros = require( '@stdlib/array/zeros' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var meankbn2 = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out; + var x; + + x = uniform( len, -50.0, 50.0, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + out = new ndarray( options.dtype, zeros( 1, options.dtype ), [], [ 0 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = meankbn2.assign( x, out ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + 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( pkg+':assign:dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/meankbn2/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/meankbn2/benchmark/benchmark.js new file mode 100644 index 000000000000..15fcc5645f15 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn2/benchmark/benchmark.js @@ -0,0 +1,105 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var meankbn2 = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -50.0, 50.0, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = meankbn2( x ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + 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( pkg+':dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/meankbn2/docs/repl.txt b/lib/node_modules/@stdlib/stats/meankbn2/docs/repl.txt new file mode 100644 index 000000000000..83cdd5cb9dad --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn2/docs/repl.txt @@ -0,0 +1,79 @@ + +{{alias}}( x[, options] ) + Computes the arithmetic mean along one or more ndarray dimensions using a + second-order iterative Kahan–Babuška algorithm. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or "generic" data type. + + options: Object (optional) + Function options. + + options.dtype: string (optional) + Output array data type. Must be a real-valued floating-point or + "generic" data type. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in a provided input + ndarray. + + options.keepdims: boolean (optional) + Boolean indicating whether the reduced dimensions should be included in + the returned ndarray as singleton dimensions. Default: false. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ -1.0, 2.0, -3.0, -4.0 ] ); + > var y = {{alias}}( x ); + > var v = y.get() + -1.5 + + +{{alias}}.assign( x, out[, options] ) + Computes the arithmetic mean along one or more ndarray dimensions using a + second-order iterative Kahan–Babuška algorithm and assigns results to a + provided output ndarray. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or "generic" data type. + + out: ndarray + Output array. + + options: Object (optional) + Function options. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in a provided input + ndarray. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ -1.0, 2.0, -3.0, -4.0 ] ); + > var out = {{alias:@stdlib/ndarray/zeros}}( [] ); + > var y = {{alias}}.assign( x, out ) + + > var bool = ( out === y ) + true + > var v = out.get() + -1.5 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/meankbn2/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/meankbn2/docs/types/index.d.ts new file mode 100644 index 000000000000..201590332f57 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn2/docs/types/index.d.ts @@ -0,0 +1,151 @@ +/* +* @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 { ArrayLike } from '@stdlib/types/array'; +import { RealFloatingPointAndGenericDataType as DataType, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Input array. +*/ +type InputArray = typedndarray; + +/** +* Output array. +*/ +type OutputArray = typedndarray; + +/** +* Interface defining "base" options. +*/ +interface BaseOptions { + /** + * List of dimensions over which to perform a reduction. + */ + dims?: ArrayLike; +} + +/** +* Interface defining options. +*/ +interface Options extends BaseOptions { + /** + * Output array data type. + */ + dtype?: DataType; + + /** + * Boolean indicating whether the reduced dimensions should be included in the returned array as singleton dimensions. Default: `false`. + */ + keepdims?: boolean; +} + +/** +* Interface for performing a reduction on an ndarray. +*/ +interface Unary { + /** + * Computes the arithmetic mean along one or more ndarray dimensions using a second-order iterative Kahan–Babuška algorithm. + * + * @param x - input ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * + * var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); + * + * var y = meankbn2( x ); + * // returns + * + * var v = y.get(); + * // returns 1.25 + */ + ( x: InputArray, options?: Options ): OutputArray; // NOTE: we lose type specificity here, but retaining specificity would likely be difficult and/or tedious to completely enumerate, as the output ndarray data type is dependent on how `x` interacts with output data type policy and whether that policy has been overridden by `options.dtype`. + + /** + * Computes the arithmetic mean along one or more ndarray dimensions using a second-order iterative Kahan–Babuška algorithm and assigns results to a provided output ndarray. + * + * @param x - input ndarray + * @param out - output ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * var zeros = require( '@stdlib/ndarray/zeros' ); + * + * var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); + * var y = zeros( [] ); + * + * var out = meankbn2.assign( x, y ); + * // returns + * + * var v = out.get(); + * // returns 1.25 + * + * var bool = ( out === y ); + * // returns true + */ + assign = OutputArray>( x: InputArray, out: U, options?: BaseOptions ): U; +} + +/** +* Computes the arithmetic mean along one or more ndarray dimensions using a second-order iterative Kahan-Babuska algorithm. +* +* @param x - input ndarray +* @param options - function options +* @returns output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); +* +* var y = meankbn2( x ); +* // returns +* +* var v = y.get(); +* // returns 1.25 +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); +* var y = zeros( [] ); +* +* var out = meankbn2.assign( x, y ); +* // returns +* +* var v = out.get(); +* // returns 1.25 +* +* var bool = ( out === y ); +* // returns true +*/ +declare const meankbn2: Unary; + + +// EXPORTS // + +export = meankbn2; diff --git a/lib/node_modules/@stdlib/stats/meankbn2/docs/types/test.ts b/lib/node_modules/@stdlib/stats/meankbn2/docs/types/test.ts new file mode 100644 index 000000000000..b63a27fc73b5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn2/docs/types/test.ts @@ -0,0 +1,225 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +/// + +import zeros = require( '@stdlib/ndarray/zeros' ); +import meankbn2 = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn2( x ); // $ExpectType OutputArray + meankbn2( x, {} ); // $ExpectType OutputArray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + meankbn2( '5' ); // $ExpectError + meankbn2( 5 ); // $ExpectError + meankbn2( true ); // $ExpectError + meankbn2( false ); // $ExpectError + meankbn2( null ); // $ExpectError + meankbn2( void 0 ); // $ExpectError + meankbn2( {} ); // $ExpectError + meankbn2( ( x: number ): number => x ); // $ExpectError + + meankbn2( '5', {} ); // $ExpectError + meankbn2( 5, {} ); // $ExpectError + meankbn2( true, {} ); // $ExpectError + meankbn2( false, {} ); // $ExpectError + meankbn2( null, {} ); // $ExpectError + meankbn2( void 0, {} ); // $ExpectError + meankbn2( {}, {} ); // $ExpectError + meankbn2( ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn2( x, '5' ); // $ExpectError + meankbn2( x, true ); // $ExpectError + meankbn2( x, false ); // $ExpectError + meankbn2( x, null ); // $ExpectError + meankbn2( x, [] ); // $ExpectError + meankbn2( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dtype` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn2( x, { 'dtype': '5' } ); // $ExpectError + meankbn2( x, { 'dtype': 5 } ); // $ExpectError + meankbn2( x, { 'dtype': true } ); // $ExpectError + meankbn2( x, { 'dtype': false } ); // $ExpectError + meankbn2( x, { 'dtype': null } ); // $ExpectError + meankbn2( x, { 'dtype': [] } ); // $ExpectError + meankbn2( x, { 'dtype': {} } ); // $ExpectError + meankbn2( x, { 'dtype': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `keepdims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn2( x, { 'keepdims': '5' } ); // $ExpectError + meankbn2( x, { 'keepdims': 5 } ); // $ExpectError + meankbn2( x, { 'keepdims': null } ); // $ExpectError + meankbn2( x, { 'keepdims': [] } ); // $ExpectError + meankbn2( x, { 'keepdims': {} } ); // $ExpectError + meankbn2( x, { 'keepdims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn2( x, { 'dims': '5' } ); // $ExpectError + meankbn2( x, { 'dims': 5 } ); // $ExpectError + meankbn2( x, { 'dims': true } ); // $ExpectError + meankbn2( x, { 'dims': false } ); // $ExpectError + meankbn2( x, { 'dims': null } ); // $ExpectError + meankbn2( x, { 'dims': {} } ); // $ExpectError + meankbn2( x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn2(); // $ExpectError + meankbn2( x, {}, {} ); // $ExpectError +} + +// Attached to the function is an `assign` method which returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn2.assign( x, x ); // $ExpectType float64ndarray + meankbn2.assign( x, x, {} ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn2.assign( '5', x ); // $ExpectError + meankbn2.assign( 5, x ); // $ExpectError + meankbn2.assign( true, x ); // $ExpectError + meankbn2.assign( false, x ); // $ExpectError + meankbn2.assign( null, x ); // $ExpectError + meankbn2.assign( void 0, x ); // $ExpectError + meankbn2.assign( {}, x ); // $ExpectError + meankbn2.assign( ( x: number ): number => x, x ); // $ExpectError + + meankbn2.assign( '5', x, {} ); // $ExpectError + meankbn2.assign( 5, x, {} ); // $ExpectError + meankbn2.assign( true, x, {} ); // $ExpectError + meankbn2.assign( false, x, {} ); // $ExpectError + meankbn2.assign( null, x, {} ); // $ExpectError + meankbn2.assign( void 0, x, {} ); // $ExpectError + meankbn2.assign( {}, x, {} ); // $ExpectError + meankbn2.assign( ( x: number ): number => x, x, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn2.assign( x, '5' ); // $ExpectError + meankbn2.assign( x, 5 ); // $ExpectError + meankbn2.assign( x, true ); // $ExpectError + meankbn2.assign( x, false ); // $ExpectError + meankbn2.assign( x, null ); // $ExpectError + meankbn2.assign( x, void 0 ); // $ExpectError + meankbn2.assign( x, ( x: number ): number => x ); // $ExpectError + + meankbn2.assign( x, '5', {} ); // $ExpectError + meankbn2.assign( x, 5, {} ); // $ExpectError + meankbn2.assign( x, true, {} ); // $ExpectError + meankbn2.assign( x, false, {} ); // $ExpectError + meankbn2.assign( x, null, {} ); // $ExpectError + meankbn2.assign( x, void 0, {} ); // $ExpectError + meankbn2.assign( x, ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a third argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn2.assign( x, x, '5' ); // $ExpectError + meankbn2.assign( x, x, 5 ); // $ExpectError + meankbn2.assign( x, x, true ); // $ExpectError + meankbn2.assign( x, x, false ); // $ExpectError + meankbn2.assign( x, x, null ); // $ExpectError + meankbn2.assign( x, x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn2.assign( x, x, { 'dims': '5' } ); // $ExpectError + meankbn2.assign( x, x, { 'dims': 5 } ); // $ExpectError + meankbn2.assign( x, x, { 'dims': true } ); // $ExpectError + meankbn2.assign( x, x, { 'dims': false } ); // $ExpectError + meankbn2.assign( x, x, { 'dims': null } ); // $ExpectError + meankbn2.assign( x, x, { 'dims': {} } ); // $ExpectError + meankbn2.assign( x, x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meankbn2.assign(); // $ExpectError + meankbn2.assign( x ); // $ExpectError + meankbn2.assign( x, x, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/meankbn2/examples/index.js b/lib/node_modules/@stdlib/stats/meankbn2/examples/index.js new file mode 100644 index 000000000000..fd4b5a0d9c9b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn2/examples/index.js @@ -0,0 +1,40 @@ +/** +* @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 uniform = require( '@stdlib/random/uniform' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var meankbn2 = require( './../lib' ); + +// Generate an array of random numbers: +var x = uniform( [ 5, 5 ], 0.0, 20.0 ); +console.log( ndarray2array( x ) ); + +// Perform a reduction: +var y = meankbn2( x, { + 'dims': [ 0 ] +}); + +// Resolve the output array data type: +var dt = getDType( y ); +console.log( dt ); + +// Print the results: +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/stats/meankbn2/lib/index.js b/lib/node_modules/@stdlib/stats/meankbn2/lib/index.js new file mode 100644 index 000000000000..af8f2789901b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn2/lib/index.js @@ -0,0 +1,63 @@ +/** +* @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 arithmetic mean along one or more ndarray dimensions using a second-order iterative Kahan–Babuška algorithm. +* +* @module @stdlib/stats/meankbn2 +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var meankbn2 = require( '@stdlib/stats/meankbn2' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 0.0, 2.0, 3.0, 0.0, 0.0, 6.0, 7.0, 0.0, 0.0, 10.0, 11.0, 0.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform reduction: +* var out = meankbn2( x ); +* // returns +* +* var v = out.get(); +* // returns 6.5 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/stats/meankbn2/lib/main.js b/lib/node_modules/@stdlib/stats/meankbn2/lib/main.js new file mode 100644 index 000000000000..8dfcf24d61e2 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn2/lib/main.js @@ -0,0 +1,101 @@ +/** +* @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 dtypes = require( '@stdlib/ndarray/dtypes' ); +var gmeankbn2 = require( '@stdlib/stats/base/ndarray/meankbn2' ); +var dmeankbn2 = require( '@stdlib/stats/base/ndarray/dmeankbn2' ); +var smeankbn2 = require( '@stdlib/stats/base/ndarray/smeankbn2' ); +var factory = require( '@stdlib/ndarray/base/unary-reduce-strided1d-dispatch-factory' ); + + +// VARIABLES // + +var idtypes = dtypes( 'real_and_generic' ); +var odtypes = dtypes( 'real_floating_point_and_generic' ); +var policies = { + 'output': 'real_floating_point_and_generic', + 'casting': 'none' +}; +var table = { + 'types': [ + 'float64', // input + 'float32' // input + ], + 'fcns': [ + dmeankbn2, + smeankbn2 + ], + 'default': gmeankbn2 +}; + + +// MAIN // + +/** +* Computes the arithmetic mean along one or more ndarray dimensions using a second-order iterative Kahan–Babuška algorithm. +* +* @name meankbn2 +* @type {Function} +* @param {ndarray} x - input ndarray +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction +* @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions +* @param {string} [options.dtype] - output ndarray data type +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 0.0, 2.0, 3.0, 0.0, 0.0, 6.0, 7.0, 0.0, 0.0, 10.0, 11.0, 0.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform reduction: +* var out = meankbn2( x ); +* // returns +* +* var v = out.get(); +* // returns 6.5 +*/ +var meankbn2 = factory( table, [ idtypes ], odtypes, policies ); + + +// EXPORTS // + +module.exports = meankbn2; diff --git a/lib/node_modules/@stdlib/stats/meankbn2/package.json b/lib/node_modules/@stdlib/stats/meankbn2/package.json new file mode 100644 index 000000000000..c8a4e152b7ae --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn2/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/stats/meankbn2", + "version": "0.0.0", + "description": "Compute the arithmetic mean along one or more ndarray dimensions using a second-order iterative Kahan–Babuška algorithm.", + "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", + "average", + "avg", + "mean", + "arithmetic mean", + "central tendency", + "extent", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/meankbn2/test/test.assign.js b/lib/node_modules/@stdlib/stats/meankbn2/test/test.assign.js new file mode 100644 index 000000000000..05b3d0f5a7fa --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn2/test/test.assign.js @@ -0,0 +1,717 @@ +/** +* @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 ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var meankbn2 = require( './../lib' ).assign; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof meankbn2, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( value, out ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( value, out, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( value, out ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (options)', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( value, out, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( x, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not an object', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( x, out, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [ 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (default)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 2, 2 ], + [ 2 ], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + meankbn2( x, out ); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (all dimensions)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 2, 2 ], + [ 2 ], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + meankbn2( x, out, { + 'dims': [ 0, 1 ] + }); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (some dimensions)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + meankbn2( x, out, { + 'dims': [ 0 ] + }); + }; + } +}); + +tape( 'the function performs a reduction on an ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = meankbn2( x, out ); + expected = 1.25; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = meankbn2( x, out ); + expected = 1.25; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = meankbn2( x, out, { + 'dims': [ 0, 1 ] + }); + expected = 1.25; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = meankbn2( x, out, { + 'dims': [ 0, 1 ] + }); + expected = 1.25; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2, 2 ] + }); + + actual = meankbn2( x, out, { + 'dims': [] + }); + expected = [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2, 2 ] + }); + + actual = meankbn2( x, out, { + 'dims': [] + }); + expected = [ [ 1.0, -2.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = meankbn2( x, out, { + 'dims': [ 0 ] + }); + expected = [ -0.5, 3.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = meankbn2( x, out, { + 'dims': [ 1 ] + }); + expected = [ 1.5, 1.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = meankbn2( x, out, { + 'dims': [ 0 ] + }); + expected = [ 1.5, 1.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = meankbn2( x, out, { + 'dims': [ 1 ] + }); + expected = [ -0.5, 3.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/meankbn2/test/test.js b/lib/node_modules/@stdlib/stats/meankbn2/test/test.js new file mode 100644 index 000000000000..a4f098e6742c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn2/test/test.js @@ -0,0 +1,39 @@ +/** +* @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 isMethod = require( '@stdlib/assert/is-method' ); +var meankbn2 = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof meankbn2, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( meankbn2, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/meankbn2/test/test.main.js b/lib/node_modules/@stdlib/stats/meankbn2/test/test.main.js new file mode 100644 index 000000000000..08d595bb5cfd --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meankbn2/test/test.main.js @@ -0,0 +1,753 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var meankbn2 = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof meankbn2, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (options)', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dtype` option which is not a supported data type', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + 'foo', + 'bar', + 'beep', + 'boop' + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( x, { + 'dtype': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `keepdims` option which is not a boolean', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( x, { + 'keepdims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 0, 20 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meankbn2( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function performs a reduction on an ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn2( x ); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn2( x ); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn2( x, { + 'dims': [ 0, 1 ] + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn2( x, { + 'dims': [ 0, 1 ], + 'keepdims': false + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn2( x, { + 'dims': [ 0, 1 ], + 'keepdims': true + }); + expected = [ [ 1.25 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn2( x, { + 'dims': [ 0, 1 ] + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn2( x, { + 'dims': [ 0, 1 ], + 'keepdims': false + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn2( x, { + 'dims': [ 0, 1 ], + 'keepdims': true + }); + expected = [ [ 1.25 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn2( x, { + 'dims': [], + 'keepdims': false + }); + expected = [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn2( x, { + 'dims': [], + 'keepdims': true + }); + expected = [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn2( x, { + 'dims': [], + 'keepdims': false + }); + expected = [ [ 1.0, -2.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn2( x, { + 'dims': [], + 'keepdims': true + }); + expected = [ [ 1.0, -2.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn2( x, { + 'dims': [ 0 ], + 'keepdims': false + }); + expected = [ -0.5, 3.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn2( x, { + 'dims': [ 0 ], + 'keepdims': true + }); + expected = [ [ -0.5, 3.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn2( x, { + 'dims': [ 1 ], + 'keepdims': false + }); + expected = [ 1.5, 1.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn2( x, { + 'dims': [ 1 ], + 'keepdims': true + }); + expected = [ [ 1.5 ], [ 1.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn2( x, { + 'dims': [ 0 ], + 'keepdims': false + }); + expected = [ 1.5, 1.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn2( x, { + 'dims': [ 0 ], + 'keepdims': true + }); + expected = [ [ 1.5, 1.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn2( x, { + 'dims': [ 1 ], + 'keepdims': false + }); + expected = [ -0.5, 3.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn2( x, { + 'dims': [ 1 ], + 'keepdims': true + }); + expected = [ [ -0.5 ], [ 3.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the output array data type', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meankbn2( x, { + 'dtype': 'float64' + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meankbn2( x, { + 'dtype': 'float64' + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/meanpn/README.md b/lib/node_modules/@stdlib/stats/meanpn/README.md new file mode 100644 index 000000000000..9bfb41d02c14 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpn/README.md @@ -0,0 +1,300 @@ + + +# meanpn + +> Compute the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using a two-pass error correction algorithm. + +
+ +The [arithmetic mean][arithmetic-mean] is defined as + + + +```math +\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var meanpn = require( '@stdlib/stats/meanpn' ); +``` + +#### meanpn( x\[, options] ) + +Computes the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using a two-pass error correction algorithm. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); + +var y = meanpn( x ); +// returns + +var v = y.get(); +// returns 1.25 +``` + +The function has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **options**: function options (_optional_). + +The function accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. + +By default, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform a reduction over specific dimensions, provide a `dims` option. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ], { + 'shape': [ 2, 2 ], + 'order': 'row-major' +}); +var v = ndarray2array( x ); +// returns [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ] + +var y = meanpn( x, { + 'dims': [ 0 ] +}); +// returns + +v = ndarray2array( y ); +// returns [ -0.5, 3.0 ] + +y = meanpn( x, { + 'dims': [ 1 ] +}); +// returns + +v = ndarray2array( y ); +// returns [ 1.5, 1.0 ] + +y = meanpn( x, { + 'dims': [ 0, 1 ] +}); +// returns + +v = y.get(); +// returns 1.25 +``` + +By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ], { + 'shape': [ 2, 2 ], + 'order': 'row-major' +}); + +var v = ndarray2array( x ); +// returns [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ] + +var y = meanpn( x, { + 'dims': [ 0 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ -0.5, 3.0 ] ] + +y = meanpn( x, { + 'dims': [ 1 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 1.5 ], [ 1.0 ] ] + +y = meanpn( x, { + 'dims': [ 0, 1 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 1.25 ] ] +``` + +By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option. + +```javascript +var getDType = require( '@stdlib/ndarray/dtype' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ], { + 'dtype': 'generic' +}); + +var y = meanpn( x, { + 'dtype': 'float64' +}); +// returns + +var dt = String( getDType( y ) ); +// returns 'float64' +``` + +#### meanpn.assign( x, out\[, options] ) + +Computes the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using a two-pass error correction algorithm and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); +var y = zeros( [] ); + +var out = meanpn.assign( x, y ); +// returns + +var v = out.get(); +// returns 1.25 + +var bool = ( out === y ); +// returns true +``` + +The method has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or generic [data type][@stdlib/ndarray/dtypes]. +- **out**: output [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). + +The method accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. + +
+ + + +
+ +## Notes + +- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor]. +- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes]. + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var meanpn = require( '@stdlib/stats/meanpn' ); + +// Generate an array of random numbers: +var x = uniform( [ 5, 5 ], 0.0, 20.0 ); +console.log( ndarray2array( x ) ); + +// Perform a reduction: +var y = meanpn( x, { + 'dims': [ 0 ] +}); + +// Resolve the output array data type: +var dt = getDType( y ); +console.log( dt ); + +// Print the results: +console.log( ndarray2array( y ) ); +``` + +
+ + + +* * * + +
+ +## References + +- Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." _Communications of the ACM_ 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958][@neely:1966a]. +- Schubert, Erich, and Michael Gertz. 2018. "Numerically Stable Parallel Computation of (Co-)Variance." In _Proceedings of the 30th International Conference on Scientific and Statistical Database Management_. New York, NY, USA: Association for Computing Machinery. doi:[10.1145/3221269.3223036][@schubert:2018a]. + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/meanpn/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/stats/meanpn/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..166bb6697513 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpn/benchmark/benchmark.assign.js @@ -0,0 +1,111 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var zeros = require( '@stdlib/array/zeros' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var meanpn = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out; + var x; + + x = uniform( len, -50.0, 50.0, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + out = new ndarray( options.dtype, zeros( 1, options.dtype ), [], [ 0 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = meanpn.assign( x, out ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + 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( pkg+':assign:dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/meanpn/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/meanpn/benchmark/benchmark.js new file mode 100644 index 000000000000..d160a803ad2a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpn/benchmark/benchmark.js @@ -0,0 +1,105 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var meanpn = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -50.0, 50.0, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = meanpn( x ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + 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( pkg+':dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/meanpn/docs/repl.txt b/lib/node_modules/@stdlib/stats/meanpn/docs/repl.txt new file mode 100644 index 000000000000..57ea8aaeefcb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpn/docs/repl.txt @@ -0,0 +1,79 @@ + +{{alias}}( x[, options] ) + Computes the arithmetic mean along one or more ndarray dimensions using a + two-pass error correction algorithm. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or "generic" data type. + + options: Object (optional) + Function options. + + options.dtype: string (optional) + Output array data type. Must be a real-valued floating-point or + "generic" data type. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in a provided input + ndarray. + + options.keepdims: boolean (optional) + Boolean indicating whether the reduced dimensions should be included in + the returned ndarray as singleton dimensions. Default: false. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ -1.0, 2.0, -3.0, -4.0 ] ); + > var y = {{alias}}( x ); + > var v = y.get() + -1.5 + + +{{alias}}.assign( x, out[, options] ) + Computes the arithmetic mean along one or more ndarray dimensions using a + two-pass error correction algorithm and assigns results to a provided output + ndarray. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or "generic" data type. + + out: ndarray + Output array. + + options: Object (optional) + Function options. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in a provided input + ndarray. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ -1.0, 2.0, -3.0, -4.0 ] ); + > var out = {{alias:@stdlib/ndarray/zeros}}( [] ); + > var y = {{alias}}.assign( x, out ) + + > var bool = ( out === y ) + true + > var v = out.get() + -1.5 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/meanpn/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/meanpn/docs/types/index.d.ts new file mode 100644 index 000000000000..437bfb349a8e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpn/docs/types/index.d.ts @@ -0,0 +1,151 @@ +/* +* @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 { ArrayLike } from '@stdlib/types/array'; +import { RealFloatingPointAndGenericDataType as DataType, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Input array. +*/ +type InputArray = typedndarray; + +/** +* Output array. +*/ +type OutputArray = typedndarray; + +/** +* Interface defining "base" options. +*/ +interface BaseOptions { + /** + * List of dimensions over which to perform a reduction. + */ + dims?: ArrayLike; +} + +/** +* Interface defining options. +*/ +interface Options extends BaseOptions { + /** + * Output array data type. + */ + dtype?: DataType; + + /** + * Boolean indicating whether the reduced dimensions should be included in the returned array as singleton dimensions. Default: `false`. + */ + keepdims?: boolean; +} + +/** +* Interface for performing a reduction on an ndarray. +*/ +interface Unary { + /** + * Computes the arithmetic mean along one or more ndarray dimensions using a two-pass error correction algorithm. + * + * @param x - input ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * + * var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); + * + * var y = meanpn( x ); + * // returns + * + * var v = y.get(); + * // returns 1.25 + */ + ( x: InputArray, options?: Options ): OutputArray; // NOTE: we lose type specificity here, but retaining specificity would likely be difficult and/or tedious to completely enumerate, as the output ndarray data type is dependent on how `x` interacts with output data type policy and whether that policy has been overridden by `options.dtype`. + + /** + * Computes the arithmetic mean along one or more ndarray dimensions using a two-pass error correction algorithm and assigns results to a provided output ndarray. + * + * @param x - input ndarray + * @param out - output ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * var zeros = require( '@stdlib/ndarray/zeros' ); + * + * var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); + * var y = zeros( [] ); + * + * var out = meanpn.assign( x, y ); + * // returns + * + * var v = out.get(); + * // returns 1.25 + * + * var bool = ( out === y ); + * // returns true + */ + assign = OutputArray>( x: InputArray, out: U, options?: BaseOptions ): U; +} + +/** +* Computes the arithmetic mean along one or more ndarray dimensions using a two-pass error correction algorithm. +* +* @param x - input ndarray +* @param options - function options +* @returns output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); +* +* var y = meanpn( x ); +* // returns +* +* var v = y.get(); +* // returns 1.25 +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); +* var y = zeros( [] ); +* +* var out = meanpn.assign( x, y ); +* // returns +* +* var v = out.get(); +* // returns 1.25 +* +* var bool = ( out === y ); +* // returns true +*/ +declare const meanpn: Unary; + + +// EXPORTS // + +export = meanpn; diff --git a/lib/node_modules/@stdlib/stats/meanpn/docs/types/test.ts b/lib/node_modules/@stdlib/stats/meanpn/docs/types/test.ts new file mode 100644 index 000000000000..7bed42031b7c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpn/docs/types/test.ts @@ -0,0 +1,225 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +/// + +import zeros = require( '@stdlib/ndarray/zeros' ); +import meanpn = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpn( x ); // $ExpectType OutputArray + meanpn( x, {} ); // $ExpectType OutputArray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + meanpn( '5' ); // $ExpectError + meanpn( 5 ); // $ExpectError + meanpn( true ); // $ExpectError + meanpn( false ); // $ExpectError + meanpn( null ); // $ExpectError + meanpn( void 0 ); // $ExpectError + meanpn( {} ); // $ExpectError + meanpn( ( x: number ): number => x ); // $ExpectError + + meanpn( '5', {} ); // $ExpectError + meanpn( 5, {} ); // $ExpectError + meanpn( true, {} ); // $ExpectError + meanpn( false, {} ); // $ExpectError + meanpn( null, {} ); // $ExpectError + meanpn( void 0, {} ); // $ExpectError + meanpn( {}, {} ); // $ExpectError + meanpn( ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpn( x, '5' ); // $ExpectError + meanpn( x, true ); // $ExpectError + meanpn( x, false ); // $ExpectError + meanpn( x, null ); // $ExpectError + meanpn( x, [] ); // $ExpectError + meanpn( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dtype` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpn( x, { 'dtype': '5' } ); // $ExpectError + meanpn( x, { 'dtype': 5 } ); // $ExpectError + meanpn( x, { 'dtype': true } ); // $ExpectError + meanpn( x, { 'dtype': false } ); // $ExpectError + meanpn( x, { 'dtype': null } ); // $ExpectError + meanpn( x, { 'dtype': [] } ); // $ExpectError + meanpn( x, { 'dtype': {} } ); // $ExpectError + meanpn( x, { 'dtype': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `keepdims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpn( x, { 'keepdims': '5' } ); // $ExpectError + meanpn( x, { 'keepdims': 5 } ); // $ExpectError + meanpn( x, { 'keepdims': null } ); // $ExpectError + meanpn( x, { 'keepdims': [] } ); // $ExpectError + meanpn( x, { 'keepdims': {} } ); // $ExpectError + meanpn( x, { 'keepdims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpn( x, { 'dims': '5' } ); // $ExpectError + meanpn( x, { 'dims': 5 } ); // $ExpectError + meanpn( x, { 'dims': true } ); // $ExpectError + meanpn( x, { 'dims': false } ); // $ExpectError + meanpn( x, { 'dims': null } ); // $ExpectError + meanpn( x, { 'dims': {} } ); // $ExpectError + meanpn( x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpn(); // $ExpectError + meanpn( x, {}, {} ); // $ExpectError +} + +// Attached to the function is an `assign` method which returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpn.assign( x, x ); // $ExpectType float64ndarray + meanpn.assign( x, x, {} ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpn.assign( '5', x ); // $ExpectError + meanpn.assign( 5, x ); // $ExpectError + meanpn.assign( true, x ); // $ExpectError + meanpn.assign( false, x ); // $ExpectError + meanpn.assign( null, x ); // $ExpectError + meanpn.assign( void 0, x ); // $ExpectError + meanpn.assign( {}, x ); // $ExpectError + meanpn.assign( ( x: number ): number => x, x ); // $ExpectError + + meanpn.assign( '5', x, {} ); // $ExpectError + meanpn.assign( 5, x, {} ); // $ExpectError + meanpn.assign( true, x, {} ); // $ExpectError + meanpn.assign( false, x, {} ); // $ExpectError + meanpn.assign( null, x, {} ); // $ExpectError + meanpn.assign( void 0, x, {} ); // $ExpectError + meanpn.assign( {}, x, {} ); // $ExpectError + meanpn.assign( ( x: number ): number => x, x, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpn.assign( x, '5' ); // $ExpectError + meanpn.assign( x, 5 ); // $ExpectError + meanpn.assign( x, true ); // $ExpectError + meanpn.assign( x, false ); // $ExpectError + meanpn.assign( x, null ); // $ExpectError + meanpn.assign( x, void 0 ); // $ExpectError + meanpn.assign( x, ( x: number ): number => x ); // $ExpectError + + meanpn.assign( x, '5', {} ); // $ExpectError + meanpn.assign( x, 5, {} ); // $ExpectError + meanpn.assign( x, true, {} ); // $ExpectError + meanpn.assign( x, false, {} ); // $ExpectError + meanpn.assign( x, null, {} ); // $ExpectError + meanpn.assign( x, void 0, {} ); // $ExpectError + meanpn.assign( x, ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a third argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpn.assign( x, x, '5' ); // $ExpectError + meanpn.assign( x, x, true ); // $ExpectError + meanpn.assign( x, x, false ); // $ExpectError + meanpn.assign( x, x, null ); // $ExpectError + meanpn.assign( x, x, [] ); // $ExpectError + meanpn.assign( x, x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpn.assign( x, x, { 'dims': '5' } ); // $ExpectError + meanpn.assign( x, x, { 'dims': 5 } ); // $ExpectError + meanpn.assign( x, x, { 'dims': true } ); // $ExpectError + meanpn.assign( x, x, { 'dims': false } ); // $ExpectError + meanpn.assign( x, x, { 'dims': null } ); // $ExpectError + meanpn.assign( x, x, { 'dims': {} } ); // $ExpectError + meanpn.assign( x, x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpn.assign(); // $ExpectError + meanpn.assign( x ); // $ExpectError + meanpn.assign( x, x, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/meanpn/examples/index.js b/lib/node_modules/@stdlib/stats/meanpn/examples/index.js new file mode 100644 index 000000000000..14022fad8d0a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpn/examples/index.js @@ -0,0 +1,40 @@ +/** +* @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 uniform = require( '@stdlib/random/uniform' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var meanpn = require( './../lib' ); + +// Generate an array of random numbers: +var x = uniform( [ 5, 5 ], 0.0, 20.0 ); +console.log( ndarray2array( x ) ); + +// Perform a reduction: +var y = meanpn( x, { + 'dims': [ 0 ] +}); + +// Resolve the output array data type: +var dt = getDType( y ); +console.log( dt ); + +// Print the results: +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/stats/meanpn/lib/index.js b/lib/node_modules/@stdlib/stats/meanpn/lib/index.js new file mode 100644 index 000000000000..f50020a95f5a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpn/lib/index.js @@ -0,0 +1,63 @@ +/** +* @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 arithmetic mean along one or more ndarray dimensions using a two-pass error correction algorithm. +* +* @module @stdlib/stats/meanpn +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var meanpn = require( '@stdlib/stats/meanpn' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 0.0, 2.0, 3.0, 0.0, 0.0, 6.0, 7.0, 0.0, 0.0, 10.0, 11.0, 0.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform reduction: +* var out = meanpn( x ); +* // returns +* +* var v = out.get(); +* // returns 6.5 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/stats/meanpn/lib/main.js b/lib/node_modules/@stdlib/stats/meanpn/lib/main.js new file mode 100644 index 000000000000..fed38a27d8ad --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpn/lib/main.js @@ -0,0 +1,101 @@ +/** +* @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 dtypes = require( '@stdlib/ndarray/dtypes' ); +var gmeanpn = require( '@stdlib/stats/base/ndarray/meanpn' ); +var dmeanpn = require( '@stdlib/stats/base/ndarray/dmeanpn' ); +var smeanpn = require( '@stdlib/stats/base/ndarray/smeanpn' ); +var factory = require( '@stdlib/ndarray/base/unary-reduce-strided1d-dispatch-factory' ); + + +// VARIABLES // + +var idtypes = dtypes( 'real_and_generic' ); +var odtypes = dtypes( 'real_floating_point_and_generic' ); +var policies = { + 'output': 'real_floating_point_and_generic', + 'casting': 'none' +}; +var table = { + 'types': [ + 'float64', // input + 'float32' // input + ], + 'fcns': [ + dmeanpn, + smeanpn + ], + 'default': gmeanpn +}; + + +// MAIN // + +/** +* Computes the arithmetic mean along one or more ndarray dimensions using a two-pass error correction algorithm. +* +* @name meanpn +* @type {Function} +* @param {ndarray} x - input ndarray +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction +* @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions +* @param {string} [options.dtype] - output ndarray data type +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 0.0, 2.0, 3.0, 0.0, 0.0, 6.0, 7.0, 0.0, 0.0, 10.0, 11.0, 0.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform reduction: +* var out = meanpn( x ); +* // returns +* +* var v = out.get(); +* // returns 6.5 +*/ +var meanpn = factory( table, [ idtypes ], odtypes, policies ); + + +// EXPORTS // + +module.exports = meanpn; diff --git a/lib/node_modules/@stdlib/stats/meanpn/package.json b/lib/node_modules/@stdlib/stats/meanpn/package.json new file mode 100644 index 000000000000..d9eec3fe1a79 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpn/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/stats/meanpn", + "version": "0.0.0", + "description": "Compute the arithmetic mean along one or more ndarray dimensions using a two-pass error correction algorithm.", + "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", + "average", + "avg", + "mean", + "arithmetic mean", + "central tendency", + "extent", + "ndarray", + "two-pass", + "error correction", + "compensated summation", + "numerical stability" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/meanpn/test/test.assign.js b/lib/node_modules/@stdlib/stats/meanpn/test/test.assign.js new file mode 100644 index 000000000000..50897658072f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpn/test/test.assign.js @@ -0,0 +1,717 @@ +/** +* @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 ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var meanpn = require( './../lib' ).assign; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof meanpn, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( value, out ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( value, out, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( value, out ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (options)', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( value, out, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( x, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not an object', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( x, out, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [ 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (default)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 2, 2 ], + [ 2 ], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + meanpn( x, out ); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (all dimensions)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 2, 2 ], + [ 2 ], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + meanpn( x, out, { + 'dims': [ 0, 1 ] + }); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (some dimensions)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + meanpn( x, out, { + 'dims': [ 0 ] + }); + }; + } +}); + +tape( 'the function performs a reduction on an ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = meanpn( x, out ); + expected = 1.25; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = meanpn( x, out ); + expected = 1.25; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = meanpn( x, out, { + 'dims': [ 0, 1 ] + }); + expected = 1.25; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = meanpn( x, out, { + 'dims': [ 0, 1 ] + }); + expected = 1.25; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2, 2 ] + }); + + actual = meanpn( x, out, { + 'dims': [] + }); + expected = [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2, 2 ] + }); + + actual = meanpn( x, out, { + 'dims': [] + }); + expected = [ [ 1.0, -2.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = meanpn( x, out, { + 'dims': [ 0 ] + }); + expected = [ -0.5, 3.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = meanpn( x, out, { + 'dims': [ 1 ] + }); + expected = [ 1.5, 1.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = meanpn( x, out, { + 'dims': [ 0 ] + }); + expected = [ 1.5, 1.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = meanpn( x, out, { + 'dims': [ 1 ] + }); + expected = [ -0.5, 3.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/meanpn/test/test.js b/lib/node_modules/@stdlib/stats/meanpn/test/test.js new file mode 100644 index 000000000000..4db33999320c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpn/test/test.js @@ -0,0 +1,39 @@ +/** +* @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 isMethod = require( '@stdlib/assert/is-method' ); +var meanpn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof meanpn, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( meanpn, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/meanpn/test/test.main.js b/lib/node_modules/@stdlib/stats/meanpn/test/test.main.js new file mode 100644 index 000000000000..b4b4b7ed2a95 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpn/test/test.main.js @@ -0,0 +1,753 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var meanpn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof meanpn, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (options)', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dtype` option which is not a supported data type', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + 'foo', + 'bar', + 'beep', + 'boop' + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( x, { + 'dtype': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `keepdims` option which is not a boolean', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( x, { + 'keepdims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 0, 20 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpn( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function performs a reduction on an ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpn( x ); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpn( x ); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpn( x, { + 'dims': [ 0, 1 ] + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpn( x, { + 'dims': [ 0, 1 ], + 'keepdims': false + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpn( x, { + 'dims': [ 0, 1 ], + 'keepdims': true + }); + expected = [ [ 1.25 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpn( x, { + 'dims': [ 0, 1 ] + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpn( x, { + 'dims': [ 0, 1 ], + 'keepdims': false + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpn( x, { + 'dims': [ 0, 1 ], + 'keepdims': true + }); + expected = [ [ 1.25 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpn( x, { + 'dims': [], + 'keepdims': false + }); + expected = [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpn( x, { + 'dims': [], + 'keepdims': true + }); + expected = [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpn( x, { + 'dims': [], + 'keepdims': false + }); + expected = [ [ 1.0, -2.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpn( x, { + 'dims': [], + 'keepdims': true + }); + expected = [ [ 1.0, -2.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpn( x, { + 'dims': [ 0 ], + 'keepdims': false + }); + expected = [ -0.5, 3.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpn( x, { + 'dims': [ 0 ], + 'keepdims': true + }); + expected = [ [ -0.5, 3.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpn( x, { + 'dims': [ 1 ], + 'keepdims': false + }); + expected = [ 1.5, 1.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpn( x, { + 'dims': [ 1 ], + 'keepdims': true + }); + expected = [ [ 1.5 ], [ 1.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpn( x, { + 'dims': [ 0 ], + 'keepdims': false + }); + expected = [ 1.5, 1.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpn( x, { + 'dims': [ 0 ], + 'keepdims': true + }); + expected = [ [ 1.5, 1.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpn( x, { + 'dims': [ 1 ], + 'keepdims': false + }); + expected = [ -0.5, 3.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpn( x, { + 'dims': [ 1 ], + 'keepdims': true + }); + expected = [ [ -0.5 ], [ 3.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the output array data type', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpn( x, { + 'dtype': 'float64' + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpn( x, { + 'dtype': 'float64' + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/meanpw/README.md b/lib/node_modules/@stdlib/stats/meanpw/README.md new file mode 100644 index 000000000000..ba54dfc14459 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpw/README.md @@ -0,0 +1,298 @@ + + +# meanpw + +> Compute the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using pairwise summation. + +
+ +The [arithmetic mean][arithmetic-mean] is defined as + + + +```math +\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var meanpw = require( '@stdlib/stats/meanpw' ); +``` + +#### meanpw( x\[, options] ) + +Computes the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using pairwise summation. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); + +var y = meanpw( x ); +// returns + +var v = y.get(); +// returns 1.25 +``` + +The function has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **options**: function options (_optional_). + +The function accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. + +By default, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform a reduction over specific dimensions, provide a `dims` option. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ], { + 'shape': [ 2, 2 ], + 'order': 'row-major' +}); +var v = ndarray2array( x ); +// returns [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ] + +var y = meanpw( x, { + 'dims': [ 0 ] +}); +// returns + +v = ndarray2array( y ); +// returns [ -0.5, 3.0 ] + +y = meanpw( x, { + 'dims': [ 1 ] +}); +// returns + +v = ndarray2array( y ); +// returns [ 1.5, 1.0 ] + +y = meanpw( x, { + 'dims': [ 0, 1 ] +}); +// returns + +v = y.get(); +// returns 1.25 +``` + +By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ], { + 'shape': [ 2, 2 ], + 'order': 'row-major' +}); + +var v = ndarray2array( x ); +// returns [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ] + +var y = meanpw( x, { + 'dims': [ 0 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ -0.5, 3.0 ] ] + +y = meanpw( x, { + 'dims': [ 1 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 1.5 ], [ 1.0 ] ] + +y = meanpw( x, { + 'dims': [ 0, 1 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 1.25 ] ] +``` + +By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option. + +```javascript +var getDType = require( '@stdlib/ndarray/dtype' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ], { + 'dtype': 'generic' +}); + +var y = meanpw( x, { + 'dtype': 'float64' +}); +// returns + +var dt = String( getDType( y ) ); +// returns 'float64' +``` + +#### meanpw.assign( x, out\[, options] ) + +Computes the [arithmetic mean][arithmetic-mean] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions using pairwise summation and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); + +var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); +var y = zeros( [] ); + +var out = meanpw.assign( x, y ); +// returns + +var v = out.get(); +// returns 1.25 + +var bool = ( out === y ); +// returns true +``` + +The method has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or generic [data type][@stdlib/ndarray/dtypes]. +- **out**: output [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). + +The method accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. + +
+ + + +
+ +## Notes + +- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor]. +- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes]. +- In general, pairwise summation is more numerically stable than ordinary recursive summation (i.e., "simple" summation), with slightly worse performance. While not the most numerically stable summation technique (e.g., compensated summation techniques such as the Kahan–Babuška-Neumaier algorithm are generally more numerically stable), pairwise summation strikes a reasonable balance between numerical stability and performance. If either numerical stability or performance is more desirable for your use case, consider alternative summation techniques. + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var meanpw = require( '@stdlib/stats/meanpw' ); + +// Generate an array of random numbers: +var x = uniform( [ 5, 5 ], 0.0, 20.0 ); +console.log( ndarray2array( x ) ); + +// Perform a reduction: +var y = meanpw( x, { + 'dims': [ 0 ] +}); + +// Resolve the output array data type: +var dt = getDType( y ); +console.log( dt ); + +// Print the results: +console.log( ndarray2array( y ) ); +``` + +
+ + + +* * * + +
+ +## References + +- Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050][@higham:1993a]. + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/meanpw/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/stats/meanpw/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..d60d5698c993 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpw/benchmark/benchmark.assign.js @@ -0,0 +1,111 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var zeros = require( '@stdlib/array/zeros' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var meanpw = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out; + var x; + + x = uniform( len, -50.0, 50.0, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + out = new ndarray( options.dtype, zeros( 1, options.dtype ), [], [ 0 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = meanpw.assign( x, out ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + 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( pkg+':assign:dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/meanpw/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/meanpw/benchmark/benchmark.js new file mode 100644 index 000000000000..b7a62040bb97 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpw/benchmark/benchmark.js @@ -0,0 +1,105 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var meanpw = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -50.0, 50.0, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = meanpw( x ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + 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( pkg+':dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/meanpw/docs/repl.txt b/lib/node_modules/@stdlib/stats/meanpw/docs/repl.txt new file mode 100644 index 000000000000..3886a34865ad --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpw/docs/repl.txt @@ -0,0 +1,78 @@ + +{{alias}}( x[, options] ) + Computes the arithmetic mean along one or more ndarray dimensions using + pairwise summation. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or "generic" data type. + + options: Object (optional) + Function options. + + options.dtype: string (optional) + Output array data type. Must be a real-valued floating-point or + "generic" data type. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in a provided input + ndarray. + + options.keepdims: boolean (optional) + Boolean indicating whether the reduced dimensions should be included in + the returned ndarray as singleton dimensions. Default: false. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ -1.0, 2.0, -3.0, -4.0 ] ); + > var y = {{alias}}( x ); + > var v = y.get() + -1.5 + + +{{alias}}.assign( x, out[, options] ) + Computes the arithmetic mean along one or more ndarray dimensions using + pairwise summation and assigns results to a provided output ndarray. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or "generic" data type. + + out: ndarray + Output array. + + options: Object (optional) + Function options. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in a provided input + ndarray. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ -1.0, 2.0, -3.0, -4.0 ] ); + > var out = {{alias:@stdlib/ndarray/zeros}}( [] ); + > var y = {{alias}}.assign( x, out ) + + > var bool = ( out === y ) + true + > var v = out.get() + -1.5 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/meanpw/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/meanpw/docs/types/index.d.ts new file mode 100644 index 000000000000..fb6525c94d6c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpw/docs/types/index.d.ts @@ -0,0 +1,151 @@ +/* +* @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 { ArrayLike } from '@stdlib/types/array'; +import { RealFloatingPointAndGenericDataType as DataType, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Input array. +*/ +type InputArray = typedndarray; + +/** +* Output array. +*/ +type OutputArray = typedndarray; + +/** +* Interface defining "base" options. +*/ +interface BaseOptions { + /** + * List of dimensions over which to perform a reduction. + */ + dims?: ArrayLike; +} + +/** +* Interface defining options. +*/ +interface Options extends BaseOptions { + /** + * Output array data type. + */ + dtype?: DataType; + + /** + * Boolean indicating whether the reduced dimensions should be included in the returned array as singleton dimensions. Default: `false`. + */ + keepdims?: boolean; +} + +/** +* Interface for performing a reduction on an ndarray. +*/ +interface Unary { + /** + * Computes the arithmetic mean along one or more ndarray dimensions using pairwise summation. + * + * @param x - input ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * + * var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); + * + * var y = meanpw( x ); + * // returns + * + * var v = y.get(); + * // returns 1.25 + */ + ( x: InputArray, options?: Options ): OutputArray; // NOTE: we lose type specificity here, but retaining specificity would likely be difficult and/or tedious to completely enumerate, as the output ndarray data type is dependent on how `x` interacts with output data type policy and whether that policy has been overridden by `options.dtype`. + + /** + * Computes the arithmetic mean along one or more ndarray dimensions using pairwise summation and assigns results to a provided output ndarray. + * + * @param x - input ndarray + * @param out - output ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * var zeros = require( '@stdlib/ndarray/zeros' ); + * + * var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); + * var y = zeros( [] ); + * + * var out = meanpw.assign( x, y ); + * // returns + * + * var v = out.get(); + * // returns 1.25 + * + * var bool = ( out === y ); + * // returns true + */ + assign = OutputArray>( x: InputArray, out: U, options?: BaseOptions ): U; +} + +/** +* Computes the arithmetic mean along one or more ndarray dimensions using pairwise summation. +* +* @param x - input ndarray +* @param options - function options +* @returns output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); +* +* var y = meanpw( x ); +* // returns +* +* var v = y.get(); +* // returns 1.25 +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = array( [ 1.0, 2.0, -2.0, 4.0 ] ); +* var y = zeros( [] ); +* +* var out = meanpw.assign( x, y ); +* // returns +* +* var v = out.get(); +* // returns 1.25 +* +* var bool = ( out === y ); +* // returns true +*/ +declare const meanpw: Unary; + + +// EXPORTS // + +export = meanpw; diff --git a/lib/node_modules/@stdlib/stats/meanpw/docs/types/test.ts b/lib/node_modules/@stdlib/stats/meanpw/docs/types/test.ts new file mode 100644 index 000000000000..558dfb7a1b0c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpw/docs/types/test.ts @@ -0,0 +1,225 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +/// + +import zeros = require( '@stdlib/ndarray/zeros' ); +import meanpw = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpw( x ); // $ExpectType OutputArray + meanpw( x, {} ); // $ExpectType OutputArray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + meanpw( '5' ); // $ExpectError + meanpw( 5 ); // $ExpectError + meanpw( true ); // $ExpectError + meanpw( false ); // $ExpectError + meanpw( null ); // $ExpectError + meanpw( void 0 ); // $ExpectError + meanpw( {} ); // $ExpectError + meanpw( ( x: number ): number => x ); // $ExpectError + + meanpw( '5', {} ); // $ExpectError + meanpw( 5, {} ); // $ExpectError + meanpw( true, {} ); // $ExpectError + meanpw( false, {} ); // $ExpectError + meanpw( null, {} ); // $ExpectError + meanpw( void 0, {} ); // $ExpectError + meanpw( {}, {} ); // $ExpectError + meanpw( ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpw( x, '5' ); // $ExpectError + meanpw( x, true ); // $ExpectError + meanpw( x, false ); // $ExpectError + meanpw( x, null ); // $ExpectError + meanpw( x, [] ); // $ExpectError + meanpw( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dtype` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpw( x, { 'dtype': '5' } ); // $ExpectError + meanpw( x, { 'dtype': 5 } ); // $ExpectError + meanpw( x, { 'dtype': true } ); // $ExpectError + meanpw( x, { 'dtype': false } ); // $ExpectError + meanpw( x, { 'dtype': null } ); // $ExpectError + meanpw( x, { 'dtype': [] } ); // $ExpectError + meanpw( x, { 'dtype': {} } ); // $ExpectError + meanpw( x, { 'dtype': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `keepdims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpw( x, { 'keepdims': '5' } ); // $ExpectError + meanpw( x, { 'keepdims': 5 } ); // $ExpectError + meanpw( x, { 'keepdims': null } ); // $ExpectError + meanpw( x, { 'keepdims': [] } ); // $ExpectError + meanpw( x, { 'keepdims': {} } ); // $ExpectError + meanpw( x, { 'keepdims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpw( x, { 'dims': '5' } ); // $ExpectError + meanpw( x, { 'dims': 5 } ); // $ExpectError + meanpw( x, { 'dims': true } ); // $ExpectError + meanpw( x, { 'dims': false } ); // $ExpectError + meanpw( x, { 'dims': null } ); // $ExpectError + meanpw( x, { 'dims': {} } ); // $ExpectError + meanpw( x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpw(); // $ExpectError + meanpw( x, {}, {} ); // $ExpectError +} + +// Attached to the function is an `assign` method which returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpw.assign( x, x ); // $ExpectType float64ndarray + meanpw.assign( x, x, {} ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpw.assign( '5', x ); // $ExpectError + meanpw.assign( 5, x ); // $ExpectError + meanpw.assign( true, x ); // $ExpectError + meanpw.assign( false, x ); // $ExpectError + meanpw.assign( null, x ); // $ExpectError + meanpw.assign( void 0, x ); // $ExpectError + meanpw.assign( {}, x ); // $ExpectError + meanpw.assign( ( x: number ): number => x, x ); // $ExpectError + + meanpw.assign( '5', x, {} ); // $ExpectError + meanpw.assign( 5, x, {} ); // $ExpectError + meanpw.assign( true, x, {} ); // $ExpectError + meanpw.assign( false, x, {} ); // $ExpectError + meanpw.assign( null, x, {} ); // $ExpectError + meanpw.assign( void 0, x, {} ); // $ExpectError + meanpw.assign( {}, x, {} ); // $ExpectError + meanpw.assign( ( x: number ): number => x, x, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpw.assign( x, '5' ); // $ExpectError + meanpw.assign( x, 5 ); // $ExpectError + meanpw.assign( x, true ); // $ExpectError + meanpw.assign( x, false ); // $ExpectError + meanpw.assign( x, null ); // $ExpectError + meanpw.assign( x, void 0 ); // $ExpectError + meanpw.assign( x, ( x: number ): number => x ); // $ExpectError + + meanpw.assign( x, '5', {} ); // $ExpectError + meanpw.assign( x, 5, {} ); // $ExpectError + meanpw.assign( x, true, {} ); // $ExpectError + meanpw.assign( x, false, {} ); // $ExpectError + meanpw.assign( x, null, {} ); // $ExpectError + meanpw.assign( x, void 0, {} ); // $ExpectError + meanpw.assign( x, ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a third argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpw.assign( x, x, '5' ); // $ExpectError + meanpw.assign( x, x, true ); // $ExpectError + meanpw.assign( x, x, false ); // $ExpectError + meanpw.assign( x, x, null ); // $ExpectError + meanpw.assign( x, x, [] ); // $ExpectError + meanpw.assign( x, x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpw.assign( x, x, { 'dims': '5' } ); // $ExpectError + meanpw.assign( x, x, { 'dims': 5 } ); // $ExpectError + meanpw.assign( x, x, { 'dims': true } ); // $ExpectError + meanpw.assign( x, x, { 'dims': false } ); // $ExpectError + meanpw.assign( x, x, { 'dims': null } ); // $ExpectError + meanpw.assign( x, x, { 'dims': {} } ); // $ExpectError + meanpw.assign( x, x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + meanpw.assign(); // $ExpectError + meanpw.assign( x ); // $ExpectError + meanpw.assign( x, x, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/meanpw/examples/index.js b/lib/node_modules/@stdlib/stats/meanpw/examples/index.js new file mode 100644 index 000000000000..352961b9b183 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpw/examples/index.js @@ -0,0 +1,40 @@ +/** +* @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 uniform = require( '@stdlib/random/uniform' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var meanpw = require( './../lib' ); + +// Generate an array of random numbers: +var x = uniform( [ 5, 5 ], 0.0, 20.0 ); +console.log( ndarray2array( x ) ); + +// Perform a reduction: +var y = meanpw( x, { + 'dims': [ 0 ] +}); + +// Resolve the output array data type: +var dt = getDType( y ); +console.log( dt ); + +// Print the results: +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/stats/meanpw/lib/index.js b/lib/node_modules/@stdlib/stats/meanpw/lib/index.js new file mode 100644 index 000000000000..d4d674ee9d75 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpw/lib/index.js @@ -0,0 +1,63 @@ +/** +* @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 arithmetic mean along one or more ndarray dimensions using pairwise summation. +* +* @module @stdlib/stats/meanpw +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var meanpw = require( '@stdlib/stats/meanpw' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 0.0, 2.0, 3.0, 0.0, 0.0, 6.0, 7.0, 0.0, 0.0, 10.0, 11.0, 0.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform reduction: +* var out = meanpw( x ); +* // returns +* +* var v = out.get(); +* // returns 6.5 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/stats/meanpw/lib/main.js b/lib/node_modules/@stdlib/stats/meanpw/lib/main.js new file mode 100644 index 000000000000..0743b1300d6d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpw/lib/main.js @@ -0,0 +1,101 @@ +/** +* @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 dtypes = require( '@stdlib/ndarray/dtypes' ); +var gmeanpw = require( '@stdlib/stats/base/ndarray/meanpw' ); +var dmeanpw = require( '@stdlib/stats/base/ndarray/dmeanpw' ); +var smeanpw = require( '@stdlib/stats/base/ndarray/smeanpw' ); +var factory = require( '@stdlib/ndarray/base/unary-reduce-strided1d-dispatch-factory' ); + + +// VARIABLES // + +var idtypes = dtypes( 'real_and_generic' ); +var odtypes = dtypes( 'real_floating_point_and_generic' ); +var policies = { + 'output': 'real_floating_point_and_generic', + 'casting': 'none' +}; +var table = { + 'types': [ + 'float64', // input + 'float32' // input + ], + 'fcns': [ + dmeanpw, + smeanpw + ], + 'default': gmeanpw +}; + + +// MAIN // + +/** +* Computes the arithmetic mean along one or more ndarray dimensions using pairwise summation. +* +* @name meanpw +* @type {Function} +* @param {ndarray} x - input ndarray +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction +* @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions +* @param {string} [options.dtype] - output ndarray data type +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 0.0, 2.0, 3.0, 0.0, 0.0, 6.0, 7.0, 0.0, 0.0, 10.0, 11.0, 0.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform reduction: +* var out = meanpw( x ); +* // returns +* +* var v = out.get(); +* // returns 6.5 +*/ +var meanpw = factory( table, [ idtypes ], odtypes, policies ); + + +// EXPORTS // + +module.exports = meanpw; diff --git a/lib/node_modules/@stdlib/stats/meanpw/package.json b/lib/node_modules/@stdlib/stats/meanpw/package.json new file mode 100644 index 000000000000..703d9bc73952 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpw/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/stats/meanpw", + "version": "0.0.0", + "description": "Compute the arithmetic mean along one or more ndarray dimensions using pairwise summation.", + "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", + "average", + "avg", + "mean", + "arithmetic mean", + "central tendency", + "extent", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/meanpw/test/test.assign.js b/lib/node_modules/@stdlib/stats/meanpw/test/test.assign.js new file mode 100644 index 000000000000..d5488e64ac56 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpw/test/test.assign.js @@ -0,0 +1,717 @@ +/** +* @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 ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var meanpw = require( './../lib' ).assign; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof meanpw, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( value, out ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( value, out, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( value, out ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (options)', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( value, out, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( x, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not an object', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( x, out, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [ 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (default)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 2, 2 ], + [ 2 ], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + meanpw( x, out ); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (all dimensions)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 2, 2 ], + [ 2 ], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + meanpw( x, out, { + 'dims': [ 0, 1 ] + }); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (some dimensions)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + meanpw( x, out, { + 'dims': [ 0 ] + }); + }; + } +}); + +tape( 'the function performs a reduction on an ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = meanpw( x, out ); + expected = 1.25; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = meanpw( x, out ); + expected = 1.25; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = meanpw( x, out, { + 'dims': [ 0, 1 ] + }); + expected = 1.25; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = meanpw( x, out, { + 'dims': [ 0, 1 ] + }); + expected = 1.25; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2, 2 ] + }); + + actual = meanpw( x, out, { + 'dims': [] + }); + expected = [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2, 2 ] + }); + + actual = meanpw( x, out, { + 'dims': [] + }); + expected = [ [ 1.0, -2.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = meanpw( x, out, { + 'dims': [ 0 ] + }); + expected = [ -0.5, 3.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = meanpw( x, out, { + 'dims': [ 1 ] + }); + expected = [ 1.5, 1.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = meanpw( x, out, { + 'dims': [ 0 ] + }); + expected = [ 1.5, 1.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = meanpw( x, out, { + 'dims': [ 1 ] + }); + expected = [ -0.5, 3.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/meanpw/test/test.js b/lib/node_modules/@stdlib/stats/meanpw/test/test.js new file mode 100644 index 000000000000..77182b89ef17 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpw/test/test.js @@ -0,0 +1,39 @@ +/** +* @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 isMethod = require( '@stdlib/assert/is-method' ); +var meanpw = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof meanpw, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( meanpw, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/meanpw/test/test.main.js b/lib/node_modules/@stdlib/stats/meanpw/test/test.main.js new file mode 100644 index 000000000000..4ad825b1aa46 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/meanpw/test/test.main.js @@ -0,0 +1,753 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var meanpw = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof meanpw, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (options)', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dtype` option which is not a supported data type', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + 'foo', + 'bar', + 'beep', + 'boop' + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( x, { + 'dtype': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `keepdims` option which is not a boolean', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( x, { + 'keepdims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 0, 20 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + meanpw( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function performs a reduction on an ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpw( x ); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpw( x ); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpw( x, { + 'dims': [ 0, 1 ] + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpw( x, { + 'dims': [ 0, 1 ], + 'keepdims': false + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpw( x, { + 'dims': [ 0, 1 ], + 'keepdims': true + }); + expected = [ [ 1.25 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpw( x, { + 'dims': [ 0, 1 ] + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpw( x, { + 'dims': [ 0, 1 ], + 'keepdims': false + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpw( x, { + 'dims': [ 0, 1 ], + 'keepdims': true + }); + expected = [ [ 1.25 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpw( x, { + 'dims': [], + 'keepdims': false + }); + expected = [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpw( x, { + 'dims': [], + 'keepdims': true + }); + expected = [ [ 1.0, 2.0 ], [ -2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpw( x, { + 'dims': [], + 'keepdims': false + }); + expected = [ [ 1.0, -2.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpw( x, { + 'dims': [], + 'keepdims': true + }); + expected = [ [ 1.0, -2.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpw( x, { + 'dims': [ 0 ], + 'keepdims': false + }); + expected = [ -0.5, 3.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpw( x, { + 'dims': [ 0 ], + 'keepdims': true + }); + expected = [ [ -0.5, 3.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpw( x, { + 'dims': [ 1 ], + 'keepdims': false + }); + expected = [ 1.5, 1.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpw( x, { + 'dims': [ 1 ], + 'keepdims': true + }); + expected = [ [ 1.5 ], [ 1.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpw( x, { + 'dims': [ 0 ], + 'keepdims': false + }); + expected = [ 1.5, 1.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpw( x, { + 'dims': [ 0 ], + 'keepdims': true + }); + expected = [ [ 1.5, 1.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpw( x, { + 'dims': [ 1 ], + 'keepdims': false + }); + expected = [ -0.5, 3.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpw( x, { + 'dims': [ 1 ], + 'keepdims': true + }); + expected = [ [ -0.5 ], [ 3.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the output array data type', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = meanpw( x, { + 'dtype': 'float64' + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, -2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = meanpw( x, { + 'dtype': 'float64' + }); + expected = 1.25; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/mediansorted/README.md b/lib/node_modules/@stdlib/stats/mediansorted/README.md new file mode 100644 index 000000000000..eaf694e02f65 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/mediansorted/README.md @@ -0,0 +1,260 @@ + + +# mediansorted + +> Compute the median value along one or more sorted [ndarray][@stdlib/ndarray/ctor] dimensions. + +
+ +## Usage + +```javascript +var mediansorted = require( '@stdlib/stats/mediansorted' ); +``` + +#### mediansorted( x\[, options] ) + +Computes the median value along one or more sorted [ndarray][@stdlib/ndarray/ctor] dimensions. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0 ] ); + +var y = mediansorted( x ); +// returns + +var v = y.get(); +// returns 2.0 +``` + +The function has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **options**: function options (_optional_). + +The function accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. + +By default, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform a reduction over specific dimensions, provide a `dims` option. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0, 4.0 ], { + 'shape': [ 2, 2 ], + 'order': 'row-major' +}); +var v = ndarray2array( x ); +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] + +var y = mediansorted( x, { + 'dims': [ 0 ] +}); +// returns + +v = ndarray2array( y ); +// returns [ 2.0, 3.0 ] + +y = mediansorted( x, { + 'dims': [ 1 ] +}); +// returns + +v = ndarray2array( y ); +// returns [ 1.5, 3.5 ] + +y = mediansorted( x, { + 'dims': [ 0, 1 ] +}); +// returns + +v = y.get(); +// returns 2.5 +``` + +By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0, 4.0 ], { + 'shape': [ 2, 2 ], + 'order': 'row-major' +}); + +var v = ndarray2array( x ); +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] + +var y = mediansorted( x, { + 'dims': [ 0 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 2.0, 3.0 ] ] + +y = mediansorted( x, { + 'dims': [ 1 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 1.5 ], [ 3.5 ] ] + +y = mediansorted( x, { + 'dims': [ 0, 1 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 2.5 ] ] +``` + +By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option. + +```javascript +var getDType = require( '@stdlib/ndarray/dtype' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0 ], { + 'dtype': 'generic' +}); + +var y = mediansorted( x, { + 'dtype': 'float64' +}); +// returns + +var dt = String( getDType( y ) ); +// returns 'float64' +``` + +#### mediansorted.assign( x, out\[, options] ) + +Computes the median value along one or more sorted [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); + +var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); +var y = zeros( [] ); + +var out = mediansorted.assign( x, y ); +// returns + +var v = out.get(); +// returns 2.5 + +var bool = ( out === y ); +// returns true +``` + +The method has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or generic [data type][@stdlib/ndarray/dtypes]. +- **out**: output [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). + +The method accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. + +
+ + + +
+ +## Notes + +- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor]. +- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having the same [data type][@stdlib/ndarray/dtypes] as the input [ndarray][@stdlib/ndarray/ctor]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes]. + +
+ + + +
+ +## Examples + + + +```javascript +var linspace = require( '@stdlib/blas/ext/linspace' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var mediansorted = require( '@stdlib/stats/mediansorted' ); + +// Generate a sorted ndarray: +var x = linspace( [ 5, 5 ], 0.0, 20.0 ); +console.log( ndarray2array( x ) ); + +// Perform a reduction: +var y = mediansorted( x, { + 'dims': [ 0 ] +}); + +// Resolve the output array data type: +var dt = getDType( y ); +console.log( dt ); + +// Print the results: +console.log( ndarray2array( y ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/mediansorted/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/stats/mediansorted/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..6c8b9e32625c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/mediansorted/benchmark/benchmark.assign.js @@ -0,0 +1,111 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var linspace = require( '@stdlib/array/linspace' ); +var zeros = require( '@stdlib/array/zeros' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var mediansorted = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out; + var x; + + x = linspace( 0.0, 100.0, len, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + out = new ndarray( options.dtype, zeros( 1, options.dtype ), [], [ 0 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = mediansorted.assign( x, out ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + 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( pkg+':assign:dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/mediansorted/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/mediansorted/benchmark/benchmark.js new file mode 100644 index 000000000000..ba6f6328a4e6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/mediansorted/benchmark/benchmark.js @@ -0,0 +1,105 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var linspace = require( '@stdlib/array/linspace' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var mediansorted = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = linspace( 0.0, 100.0, len, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = mediansorted( x ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + 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( pkg+':dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/mediansorted/docs/repl.txt b/lib/node_modules/@stdlib/stats/mediansorted/docs/repl.txt new file mode 100644 index 000000000000..73eb71686276 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/mediansorted/docs/repl.txt @@ -0,0 +1,76 @@ + +{{alias}}( x[, options] ) + Computes the median value along one or more sorted ndarray dimensions. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or "generic" data type. + + options: Object (optional) + Function options. + + options.dtype: string (optional) + Output array data type. Must be a real-valued or "generic" data type. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in a provided input + ndarray. + + options.keepdims: boolean (optional) + Boolean indicating whether the reduced dimensions should be included in + the returned ndarray as singleton dimensions. Default: false. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ 1.0, 2.0, 3.0 ] ); + > var y = {{alias}}( x ); + > var v = y.get() + 2.0 + + +{{alias}}.assign( x, out[, options] ) + Computes the median value along one or more sorted ndarray dimensions and + assigns results to a provided output ndarray. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or "generic" data type. + + out: ndarray + Output array. + + options: Object (optional) + Function options. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in a provided input + ndarray. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var out = {{alias:@stdlib/ndarray/zeros}}( [] ); + > var y = {{alias}}.assign( x, out ) + + > var bool = ( out === y ) + true + > var v = out.get() + 2.5 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/mediansorted/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/mediansorted/docs/types/index.d.ts new file mode 100644 index 000000000000..8ef2044a3e46 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/mediansorted/docs/types/index.d.ts @@ -0,0 +1,151 @@ +/* +* @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 { ArrayLike } from '@stdlib/types/array'; +import { RealAndGenericDataType as DataType, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Input array. +*/ +type InputArray = typedndarray; + +/** +* Output array. +*/ +type OutputArray = typedndarray; + +/** +* Interface defining "base" options. +*/ +interface BaseOptions { + /** + * List of dimensions over which to perform a reduction. + */ + dims?: ArrayLike; +} + +/** +* Interface defining options. +*/ +interface Options extends BaseOptions { + /** + * Output array data type. + */ + dtype?: DataType; + + /** + * Boolean indicating whether the reduced dimensions should be included in the returned array as singleton dimensions. Default: `false`. + */ + keepdims?: boolean; +} + +/** +* Interface for performing a reduction on an ndarray. +*/ +interface Unary { + /** + * Computes the median value along one or more sorted ndarray dimensions. + * + * @param x - input ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * + * var x = array( [ 1.0, 2.0, 3.0 ] ); + * + * var y = mediansorted( x ); + * // returns + * + * var v = y.get(); + * // returns 2.0 + */ + ( x: InputArray, options?: Options ): OutputArray; // NOTE: we lose type specificity here, but retaining specificity would likely be difficult and/or tedious to completely enumerate, as the output ndarray data type is dependent on how `x` interacts with output data type policy and whether that policy has been overridden by `options.dtype`. + + /** + * Computes the median value along one or more sorted ndarray dimensions and assigns results to a provided output ndarray. + * + * @param x - input ndarray + * @param out - output ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * var zeros = require( '@stdlib/ndarray/zeros' ); + * + * var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); + * var y = zeros( [] ); + * + * var out = mediansorted.assign( x, y ); + * // returns + * + * var v = out.get(); + * // returns 2.5 + * + * var bool = ( out === y ); + * // returns true + */ + assign = OutputArray>( x: InputArray, out: U, options?: BaseOptions ): U; +} + +/** +* Computes the median value along one or more sorted ndarray dimensions. +* +* @param x - input ndarray +* @param options - function options +* @returns output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ 1.0, 2.0, 3.0 ] ); +* +* var y = mediansorted( x ); +* // returns +* +* var v = y.get(); +* // returns 2.0 +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* var y = zeros( [] ); +* +* var out = mediansorted.assign( x, y ); +* // returns +* +* var v = out.get(); +* // returns 2.5 +* +* var bool = ( out === y ); +* // returns true +*/ +declare const mediansorted: Unary; + + +// EXPORTS // + +export = mediansorted; diff --git a/lib/node_modules/@stdlib/stats/mediansorted/docs/types/test.ts b/lib/node_modules/@stdlib/stats/mediansorted/docs/types/test.ts new file mode 100644 index 000000000000..08606c52aa6b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/mediansorted/docs/types/test.ts @@ -0,0 +1,225 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +/// + +import zeros = require( '@stdlib/ndarray/zeros' ); +import mediansorted = require( '@stdlib/stats/mediansorted' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + mediansorted( x ); // $ExpectType OutputArray + mediansorted( x, {} ); // $ExpectType OutputArray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + mediansorted( '5' ); // $ExpectError + mediansorted( 5 ); // $ExpectError + mediansorted( true ); // $ExpectError + mediansorted( false ); // $ExpectError + mediansorted( null ); // $ExpectError + mediansorted( void 0 ); // $ExpectError + mediansorted( {} ); // $ExpectError + mediansorted( ( x: number ): number => x ); // $ExpectError + + mediansorted( '5', {} ); // $ExpectError + mediansorted( 5, {} ); // $ExpectError + mediansorted( true, {} ); // $ExpectError + mediansorted( false, {} ); // $ExpectError + mediansorted( null, {} ); // $ExpectError + mediansorted( void 0, {} ); // $ExpectError + mediansorted( {}, {} ); // $ExpectError + mediansorted( ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + mediansorted( x, '5' ); // $ExpectError + mediansorted( x, true ); // $ExpectError + mediansorted( x, false ); // $ExpectError + mediansorted( x, null ); // $ExpectError + mediansorted( x, [] ); // $ExpectError + mediansorted( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dtype` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + mediansorted( x, { 'dtype': '5' } ); // $ExpectError + mediansorted( x, { 'dtype': 5 } ); // $ExpectError + mediansorted( x, { 'dtype': true } ); // $ExpectError + mediansorted( x, { 'dtype': false } ); // $ExpectError + mediansorted( x, { 'dtype': null } ); // $ExpectError + mediansorted( x, { 'dtype': [] } ); // $ExpectError + mediansorted( x, { 'dtype': {} } ); // $ExpectError + mediansorted( x, { 'dtype': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `keepdims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + mediansorted( x, { 'keepdims': '5' } ); // $ExpectError + mediansorted( x, { 'keepdims': 5 } ); // $ExpectError + mediansorted( x, { 'keepdims': null } ); // $ExpectError + mediansorted( x, { 'keepdims': [] } ); // $ExpectError + mediansorted( x, { 'keepdims': {} } ); // $ExpectError + mediansorted( x, { 'keepdims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + mediansorted( x, { 'dims': '5' } ); // $ExpectError + mediansorted( x, { 'dims': 5 } ); // $ExpectError + mediansorted( x, { 'dims': true } ); // $ExpectError + mediansorted( x, { 'dims': false } ); // $ExpectError + mediansorted( x, { 'dims': null } ); // $ExpectError + mediansorted( x, { 'dims': {} } ); // $ExpectError + mediansorted( x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + mediansorted(); // $ExpectError + mediansorted( x, {}, {} ); // $ExpectError +} + +// Attached to the function is an `assign` method which returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + mediansorted.assign( x, x ); // $ExpectType float64ndarray + mediansorted.assign( x, x, {} ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + mediansorted.assign( '5', x ); // $ExpectError + mediansorted.assign( 5, x ); // $ExpectError + mediansorted.assign( true, x ); // $ExpectError + mediansorted.assign( false, x ); // $ExpectError + mediansorted.assign( null, x ); // $ExpectError + mediansorted.assign( void 0, x ); // $ExpectError + mediansorted.assign( {}, x ); // $ExpectError + mediansorted.assign( ( x: number ): number => x, x ); // $ExpectError + + mediansorted.assign( '5', x, {} ); // $ExpectError + mediansorted.assign( 5, x, {} ); // $ExpectError + mediansorted.assign( true, x, {} ); // $ExpectError + mediansorted.assign( false, x, {} ); // $ExpectError + mediansorted.assign( null, x, {} ); // $ExpectError + mediansorted.assign( void 0, x, {} ); // $ExpectError + mediansorted.assign( {}, x, {} ); // $ExpectError + mediansorted.assign( ( x: number ): number => x, x, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + mediansorted.assign( x, '5' ); // $ExpectError + mediansorted.assign( x, 5 ); // $ExpectError + mediansorted.assign( x, true ); // $ExpectError + mediansorted.assign( x, false ); // $ExpectError + mediansorted.assign( x, null ); // $ExpectError + mediansorted.assign( x, void 0 ); // $ExpectError + mediansorted.assign( x, ( x: number ): number => x ); // $ExpectError + + mediansorted.assign( x, '5', {} ); // $ExpectError + mediansorted.assign( x, 5, {} ); // $ExpectError + mediansorted.assign( x, true, {} ); // $ExpectError + mediansorted.assign( x, false, {} ); // $ExpectError + mediansorted.assign( x, null, {} ); // $ExpectError + mediansorted.assign( x, void 0, {} ); // $ExpectError + mediansorted.assign( x, ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a third argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + mediansorted.assign( x, x, '5' ); // $ExpectError + mediansorted.assign( x, x, true ); // $ExpectError + mediansorted.assign( x, x, false ); // $ExpectError + mediansorted.assign( x, x, null ); // $ExpectError + mediansorted.assign( x, x, [] ); // $ExpectError + mediansorted.assign( x, x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + mediansorted.assign( x, x, { 'dims': '5' } ); // $ExpectError + mediansorted.assign( x, x, { 'dims': 5 } ); // $ExpectError + mediansorted.assign( x, x, { 'dims': true } ); // $ExpectError + mediansorted.assign( x, x, { 'dims': false } ); // $ExpectError + mediansorted.assign( x, x, { 'dims': null } ); // $ExpectError + mediansorted.assign( x, x, { 'dims': {} } ); // $ExpectError + mediansorted.assign( x, x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + mediansorted.assign(); // $ExpectError + mediansorted.assign( x ); // $ExpectError + mediansorted.assign( x, x, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/mediansorted/examples/index.js b/lib/node_modules/@stdlib/stats/mediansorted/examples/index.js new file mode 100644 index 000000000000..ee6aea3ff9f1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/mediansorted/examples/index.js @@ -0,0 +1,40 @@ +/** +* @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 linspace = require( '@stdlib/blas/ext/linspace' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var mediansorted = require( './../lib' ); + +// Generate a sorted ndarray: +var x = linspace( [ 5, 5 ], 0.0, 20.0 ); +console.log( ndarray2array( x ) ); + +// Perform a reduction: +var y = mediansorted( x, { + 'dims': [ 0 ] +}); + +// Resolve the output array data type: +var dt = getDType( y ); +console.log( dt ); + +// Print the results: +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/stats/mediansorted/lib/index.js b/lib/node_modules/@stdlib/stats/mediansorted/lib/index.js new file mode 100644 index 000000000000..428a7b39dbb1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/mediansorted/lib/index.js @@ -0,0 +1,63 @@ +/** +* @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 median value along one or more sorted ndarray dimensions. +* +* @module @stdlib/stats/mediansorted +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var mediansorted = require( '@stdlib/stats/mediansorted' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 0.0, 1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0, 5.0, 6.0, 0.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform reduction: +* var out = mediansorted( x ); +* // returns +* +* var v = out.get(); +* // returns 3.5 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/stats/mediansorted/lib/main.js b/lib/node_modules/@stdlib/stats/mediansorted/lib/main.js new file mode 100644 index 000000000000..998686931675 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/mediansorted/lib/main.js @@ -0,0 +1,101 @@ +/** +* @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 dtypes = require( '@stdlib/ndarray/dtypes' ); +var gmediansorted = require( '@stdlib/stats/base/ndarray/mediansorted' ); +var dmediansorted = require( '@stdlib/stats/base/ndarray/dmediansorted' ); +var smediansorted = require( '@stdlib/stats/base/ndarray/smediansorted' ); +var factory = require( '@stdlib/ndarray/base/unary-reduce-strided1d-dispatch-factory' ); + + +// VARIABLES // + +var idtypes = dtypes( 'real_and_generic' ); +var odtypes = dtypes( 'real_and_generic' ); +var policies = { + 'output': 'same', + 'casting': 'none' +}; +var table = { + 'types': [ + 'float64', // input + 'float32' // input + ], + 'fcns': [ + dmediansorted, + smediansorted + ], + 'default': gmediansorted +}; + + +// MAIN // + +/** +* Computes the median value along one or more sorted ndarray dimensions. +* +* @name mediansorted +* @type {Function} +* @param {ndarray} x - input ndarray +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction +* @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions +* @param {string} [options.dtype] - output ndarray data type +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 0.0, 1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0, 5.0, 6.0, 0.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform reduction: +* var out = mediansorted( x ); +* // returns +* +* var v = out.get(); +* // returns 3.5 +*/ +var mediansorted = factory( table, [ idtypes ], odtypes, policies ); + + +// EXPORTS // + +module.exports = mediansorted; diff --git a/lib/node_modules/@stdlib/stats/mediansorted/package.json b/lib/node_modules/@stdlib/stats/mediansorted/package.json new file mode 100644 index 000000000000..c4d116199980 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/mediansorted/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/stats/mediansorted", + "version": "0.0.0", + "description": "Compute the median value along one or more sorted ndarray dimensions.", + "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", + "median", + "middle", + "central tendency", + "extent", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/mediansorted/test/test.assign.js b/lib/node_modules/@stdlib/stats/mediansorted/test/test.assign.js new file mode 100644 index 000000000000..efe926640f25 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/mediansorted/test/test.assign.js @@ -0,0 +1,717 @@ +/** +* @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 ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var mediansorted = require( './../lib' ).assign; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof mediansorted, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( value, out ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( value, out, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( value, out ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (options)', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( value, out, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( x, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not an object', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( x, out, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [ 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (default)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 2, 2 ], + [ 2 ], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + mediansorted( x, out ); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (all dimensions)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 2, 2 ], + [ 2 ], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + mediansorted( x, out, { + 'dims': [ 0, 1 ] + }); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (some dimensions)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + mediansorted( x, out, { + 'dims': [ 0 ] + }); + }; + } +}); + +tape( 'the function performs a reduction on an ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = mediansorted( x, out ); + expected = 2.5; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = mediansorted( x, out ); + expected = 2.5; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = mediansorted( x, out, { + 'dims': [ 0, 1 ] + }); + expected = 2.5; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = mediansorted( x, out, { + 'dims': [ 0, 1 ] + }); + expected = 2.5; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2, 2 ] + }); + + actual = mediansorted( x, out, { + 'dims': [] + }); + expected = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2, 2 ] + }); + + actual = mediansorted( x, out, { + 'dims': [] + }); + expected = [ [ 1.0, 3.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = mediansorted( x, out, { + 'dims': [ 0 ] + }); + expected = [ 2.0, 3.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = mediansorted( x, out, { + 'dims': [ 1 ] + }); + expected = [ 1.5, 3.5 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = mediansorted( x, out, { + 'dims': [ 0 ] + }); + expected = [ 1.5, 3.5 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = mediansorted( x, out, { + 'dims': [ 1 ] + }); + expected = [ 2.0, 3.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/mediansorted/test/test.js b/lib/node_modules/@stdlib/stats/mediansorted/test/test.js new file mode 100644 index 000000000000..c0f9c6f80444 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/mediansorted/test/test.js @@ -0,0 +1,39 @@ +/** +* @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 isMethod = require( '@stdlib/assert/is-method' ); +var mediansorted = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof mediansorted, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( mediansorted, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/mediansorted/test/test.main.js b/lib/node_modules/@stdlib/stats/mediansorted/test/test.main.js new file mode 100644 index 000000000000..d5d74d870cc4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/mediansorted/test/test.main.js @@ -0,0 +1,753 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var mediansorted = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof mediansorted, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (options)', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dtype` option which is not a supported data type', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + 'foo', + 'bar', + 'beep', + 'boop' + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( x, { + 'dtype': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `keepdims` option which is not a boolean', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( x, { + 'keepdims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 0, 20 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mediansorted( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function performs a reduction on an ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = mediansorted( x ); + expected = 2.5; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = mediansorted( x ); + expected = 2.5; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = mediansorted( x, { + 'dims': [ 0, 1 ] + }); + expected = 2.5; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = mediansorted( x, { + 'dims': [ 0, 1 ], + 'keepdims': false + }); + expected = 2.5; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = mediansorted( x, { + 'dims': [ 0, 1 ], + 'keepdims': true + }); + expected = [ [ 2.5 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = mediansorted( x, { + 'dims': [ 0, 1 ] + }); + expected = 2.5; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = mediansorted( x, { + 'dims': [ 0, 1 ], + 'keepdims': false + }); + expected = 2.5; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = mediansorted( x, { + 'dims': [ 0, 1 ], + 'keepdims': true + }); + expected = [ [ 2.5 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = mediansorted( x, { + 'dims': [], + 'keepdims': false + }); + expected = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = mediansorted( x, { + 'dims': [], + 'keepdims': true + }); + expected = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = mediansorted( x, { + 'dims': [], + 'keepdims': false + }); + expected = [ [ 1.0, 3.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = mediansorted( x, { + 'dims': [], + 'keepdims': true + }); + expected = [ [ 1.0, 3.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = mediansorted( x, { + 'dims': [ 0 ], + 'keepdims': false + }); + expected = [ 2.0, 3.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = mediansorted( x, { + 'dims': [ 0 ], + 'keepdims': true + }); + expected = [ [ 2.0, 3.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = mediansorted( x, { + 'dims': [ 1 ], + 'keepdims': false + }); + expected = [ 1.5, 3.5 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = mediansorted( x, { + 'dims': [ 1 ], + 'keepdims': true + }); + expected = [ [ 1.5 ], [ 3.5 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = mediansorted( x, { + 'dims': [ 0 ], + 'keepdims': false + }); + expected = [ 1.5, 3.5 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = mediansorted( x, { + 'dims': [ 0 ], + 'keepdims': true + }); + expected = [ [ 1.5, 3.5 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = mediansorted( x, { + 'dims': [ 1 ], + 'keepdims': false + }); + expected = [ 2.0, 3.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = mediansorted( x, { + 'dims': [ 1 ], + 'keepdims': true + }); + expected = [ [ 2.0 ], [ 3.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the output array data type', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = mediansorted( x, { + 'dtype': 'float64' + }); + expected = 2.5; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = mediansorted( x, { + 'dtype': 'float64' + }); + expected = 2.5; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/min-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/min-by/docs/types/index.d.ts index 630dedaffadc..fb2b8c6ac49a 100644 --- a/lib/node_modules/@stdlib/stats/min-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/min-by/docs/types/index.d.ts @@ -233,7 +233,7 @@ interface Unary { * @example * var array = require( '@stdlib/ndarray/array' ); * -* var x = array( [ -1.0, 2.0, -3.0 ] ) +* var x = array( [ -1.0, 2.0, -3.0 ] ); * * function clbk( value ) { * return value * 2.0; @@ -249,7 +249,7 @@ interface Unary { * var array = require( '@stdlib/ndarray/array' ); * var zeros = require( '@stdlib/ndarray/zeros' ); * -* var x = array( [ -1.0, 2.0, -3.0 ] ) +* var x = array( [ -1.0, 2.0, -3.0 ] ); * var y = zeros( [] ); * * function clbk( value ) { diff --git a/lib/node_modules/@stdlib/stats/minsorted/README.md b/lib/node_modules/@stdlib/stats/minsorted/README.md new file mode 100644 index 000000000000..f6f15e5cf841 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minsorted/README.md @@ -0,0 +1,260 @@ + + +# minsorted + +> Compute the minimum value along one or more sorted [ndarray][@stdlib/ndarray/ctor] dimensions. + +
+ +## Usage + +```javascript +var minsorted = require( '@stdlib/stats/minsorted' ); +``` + +#### minsorted( x\[, options] ) + +Computes the minimum value along one or more sorted [ndarray][@stdlib/ndarray/ctor] dimensions. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0 ] ); + +var y = minsorted( x ); +// returns + +var v = y.get(); +// returns 1.0 +``` + +The function has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **options**: function options (_optional_). + +The function accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. + +By default, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform a reduction over specific dimensions, provide a `dims` option. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0, 4.0 ], { + 'shape': [ 2, 2 ], + 'order': 'row-major' +}); +var v = ndarray2array( x ); +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] + +var y = minsorted( x, { + 'dims': [ 0 ] +}); +// returns + +v = ndarray2array( y ); +// returns [ 1.0, 2.0 ] + +y = minsorted( x, { + 'dims': [ 1 ] +}); +// returns + +v = ndarray2array( y ); +// returns [ 1.0, 3.0 ] + +y = minsorted( x, { + 'dims': [ 0, 1 ] +}); +// returns + +v = y.get(); +// returns 1.0 +``` + +By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0, 4.0 ], { + 'shape': [ 2, 2 ], + 'order': 'row-major' +}); + +var v = ndarray2array( x ); +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] + +var y = minsorted( x, { + 'dims': [ 0 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 1.0, 2.0 ] ] + +y = minsorted( x, { + 'dims': [ 1 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 1.0 ], [ 3.0 ] ] + +y = minsorted( x, { + 'dims': [ 0, 1 ], + 'keepdims': true +}); +// returns + +v = ndarray2array( y ); +// returns [ [ 1.0 ] ] +``` + +By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option. + +```javascript +var getDType = require( '@stdlib/ndarray/dtype' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0 ], { + 'dtype': 'generic' +}); + +var y = minsorted( x, { + 'dtype': 'float64' +}); +// returns + +var dt = String( getDType( y ) ); +// returns 'float64' +``` + +#### minsorted.assign( x, out\[, options] ) + +Computes the minimum value along one or more sorted [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); + +var x = array( [ 1.0, 2.0, 3.0 ] ); +var y = zeros( [] ); + +var out = minsorted.assign( x, y ); +// returns + +var v = out.get(); +// returns 1.0 + +var bool = ( out === y ); +// returns true +``` + +The method has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or generic [data type][@stdlib/ndarray/dtypes]. +- **out**: output [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). + +The method accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. + +
+ + + +
+ +## Notes + +- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor]. +- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having the same [data type][@stdlib/ndarray/dtypes] as the input [ndarray][@stdlib/ndarray/ctor]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes]. + +
+ + + +
+ +## Examples + + + +```javascript +var linspace = require( '@stdlib/blas/ext/linspace' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var minsorted = require( '@stdlib/stats/minsorted' ); + +// Generate a sorted ndarray: +var x = linspace( [ 5, 5 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +// Perform a reduction: +var y = minsorted( x, { + 'dims': [ 0 ] +}); + +// Resolve the output array data type: +var dt = String( getDType( y ) ); +console.log( dt ); + +// Print the results: +console.log( ndarray2array( y ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/minsorted/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/stats/minsorted/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..4e482beb3b74 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minsorted/benchmark/benchmark.assign.js @@ -0,0 +1,111 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var linspace = require( '@stdlib/array/linspace' ); +var zeros = require( '@stdlib/array/zeros' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var minsorted = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out; + var x; + + x = linspace( -50.0, 50.0, len, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + out = new ndarray( options.dtype, zeros( 1, options.dtype ), [], [ 0 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = minsorted.assign( x, out ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + 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( pkg+':assign:dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/minsorted/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/minsorted/benchmark/benchmark.js new file mode 100644 index 000000000000..4b6485b2828a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minsorted/benchmark/benchmark.js @@ -0,0 +1,105 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var linspace = require( '@stdlib/array/linspace' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var pkg = require( './../package.json' ).name; +var minsorted = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = linspace( -50.0, 50.0, len, options ); + x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = minsorted( x ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + 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( pkg+':dtype='+options.dtype+',len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/minsorted/docs/repl.txt b/lib/node_modules/@stdlib/stats/minsorted/docs/repl.txt new file mode 100644 index 000000000000..f437b5ef286a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minsorted/docs/repl.txt @@ -0,0 +1,76 @@ + +{{alias}}( x[, options] ) + Computes the minimum value along one or more sorted ndarray dimensions. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or "generic" data type. + + options: Object (optional) + Function options. + + options.dtype: string (optional) + Output array data type. Must be a real-valued or "generic" data type. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in a provided input + ndarray. + + options.keepdims: boolean (optional) + Boolean indicating whether the reduced dimensions should be included in + the returned ndarray as singleton dimensions. Default: false. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var y = {{alias}}( x ); + > var v = y.get() + 1.0 + + +{{alias}}.assign( x, out[, options] ) + Computes the minimum value along one or more sorted ndarray dimensions and + assigns results to a provided output ndarray. + + Parameters + ---------- + x: ndarray + Input array. Must have a real-valued or generic data type. + + out: ndarray + Output array. + + options: Object (optional) + Function options. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in a provided input + ndarray. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var out = {{alias:@stdlib/ndarray/zeros}}( [] ); + > var y = {{alias}}.assign( x, out ) + + > var bool = ( out === y ) + true + > var v = out.get() + 1.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/minsorted/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/minsorted/docs/types/index.d.ts new file mode 100644 index 000000000000..ee77004b0814 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minsorted/docs/types/index.d.ts @@ -0,0 +1,151 @@ +/* +* @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 { ArrayLike } from '@stdlib/types/array'; +import { RealAndGenericDataType as DataType, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Input array. +*/ +type InputArray = typedndarray; + +/** +* Output array. +*/ +type OutputArray = typedndarray; + +/** +* Interface defining "base" options. +*/ +interface BaseOptions { + /** + * List of dimensions over which to perform a reduction. + */ + dims?: ArrayLike; +} + +/** +* Interface defining options. +*/ +interface Options extends BaseOptions { + /** + * Output array data type. + */ + dtype?: DataType; + + /** + * Boolean indicating whether the reduced dimensions should be included in the returned array as singleton dimensions. Default: `false`. + */ + keepdims?: boolean; +} + +/** +* Interface for performing a reduction on an ndarray. +*/ +interface Unary { + /** + * Computes the minimum value along one or more sorted ndarray dimensions. + * + * @param x - input ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * + * var x = array( [ 1.0, 2.0, 3.0 ] ); + * + * var y = minsorted( x ); + * // returns + * + * var v = y.get(); + * // returns 1.0 + */ + ( x: InputArray, options?: Options ): OutputArray; // NOTE: we lose type specificity here, but retaining specificity would likely be difficult and/or tedious to completely enumerate, as the output ndarray data type is dependent on how `x` interacts with output data type policy and whether that policy has been overridden by `options.dtype`. + + /** + * Computes the minimum value along one or more sorted ndarray dimensions and assigns results to a provided output ndarray. + * + * @param x - input ndarray + * @param out - output ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * var zeros = require( '@stdlib/ndarray/zeros' ); + * + * var x = array( [ 1.0, 2.0, 3.0 ] ); + * var y = zeros( [] ); + * + * var out = minsorted.assign( x, y ); + * // returns + * + * var v = out.get(); + * // returns 1.0 + * + * var bool = ( out === y ); + * // returns true + */ + assign = OutputArray>( x: InputArray, out: U, options?: BaseOptions ): U; +} + +/** +* Computes the minimum value along one or more sorted ndarray dimensions. +* +* @param x - input ndarray +* @param options - function options +* @returns output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ 1.0, 2.0, 3.0 ] ); +* +* var y = minsorted( x ); +* // returns +* +* var v = y.get(); +* // returns 1.0 +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = array( [ 1.0, 2.0, 3.0 ] ); +* var y = zeros( [] ); +* +* var out = minsorted.assign( x, y ); +* // returns +* +* var v = out.get(); +* // returns 1.0 +* +* var bool = ( out === y ); +* // returns true +*/ +declare const minsorted: Unary; + + +// EXPORTS // + +export = minsorted; diff --git a/lib/node_modules/@stdlib/stats/minsorted/docs/types/test.ts b/lib/node_modules/@stdlib/stats/minsorted/docs/types/test.ts new file mode 100644 index 000000000000..83f64d26ec5a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minsorted/docs/types/test.ts @@ -0,0 +1,225 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +/// + +import zeros = require( '@stdlib/ndarray/zeros' ); +import minsorted = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minsorted( x ); // $ExpectType OutputArray + minsorted( x, {} ); // $ExpectType OutputArray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + minsorted( '5' ); // $ExpectError + minsorted( 5 ); // $ExpectError + minsorted( true ); // $ExpectError + minsorted( false ); // $ExpectError + minsorted( null ); // $ExpectError + minsorted( void 0 ); // $ExpectError + minsorted( {} ); // $ExpectError + minsorted( ( x: number ): number => x ); // $ExpectError + + minsorted( '5', {} ); // $ExpectError + minsorted( 5, {} ); // $ExpectError + minsorted( true, {} ); // $ExpectError + minsorted( false, {} ); // $ExpectError + minsorted( null, {} ); // $ExpectError + minsorted( void 0, {} ); // $ExpectError + minsorted( {}, {} ); // $ExpectError + minsorted( ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minsorted( x, '5' ); // $ExpectError + minsorted( x, true ); // $ExpectError + minsorted( x, false ); // $ExpectError + minsorted( x, null ); // $ExpectError + minsorted( x, [] ); // $ExpectError + minsorted( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dtype` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minsorted( x, { 'dtype': '5' } ); // $ExpectError + minsorted( x, { 'dtype': 5 } ); // $ExpectError + minsorted( x, { 'dtype': true } ); // $ExpectError + minsorted( x, { 'dtype': false } ); // $ExpectError + minsorted( x, { 'dtype': null } ); // $ExpectError + minsorted( x, { 'dtype': [] } ); // $ExpectError + minsorted( x, { 'dtype': {} } ); // $ExpectError + minsorted( x, { 'dtype': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `keepdims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minsorted( x, { 'keepdims': '5' } ); // $ExpectError + minsorted( x, { 'keepdims': 5 } ); // $ExpectError + minsorted( x, { 'keepdims': null } ); // $ExpectError + minsorted( x, { 'keepdims': [] } ); // $ExpectError + minsorted( x, { 'keepdims': {} } ); // $ExpectError + minsorted( x, { 'keepdims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minsorted( x, { 'dims': '5' } ); // $ExpectError + minsorted( x, { 'dims': 5 } ); // $ExpectError + minsorted( x, { 'dims': true } ); // $ExpectError + minsorted( x, { 'dims': false } ); // $ExpectError + minsorted( x, { 'dims': null } ); // $ExpectError + minsorted( x, { 'dims': {} } ); // $ExpectError + minsorted( x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minsorted(); // $ExpectError + minsorted( x, {}, {} ); // $ExpectError +} + +// Attached to the function is an `assign` method which returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minsorted.assign( x, x ); // $ExpectType float64ndarray + minsorted.assign( x, x, {} ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minsorted.assign( '5', x ); // $ExpectError + minsorted.assign( 5, x ); // $ExpectError + minsorted.assign( true, x ); // $ExpectError + minsorted.assign( false, x ); // $ExpectError + minsorted.assign( null, x ); // $ExpectError + minsorted.assign( void 0, x ); // $ExpectError + minsorted.assign( {}, x ); // $ExpectError + minsorted.assign( ( x: number ): number => x, x ); // $ExpectError + + minsorted.assign( '5', x, {} ); // $ExpectError + minsorted.assign( 5, x, {} ); // $ExpectError + minsorted.assign( true, x, {} ); // $ExpectError + minsorted.assign( false, x, {} ); // $ExpectError + minsorted.assign( null, x, {} ); // $ExpectError + minsorted.assign( void 0, x, {} ); // $ExpectError + minsorted.assign( {}, x, {} ); // $ExpectError + minsorted.assign( ( x: number ): number => x, x, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minsorted.assign( x, '5' ); // $ExpectError + minsorted.assign( x, 5 ); // $ExpectError + minsorted.assign( x, true ); // $ExpectError + minsorted.assign( x, false ); // $ExpectError + minsorted.assign( x, null ); // $ExpectError + minsorted.assign( x, void 0 ); // $ExpectError + minsorted.assign( x, ( x: number ): number => x ); // $ExpectError + + minsorted.assign( x, '5', {} ); // $ExpectError + minsorted.assign( x, 5, {} ); // $ExpectError + minsorted.assign( x, true, {} ); // $ExpectError + minsorted.assign( x, false, {} ); // $ExpectError + minsorted.assign( x, null, {} ); // $ExpectError + minsorted.assign( x, void 0, {} ); // $ExpectError + minsorted.assign( x, ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a third argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minsorted.assign( x, x, '5' ); // $ExpectError + minsorted.assign( x, x, true ); // $ExpectError + minsorted.assign( x, x, false ); // $ExpectError + minsorted.assign( x, x, null ); // $ExpectError + minsorted.assign( x, x, [] ); // $ExpectError + minsorted.assign( x, x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minsorted.assign( x, x, { 'dims': '5' } ); // $ExpectError + minsorted.assign( x, x, { 'dims': 5 } ); // $ExpectError + minsorted.assign( x, x, { 'dims': true } ); // $ExpectError + minsorted.assign( x, x, { 'dims': false } ); // $ExpectError + minsorted.assign( x, x, { 'dims': null } ); // $ExpectError + minsorted.assign( x, x, { 'dims': {} } ); // $ExpectError + minsorted.assign( x, x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + minsorted.assign(); // $ExpectError + minsorted.assign( x ); // $ExpectError + minsorted.assign( x, x, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/minsorted/examples/index.js b/lib/node_modules/@stdlib/stats/minsorted/examples/index.js new file mode 100644 index 000000000000..dd5ae7f0d67f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minsorted/examples/index.js @@ -0,0 +1,40 @@ +/** +* @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 linspace = require( '@stdlib/blas/ext/linspace' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var minsorted = require( './../lib' ); + +// Generate a sorted ndarray: +var x = linspace( [ 5, 5 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +// Perform a reduction: +var y = minsorted( x, { + 'dims': [ 0 ] +}); + +// Resolve the output array data type: +var dt = getDType( y ); +console.log( dt ); + +// Print the results: +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/stats/minsorted/lib/index.js b/lib/node_modules/@stdlib/stats/minsorted/lib/index.js new file mode 100644 index 000000000000..b9b3f4b56e68 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minsorted/lib/index.js @@ -0,0 +1,63 @@ +/** +* @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 minimum value along one or more sorted ndarray dimensions. +* +* @module @stdlib/stats/minsorted +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var minsorted = require( '@stdlib/stats/minsorted' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 0.0, 2.0, 3.0, 0.0, 0.0, 6.0, 7.0, 0.0, 0.0, 10.0, 11.0, 0.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform reduction: +* var out = minsorted( x ); +* // returns +* +* var v = out.get(); +* // returns 2.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/stats/minsorted/lib/main.js b/lib/node_modules/@stdlib/stats/minsorted/lib/main.js new file mode 100644 index 000000000000..142bbe67a252 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minsorted/lib/main.js @@ -0,0 +1,101 @@ +/** +* @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 dtypes = require( '@stdlib/ndarray/dtypes' ); +var gminsorted = require( '@stdlib/stats/base/ndarray/minsorted' ); +var dminsorted = require( '@stdlib/stats/base/ndarray/dminsorted' ); +var sminsorted = require( '@stdlib/stats/base/ndarray/sminsorted' ); +var factory = require( '@stdlib/ndarray/base/unary-reduce-strided1d-dispatch-factory' ); + + +// VARIABLES // + +var idtypes = dtypes( 'real_and_generic' ); +var odtypes = dtypes( 'real_and_generic' ); +var policies = { + 'output': 'same', + 'casting': 'none' +}; +var table = { + 'types': [ + 'float64', // input + 'float32' // input + ], + 'fcns': [ + dminsorted, + sminsorted + ], + 'default': gminsorted +}; + + +// MAIN // + +/** +* Computes the minimum value along one or more sorted ndarray dimensions. +* +* @name minsorted +* @type {Function} +* @param {ndarray} x - input ndarray +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction +* @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions +* @param {string} [options.dtype] - output ndarray data type +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 0.0, 2.0, 3.0, 0.0, 0.0, 6.0, 7.0, 0.0, 0.0, 10.0, 11.0, 0.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 4, 1 ]; +* +* // Define the index offset: +* var ox = 1; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform reduction: +* var out = minsorted( x ); +* // returns +* +* var v = out.get(); +* // returns 2.0 +*/ +var minsorted = factory( table, [ idtypes ], odtypes, policies ); + + +// EXPORTS // + +module.exports = minsorted; diff --git a/lib/node_modules/@stdlib/stats/minsorted/package.json b/lib/node_modules/@stdlib/stats/minsorted/package.json new file mode 100644 index 000000000000..624b27d410a8 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minsorted/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/stats/minsorted", + "version": "0.0.0", + "description": "Compute the minimum value along one or more sorted ndarray dimensions.", + "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", + "range", + "extremes", + "domain", + "extent", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/minsorted/test/test.assign.js b/lib/node_modules/@stdlib/stats/minsorted/test/test.assign.js new file mode 100644 index 000000000000..665c47a66f67 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minsorted/test/test.assign.js @@ -0,0 +1,667 @@ +/** +* @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 ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var minsorted = require( './../lib' ).assign; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof minsorted, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( value, out ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( value, out, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( value, out ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (options)', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( value, out, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( x, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not an object', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( x, out, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [ 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + out = zeros( [], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( x, out, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (default)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 2, 2 ], + [ 2 ], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + minsorted( x, out ); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (all dimensions)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 2, 2 ], + [ 2 ], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + minsorted( x, out, { + 'dims': [ 0, 1 ] + }); + }; + } +}); + +tape( 'the function throws an error if provided an output array which has an invalid shape (some dimensions)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [], + [ 4, 4 ], + [ 4 ], + [ 1 ], + [ 1, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var out = zeros( value, { + 'dtype': 'generic' + }); + minsorted( x, out, { + 'dims': [ 0 ] + }); + }; + } +}); + +tape( 'the function performs a reduction on an ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = minsorted( x, out ); + expected = 1.0; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = minsorted( x, out ); + expected = 1.0; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = minsorted( x, out, { + 'dims': [ 0, 1 ] + }); + expected = 1.0; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [] + }); + + actual = minsorted( x, out, { + 'dims': [ 0, 1 ] + }); + expected = 1.0; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = minsorted( x, out, { + 'dims': [ 0 ] + }); + expected = [ 1.0, 2.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = minsorted( x, out, { + 'dims': [ 1 ] + }); + expected = [ 1.0, 3.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = [ 1.0, 3.0, 2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = minsorted( x, out, { + 'dims': [ 0 ] + }); + expected = [ 1.0, 2.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 3.0, 2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + out = emptyLike( x, { + 'shape': [ 2 ] + }); + + actual = minsorted( x, out, { + 'dims': [ 1 ] + }); + expected = [ 1.0, 3.0 ]; + + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/minsorted/test/test.js b/lib/node_modules/@stdlib/stats/minsorted/test/test.js new file mode 100644 index 000000000000..8fa759e484b9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minsorted/test/test.js @@ -0,0 +1,39 @@ +/** +* @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 isMethod = require( '@stdlib/assert/is-method' ); +var minsorted = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof minsorted, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( minsorted, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/minsorted/test/test.main.js b/lib/node_modules/@stdlib/stats/minsorted/test/test.main.js new file mode 100644 index 000000000000..256bb13445c7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/minsorted/test/test.main.js @@ -0,0 +1,753 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var minsorted = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof minsorted, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type (options)', function test( t ) { + var values; + var i; + + values = [ + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dtype` option which is not a supported data type', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + 'foo', + 'bar', + 'beep', + 'boop' + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( x, { + 'dtype': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `keepdims` option which is not a boolean', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( x, { + 'keepdims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 'a' ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 0, 20 ], + [ 20 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minsorted( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function performs a reduction on an ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minsorted( x ); + expected = 1.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minsorted( x ); + expected = 1.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minsorted( x, { + 'dims': [ 0, 1 ] + }); + expected = 1.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minsorted( x, { + 'dims': [ 0, 1 ], + 'keepdims': false + }); + expected = 1.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minsorted( x, { + 'dims': [ 0, 1 ], + 'keepdims': true + }); + expected = [ [ 1.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (all dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minsorted( x, { + 'dims': [ 0, 1 ] + }); + expected = 1.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minsorted( x, { + 'dims': [ 0, 1 ], + 'keepdims': false + }); + expected = 1.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minsorted( x, { + 'dims': [ 0, 1 ], + 'keepdims': true + }); + expected = [ [ 1.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 1.0, 1.0, 1.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minsorted( x, { + 'dims': [], + 'keepdims': false + }); + expected = [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 1.0, 1.0, 1.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minsorted( x, { + 'dims': [], + 'keepdims': true + }); + expected = [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs a reduction on an ndarray (no dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 1.0, 1.0, 1.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minsorted( x, { + 'dims': [], + 'keepdims': false + }); + expected = [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 1.0, 1.0, 1.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minsorted( x, { + 'dims': [], + 'keepdims': true + }); + expected = [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minsorted( x, { + 'dims': [ 0 ], + 'keepdims': false + }); + expected = [ 1.0, 2.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minsorted( x, { + 'dims': [ 0 ], + 'keepdims': true + }); + expected = [ [ 1.0, 2.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minsorted( x, { + 'dims': [ 1 ], + 'keepdims': false + }); + expected = [ 1.0, 3.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minsorted( x, { + 'dims': [ 1 ], + 'keepdims': true + }); + expected = [ [ 1.0 ], [ 3.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 3.0, 2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minsorted( x, { + 'dims': [ 1 ], + 'keepdims': false + }); + expected = [ 1.0, 3.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 3.0, 2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minsorted( x, { + 'dims': [ 0 ], + 'keepdims': true + }); + expected = [ [ 1.0, 2.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 1, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 3.0, 2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minsorted( x, { + 'dims': [ 1 ], + 'keepdims': false + }); + expected = [ 1.0, 3.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 3.0, 2.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minsorted( x, { + 'dims': [ 1 ], + 'keepdims': true + }); + expected = [ [ 1.0 ], [ 3.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 1 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the output array data type', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = minsorted( x, { + 'dtype': 'float64' + }); + expected = 1.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = minsorted( x, { + 'dtype': 'float64' + }); + expected = 1.0; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( actual.get(), expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/nanmax-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/nanmax-by/docs/types/index.d.ts index 1902c6f5941c..ccad49b15b12 100644 --- a/lib/node_modules/@stdlib/stats/nanmax-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/nanmax-by/docs/types/index.d.ts @@ -233,7 +233,7 @@ interface Unary { * @example * var array = require( '@stdlib/ndarray/array' ); * -* var x = array( [ -1.0, 2.0, NaN ] ) +* var x = array( [ -1.0, 2.0, NaN ] ); * * function clbk( value ) { * return value * 2.0; @@ -249,7 +249,7 @@ interface Unary { * var array = require( '@stdlib/ndarray/array' ); * var zeros = require( '@stdlib/ndarray/zeros' ); * -* var x = array( [ -1.0, 2.0, NaN ] ) +* var x = array( [ -1.0, 2.0, NaN ] ); * var y = zeros( [] ); * * function clbk( value ) { diff --git a/lib/node_modules/@stdlib/stats/nanmin-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/nanmin-by/docs/types/index.d.ts index 0c34ee9ede3e..29cd4b286819 100644 --- a/lib/node_modules/@stdlib/stats/nanmin-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/nanmin-by/docs/types/index.d.ts @@ -233,7 +233,7 @@ interface Unary { * @example * var array = require( '@stdlib/ndarray/array' ); * -* var x = array( [ -1.0, 2.0, NaN ] ) +* var x = array( [ -1.0, 2.0, NaN ] ); * * function clbk( value ) { * return value * 2.0; @@ -249,7 +249,7 @@ interface Unary { * var array = require( '@stdlib/ndarray/array' ); * var zeros = require( '@stdlib/ndarray/zeros' ); * -* var x = array( [ -1.0, 2.0, NaN ] ) +* var x = array( [ -1.0, 2.0, NaN ] ); * var y = zeros( [] ); * * function clbk( value ) { diff --git a/lib/node_modules/@stdlib/stats/range-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/range-by/docs/types/index.d.ts index 70c259db06ac..1a214b2110dc 100644 --- a/lib/node_modules/@stdlib/stats/range-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/range-by/docs/types/index.d.ts @@ -233,7 +233,7 @@ interface Unary { * @example * var array = require( '@stdlib/ndarray/array' ); * -* var x = array( [ -1.0, 2.0, -3.0 ] ) +* var x = array( [ -1.0, 2.0, -3.0 ] ); * * function clbk( value ) { * return value * 2.0;