diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/README.md new file mode 100644 index 000000000000..586abb1bdfe7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/README.md @@ -0,0 +1,171 @@ + + +# dmeanstdev + +> Compute the [arithmetic mean][arithmetic-mean] and [standard deviation][standard-deviation] of a one-dimensional double-precision floating-point ndarray. + +
+ +The [arithmetic mean][arithmetic-mean] and [standard deviation][standard-deviation] are defined as + + + +```math +\mu = \frac{1}{n} \sum_{i=0}^{n-1} x_i +``` + + + + + +and + + + +```math +\sigma = \sqrt{\frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \mu)^2} +``` + + + + + +where the use of the term `n-1` is commonly referred to as Bessel's correction. + +
+ + + +
+ +## Usage + +```javascript +var dmeanstdev = require( '@stdlib/stats/base/ndarray/dmeanstdev' ); +``` + +#### dmeanstdev( arrays ) + +Computes the [arithmetic mean][arithmetic-mean] and [standard deviation][standard-deviation] of a one-dimensional double-precision floating-point ndarray. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +var opts = { + 'dtype': 'float64' +}; + +var xbuf = new Float64Array( [ 1.0, 3.0, 4.0, 2.0 ] ); +var x = new ndarray( opts.dtype, xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +var out = new ndarray( opts.dtype, new Float64Array( 2 ), [ 2 ], [ 1 ], 0, 'row-major' ); + +var correction = scalar2ndarray( 1.0, opts ); + +var v = dmeanstdev( [ x, out, correction ] ); +// returns +``` + +The function has the following parameters: + +- **arrays**: array-like object containing the following ndarrays in order: + + 1. a one-dimensional input ndarray. + 2. a one-dimensional output ndarray (of length 2) to store the [mean][arithmetic-mean] and [standard deviation][standard-deviation]. + 3. a zero-dimensional ndarray specifying the degrees of freedom adjustment. + +The output ndarray contains two elements: the [arithmetic mean][arithmetic-mean] at index 0 and the [standard deviation][standard-deviation] at index 1. The [standard deviation][standard-deviation] is computed using the provided degrees of freedom adjustment. Setting the correction parameter to `1` corresponds to Bessel's correction (i.e., the corrected sample standard deviation). Setting it to `0` computes the population standard deviation. + +
+ + + +
+ +## Notes + +- If provided an empty one-dimensional ndarray, the computed [arithmetic mean][arithmetic-mean] and [standard deviation][standard-deviation] are equal to `NaN`. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var Float64Array = require( '@stdlib/array/float64' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dmeanstdev = require( '@stdlib/stats/base/ndarray/dmeanstdev' ); + +var opts = { + 'dtype': 'float64' +}; + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +var x = new ndarray( opts.dtype, xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +var out = new ndarray( opts.dtype, new Float64Array( 2 ), [ 2 ], [ 1 ], 0, 'row-major' ); +var correction = scalar2ndarray( 1.0, opts ); + +console.log( ndarray2array( x ) ); + +var v = dmeanstdev( [ x, out, correction ] ); +console.log( v ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/benchmark/benchmark.js new file mode 100644 index 000000000000..76098ffaa7d4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/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 pow = require( '@stdlib/math/base/special/pow' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var pkg = require( './../package.json' ).name; +var dmeanstdev = 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 correction; + var xbuf; + var out; + var x; + + xbuf = uniform( len, -10.0, 10.0, options ); + x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); + out = new ndarray( 'float64', new Float64Array( 2 ), [ 2 ], [ 1 ], 0, 'row-major' ); + correction = scalar2ndarray( 1.0, options ); + + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = dmeanstdev( [ x, out, correction ] ); + if ( isnan( v[ 0 ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( v[ 0 ] ) ) { + 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/stats/base/ndarray/dmeanstdev/docs/img/equation_arithmetic_mean.svg b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/docs/img/equation_arithmetic_mean.svg new file mode 100644 index 000000000000..c31439606fb6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/docs/img/equation_arithmetic_mean.svg @@ -0,0 +1,42 @@ + +mu equals StartFraction 1 Over n EndFraction sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts x Subscript i + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/docs/repl.txt new file mode 100644 index 000000000000..9946433f04bc --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/docs/repl.txt @@ -0,0 +1,43 @@ + +{{alias}}( arrays ) + Computes the mean and standard deviation of a one-dimensional double- + precision floating-point ndarray. + + If provided an empty ndarray, the function returns `NaN` values. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing the following ndarrays in order: + + - a one-dimensional input ndarray. + - a one-dimensional output ndarray (of length 2) to store the mean + and standard deviation. + - a zero-dimensional ndarray specifying the degrees of freedom + adjustment. + + Returns + ------- + out: ndarray + An ndarray containing the mean and standard deviation. + + Examples + -------- + > var xbuf = new {{alias:@stdlib/array/float64}}( [ 2.0, 1.0, 2.0, -2.0 ] ); + > var dt = 'float64'; + > 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 ); + > var o = new {{alias:@stdlib/array/float64}}( 2 ); + > var out = new {{alias:@stdlib/ndarray/ctor}}( dt, o, [ 2 ], [ 1 ], ox, ord ); + > var opts = { 'dtype': dt }; + > var correction = {{alias:@stdlib/ndarray/from-scalar}}( 1.0, opts ); + > {{alias}}( [ x, out, correction ] ); + > {{alias:@stdlib/ndarray/to-array}}( out ) + [ ~0.75, ~1.8930 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/docs/types/index.d.ts new file mode 100644 index 000000000000..97eb527c4575 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/docs/types/index.d.ts @@ -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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { float64ndarray } from '@stdlib/types/ndarray'; + +/** +* Computes the mean and standard deviation of a one-dimensional double-precision floating-point ndarray. +* +* @param arrays - array-like object containing input and output ndarrays +* @returns output ndarray containing [ mean, stdev ] +*/ +declare function dmeanstdev( arrays: [ float64ndarray, float64ndarray ] ): float64ndarray; + + +// EXPORTS // + +export = dmeanstdev; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/docs/types/test.ts new file mode 100644 index 000000000000..4da71429a28d --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/docs/types/test.ts @@ -0,0 +1,71 @@ +/* +* @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' ); // eslint-disable-line import/no-unresolved +import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); // eslint-disable-line import/no-unresolved +import dmeanstdev = require( './index' ); // eslint-disable-line import/no-unresolved + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float64' + }); + const out = zeros( [ 2 ], { + 'dtype': 'float64' + }); + const correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' + }); + + dmeanstdev( [ x, out, correction ] ); // $ExpectType ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + dmeanstdev( '10' ); // $ExpectError + dmeanstdev( 10 ); // $ExpectError + dmeanstdev( true ); // $ExpectError + dmeanstdev( false ); // $ExpectError + dmeanstdev( null ); // $ExpectError + dmeanstdev( undefined ); // $ExpectError + dmeanstdev( [] ); // $ExpectError + dmeanstdev( {} ); // $ExpectError + dmeanstdev( ( 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 out = zeros( [ 2 ], { + 'dtype': 'float64' + }); + const correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' + }); + + dmeanstdev(); // $ExpectError + dmeanstdev( [ x, out ], {} ); // $ExpectError + dmeanstdev( [ x, out ] ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/examples/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/examples/index.js new file mode 100644 index 000000000000..9ee89317bca8 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/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 Float64Array = require( '@stdlib/array/float64' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dmeanstdev = require( './../lib' ); + +var opts = { + 'dtype': 'float64' +}; + +var xbuf = discreteUniform( 10, -50, 50, { + 'dtype': 'float64' +}); +var x = new ndarray( opts.dtype, xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +var out = new ndarray( opts.dtype, new Float64Array( 2 ), [ 2 ], [ 1 ], 0, 'row-major' ); +var correction = scalar2ndarray( 1.0, opts ); + +console.log( ndarray2array( x ) ); + +var v = dmeanstdev( [ x, out, correction ] ); +console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/lib/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/lib/index.js new file mode 100644 index 000000000000..43172d82c2a7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/lib/index.js @@ -0,0 +1,52 @@ +/** +* @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 mean and standard deviation of a one-dimensional double-precision floating-point ndarray. +* +* @module @stdlib/stats/base/ndarray/dmeanstdev +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var dmeanstdev = require( '@stdlib/stats/base/ndarray/dmeanstdev' ); +* +* var opts = { +* 'dtype': 'float64' +* }; +* +* var xbuf = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); +* var x = new ndarray( opts.dtype, xbuf, [ 4 ], [ 2 ], 1, 'row-major' ); +* var out = new ndarray( opts.dtype, new Float64Array( 2 ), [ 2 ], [ 1 ], 0, 'row-major' ); +* var correction = scalar2ndarray( 1.0, opts ); +* +* var v = dmeanstdev( [ x, out, correction ] ); +* // returns +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/lib/main.js b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/lib/main.js new file mode 100644 index 000000000000..3d62e1639da0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/lib/main.js @@ -0,0 +1,74 @@ +/** +* @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/stats/strided/dmeanstdev' ).ndarray; + + +// MAIN // + +/** +* Computes the mean and standard deviation of a one-dimensional double-precision floating-point ndarray. +* +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @returns {ndarrayLike} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var opts = { +* 'dtype': 'float64' +* }; +* +* var xbuf = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); +* var x = new ndarray( opts.dtype, xbuf, [ 4 ], [ 2 ], 1, 'row-major' ); +* var out = new ndarray( opts.dtype, new Float64Array( 2 ), [ 2 ], [ 1 ], 0, 'row-major' ); +* +* var correction = scalar2ndarray( 1.0, opts ); +* +* var v = dmeanstdev( [ x, out, correction ] ); +* // returns +*/ +function dmeanstdev( arrays ) { + var correction; + var out; + var x; + + x = arrays[ 0 ]; + out = arrays[ 1 ]; + correction = ndarraylike2scalar( arrays[ 2 ] ); + + strided( numelDimension( x, 0 ), correction, getData( x ), getStride( x, 0 ), getOffset( x ), getData( out ), getStride( out, 0 ), getOffset( out ) ); // eslint-disable-line max-len + + return out; +} + + +// EXPORTS // + +module.exports = dmeanstdev; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/package.json b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/package.json new file mode 100644 index 000000000000..da4304547960 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/package.json @@ -0,0 +1,62 @@ +{ + "name": "@stdlib/stats/base/ndarray/dmeanstdev", + "version": "0.0.0", + "description": "Compute the mean and standard deviation of a one-dimensional double-precision floating-point ndarray.", + "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", + "statistics", + "stats", + "mean", + "standard deviation", + "stddev", + "ndarray", + "strided", + "float64" + ] +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/test/test.js b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/test/test.js new file mode 100644 index 000000000000..040591f84cc1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dmeanstdev/test/test.js @@ -0,0 +1,248 @@ +/** +* @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 sqrt = require( '@stdlib/math/base/special/sqrt' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var Float64Array = require( '@stdlib/array/float64' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var dmeanstdev = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Collection} 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 dmeanstdev, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( dmeanstdev.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function calculates the arithmetic mean and standard deviation of a one-dimensional ndarray', function test( t ) { + var correction; + var expected; + var opts; + var out; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] ); + out = new Float64Array( 2 ); + correction = scalar2ndarray( 1.0, opts ); + + v = dmeanstdev( [ vector( x, x.length, 1, 0 ), vector( out, 2, 1, 0 ), correction ] ); + expected = new Float64Array( [ 0.5, sqrt( 53.5/(x.length-1) ) ] ); + t.deepEqual( getData( v ), expected, 'returns expected value' ); + + x = new Float64Array( [ -4.0, -5.0 ] ); + out = new Float64Array( 2 ); + correction = scalar2ndarray( 1.0, opts ); + + v = dmeanstdev( [ vector( x, x.length, 1, 0 ), vector( out, 2, 1, 0 ), correction ] ); + expected = new Float64Array( [ -4.5, sqrt( 0.5 ) ] ); + t.deepEqual( getData( v ), expected, 'returns expected value' ); + + x = new Float64Array( [ NaN ] ); + out = new Float64Array( 2 ); + correction = scalar2ndarray( 1.0, opts ); + + v = dmeanstdev( [ vector( x, x.length, 1, 0 ), vector( out, 2, 1, 0 ), correction ] ); + t.strictEqual( isnan( getData( v )[ 0 ] ), true, 'returns expected value' ); + t.strictEqual( isnan( getData( v )[ 1 ] ), true, 'returns expected value' ); + + x = new Float64Array( [ NaN, NaN ] ); + out = new Float64Array( 2 ); + correction = scalar2ndarray( 1.0, opts ); + + v = dmeanstdev( [ vector( x, x.length, 1, 0 ), vector( out, 2, 1, 0 ), correction ] ); + t.strictEqual( isnan( getData( v )[ 0 ] ), true, 'returns expected value' ); + t.strictEqual( isnan( getData( v )[ 1 ] ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an empty vector, the function returns `NaN` values', function test( t ) { + var correction; + var opts; + var out; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array( [] ); + out = new Float64Array( 2 ); + correction = scalar2ndarray( 1.0, opts ); + + v = dmeanstdev( [ vector( x, 0, 1, 0 ), vector( out, 2, 1, 0 ), correction ] ); + t.strictEqual( isnan( getData( v )[ 0 ] ), true, 'returns expected value' ); + t.strictEqual( isnan( getData( v )[ 1 ] ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a vector containing a single element, the function returns that element as mean and `NaN` as standard deviation when using sample correction', function test( t ) { + var correction; + var opts; + var out; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array( [ 1.0 ] ); + out = new Float64Array( 2 ); + correction = scalar2ndarray( 1.0, opts ); + + v = dmeanstdev( [ vector( x, 1, 1, 0 ), vector( out, 2, 1, 0 ), correction ] ); + t.strictEqual( getData( v )[ 0 ], 1.0, 'returns expected mean' ); + t.strictEqual( isnan( getData( v )[ 1 ] ), true, 'returns NaN for stdev' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-unit strides', function test( t ) { + var correction; + var expected; + var opts; + var out; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array([ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]); + out = new Float64Array( 2 ); + correction = scalar2ndarray( 1.0, opts ); + + v = dmeanstdev( [ vector( x, 4, 2, 0 ), vector( out, 2, 1, 0 ), correction ] ); + expected = new Float64Array( [ 1.25, 2.5 ] ); + t.deepEqual( getData( v ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having negative strides', function test( t ) { + var correction; + var expected; + var opts; + var out; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array([ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]); + out = new Float64Array( 2 ); + correction = scalar2ndarray( 1.0, opts ); + + v = dmeanstdev( [ vector( x, 4, -2, 6 ), vector( out, 2, 1, 0 ), correction ] ); + expected = new Float64Array( [ 1.25, 2.5 ] ); + t.deepEqual( getData( v ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-zero offsets', function test( t ) { + var correction; + var expected; + var opts; + var out; + var x; + var v; + + opts = { + 'dtype': 'float64' + }; + + x = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]); + out = new Float64Array( 2 ); + correction = scalar2ndarray( 1.0, opts ); + + v = dmeanstdev( [ vector( x, 4, 2, 1 ), vector( out, 2, 1, 0 ), correction ] ); + expected = new Float64Array( [ 1.25, 2.5 ] ); + t.deepEqual( getData( v ), expected, 'returns expected value' ); + + t.end(); +});