Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ limitations under the License.

-->

# smean
# smeanors

> Compute the [arithmetic mean][arithmetic-mean] of a one-dimensional single-precision floating-point ndarray.
> Compute the [arithmetic mean][arithmetic-mean] of a one-dimensional single-precision floating-point ndarray using ordinary recursive summation.

<section class="intro">

Expand Down Expand Up @@ -48,12 +48,12 @@ The [arithmetic mean][arithmetic-mean] is defined as
## Usage

```javascript
var smean = require( '@stdlib/stats/base/ndarray/smean' );
var smeanors = require( '@stdlib/stats/base/ndarray/smeanors' );
```

#### smean( arrays )
#### smeanors( arrays )

Computes the [arithmetic mean][arithmetic-mean] of a one-dimensional single-precision floating-point ndarray.
Computes the [arithmetic mean][arithmetic-mean] of a one-dimensional single-precision floating-point ndarray using ordinary recursive summation.

```javascript
var Float32Array = require( '@stdlib/array/float32' );
Expand All @@ -62,7 +62,7 @@ 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 v = smean( [ x ] );
var v = smeanors( [ x ] );
// returns 2.5
```

Expand All @@ -79,6 +79,7 @@ The function has the following parameters:
## Notes

- If provided an empty one-dimensional ndarray, the function returns `NaN`.
- Ordinary recursive summation (i.e., a "simple" sum) is performant, but can incur significant numerical error. If performance is paramount and error tolerated, using ordinary recursive summation to compute an arithmetic mean is acceptable; in all other cases, exercise due caution.

</section>

Expand All @@ -94,15 +95,15 @@ The function has the following parameters:
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var smean = require( '@stdlib/stats/base/ndarray/smean' );
var smeanors = require( '@stdlib/stats/base/ndarray/smeanors' );

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 v = smean( [ x ] );
var v = smeanors( [ x ] );
console.log( v );
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ 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 pkg = require( './../package.json' ).name;
var smean = require( './../lib' );
var smeanors = require( './../lib' );


// VARIABLES //
Expand Down Expand Up @@ -60,7 +60,7 @@ function createBenchmark( len ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = smean( [ x ] );
v = smeanors( [ x ] );
if ( isnan( v ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

{{alias}}( arrays )
Computes the arithmetic mean of a one-dimensional single-precision floating-
point ndarray.
point ndarray using ordinary recursive summation.

If provided an empty ndarray, the function returns `NaN`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import { float32ndarray } from '@stdlib/types/ndarray';

/**
* Computes the arithmetic mean of a one-dimensional single-precision floating-point ndarray.
* Computes the arithmetic mean of a one-dimensional single-precision floating-point ndarray using ordinary recursive summation.
*
* @param arrays - array-like object containing an input ndarray
* @returns arithmetic mean
Expand All @@ -35,12 +35,12 @@ import { float32ndarray } from '@stdlib/types/ndarray';
* var xbuf = new Float32Array( [ 1.0, 3.0, 4.0, 2.0 ] );
* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
*
* var v = smean( [ x ] );
* var v = smeanors( [ x ] );
* // returns 2.5
*/
declare function smean( arrays: [ float32ndarray ] ): number;
declare function smeanors( arrays: [ float32ndarray ] ): number;


// EXPORTS //

export = smean;
export = smeanors;
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/* eslint-disable space-in-parens */

import zeros = require( '@stdlib/ndarray/zeros' );
import smean = require( './index' );
import smeanors = require( './index' );


// TESTS //
Expand All @@ -30,20 +30,20 @@ import smean = require( './index' );
'dtype': 'float32'
});

smean( [ x ] ); // $ExpectType number
smeanors( [ x ] ); // $ExpectType number
}

// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
{
smean( '10' ); // $ExpectError
smean( 10 ); // $ExpectError
smean( true ); // $ExpectError
smean( false ); // $ExpectError
smean( null ); // $ExpectError
smean( undefined ); // $ExpectError
smean( [] ); // $ExpectError
smean( {} ); // $ExpectError
smean( ( x: number ): number => x ); // $ExpectError
smeanors( '10' ); // $ExpectError
smeanors( 10 ); // $ExpectError
smeanors( true ); // $ExpectError
smeanors( false ); // $ExpectError
smeanors( null ); // $ExpectError
smeanors( undefined ); // $ExpectError
smeanors( [] ); // $ExpectError
smeanors( {} ); // $ExpectError
smeanors( ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
Expand All @@ -52,6 +52,6 @@ import smean = require( './index' );
'dtype': 'float32'
});

smean(); // $ExpectError
smean( [ x ], {} ); // $ExpectError
smeanors(); // $ExpectError
smeanors( [ x ], {} ); // $ExpectError
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var smean = require( './../lib' );
var smeanors = 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 v = smean( [ x ] );
var v = smeanors( [ x ] );
console.log( v );
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
'use strict';

