Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

anyBy

Test whether at least one element along one or more ndarray dimensions passes a test implemented by a predicate function.

Usage

var anyBy = require( '@stdlib/ndarray/any-by' );

anyBy( x[, options], predicate[, thisArg] )

Tests whether at least one element along one or more ndarray dimensions passes a test implemented by a predicate function.

var array = require( '@stdlib/ndarray/array' );

function isPositive( value ) {
    return value > 0.0;
}

// Create an input ndarray:
var x = array( [ [ 1.0, -2.0 ], [ 3.0, -4.0 ] ] );

// Test whether at least one element is positive:
var out = anyBy( x, isPositive );
// returns <ndarray>[ true ]

The function accepts the following arguments:

  • x: input ndarray.
  • options: function options (optional).
  • predicate: predicate function.
  • thisArg: predicate function execution context (optional).

The function accepts the following options:

  • dims: list of dimensions over which to perform a reduction.
  • keepdims: boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions. Default: false.

By default, the function performs a reduction over all elements in a provided ndarray. To reduce specific dimensions, set the dims option.

var array = require( '@stdlib/ndarray/array' );

function isPositive( value ) {
    return value > 0.0;
}

// Create an input ndarray:
var x = array( [ [ 1.0, 2.0 ], [ -3.0, -4.0 ] ] );
// returns <ndarray>

var opts = {
    'dims': [ 1 ]
};

// Perform reduction:
var out = anyBy( x, opts, isPositive );
// returns <ndarray>[ true, false ]

By default, the function returns an ndarray having a shape matching only the non-reduced dimensions of the input ndarray (i.e., the reduced dimensions are dropped). To include the reduced dimensions as singleton dimensions in the output ndarray, set the keepdims option to true.

var array = require( '@stdlib/ndarray/array' );

function isPositive( value ) {
    return value > 0.0;
}

// Create an input ndarray:
var x = array( [ [ 1.0, 2.0 ], [ -3.0, -4.0 ] ] );

var opts = {
    'keepdims': true
};

// Perform reduction:
var out = anyBy( x, opts, isPositive );
// returns <ndarray>[ [ true ] ]

To set the function execution context, provide a thisArg.

var array = require( '@stdlib/ndarray/array' );

function isPositive( value ) {
    this.count += 1;
    return value > 0.0;
}

// Create an input ndarray:
var x = array( [ [ -1.0, -2.0 ], [ -3.0, 4.0 ] ] );
// returns <ndarray>

// Create a context object:
var ctx = {
    'count': 0
};

// Perform reduction:
var out = anyBy( x, isPositive, ctx );
// returns <ndarray>[ true ]

var count = ctx.count;
// returns 4

anyBy.assign( x, y[, options], predicate[, thisArg] )

Tests whether at least one element along one or more ndarray dimensions passes a test implemented by a predicate function and assigns the result to a provided output ndarray.

var array = require( '@stdlib/ndarray/array' );
var empty = require( '@stdlib/ndarray/empty' );

function isPositive( value ) {
    return value > 0.0;
}

// Create an input ndarray:
var x = array( [ [ 1.0, -2.0 ], [ 3.0, -4.0 ] ] );

// Create an output ndarray:
var y = empty( [], {
    'dtype': 'bool'
});

// Perform reduction:
var out = anyBy.assign( x, y, isPositive );
// returns <ndarray>[ true ]

var bool = ( out === y );
// returns true

The function accepts the following arguments:

  • x: input ndarray.
  • y: output ndarray. The output ndarray must have a shape matching the non-reduced dimensions of the input ndarray.
  • options: function options (optional).
  • predicate: predicate function.
  • thisArg: predicate function execution context (optional).

The function accepts the following options:

  • dims: list of dimensions over which to perform a reduction.

By default, the function performs a reduction over all elements in a provided ndarray. To reduce specific dimensions, set the dims option.

var array = require( '@stdlib/ndarray/array' );
var empty = require( '@stdlib/ndarray/empty' );

function predicate( value ) {
    return value > 0.0;
}

// Create an input ndarray:
var x = array( [ [ 1.0, 2.0 ], [ -3.0, -4.0 ] ] );
// returns <ndarray>

// Create an output ndarray:
var y = empty( [ 2 ], {
    'dtype': 'bool'
});

var opts = {
    'dims': [ 1 ]
};

// Perform reduction:
var out = anyBy.assign( x, y, opts, predicate );
// returns <ndarray>[ true, false ]

var bool = ( out === y );
// returns true

Notes

  • The predicate function is provided the following arguments:

    • value: current array element.
    • indices: current array element indices.
    • arr: the input ndarray.

Examples

var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
var isEven = require( '@stdlib/assert/is-even' ).isPrimitive;
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var fillBy = require( '@stdlib/ndarray/fill-by' );
var zeros = require( '@stdlib/ndarray/zeros' );
var anyBy = require( '@stdlib/ndarray/any-by' );

var x = zeros( [ 2, 4, 5 ], {
    'dtype': 'float64'
});
x = fillBy( x, discreteUniform( 0, 10 ) );
console.log( ndarray2array( x ) );

var y = anyBy( x, isEven );
console.log( y.get() );