Skip to content

Commit aabf7be

Browse files
MeKaustubh07kgryte
andauthored
feat: add blas/base/ndarray/zcopy
PR-URL: #11684 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 73d09de commit aabf7be

File tree

10 files changed

+954
-0
lines changed

10 files changed

+954
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
# zcopy
22+
23+
> Copy values from a one-dimensional double-precision complex floating-point ndarray `x` into a one-dimensional double-precision complex floating-point ndarray `y`.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var zcopy = require( '@stdlib/blas/base/ndarray/zcopy' );
37+
```
38+
39+
#### zcopy( arrays )
40+
41+
Copies values from a one-dimensional double-precision complex floating-point ndarray `x` into a one-dimensional double-precision complex floating-point ndarray `y`.
42+
43+
```javascript
44+
var Complex128Array = require( '@stdlib/array/complex128' );
45+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
46+
47+
var xbuf = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
48+
var x = new ndarray( 'complex128', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
49+
50+
var ybuf = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
51+
var y = new ndarray( 'complex128', ybuf, [ 3 ], [ 1 ], 0, 'row-major' );
52+
53+
var z = zcopy( [ x, y ] );
54+
// returns <ndarray>[ <Complex128>[ 1.0, 2.0 ], <Complex128>[ 3.0, 4.0 ], <Complex128>[ 5.0, 6.0 ] ]
55+
56+
var bool = ( y === z );
57+
// returns true
58+
```
59+
60+
The function has the following parameters:
61+
62+
- **arrays**: array-like object containing an input ndarray and an output ndarray.
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<section class="notes">
69+
70+
</section>
71+
72+
<!-- /.notes -->
73+
74+
<section class="examples">
75+
76+
## Examples
77+
78+
<!-- eslint no-undef: "error" -->
79+
80+
```javascript
81+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
82+
var Complex128Array = require( '@stdlib/array/complex128' );
83+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
84+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
85+
var zcopy = require( '@stdlib/blas/base/ndarray/zcopy' );
86+
87+
var opts = {
88+
'dtype': 'float64'
89+
};
90+
91+
var xbuf = new Complex128Array( discreteUniform( 10, 0, 100, opts ) );
92+
var x = new ndarray( 'complex128', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
93+
console.log( ndarray2array( x ) );
94+
95+
var ybuf = new Complex128Array( discreteUniform( xbuf.length*2, 0, 10, opts ) );
96+
var y = new ndarray( 'complex128', ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' );
97+
console.log( ndarray2array( y ) );
98+
99+
var out = zcopy( [ x, y ] );
100+
console.log( ndarray2array( out ) );
101+
```
102+
103+
</section>
104+
105+
<!-- /.examples -->
106+
107+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
108+
109+
<section class="related">
110+
111+
</section>
112+
113+
<!-- /.related -->
114+
115+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
116+
117+
<section class="links">
118+
119+
</section>
120+
121+
<!-- /.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 real = require( '@stdlib/complex/float64/real' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
29+
var Complex128Array = require( '@stdlib/array/complex128' );
30+
var format = require( '@stdlib/string/format' );
31+
var pkg = require( './../package.json' ).name;
32+
var zcopy = require( './../lib' );
33+
34+
35+
// VARIABLES //
36+
37+
var options = {
38+
'dtype': 'complex128'
39+
};
40+
41+
42+
// FUNCTIONS //
43+
44+
/**
45+
* Creates a benchmark function.
46+
*
47+
* @private
48+
* @param {PositiveInteger} len - array length
49+
* @returns {Function} benchmark function
50+
*/
51+
function createBenchmark( len ) {
52+
var xbuf;
53+
var ybuf;
54+
var x;
55+
var y;
56+
57+
xbuf = uniform( len*2, -100.0, 100.0, {
58+
'dtype': 'float64'
59+
});
60+
x = new ndarray( options.dtype, new Complex128Array( xbuf ), [ len ], [ 1 ], 0, 'row-major' );
61+
62+
ybuf = uniform( len*2, -100.0, 100.0, {
63+
'dtype': 'float64'
64+
});
65+
y = new ndarray( options.dtype, new Complex128Array( ybuf ), [ len ], [ 1 ], 0, 'row-major' );
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 z;
77+
var i;
78+
79+
b.tic();
80+
for ( i = 0; i < b.iterations; i++ ) {
81+
z = zcopy( [ x, y ] );
82+
if ( typeof z !== 'object' ) {
83+
b.fail( 'should return an ndarray' );
84+
}
85+
}
86+
b.toc();
87+
if ( isnan( real( z.get( i%len ) ) ) ) {
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
{{alias}}( arrays )
3+
Copies values from a one-dimensional double-precision complex floating-point
4+
ndarray `x` into a one-dimensional double-precision complex floating-point
5+
ndarray `y`.
6+
7+
If provided an empty input ndarray, the function returns the output ndarray
8+
unchanged.
9+
10+
Parameters
11+
----------
12+
arrays: ArrayLikeObject<ndarray>
13+
Array-like object containing an input ndarray and an output ndarray.
14+
15+
Returns
16+
-------
17+
out: ndarray
18+
Output ndarray.
19+
20+
Examples
21+
--------
22+
> var xbuf = new {{alias:@stdlib/array/complex128}}( [ 1.0, 2.0, 3.0, 4.0 ] );
23+
> var ybuf = new {{alias:@stdlib/array/complex128}}( [ 0.0, 0.0, 0.0, 0.0 ] );
24+
> var dt = 'complex128';
25+
> var sh = [ xbuf.length ];
26+
> var st = [ 1 ];
27+
> var oo = 0;
28+
> var ord = 'row-major';
29+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, st, oo, ord );
30+
> var y = new {{alias:@stdlib/ndarray/ctor}}( dt, ybuf, sh, st, oo, ord );
31+
32+
> {{alias}}( [ x, y ] );
33+
> y
34+
<ndarray>[ <Complex128>[ 1.0, 2.0 ], <Complex128>[ 3.0, 4.0 ] ]
35+
36+
See Also
37+
--------
38+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 { complex128ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Copies values from a one-dimensional double-precision complex floating-point ndarray `x` into a one-dimensional double-precision complex floating-point ndarray `y`.
27+
*
28+
* @param arrays - array-like object containing an input ndarray and an output ndarray
29+
* @returns output ndarray
30+
*
31+
* @example
32+
* var Complex128Array = require( '@stdlib/array/complex128' );
33+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
34+
*
35+
* var xbuf = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
36+
* var x = new ndarray( 'complex128', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
37+
*
38+
* var ybuf = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
39+
* var y = new ndarray( 'complex128', ybuf, [ 3 ], [ 1 ], 0, 'row-major' );
40+
*
41+
* var z = zcopy( [ x, y ] );
42+
* // returns <ndarray>[ <Complex128>[ 1.0, 2.0 ], <Complex128>[ 3.0, 4.0 ], <Complex128>[ 5.0, 6.0 ] ]
43+
*
44+
* var bool = ( z === y );
45+
* // returns true
46+
*/
47+
declare function zcopy( arrays: [ complex128ndarray, complex128ndarray ] ): complex128ndarray;
48+
49+
50+
// EXPORTS //
51+
52+
export = zcopy;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
/* eslint-disable space-in-parens */
20+
21+
import zeros = require( '@stdlib/ndarray/zeros' );
22+
import zcopy = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns an ndarray...
28+
{
29+
const x = zeros( [ 10 ], {
30+
'dtype': 'complex128'
31+
});
32+
const y = zeros( [ 10 ], {
33+
'dtype': 'complex128'
34+
});
35+
36+
zcopy( [ x, y ] ); // $ExpectType complex128ndarray
37+
}
38+
39+
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
40+
{
41+
zcopy( '10' ); // $ExpectError
42+
zcopy( 10 ); // $ExpectError
43+
zcopy( true ); // $ExpectError
44+
zcopy( false ); // $ExpectError
45+
zcopy( null ); // $ExpectError
46+
zcopy( undefined ); // $ExpectError
47+
zcopy( [] ); // $ExpectError
48+
zcopy( {} ); // $ExpectError
49+
zcopy( ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided an unsupported number of arguments...
53+
{
54+
const x = zeros( [ 10 ], {
55+
'dtype': 'complex128'
56+
});
57+
const y = zeros( [ 10 ], {
58+
'dtype': 'complex128'
59+
});
60+
61+
zcopy(); // $ExpectError
62+
zcopy( [ x, y ], {} ); // $ExpectError
63+
}

0 commit comments

Comments
 (0)