Skip to content

Commit 6dbb82e

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/ndarray/zcopy-within
PR-URL: #14130 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#1140
1 parent ba1e337 commit 6dbb82e

10 files changed

Lines changed: 1064 additions & 0 deletions

File tree

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# zcopyWithin
22+
23+
> Perform an in-place copy of elements within a one-dimensional double-precision complex floating-point ndarray.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var zcopyWithin = require( '@stdlib/blas/ext/base/ndarray/zcopy-within' );
37+
```
38+
39+
#### zcopyWithin( arrays )
40+
41+
Performs an in-place copy of elements within a one-dimensional double-precision complex floating-point ndarray.
42+
43+
<!-- eslint-disable max-len -->
44+
45+
```javascript
46+
var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );
47+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
48+
var zeros = require( '@stdlib/ndarray/zeros' );
49+
50+
var x = new Complex128Vector( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
51+
var target = scalar2ndarray( 1, {
52+
'dtype': 'generic'
53+
});
54+
var start = scalar2ndarray( 0, {
55+
'dtype': 'generic'
56+
});
57+
var end = scalar2ndarray( 2, {
58+
'dtype': 'generic'
59+
});
60+
61+
var w = zeros( [ 3 ], {
62+
'dtype': 'complex128'
63+
});
64+
65+
var out = zcopyWithin( [ x, target, start, end, w ] );
66+
// returns <ndarray>[ <Complex128>[ 1.0, 2.0 ], <Complex128>[ 1.0, 2.0 ], <Complex128>[ 3.0, 4.0 ] ]
67+
```
68+
69+
<!-- eslint-enable max-len -->
70+
71+
The function has the following parameters:
72+
73+
- **arrays**: array-like object containing the following ndarrays:
74+
75+
- a one-dimensional input ndarray.
76+
- a zero-dimensional ndarray specifying a target index.
77+
- a zero-dimensional ndarray specifying a source start index (inclusive).
78+
- a zero-dimensional ndarray specifying a source end index (exclusive).
79+
- a one-dimensional workspace ndarray.
80+
81+
</section>
82+
83+
<!-- /.usage -->
84+
85+
<section class="notes">
86+
87+
## Notes
88+
89+
- The input ndarray is copied **in-place** (i.e., the input ndarray is **mutated**).
90+
- If the `start` and `target` index ranges do not overlap, the `workspace` ndarray is unused and thus ignored.
91+
92+
</section>
93+
94+
<!-- /.notes -->
95+
96+
<section class="examples">
97+
98+
## Examples
99+
100+
<!-- eslint no-undef: "error" -->
101+
102+
```javascript
103+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
104+
var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );
105+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
106+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
107+
var zeros = require( '@stdlib/ndarray/zeros' );
108+
var zcopyWithin = require( '@stdlib/blas/ext/base/ndarray/zcopy-within' );
109+
110+
var x = new Complex128Vector( discreteUniform( 20, -100, 100, {
111+
'dtype': 'float64'
112+
}));
113+
console.log( ndarray2array( x ) );
114+
115+
var target = scalar2ndarray( 5, {
116+
'dtype': 'generic'
117+
});
118+
var start = scalar2ndarray( 0, {
119+
'dtype': 'generic'
120+
});
121+
var end = scalar2ndarray( 3, {
122+
'dtype': 'generic'
123+
});
124+
125+
var w = zeros( [ 10 ], {
126+
'dtype': 'complex128'
127+
});
128+
129+
zcopyWithin( [ x, target, start, end, w ] );
130+
console.log( ndarray2array( x ) );
131+
```
132+
133+
</section>
134+
135+
<!-- /.examples -->
136+
137+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
138+
139+
<section class="related">
140+
141+
</section>
142+
143+
<!-- /.related -->
144+
145+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
146+
147+
<section class="links">
148+
149+
</section>
150+
151+
<!-- /.links -->
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );
28+
var zeros = require( '@stdlib/ndarray/zeros' );
29+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
30+
var format = require( '@stdlib/string/format' );
31+
var pkg = require( './../package.json' ).name;
32+
var zcopyWithin = require( './../lib' );
33+
34+
35+
// FUNCTIONS //
36+
37+
/**
38+
* Creates a benchmark function.
39+
*
40+
* @private
41+
* @param {PositiveInteger} len - array length
42+
* @returns {Function} benchmark function
43+
*/
44+
function createBenchmark( len ) {
45+
var target;
46+
var start;
47+
var xbuf;
48+
var opts;
49+
var end;
50+
var x;
51+
var w;
52+
53+
opts = {
54+
'dtype': 'generic'
55+
};
56+
xbuf = uniform( len*2, -100.0, 100.0, {
57+
'dtype': 'float64'
58+
});
59+
x = new Complex128Vector( xbuf.buffer );
60+
w = zeros( [ len ], {
61+
'dtype': 'complex128'
62+
});
63+
target = scalar2ndarray( 0, opts );
64+
start = scalar2ndarray( 0, opts );
65+
end = scalar2ndarray( len, opts );
66+
67+
return benchmark;
68+
69+
/**
70+
* Benchmark function.
71+
*
72+
* @private
73+
* @param {Benchmark} b - benchmark instance
74+
*/
75+
function benchmark( b ) {
76+
var out;
77+
var i;
78+
79+
b.tic();
80+
for ( i = 0; i < b.iterations; i++ ) {
81+
out = zcopyWithin( [ x, target, start, end, w ] );
82+
if ( typeof out !== 'object' ) {
83+
b.fail( 'should return an ndarray' );
84+
}
85+
}
86+
b.toc();
87+
if ( isnan( xbuf[ i%(len*2) ] ) ) {
88+
b.fail( 'should not return NaN' );
89+
}
90+
b.pass( 'benchmark finished' );
91+
b.end();
92+
}
93+
}
94+
95+
96+
// MAIN //
97+
98+
/**
99+
* Main execution sequence.
100+
*
101+
* @private
102+
*/
103+
function main() {
104+
var len;
105+
var min;
106+
var max;
107+
var f;
108+
var i;
109+
110+
min = 1; // 10^min
111+
max = 6; // 10^max
112+
113+
for ( i = min; i <= max; i++ ) {
114+
len = pow( 10, i );
115+
f = createBenchmark( len );
116+
bench( format( '%s:len=%d', pkg, len ), f );
117+
}
118+
}
119+
120+
main();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
{{alias}}( arrays )
3+
Performs an in-place copy of elements within a one-dimensional double-
4+
precision complex floating-point ndarray.
5+
6+
The input ndarray is copied *in-place* (i.e., the input ndarray is
7+
*mutated*).
8+
9+
If the `start` and `target` index ranges do not overlap, the `workspace`
10+
ndarray is unused and thus ignored.
11+
12+
Parameters
13+
----------
14+
arrays: ArrayLikeObject<ndarray>
15+
Array-like object containing the following ndarrays:
16+
17+
- a one-dimensional input ndarray.
18+
- a zero-dimensional ndarray specifying a target index.
19+
- a zero-dimensional ndarray specifying a source start index
20+
(inclusive).
21+
- a zero-dimensional ndarray specifying a source end index
22+
(exclusive).
23+
- a one-dimensional workspace ndarray.
24+
25+
Returns
26+
-------
27+
out: ndarray
28+
Input ndarray.
29+
30+
Examples
31+
--------
32+
> var xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
33+
> var x = new {{alias:@stdlib/ndarray/vector/complex128}}( xbuf );
34+
> var t = {{alias:@stdlib/ndarray/from-scalar}}( 1, { 'dtype': 'generic' } );
35+
> var s = {{alias:@stdlib/ndarray/from-scalar}}( 0, { 'dtype': 'generic' } );
36+
> var e = {{alias:@stdlib/ndarray/from-scalar}}( 2, { 'dtype': 'generic' } );
37+
> var w = {{alias:@stdlib/ndarray/zeros}}( [ 3 ], { 'dtype': 'complex128' } );
38+
> {{alias}}( [ x, t, s, e, w ] )
39+
<ndarray>[ <Complex128>[ 1,2 ], <Complex128>[ 1,2 ], <Complex128>[ 3,4 ] ]
40+
41+
See Also
42+
--------
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { typedndarray, complex128ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Performs an in-place copy of elements within a one-dimensional double-precision complex floating-point ndarray.
27+
*
28+
* ## Notes
29+
*
30+
* - The function expects the following ndarrays:
31+
*
32+
* - a one-dimensional input ndarray.
33+
* - a zero-dimensional ndarray specifying a target index.
34+
* - a zero-dimensional ndarray specifying a source start index (inclusive).
35+
* - a zero-dimensional ndarray specifying a source end index (exclusive).
36+
* - a one-dimensional workspace ndarray.
37+
*
38+
* @param arrays - array-like object containing ndarrays
39+
* @returns input ndarray
40+
*
41+
* @example
42+
* var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );
43+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
44+
* var zeros = require( '@stdlib/ndarray/zeros' );
45+
*
46+
* var x = new Complex128Vector( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
47+
* var target = scalar2ndarray( 1, {
48+
* 'dtype': 'generic'
49+
* });
50+
* var start = scalar2ndarray( 0, {
51+
* 'dtype': 'generic'
52+
* });
53+
* var end = scalar2ndarray( 2, {
54+
* 'dtype': 'generic'
55+
* });
56+
*
57+
* var w = zeros( [ 3 ], {
58+
* 'dtype': 'complex128'
59+
* });
60+
*
61+
* var out = zcopyWithin( [ x, target, start, end, w ] );
62+
* // returns <ndarray>[ <Complex128>[ 1.0, 2.0 ], <Complex128>[ 1.0, 2.0 ], <Complex128>[ 3.0, 4.0 ] ]
63+
*/
64+
declare function zcopyWithin( arrays: [ complex128ndarray, typedndarray<number>, typedndarray<number>, typedndarray<number>, complex128ndarray ] ): complex128ndarray;
65+
66+
67+
// EXPORTS //
68+
69+
export = zcopyWithin;

0 commit comments

Comments
 (0)