Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Feb 20, 2024
1 parent c559bca commit e7b9064
Show file tree
Hide file tree
Showing 14 changed files with 1,007 additions and 220 deletions.
40 changes: 35 additions & 5 deletions base/mskfilter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ limitations under the License.

# mskfilter

> Return a new array by applying a mask to a provided input array.
> Apply a mask to a provided input array.
<section class="usage">

Expand All @@ -41,6 +41,38 @@ var y = mskfilter( x, [ 0, 1, 0, 1 ] );
// returns [ 2, 4 ]
```

The function supports the following parameters:

- **x**: input array.
- **mask**: mask array.

The function **always** returns a new "generic" array.

#### mskfilter.assign( x, mask, out, stride, offset )

Applies a mask to a provided input array and assigns unmasked values to elements in a provided output array.

```javascript
var x = [ 1, 2, 3, 4 ];
var mask = [ 0, 1, 0, 1 ];

var out = [ 0, 0, 0, 0 ];

var arr = mskfilter.assign( x, mask, out, -2, out.length-1 );
// returns [ 0, 4, 0, 2 ]

var bool = ( arr === out );
// returns true
```

The function supports the following parameters:

- **x**: input array.
- **mask**: mask array.
- **out**: output array.
- **stride**: output array stride.
- **offset**: output array offset.

</section>

<!-- /.usage -->
Expand All @@ -49,7 +81,6 @@ var y = mskfilter( x, [ 0, 1, 0, 1 ] );

## Notes

- The function **always** returns a new "generic" array.
- If a `mask` array element is truthy, the corresponding element in `x` is **included** in the output array; otherwise, the corresponding element in `x` is "masked" and thus **excluded** from the output array.

</section>
Expand All @@ -69,17 +100,16 @@ var mskfilter = require( '@stdlib/array/base/mskfilter' );

// Generate a linearly spaced array:
var x = zeroTo( 20 );
console.log( x );

// Generate a random mask:
var mask = bernoulli( x.length, 0.5, {
'dtype': 'generic'
});
console.log( mask );

// Filter an array using the mask:
var y = mskfilter( x, mask );

console.log( x );
console.log( mask );
console.log( y );
```

Expand Down
107 changes: 107 additions & 0 deletions base/mskfilter/benchmark/benchmark.assign.length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 pow = require( '@stdlib/math/base/special/pow' );
var bernoulli = require( '@stdlib/random/array/bernoulli' );
var zeros = require( './../../../zeros' );
var ones = require( './../../../ones' );
var isArray = require( '@stdlib/assert/is-array' );
var isnan = require( '@stdlib/assert/is-nan' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var mskfilter = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var mask;
var out;
var x;

x = ones( len, 'generic' );
mask = bernoulli( len, 0.5, {
'dtype': 'generic'
});
out = zeros( len, 'generic' );

return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var v;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = mskfilter.assign( x, mask, out, 1, 0 );
if ( typeof v !== 'object' ) {
b.fail( 'should return an array' );
}
}
b.toc();
if ( !isArray( v ) || isnan( v[ i%len ] ) ) {
b.fail( 'should return an array' );
}
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:len='+len, f );
}
}

main();
41 changes: 41 additions & 0 deletions base/mskfilter/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,47 @@
> var y = {{alias}}( x, [ 0, 1, 0, 1 ] )
[ 2, 4 ]


{{alias}}.assign( x, mask, out, stride, offset )
Applies a mask to a provided input array and assigns unmasked values to
elements in a provided output array.

If a mask array element is truthy, the corresponding element in `x` is
included in the output array; otherwise, the corresponding element in `x` is
"masked" and thus excluded from the output array.

Parameters
----------
x: ArrayLikeObject
Input array.

mask: ArrayLikeObject
Mask array.

out: ArrayLikeObject
Output array.

stride: integer
Output array stride.

offset: integer
Output array offset.

Returns
-------
out: ArrayLikeObject
Output array.

Examples
--------
> var x = [ 1, 2, 3, 4 ];
> var m = [ 0, 1, 0, 1 ];
> var out = [ 0, 0, 0, 0 ];
> var arr = {{alias}}.assign( x, m, out, 2, 0 )
[ 2, 0, 4, 0 ]
> var bool = ( arr === out )
true

See Also
--------

5 changes: 2 additions & 3 deletions base/mskfilter/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ var mskfilter = require( './../lib' );

// Generate a linearly spaced array:
var x = zeroTo( 20 );
console.log( x );

// Generate a random mask:
var mask = bernoulli( x.length, 0.5, {
'dtype': 'generic'
});
console.log( mask );

// Filter an array using the mask:
var y = mskfilter( x, mask );

console.log( x );
console.log( mask );
console.log( y );
Loading

0 comments on commit e7b9064

Please sign in to comment.