Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions lib/node_modules/@stdlib/ndarray/with/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<!--

@license Apache-2.0

Copyright (c) 2025 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.

-->

# ndarrayWith

> Return a new ndarray with the element at the specified index replaced with a provided value.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var ndarrayWith = require( '@stdlib/ndarray/with' );
```

#### ndarrayWith( x, index, value )

Return a new ndarray with the element at the specified index replaced with a provided value.

```javascript
var ndarray = require( '@stdlib/ndarray/ctor' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );

var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
var shape = [ 3, 2 ];
var strides = [ 2, 1 ];
var offset = 0;

var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' );
// returns <ndarray>

var arr = ndarray2array( x );
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]

var out = ndarray2array( ndarrayWith( x, [ 0, 0 ], 3.0 ) );
// returns [ [ 3.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]

out = ndarray2array( ndarrayWith( x, [ 0, 0 ], [ 3.0, 4.0 ] ) );
// returns [ [ [ 3.0, 4.0 ], 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
```

The function accepts the following arguments:

- **x**: an input array.
- **index**: element index array.
- **value**: replacement value.

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- index array should have length equal to number of dimensions of a ndarray.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

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

```javascript
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var array = require( '@stdlib/ndarray/array' );
var ndarrayWith = require( '@stdlib/ndarray/with' );

var buffer = discreteUniform( 27, -10, 10, {
'dtype': 'float64'
});
var x = array( buffer, {
'shape': [ 3, 3, 3 ],
'dtype': 'float64'
});
console.log( ndarray2array( x ) );

var index = discreteUniform( 3, 0, 2, {
'dtype': 'generic'
});
var value = 100;
console.log( 'Index: %s, Value: %s', index, value );

var out = ndarrayWith( x, index, value );
console.log( ndarray2array( out ) );
```

</section>

<!-- /.examples -->

<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">

</section>

<!-- /.references -->

<!-- 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 -->
127 changes: 127 additions & 0 deletions lib/node_modules/@stdlib/ndarray/with/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 ndarray = require( '@stdlib/ndarray/ctor' );
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var pow = require( '@stdlib/math/base/special/pow' );
var pkg = require( './../package.json' ).name;
var ndarrayWith = require( './../lib' );


// VARIABLES //

var xtype = 'generic';
var orders = ['row-major', 'column-major'];


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @param {NonNegativeIntegerArray} shape - ndarray shape
* @param {string} xtype - input ndarray data type
* @param {string} order - ndarray memory layout
* @returns {Function} benchmark function
*/
function createBenchmark(len, shape, xtype, order) {
var strides;
var index;
var xbuf;
var x;

xbuf = discreteUniform( len, -100, 100, {
'dtype': xtype
});
strides = shape2strides( shape, order );
x = ndarray( xtype, xbuf, shape, strides, 0, order );
index = discreteUniform( shape.length, 0, 0 );

return benchmark;

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = ndarrayWith( x, index, i );
if ( out.get.apply( out, index ) !== i ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( !isndarrayLike( out ) ) {
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 ord;
var sh;
var t1;
var f;
var i;
var k;

min = 1; // 10^min
max = 6; // 10^max

for ( k = 0; k < orders.length; k++ ) {
t1 = xtype;
ord = orders[ k ];
for ( i = min; i <= max; i++ ) {
len = pow( 10, i );

sh = [ len ];
f = createBenchmark( len, sh, t1, ord );
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1, f );
}
}
}

main();
30 changes: 30 additions & 0 deletions lib/node_modules/@stdlib/ndarray/with/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

{{alias}}( x, index, value )
Return a new ndarray with the element at the specified index replaced with
a provided value.

Parameters
----------
x: ndarray
Input ndarray.

index: ArrayLikeObject
Input array.

value: any
Replacement value.

Returns
-------
out: ndarray
Output ndarray.

Examples
--------
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
> var y = {{alias}}( x, [ 0, 0 ], 0.0 );
> {{alias:@stdlib/ndarray/to-array}}( y )
[ [ 0.0, 2.0 ], [ 3.0, 4.0 ] ]

See Also
--------
Loading
Loading