Skip to content
131 changes: 131 additions & 0 deletions lib/node_modules/@stdlib/ndarray/with/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<!--

@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.

-->

# with

> Return an [ndarray][@stdlib/ndarray/ctor] with element at specified indices replaced by 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' );
```

#### with( x, indices, value )

Returns an [ndarray][@stdlib/ndarray/ctor] with element at specified indices replaced by a provided value.

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

var x = zeros( [ 2, 2 ], {
'dtype': 'float64'
});
// returns <ndarray>

var out = ndarrayWith( x, [ 0, 0 ], 1.0 );
// returns <ndarray>

var v = out.get( 0, 0 );
// returns 1.0
```

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

- This function doesnot validate that a provided `value` is compatible with the data type of the input [ndarray][@stdlib/ndarray/ctor]. For example, the function doesnot guard against precision loss when `value` is a real-valued number and the input [ndarray][@stdlib/ndarray/ctor] has an integer data type. This function should be considered a copy-on-write analog to using an [ndarray][@stdlib/ndarray/ctor] `set` method. Whether a `value` is silently coerced to the data type of the input [ndarray][@stdlib/ndarray/ctor] or triggers an exception when incompatible is implementation-dependent. Accordingly, any assertion logic ensuring data type compatibility should be performed **before** calling this function.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

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

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

var x = zeros( [ 1, 3, 3 ], {
'dtype': 'float64'
});

var out = ndarrayWith( x, [ 0, 1, 1 ], 10.0 );
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">

[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
150 changes: 150 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,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.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var zeros = require( '@stdlib/ndarray/zeros' );
var pkg = require( './../package.json' ).name;
var ndarrayWith = require( './../lib' );


// MAIN //

bench( pkg+'::1d', function benchmark( b ) {
var v;
var x;
var i;

x = zeros( [ 5 ], {
'dtype': 'float64'
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = ndarrayWith( x, [ 4 ], 5 );
if ( typeof v !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( v ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::2d', function benchmark( b ) {
var v;
var x;
var i;

x = zeros( [ 2, 2 ], {
'dtype': 'float64'
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = ndarrayWith( x, [ 1, 1 ], 5 );
if ( typeof v !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( v ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::3d', function benchmark( b ) {
var v;
var x;
var i;

x = zeros( [ 2, 2, 2 ], {
'dtype': 'float64'
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = ndarrayWith( x, [ 1, 1, 1 ], 5 );
if ( typeof v !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( v ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::4d', function benchmark( b ) {
var v;
var x;
var i;

x = zeros( [ 2, 2, 2, 2 ], {
'dtype': 'float64'
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = ndarrayWith( x, [ 1, 1, 1, 1 ], 5 );
if ( typeof v !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( v ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::5d', function benchmark( b ) {
var v;
var x;
var i;

x = zeros( [ 2, 2, 2, 2, 2 ], {
'dtype': 'float64'
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = ndarrayWith( x, [ 1, 1, 1, 1, 1 ], 5 );
if ( typeof v !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( v ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});
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, indices, value )
Returns an ndarray with element at specified indices replaced by a provided
value.

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

indices: Collection<integer>
Indices of the value to replace.

value: any
Replaced value.

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

Examples
--------
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] );
> var out = {{alias}}( x, [ 0, 0 ], 5 );
> var v = out.get( 0, 0 )
5

See Also
--------
55 changes: 55 additions & 0 deletions lib/node_modules/@stdlib/ndarray/with/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* @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.
*/

// TypeScript Version: 4.1

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

import { ndarray } from '@stdlib/types/ndarray';

/**
* Returns an ndarray with element at specified indices replaced by a provided value.
*
* @param x - input ndarray
* @param indices - indices of the element to replace
* @param value - value to set
* @returns output ndarray
*
* @example
* var ndarray = require( '@stdlib/ndarray/ctor' );
*
* var buffer = [ 1, 2, 3, 4 ];
* var shape = [ 2, 2 ];
* var order = 'row-major';
* var strides = [ 2, 1 ];
* var offset = 0;
*
* var x = ndarray( 'generic', buffer, shape, strides, offset, order );
*
* var out = ndarrayWith( x, [ 0, 0 ], 5 );
* // returns <ndarray>
*
* var v = out.get( 0, 0 );
* // returns 5
*/
declare function ndarrayWith( x: ndarray, indices: Array<number>, value: unknown ): ndarray;


// EXPORTS //

export = ndarrayWith;
Loading
Loading