diff --git a/convert-same/README.md b/convert-same/README.md index 6a11c40a..cf46b54e 100644 --- a/convert-same/README.md +++ b/convert-same/README.md @@ -18,7 +18,7 @@ limitations under the License. --> -# Convert +# convertSame > Convert an array to the same data type as a second input array. @@ -37,10 +37,10 @@ limitations under the License. ## Usage ```javascript -var convertArraySame = require( '@stdlib/array/convert-same' ); +var convertSame = require( '@stdlib/array/convert-same' ); ``` -#### convertArraySame( x, y ) +#### convertSame( x, y ) Converts an array to the same data type as a second input array. @@ -50,7 +50,7 @@ var Float32Array = require( '@stdlib/array/float32' ); var x = [ 1.0, 2.0, 3.0 ]; var y = new Float32Array( 0 ); -var out = convertArraySame( x, y ); +var out = convertSame( x, y ); // returns [ 1.0, 2.0, 3.0 ] ``` @@ -96,7 +96,7 @@ var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; var filledarrayBy = require( '@stdlib/array/filled-by' ); var dtypes = require( '@stdlib/array/dtypes' ); var ctors = require( '@stdlib/array/ctors' ); -var convertArraySame = require( '@stdlib/array/convert-same' ); +var convertSame = require( '@stdlib/array/convert-same' ); // Create a generic array: var arr = filledarrayBy( 5, 'generic', discreteUniform( -100, 100 ) ); @@ -108,7 +108,7 @@ var DTYPES = dtypes(); var out; var i; for ( i = 0; i < DTYPES.length; i++ ) { - out = convertArraySame( arr, new ( ctors( DTYPES[ i ] ) )( 0 ) ); + out = convertSame( arr, new ( ctors( DTYPES[ i ] ) )( 0 ) ); console.log( out ); } ``` diff --git a/convert-same/examples/index.js b/convert-same/examples/index.js index bf015070..7d24772b 100644 --- a/convert-same/examples/index.js +++ b/convert-same/examples/index.js @@ -22,7 +22,7 @@ var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; var filledarrayBy = require( './../../filled-by' ); var dtypes = require( './../../dtypes' ); var ctors = require( './../../ctors' ); -var convertArraySame = require( './../lib' ); +var convertSame = require( './../lib' ); // Create a generic array: var arr = filledarrayBy( 5, 'generic', discreteUniform( -100, 100 ) ); @@ -34,6 +34,6 @@ var DTYPES = dtypes(); var out; var i; for ( i = 0; i < DTYPES.length; i++ ) { - out = convertArraySame( arr, new ( ctors( DTYPES[ i ] ) )( 0 ) ); + out = convertSame( arr, new ( ctors( DTYPES[ i ] ) )( 0 ) ); console.log( out ); } diff --git a/convert/README.md b/convert/README.md index ac31767f..060c5585 100644 --- a/convert/README.md +++ b/convert/README.md @@ -18,7 +18,7 @@ limitations under the License. --> -# Convert +# convert > Convert an array to an array of a different data type. @@ -37,16 +37,16 @@ limitations under the License. ## Usage ```javascript -var convertArray = require( '@stdlib/array/convert' ); +var convert = require( '@stdlib/array/convert' ); ``` -#### convertArray( arr, dtype ) +#### convert( arr, dtype ) Converts an array to an array of a different data type. ```javascript var arr = [ 1.0, 2.0, 3.0 ]; -var out = convertArray( arr, 'float32' ); +var out = convert( arr, 'float32' ); // returns [ 1.0, 2.0, 3.0 ] ``` @@ -89,7 +89,7 @@ The function supports the following data types: var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; var filledarrayBy = require( '@stdlib/array/filled-by' ); var dtypes = require( '@stdlib/array/dtypes' ); -var convertArray = require( '@stdlib/array/convert' ); +var convert = require( '@stdlib/array/convert' ); // Create a generic array: var arr = filledarrayBy( 5, 'generic', discreteUniform( -100, 100 ) ); @@ -101,7 +101,7 @@ var DTYPES = dtypes(); var out; var i; for ( i = 0; i < DTYPES.length; i++ ) { - out = convertArray( arr, DTYPES[ i ] ); + out = convert( arr, DTYPES[ i ] ); console.log( out ); } ``` diff --git a/convert/examples/index.js b/convert/examples/index.js index bba6e15c..bd7d64a6 100644 --- a/convert/examples/index.js +++ b/convert/examples/index.js @@ -21,7 +21,7 @@ var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; var filledarrayBy = require( './../../filled-by' ); var dtypes = require( './../../dtypes' ); -var convertArray = require( './../lib' ); +var convert = require( './../lib' ); // Create a generic array: var arr = filledarrayBy( 5, 'generic', discreteUniform( -100, 100 ) ); @@ -33,6 +33,6 @@ var DTYPES = dtypes(); var out; var i; for ( i = 0; i < DTYPES.length; i++ ) { - out = convertArray( arr, DTYPES[ i ] ); + out = convert( arr, DTYPES[ i ] ); console.log( out ); } diff --git a/lib/index.js b/lib/index.js index 03af8058..d4afb79d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -59,31 +59,31 @@ setReadOnly( ns, 'base', require( './../base' ) ); setReadOnly( ns, 'ArrayBuffer', require( './../buffer' ) ); /** -* @name acartesianPower +* @name cartesianPower * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/cartesian-power} */ -setReadOnly( ns, 'acartesianPower', require( './../cartesian-power' ) ); +setReadOnly( ns, 'cartesianPower', require( './../cartesian-power' ) ); /** -* @name acartesianProduct +* @name cartesianProduct * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/cartesian-product} */ -setReadOnly( ns, 'acartesianProduct', require( './../cartesian-product' ) ); +setReadOnly( ns, 'cartesianProduct', require( './../cartesian-product' ) ); /** -* @name acartesianSquare +* @name cartesianSquare * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/cartesian-square} */ -setReadOnly( ns, 'acartesianSquare', require( './../cartesian-square' ) ); +setReadOnly( ns, 'cartesianSquare', require( './../cartesian-square' ) ); /** * @name Complex64Array @@ -104,31 +104,31 @@ setReadOnly( ns, 'Complex64Array', require( './../complex64' ) ); setReadOnly( ns, 'Complex128Array', require( './../complex128' ) ); /** -* @name convertArray +* @name convert * @memberof ns * @readonly * @type {Function} * @see {@link module:@stdlib/array/convert} */ -setReadOnly( ns, 'convertArray', require( './../convert' ) ); +setReadOnly( ns, 'convert', require( './../convert' ) ); /** -* @name convertArraySame +* @name convertSame * @memberof ns * @readonly * @type {Function} * @see {@link module:@stdlib/array/convert-same} */ -setReadOnly( ns, 'convertArraySame', require( './../convert-same' ) ); +setReadOnly( ns, 'convertSame', require( './../convert-same' ) ); /** -* @name arrayCtors +* @name ctors * @memberof ns * @readonly * @type {Function} * @see {@link module:@stdlib/array/ctors} */ -setReadOnly( ns, 'arrayCtors', require( './../ctors' ) ); +setReadOnly( ns, 'ctors', require( './../ctors' ) ); /** * @name DataView @@ -149,67 +149,67 @@ setReadOnly( ns, 'DataView', require( './../dataview' ) ); setReadOnly( ns, 'datespace', require( './../datespace' ) ); /** -* @name arrayDefaults +* @name defaults * @memberof ns * @readonly * @type {Function} * @see {@link module:@stdlib/array/defaults} */ -setReadOnly( ns, 'arrayDefaults', require( './../defaults' ) ); +setReadOnly( ns, 'defaults', require( './../defaults' ) ); /** -* @name arrayDataType +* @name dtype * @memberof ns * @readonly * @type {Function} * @see {@link module:@stdlib/array/dtype} */ -setReadOnly( ns, 'arrayDataType', require( './../dtype' ) ); +setReadOnly( ns, 'dtype', require( './../dtype' ) ); /** -* @name arrayDataTypes +* @name dtypes * @memberof ns * @readonly * @type {Function} * @see {@link module:@stdlib/array/dtypes} */ -setReadOnly( ns, 'arrayDataTypes', require( './../dtypes' ) ); +setReadOnly( ns, 'dtypes', require( './../dtypes' ) ); /** -* @name aempty +* @name empty * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/empty} */ -setReadOnly( ns, 'aempty', require( './../empty' ) ); +setReadOnly( ns, 'empty', require( './../empty' ) ); /** -* @name aemptyLike +* @name emptyLike * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/empty-like} */ -setReadOnly( ns, 'aemptyLike', require( './../empty-like' ) ); +setReadOnly( ns, 'emptyLike', require( './../empty-like' ) ); /** -* @name filledarray +* @name filled * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/filled} */ -setReadOnly( ns, 'filledarray', require( './../filled' ) ); +setReadOnly( ns, 'filled', require( './../filled' ) ); /** -* @name filledarrayBy +* @name filledBy * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/filled-by} */ -setReadOnly( ns, 'filledarrayBy', require( './../filled-by' ) ); +setReadOnly( ns, 'filledBy', require( './../filled-by' ) ); /** * @name Float32Array @@ -248,22 +248,22 @@ setReadOnly( ns, 'iterator2array', require( './../from-iterator' ) ); setReadOnly( ns, 'scalar2array', require( './../from-scalar' ) ); /** -* @name afull +* @name full * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/full} */ -setReadOnly( ns, 'afull', require( './../full' ) ); +setReadOnly( ns, 'full', require( './../full' ) ); /** -* @name afullLike +* @name fullLike * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/full-like} */ -setReadOnly( ns, 'afullLike', require( './../full-like' ) ); +setReadOnly( ns, 'fullLike', require( './../full-like' ) ); /** * @name incrspace @@ -329,85 +329,94 @@ setReadOnly( ns, 'linspace', require( './../linspace' ) ); setReadOnly( ns, 'logspace', require( './../logspace' ) ); /** -* @name arrayMinDataType +* @name minDataType * @memberof ns * @readonly * @type {Function} * @see {@link module:@stdlib/array/min-dtype} */ -setReadOnly( ns, 'arrayMinDataType', require( './../min-dtype' ) ); +setReadOnly( ns, 'minDataType', require( './../min-dtype' ) ); /** -* @name arrayMostlySafeCasts +* @name mostlySafeCasts * @memberof ns * @readonly * @type {Function} * @see {@link module:@stdlib/array/mostly-safe-casts} */ -setReadOnly( ns, 'arrayMostlySafeCasts', require( './../mostly-safe-casts' ) ); +setReadOnly( ns, 'mostlySafeCasts', require( './../mostly-safe-casts' ) ); /** -* @name anans +* @name mskfilter +* @memberof ns +* @readonly +* @constructor +* @see {@link module:@stdlib/array/mskfilter} +*/ +setReadOnly( ns, 'mskfilter', require( './../mskfilter' ) ); + +/** +* @name nans * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/nans} */ -setReadOnly( ns, 'anans', require( './../nans' ) ); +setReadOnly( ns, 'nans', require( './../nans' ) ); /** -* @name anansLike +* @name nansLike * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/nans-like} */ -setReadOnly( ns, 'anansLike', require( './../nans-like' ) ); +setReadOnly( ns, 'nansLike', require( './../nans-like' ) ); /** -* @name arrayNextDataType +* @name nextDataType * @memberof ns * @readonly * @type {Function} * @see {@link module:@stdlib/array/next-dtype} */ -setReadOnly( ns, 'arrayNextDataType', require( './../next-dtype' ) ); +setReadOnly( ns, 'nextDataType', require( './../next-dtype' ) ); /** -* @name aoneTo +* @name oneTo * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/one-to} */ -setReadOnly( ns, 'aoneTo', require( './../one-to' ) ); +setReadOnly( ns, 'oneTo', require( './../one-to' ) ); /** -* @name aoneToLike +* @name oneToLike * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/one-to-like} */ -setReadOnly( ns, 'aoneToLike', require( './../one-to-like' ) ); +setReadOnly( ns, 'oneToLike', require( './../one-to-like' ) ); /** -* @name aones +* @name ones * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/ones} */ -setReadOnly( ns, 'aones', require( './../ones' ) ); +setReadOnly( ns, 'ones', require( './../ones' ) ); /** -* @name aonesLike +* @name onesLike * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/ones-like} */ -setReadOnly( ns, 'aonesLike', require( './../ones-like' ) ); +setReadOnly( ns, 'onesLike', require( './../ones-like' ) ); /** * @name typedarraypool @@ -419,49 +428,49 @@ setReadOnly( ns, 'aonesLike', require( './../ones-like' ) ); setReadOnly( ns, 'typedarraypool', require( './../pool' ) ); /** -* @name arrayPromotionRules +* @name promotionRules * @memberof ns * @readonly * @type {Function} * @see {@link module:@stdlib/array/promotion-rules} */ -setReadOnly( ns, 'arrayPromotionRules', require( './../promotion-rules' ) ); +setReadOnly( ns, 'promotionRules', require( './../promotion-rules' ) ); /** -* @name reviveTypedArray +* @name typedarrayReviver * @memberof ns * @readonly * @type {Function} * @see {@link module:@stdlib/array/reviver} */ -setReadOnly( ns, 'reviveTypedArray', require( './../reviver' ) ); +setReadOnly( ns, 'typedarrayReviver', require( './../reviver' ) ); /** -* @name arraySafeCasts +* @name safeCasts * @memberof ns * @readonly * @type {Function} * @see {@link module:@stdlib/array/safe-casts} */ -setReadOnly( ns, 'arraySafeCasts', require( './../safe-casts' ) ); +setReadOnly( ns, 'safeCasts', require( './../safe-casts' ) ); /** -* @name arraySameKindCasts +* @name sameKindCasts * @memberof ns * @readonly * @type {Function} * @see {@link module:@stdlib/array/same-kind-casts} */ -setReadOnly( ns, 'arraySameKindCasts', require( './../same-kind-casts' ) ); +setReadOnly( ns, 'sameKindCasts', require( './../same-kind-casts' ) ); /** -* @name arrayShape +* @name shape * @memberof ns * @readonly * @type {Function} * @see {@link module:@stdlib/array/shape} */ -setReadOnly( ns, 'arrayShape', require( './../shape' ) ); +setReadOnly( ns, 'shape', require( './../shape' ) ); /** * @name SharedArrayBuffer @@ -473,22 +482,22 @@ setReadOnly( ns, 'arrayShape', require( './../shape' ) ); setReadOnly( ns, 'SharedArrayBuffer', require( './../shared-buffer' ) ); /** -* @name aslice +* @name slice * @memberof ns * @readonly * @type {Function} * @see {@link module:@stdlib/array/slice} */ -setReadOnly( ns, 'aslice', require( './../slice' ) ); +setReadOnly( ns, 'slice', require( './../slice' ) ); /** -* @name atake +* @name take * @memberof ns * @readonly * @type {Function} * @see {@link module:@stdlib/array/take} */ -setReadOnly( ns, 'atake', require( './../take' ) ); +setReadOnly( ns, 'take', require( './../take' ) ); /** * @name circarray2iterator @@ -788,40 +797,40 @@ setReadOnly( ns, 'Uint16Array', require( './../uint16' ) ); setReadOnly( ns, 'Uint32Array', require( './../uint32' ) ); /** -* @name azeroTo +* @name zeroTo * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/zero-to} */ -setReadOnly( ns, 'azeroTo', require( './../zero-to' ) ); +setReadOnly( ns, 'zeroTo', require( './../zero-to' ) ); /** -* @name azeroToLike +* @name zeroToLike * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/zero-to-like} */ -setReadOnly( ns, 'azeroToLike', require( './../zero-to-like' ) ); +setReadOnly( ns, 'zeroToLike', require( './../zero-to-like' ) ); /** -* @name azeros +* @name zeros * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/zeros} */ -setReadOnly( ns, 'azeros', require( './../zeros' ) ); +setReadOnly( ns, 'zeros', require( './../zeros' ) ); /** -* @name azerosLike +* @name zerosLike * @memberof ns * @readonly * @constructor * @see {@link module:@stdlib/array/zeros-like} */ -setReadOnly( ns, 'azerosLike', require( './../zeros-like' ) ); +setReadOnly( ns, 'zerosLike', require( './../zeros-like' ) ); /** * @name constants diff --git a/mskfilter/README.md b/mskfilter/README.md new file mode 100644 index 00000000..38d44af3 --- /dev/null +++ b/mskfilter/README.md @@ -0,0 +1,134 @@ + + +# mskfilter + +> Apply a mask to a provided input array. + +
+ +## 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 ] +``` + +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. + +
+ + + +
+ +## Notes + +- 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. + +
+ + + +
+ +## Examples + + + +```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 ); +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( y ); +``` + +
+ + + + + + + + + + + + + + diff --git a/mskfilter/benchmark/benchmark.js b/mskfilter/benchmark/benchmark.js new file mode 100644 index 00000000..f16ee9b5 --- /dev/null +++ b/mskfilter/benchmark/benchmark.js @@ -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(); +}); diff --git a/mskfilter/benchmark/benchmark.length.js b/mskfilter/benchmark/benchmark.length.js new file mode 100644 index 00000000..8d085694 --- /dev/null +++ b/mskfilter/benchmark/benchmark.length.js @@ -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(); diff --git a/mskfilter/docs/repl.txt b/mskfilter/docs/repl.txt new file mode 100644 index 00000000..07adc4dd --- /dev/null +++ b/mskfilter/docs/repl.txt @@ -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: Array|TypedArray|Object + Input array. + + mask: Array|TypedArray|Object + Mask array. + + Returns + ------- + out: Array|TypedArray|Object + Output array. + + Examples + -------- + > var x = [ 1, 2, 3, 4 ]; + > var y = {{alias}}( x, [ 0, 1, 0, 1 ] ) + [ 2, 4 ] + + See Also + -------- + diff --git a/mskfilter/docs/types/index.d.ts b/mskfilter/docs/types/index.d.ts new file mode 100644 index 00000000..2436ebe2 --- /dev/null +++ b/mskfilter/docs/types/index.d.ts @@ -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 + +/// + +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( x: Collection, mask: Collection ): Collection; // FIXME: use type maps once associated PR merged + + +// EXPORTS // + +export = mskfilter; diff --git a/mskfilter/docs/types/test.ts b/mskfilter/docs/types/test.ts new file mode 100644 index 00000000..a36fcb7c --- /dev/null +++ b/mskfilter/docs/types/test.ts @@ -0,0 +1,57 @@ +/* +* @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. +*/ + +import mskfilter = require( './index' ); + + +// TESTS // + +// The function returns an array... +{ + mskfilter( [ 1, 2, 3, 4 ], [ 0, 0 ] ); // $ExpectType Collection + mskfilter( [ 1, 2, 3, 4 ], [ 0, 0 ] ); // $ExpectType Collection + mskfilter( [ 1, 2, 3, 4 ], [ 0, 0 ] ); // $ExpectType Collection + mskfilter( [ '1', '2', '3', '4' ], [ 0, 0 ] ); // $ExpectType Collection +} + +// The compiler throws an error if the function is provided a first argument which is not an array-like object... +{ + mskfilter( 1, [ 0, 0 ] ); // $ExpectError + mskfilter( true, [ 0, 0 ] ); // $ExpectError + mskfilter( false, [ 0, 0 ] ); // $ExpectError + mskfilter( null, [ 0, 0 ] ); // $ExpectError + mskfilter( void 0, [ 0, 0 ] ); // $ExpectError + mskfilter( {}, [ 0, 0 ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... +{ + mskfilter( [], 1 ); // $ExpectError + mskfilter( [], true ); // $ExpectError + mskfilter( [], false ); // $ExpectError + mskfilter( [], null ); // $ExpectError + mskfilter( [], void 0 ); // $ExpectError + mskfilter( [], {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + mskfilter(); // $ExpectError + mskfilter( [] ); // $ExpectError + mskfilter( [], [], [] ); // $ExpectError +} diff --git a/mskfilter/examples/index.js b/mskfilter/examples/index.js new file mode 100644 index 00000000..7918b134 --- /dev/null +++ b/mskfilter/examples/index.js @@ -0,0 +1,37 @@ +/** +* @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'; + +var zeroTo = require( './../../zero-to' ); +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var mskfilter = require( './../lib' ); + +// Generate a linearly spaced array: +var x = zeroTo( 20, 'generic' ); +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( y ); diff --git a/mskfilter/lib/index.js b/mskfilter/lib/index.js new file mode 100644 index 00000000..c9d7fe59 --- /dev/null +++ b/mskfilter/lib/index.js @@ -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. +*/ + +'use strict'; + +/** +* Apply a mask to a provided input array. +* +* @module @stdlib/array/mskfilter +* +* @example +* var mskfilter = require( '@stdlib/array/mskfilter' ); +* +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* var y = mskfilter( x, mask ); +* // returns [ 2, 4 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/mskfilter/lib/main.js b/mskfilter/lib/main.js new file mode 100644 index 00000000..08e11da5 --- /dev/null +++ b/mskfilter/lib/main.js @@ -0,0 +1,80 @@ +/** +* @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 isCollection = require( '@stdlib/assert/is-collection' ); +var base = require( './../../base/mskfilter' ); +var countTruthy = require( './../../base/count-truthy' ); +var zeros = require( './../../zeros' ); +var dtype = require( './../../dtype' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a new array by applying a mask to a provided input array. +* +* @param {Collection} x - input array +* @param {Collection} mask - mask array +* @throws {TypeError} first argument must be a collection +* @throws {TypeError} second argument must be a collection +* @throws {Error} must provide collections of equal length +* @returns {Collection} output array +* +* @example +* var x = [ 1, 2, 3, 4 ]; +* var mask = [ 0, 1, 0, 1 ]; +* +* var y = mskfilter( x, mask ); +* // returns [ 2, 4 ] +* +* @example +* var Int32Array = require( '@stdlib/array/int32' ); +* +* var x = new Int32Array( [ 1, 2, 3, 4 ] ); +* var mask = [ 0, 1, 0, 1 ]; +* +* var y = mskfilter( x, mask ); +* // returns [ 2, 4 ] +*/ +function mskfilter( x, mask ) { + var dt; + if ( !isCollection( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an array-like object. Value: `%s`.', x ) ); + } + if ( !isCollection( mask ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an array-like object. Value: `%s`.', mask ) ); + } + if ( x.length !== mask.length ) { + throw new Error( format( 'invalid arguments. Must provide equal length array-like objects.' ) ); + } + dt = dtype( x ); + if ( dt === 'generic' || dt === null ) { + return base( x, mask ); + } + return base.assign( x, mask, zeros( countTruthy( mask ), dt ), 1, 0 ); +} + + +// EXPORTS // + +module.exports = mskfilter; diff --git a/mskfilter/package.json b/mskfilter/package.json new file mode 100644 index 00000000..90ea1324 --- /dev/null +++ b/mskfilter/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/array/mskfilter", + "version": "0.0.0", + "description": "Apply a mask to a provided input array.", + "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", + "utilities", + "utils", + "generic", + "array", + "take", + "extract", + "copy", + "index", + "mask", + "filter", + "reject" + ] +} diff --git a/mskfilter/test/test.js b/mskfilter/test/test.js new file mode 100644 index 00000000..7d2935b0 --- /dev/null +++ b/mskfilter/test/test.js @@ -0,0 +1,277 @@ +/** +* @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 tape = require( 'tape' ); +var toAccessorArray = require( './../../base/to-accessor-array' ); +var Complex64Array = require( './../../complex64' ); +var Int32Array = require( './../../int32' ); +var isSameComplex64Array = require( '@stdlib/assert/is-same-complex64array' ); +var isArray = require( '@stdlib/assert/is-array' ); +var isInt32Array = require( '@stdlib/assert/is-int32array' ); +var zeros = require( './../../zeros' ); +var mskfilter = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof mskfilter, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not a collection', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mskfilter( value, [] ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a collection', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mskfilter( [], value ); + }; + } +}); + +tape( 'the function throws an error if provided collections of unequal length', function test( t ) { + var values; + var i; + + values = [ + [], + [ 1 ], + [ 1, 2 ], + [ 1, 2, 3 ], + [ 1, 2, 3, 4 ], + [ 1, 2, 3, 4, 5, 6 ], + [ 1, 2, 3, 4, 5, 6, 7 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + mskfilter( value, zeros( 5, 'generic' ) ); + }; + } +}); + +tape( 'the function filters array elements (generic)', function test( t ) { + var expected; + var actual; + var mask; + var x; + + x = [ 1, 2, 3, 4 ]; + + mask = [ 0, 1, 0, 1 ]; + actual = mskfilter( x, mask ); + expected = [ 2, 4 ]; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 0, 0, 0 ]; + actual = mskfilter( x, mask ); + expected = []; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 0, 0, 1 ]; + actual = mskfilter( x, mask ); + expected = [ 4 ]; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 1, 1, 1 ]; + actual = mskfilter( x, mask ); + expected = [ 1, 2, 3, 4 ]; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function filters array elements (accessors)', function test( t ) { + var expected; + var actual; + var mask; + var x; + + x = toAccessorArray( [ 1, 2, 3, 4 ] ); + + mask = toAccessorArray( [ 0, 1, 0, 1 ] ); + actual = mskfilter( x, mask ); + expected = [ 2, 4 ]; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = toAccessorArray( [ 0, 0, 0, 0 ] ); + actual = mskfilter( x, mask ); + expected = []; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = toAccessorArray( [ 0, 0, 0, 1 ] ); + actual = mskfilter( x, mask ); + expected = [ 4 ]; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = toAccessorArray( [ 1, 1, 1, 1 ] ); + actual = mskfilter( x, mask ); + expected = [ 1, 2, 3, 4 ]; + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function filters array elements (real typed array)', function test( t ) { + var expected; + var actual; + var mask; + var x; + + x = new Int32Array( [ 1, 2, 3, 4 ] ); + + mask = [ 0, 1, 0, 1 ]; + actual = mskfilter( x, mask ); + expected = [ 2, 4 ]; + t.strictEqual( isInt32Array( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 0, 0, 0 ]; + actual = mskfilter( x, mask ); + expected = []; + t.strictEqual( isInt32Array( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 0, 0, 0, 1 ]; + actual = mskfilter( x, mask ); + expected = [ 4 ]; + t.strictEqual( isInt32Array( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + mask = [ 1, 1, 1, 1 ]; + actual = mskfilter( x, mask ); + expected = [ 1, 2, 3, 4 ]; + t.strictEqual( isInt32Array( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns new array' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function filters array elements (complex typed array)', function test( t ) { + var expected; + var actual; + var mask; + var x; + + x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + + mask = [ 0, 1, 0, 1 ]; + actual = mskfilter( x, mask ); + expected = new Complex64Array( [ 3.0, 4.0, 7.0, 8.0 ] ); + t.notEqual( actual, x, 'returns new array' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + mask = [ 0, 0, 0, 0 ]; + actual = mskfilter( x, mask ); + expected = new Complex64Array( [] ); + t.notEqual( actual, x, 'returns new array' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + mask = [ 0, 0, 0, 1 ]; + actual = mskfilter( x, mask ); + expected = new Complex64Array( [ 7.0, 8.0 ] ); + t.notEqual( actual, x, 'returns new array' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + mask = [ 1, 1, 1, 1 ]; + actual = mskfilter( x, mask ); + expected = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + t.notEqual( actual, x, 'returns new array' ); + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an empty array if provided empty arrays', function test( t ) { + t.deepEqual( mskfilter( [], [] ), [], 'returns expected value' ); + t.deepEqual( mskfilter( new Int32Array( [] ), [] ), new Int32Array( [] ), 'returns expected value' ); + t.end(); +});