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 Jun 20, 2024
1 parent 690a911 commit c5731b5
Show file tree
Hide file tree
Showing 15 changed files with 1,526 additions and 4 deletions.
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@

##### Features

- [`20cd086`](https://github.com/stdlib-js/stdlib/commit/20cd0868236daf523fcd659d027e2277f7e1cb56) - add `mskput` to namespace
- [`2827035`](https://github.com/stdlib-js/stdlib/commit/2827035933c44ebb301a44200ff9cd5ad73e9ef0) - add `where` to namespace
- [`d626ffa`](https://github.com/stdlib-js/stdlib/commit/d626ffac9f0504be236c94afd4b920032272c8bf) - update namespace TypeScript declarations [(#2394)](https://github.com/stdlib-js/stdlib/pull/2394)
- [`60fa5ac`](https://github.com/stdlib-js/stdlib/commit/60fa5ac214d2e5dfe310f93928a07515eeeb4a3b) - add `cuevery` to namespace
Expand Down Expand Up @@ -921,6 +922,28 @@ This release closes the following issue:

<!-- /.package -->

<section class="package" id="array-base-mskput-unreleased">

#### [@stdlib/array/base/mskput](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/mskput)

<details>

<section class="features">

##### Features

- [`e9bf843`](https://github.com/stdlib-js/stdlib/commit/e9bf843b45b204ca61eca213575aa4933026b09b) - add `array/base/mskput`

</section>

<!-- /.features -->

</details>

</section>

<!-- /.package -->

<section class="package" id="array-base-mskreject-unreleased">

#### [@stdlib/array/base/mskreject](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/mskreject)
Expand Down Expand Up @@ -2009,6 +2032,10 @@ A total of 13 people contributed to this release. Thank you to the following con

<details>

- [`20cd086`](https://github.com/stdlib-js/stdlib/commit/20cd0868236daf523fcd659d027e2277f7e1cb56) - **feat:** add `mskput` to namespace _(by Athan Reines)_
- [`e9bf843`](https://github.com/stdlib-js/stdlib/commit/e9bf843b45b204ca61eca213575aa4933026b09b) - **feat:** add `array/base/mskput` _(by Athan Reines)_
- [`33da247`](https://github.com/stdlib-js/stdlib/commit/33da2477cc34e5fcc931b39e996a373d043432e0) - **docs:** fix example _(by Athan Reines)_
- [`7879bf0`](https://github.com/stdlib-js/stdlib/commit/7879bf0ecd89e0b274fb687ff090dd2b863346a3) - **docs:** fix example _(by Athan Reines)_
- [`299be6a`](https://github.com/stdlib-js/stdlib/commit/299be6aa293d738d6be5ef6c4a1aa2efb4cbb8d9) - **feat:** add boolean dtype support to `array/base/count-falsy` _(by Athan Reines)_
- [`09f1e11`](https://github.com/stdlib-js/stdlib/commit/09f1e11557f76c8fc6a8e3f9b60b06a8701ded3e) - **feat:** add boolean dtype support to `array/base/count-truthy` _(by Athan Reines)_
- [`b981bc3`](https://github.com/stdlib-js/stdlib/commit/b981bc30e83a7f88cdc2c0efca082fb31f9e1ac0) - **refactor:** avoid repeated property access _(by Athan Reines)_
Expand Down
9 changes: 9 additions & 0 deletions base/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,15 @@ setReadOnly( ns, 'mskbinary2d', require( './../../base/mskbinary2d' ) );
*/
setReadOnly( ns, 'mskfilter', require( './../../base/mskfilter' ) );

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

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

# mskput

> Replace elements of an array with provided values according to a provided mask array.
<section class="usage">

## Usage

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

#### mskput( x, mask, values, mode )

Replaces elements of an array with provided values according to a provided mask array.

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

var out = mskput( x, [ 1, 0, 1, 0 ], [ 20, 40 ], 'throw' );
// returns [ 1, 20, 3, 40 ]

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

The function supports the following parameters:

- **x**: input array.
- **mask**: mask array.
- **values**: values to set.
- **mode**: string specifying whether to raise an exception when the number of `values` is less than the number of falsy `mask` values.

The function supports the following modes:

- `'throw'`: specifies that the function must raise an exception when the function is provided insufficient `values` to satisfy the `mask` array.
- `'repeat'`: specifies that the function must reuse provided `values` when replacing elements in `x` in order to satisfy the `mask` array.

When `mode` is equal to `'repeat`', the function supports broadcasting a `values` array containing a single element against the number of falsy values in the `mask` array.

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

var out = mskput( x, [ 1, 0, 1, 0 ], [ 20 ], 'repeat' );
// returns [ 1, 20, 3, 20 ]

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

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- The function mutates the input array `x`.
- If a `mask` array element is falsy, the corresponding element in `x` is **replaced**; otherwise, the corresponding element in `x` is "masked" and thus left unchanged.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

```javascript
var filledBy = require( '@stdlib/array/base/filled-by' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var bernoulli = require( '@stdlib/random/base/bernoulli' );
var linspace = require( '@stdlib/array/base/linspace' );
var mskput = require( '@stdlib/array/base/mskput' );

// Generate a linearly spaced array:
var x = linspace( 0, 100, 11 );
console.log( x );

// Generate a random mask array:
var N = discreteUniform( 5, 15 );
var mask = filledBy( N, bernoulli.factory( 0.3 ) );
console.log( mask );

// Generate an array of random values:
var values = filledBy( N, discreteUniform.factory( 1000, 2000 ) );
console.log( values );

// Update a random sample of elements in `x`:
var out = mskput( x, mask, values, 'throw' );
console.log( out );
```

</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 -->
79 changes: 79 additions & 0 deletions base/mskput/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @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 zeros = require( './../../../base/zeros' );
var pkg = require( './../package.json' ).name;
var mskput = require( './../lib' );


// MAIN //

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

x = zeroTo( 100 );
mask = zeros( 100 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = mskput( x, mask, x, 'throw' );
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();
});

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

x = zeroTo( 100 );
mask = zeros( 100 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = mskput( x, mask, [ i ], 'repeat' );
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();
});
113 changes: 113 additions & 0 deletions base/mskput/benchmark/benchmark.length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* @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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var zeros = require( './../../../base/zeros' );
var isArray = require( '@stdlib/assert/is-array' );
var pkg = require( './../package.json' ).name;
var mskput = require( './../lib' );


// VARIABLES //

var opts = {
'dtype': 'generic'
};


// FUNCTIONS //

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

x = discreteUniform( len, 0, 10, opts );
mask = zeros( len );
values = [
discreteUniform( len, -10, 0, opts ),
discreteUniform( len, 0, 10, opts )
];

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 = mskput( x, mask, values[ i%values.length ], 'throw' );
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();
Loading

0 comments on commit c5731b5

Please sign in to comment.