Skip to content

Commit f8edc1e

Browse files
committed
feat: add ndarray/with
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 861cd7f commit f8edc1e

File tree

10 files changed

+1457
-0
lines changed

10 files changed

+1457
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# ndarrayWith
22+
23+
> Return a new ndarray with the element at the specified index replaced with a provided value.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var ndarrayWith = require( '@stdlib/ndarray/with' );
41+
```
42+
43+
#### ndarrayWith( x, index, value )
44+
45+
Return a new ndarray with the element at the specified index replaced with a provided value.
46+
47+
```javascript
48+
var ndarray = require( '@stdlib/ndarray/ctor' );
49+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
50+
51+
var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
52+
var shape = [ 3, 2 ];
53+
var strides = [ 2, 1 ];
54+
var offset = 0;
55+
56+
var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' );
57+
// returns <ndarray>
58+
59+
var arr = ndarray2array( x );
60+
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
61+
62+
var out = ndarray2array( ndarrayWith( x, [ 0, 0 ], 3.0 ) );
63+
// returns [ [ 3.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
64+
65+
out = ndarray2array( ndarrayWith( x, [ 0, 0 ], [ 3.0, 4.0 ] ) );
66+
// returns [ [ [ 3.0, 4.0 ], 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
67+
```
68+
69+
The function accepts the following arguments:
70+
71+
- **x**: an input array.
72+
- **index**: element index array.
73+
- **value**: replacement value.
74+
75+
</section>
76+
77+
<!-- /.usage -->
78+
79+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
80+
81+
<section class="notes">
82+
83+
## Notes
84+
85+
- index array should have length equal to number of dimensions of a ndarray.
86+
87+
</section>
88+
89+
<!-- /.notes -->
90+
91+
<!-- Package usage examples. -->
92+
93+
<section class="examples">
94+
95+
## Examples
96+
97+
<!-- eslint no-undef: "error" -->
98+
99+
```javascript
100+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
101+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
102+
var array = require( '@stdlib/ndarray/array' );
103+
var ndarrayWith = require( '@stdlib/ndarray/with' );
104+
105+
var buffer = discreteUniform( 27, -10, 10, {
106+
'dtype': 'float64'
107+
});
108+
var x = array( buffer, {
109+
'shape': [ 3, 3, 3 ],
110+
'dtype': 'float64'
111+
});
112+
console.log( ndarray2array( x ) );
113+
114+
var index = discreteUniform( 3, 0, 2, {
115+
'dtype': 'generic'
116+
});
117+
var value = 100;
118+
console.log( 'Index: %s, Value: %s', index, value );
119+
120+
var out = ndarrayWith( x, index, value );
121+
console.log( ndarray2array( out ) );
122+
```
123+
124+
</section>
125+
126+
<!-- /.examples -->
127+
128+
<!-- 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. -->
129+
130+
<section class="references">
131+
132+
</section>
133+
134+
<!-- /.references -->
135+
136+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
137+
138+
<section class="related">
139+
140+
</section>
141+
142+
<!-- /.related -->
143+
144+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
145+
146+
<section class="links">
147+
148+
</section>
149+
150+
<!-- /.links -->
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var ndarray = require( '@stdlib/ndarray/ctor' );
26+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
27+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
28+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
29+
var pow = require( '@stdlib/math/base/special/pow' );
30+
var pkg = require( './../package.json' ).name;
31+
var ndarrayWith = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var xtype = 'generic';
37+
var orders = ['row-major', 'column-major'];
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @param {NonNegativeIntegerArray} shape - ndarray shape
48+
* @param {string} xtype - input ndarray data type
49+
* @param {string} order - ndarray memory layout
50+
* @returns {Function} benchmark function
51+
*/
52+
function createBenchmark(len, shape, xtype, order) {
53+
var strides;
54+
var index;
55+
var xbuf;
56+
var x;
57+
58+
xbuf = discreteUniform( len, -100, 100, {
59+
'dtype': xtype
60+
});
61+
strides = shape2strides( shape, order );
62+
x = ndarray( xtype, xbuf, shape, strides, 0, order );
63+
index = discreteUniform( shape.length, 0, 0 );
64+
65+
return benchmark;
66+
67+
/**
68+
* Benchmark function.
69+
*
70+
* @private
71+
* @param {Benchmark} b - benchmark instance
72+
*/
73+
function benchmark( b ) {
74+
var out;
75+
var i;
76+
77+
b.tic();
78+
for ( i = 0; i < b.iterations; i++ ) {
79+
out = ndarrayWith( x, index, i );
80+
if ( isnan( out.data[ i%out.length ] ) ) {
81+
b.fail( 'should not return NaN' );
82+
}
83+
}
84+
b.toc();
85+
if ( !isndarrayLike( out ) ) {
86+
b.fail( 'should return an array' );
87+
}
88+
b.pass( 'benchmark finished' );
89+
b.end();
90+
}
91+
}
92+
93+
94+
// MAIN //
95+
96+
/**
97+
* Main execution sequence.
98+
*
99+
* @private
100+
*/
101+
function main() {
102+
var len;
103+
var min;
104+
var max;
105+
var ord;
106+
var sh;
107+
var t1;
108+
var f;
109+
var i;
110+
var k;
111+
112+
min = 1; // 10^min
113+
max = 6; // 10^max
114+
115+
for ( k = 0; k < orders.length; k++ ) {
116+
t1 = xtype;
117+
ord = orders[ k ];
118+
for ( i = min; i <= max; i++ ) {
119+
len = pow( 10, i );
120+
121+
sh = [ len ];
122+
f = createBenchmark( len, sh, t1, ord );
123+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1, f );
124+
}
125+
}
126+
}
127+
128+
main();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( x, index, value )
3+
Return a new ndarray with the element at the specified index replaced with
4+
a provided value.
5+
6+
Parameters
7+
----------
8+
x: ndarray
9+
Input ndarray.
10+
11+
index: ArrayLikeObject
12+
Input array.
13+
14+
value: any
15+
Replacement value.
16+
17+
Returns
18+
-------
19+
out: ndarray
20+
Output ndarray.
21+
22+
Examples
23+
--------
24+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
25+
> var y = {{alias}}( x, [ 0, 0 ], 0.0 );
26+
> {{alias:@stdlib/ndarray/to-array}}( y )
27+
[ [ 0.0, 2.0 ], [ 3.0, 4.0 ] ]
28+
29+
See Also
30+
--------

0 commit comments

Comments
 (0)