/**
* Compute the arithmetic mean of a one-dimensional single-precision floating-point ndarray.
* Compute the arithmetic mean of a one-dimensional single-precision floating-point ndarray using ordinary recursive summation.
*
* @module @stdlib/stats/base/ndarray/smean
* @module @stdlib/stats/base/ndarray/smeanors
*
* @example
* var Float32Array = require( '@stdlib/array/float32' );
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
* var smean = require( '@stdlib/stats/base/ndarray/smean' );
* var smeanors = require( '@stdlib/stats/base/ndarray/smeanors' );
*
* var xbuf = new Float32Array( [ 1.0, 3.0, 4.0, 2.0 ] );
* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
*
* var v = smean( [ x ] );
* var v = smeanors( [ x ] );
* // returns 2.5
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ 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/stats/strided/smean' ).ndarray;
var strided = require( '@stdlib/stats/strided/smeanors' ).ndarray;


// MAIN //

/**
* Computes the arithmetic mean of a one-dimensional single-precision floating-point ndarray.
* Computes the arithmetic mean of a one-dimensional single-precision floating-point ndarray using ordinary recursive summation.
*
* @param {ArrayLikeObject<Object>} arrays - array-like object containing an input ndarray
* @returns {number} arithmetic mean
Expand All @@ -42,15 +42,15 @@ var strided = require( '@stdlib/stats/strided/smean' ).ndarray;
* var xbuf = new Float32Array( [ 1.0, 3.0, 4.0, 2.0 ] );
* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
*
* var v = smean( [ x ] );
* var v = smeanors( [ x ] );
* // returns 2.5
*/
function smean( arrays ) {
function smeanors( 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 = smean;
module.exports = smeanors;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@stdlib/stats/base/ndarray/smean",
"name": "@stdlib/stats/base/ndarray/smeanors",
"version": "0.0.0",
"description": "Compute the arithmetic mean of a one-dimensional single-precision floating-point ndarray.",
"description": "Compute the arithmetic mean of a one-dimensional single-precision floating-point ndarray using ordinary recursive summation.",
"license": "Apache-2.0",
"author": {
"name": "The Stdlib Authors",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' );
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
var Float32Array = require( '@stdlib/array/float32' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var smean = require( './../lib' );
var smeanors = require( './../lib' );


// FUNCTIONS //
Expand All @@ -49,12 +49,12 @@ function vector( buffer, length, stride, offset ) {

tape( 'main export is a function', function test( t ) {
t.ok( true, __filename );
t.strictEqual( typeof smean, 'function', 'main export is a function' );
t.strictEqual( typeof smeanors, 'function', 'main export is a function' );
t.end();
});

tape( 'the function has an arity of 1', function test( t ) {
t.strictEqual( smean.length, 1, 'has expected arity' );
t.strictEqual( smeanors.length, 1, 'has expected arity' );
t.end();
});

Expand All @@ -63,47 +63,47 @@ tape( 'the function calculates the arithmetic mean of a one-dimensional ndarray'
var v;

x = new Float32Array( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] );
v = smean( [ vector( x, 6, 1, 0 ) ] );
v = smeanors( [ vector( x, 6, 1, 0 ) ] );
t.strictEqual( v, 0.5, 'returns expected value' );

x = new Float32Array( [ -4.0, -5.0 ] );
v = smean( [ vector( x, 2, 1, 0 ) ] );
v = smeanors( [ vector( x, 2, 1, 0 ) ] );
t.strictEqual( v, -4.5, 'returns expected value' );

x = new Float32Array( [ -0.0, 0.0, -0.0 ] );
v = smean( [ vector( x, 3, 1, 0 ) ] );
v = smeanors( [ vector( x, 3, 1, 0 ) ] );
t.strictEqual( isPositiveZero( v ), true, 'returns expected value' );

x = new Float32Array( [ NaN ] );
v = smean( [ vector( x, 1, 1, 0 ) ] );
v = smeanors( [ vector( x, 1, 1, 0 ) ] );
t.strictEqual( isnan( v ), true, 'returns expected value' );

x = new Float32Array( [ NaN, NaN ] );
v = smean( [ vector( x, 2, 1, 0 ) ] );
v = smeanors( [ vector( x, 2, 1, 0 ) ] );
t.strictEqual( isnan( v ), true, 'returns expected value' );

t.end();
});

tape( 'if provided an empty vector, the function returns `NaN`', function test( t ) {
tape( 'if provided an empty ndarray, the function returns `NaN`', function test( t ) {
var x;
var v;

x = new Float32Array( [] );

v = smean( [ vector( x, 0, 1, 0 ) ] );
v = smeanors( [ vector( x, 0, 1, 0 ) ] );
t.strictEqual( isnan( v ), true, 'returns expected value' );

t.end();
});

tape( 'if provided a vector containing a single element, the function returns that element', function test( t ) {
tape( 'if provided an ndarray containing a single element, the function returns that element', function test( t ) {
var x;
var v;

x = new Float32Array( [ 1.0 ] );

v = smean( [ vector( x, 1, 1, 0 ) ] );
v = smeanors( [ vector( x, 1, 1, 0 ) ] );
t.strictEqual( v, 1.0, 'returns expected value' );

t.end();
Expand All @@ -124,7 +124,7 @@ tape( 'the function supports one-dimensional ndarrays having non-unit strides',
2.0
]);

v = smean( [ vector( x, 4, 2, 0 ) ] );
v = smeanors( [ vector( x, 4, 2, 0 ) ] );

t.strictEqual( v, 1.25, 'returns expected value' );
t.end();
Expand All @@ -145,7 +145,7 @@ tape( 'the function supports one-dimensional ndarrays having negative strides',
2.0
]);

v = smean( [ vector( x, 4, -2, 6 ) ] );
v = smeanors( [ vector( x, 4, -2, 6 ) ] );

t.strictEqual( v, 1.25, 'returns expected value' );
t.end();
Expand All @@ -166,7 +166,7 @@ tape( 'the function supports one-dimensional ndarrays having non-zero offsets',
4.0 // 3
]);

v = smean( [ vector( x, 4, 2, 1 ) ] );
v = smeanors( [ vector( x, 4, 2, 1 ) ] );
t.strictEqual( v, 1.25, 'returns expected value' );

t.end();
Expand Down