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 Jan 18, 2024
1 parent 4fdff88 commit a17c4ea
Show file tree
Hide file tree
Showing 14 changed files with 711 additions and 5 deletions.
9 changes: 9 additions & 0 deletions base/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,15 @@ setReadOnly( ns, 'map5d', require( './../../base/map5d' ) );
*/
setReadOnly( ns, 'mskbinary2d', require( './../../base/mskbinary2d' ) );

/**
* @name mskfilter
* @memberof ns
* @readonly
* @type {Function}
* @see {@link module:@stdlib/array/base/mskfilter}
*/
setReadOnly( ns, 'mskfilter', require( './../../base/mskfilter' ) );

/**
* @name mskunary2d
* @memberof ns
Expand Down
104 changes: 104 additions & 0 deletions base/mskfilter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<!--
@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.
-->

# mskfilter

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

## Usage

```javascript
var mskfilter = require( '@stdlib/array/base/mskfilter' );
```

#### mskfilter( x, mask )

Returns a new array by applying a mask to a provided input array.

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

var y = mskfilter( x, [ 0, 1, 0, 1 ] );
// returns [ 2, 4 ]
```

</section>

<!-- /.usage -->

<section class="notes">

## 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>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var zeroTo = require( '@stdlib/array/base/zero-to' );
var bernoulli = require( '@stdlib/random/array/bernoulli' );
var mskfilter = require( '@stdlib/array/base/mskfilter' );

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

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

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

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

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

</section>

<!-- /.links -->
55 changes: 55 additions & 0 deletions base/mskfilter/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @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 isArray = require( '@stdlib/assert/is-array' );
var zeroTo = require( './../../../base/zero-to' );
var ones = require( './../../../base/ones' );
var pkg = require( './../package.json' ).name;
var mskfilter = require( './../lib' );


// MAIN //

bench( pkg+'::copy:len=100', function benchmark( b ) {
var x;
var y;
var i;
var v;

x = zeroTo( 100 );
y = ones( x.length );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = mskfilter( x, y );
if ( typeof v !== 'object' ) {
b.fail( 'should return an array' );
}
}
b.toc();
if ( !isArray( v ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});
97 changes: 97 additions & 0 deletions base/mskfilter/benchmark/benchmark.length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* @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 zeroTo = require( './../../../base/zero-to' );
var ones = require( './../../../base/ones' );
var isArray = require( '@stdlib/assert/is-array' );
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 x = zeroTo( len );
var y = ones( len );
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( x, y );
if ( typeof v !== 'object' ) {
b.fail( 'should return an array' );
}
}
b.toc();
if ( !isArray( v ) ) {
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+':len='+len, f );
}
}

main();
30 changes: 30 additions & 0 deletions base/mskfilter/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

{{alias}}( x, mask )
Returns a new array by applying a mask to a provided input 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.

Returns
-------
out: Array
Output array.

Examples
--------
> var x = [ 1, 2, 3, 4 ];
> var y = {{alias}}( x, [ 0, 1, 0, 1 ] )
[ 2, 4 ]

See Also
--------

43 changes: 43 additions & 0 deletions base/mskfilter/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* @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.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { Collection } from '@stdlib/types/array';

/**
* Returns a new array by applying a mask to a provided input array.
*
* @param x - input array
* @param mask - mask array
* @returns output array
*
* @example
* var x = [ 1, 2, 3, 4 ];
*
* var y = mskfilter( x, [ 0, 1, 0, 1 ] );
* // returns [ 2, 4 ]
*/
declare function mskfilter<T = unknown>( x: Collection<T>, mask: Collection ): Array<T>;


// EXPORTS //

export = mskfilter;
Loading

0 comments on commit a17c4ea

Please sign in to comment.