Skip to content

Commit 4b8a736

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/base/copy
PR-URL: #8329 Closes: stdlib-js/metr-issue-tracker#105 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent bd65ac6 commit 4b8a736

File tree

10 files changed

+998
-0
lines changed

10 files changed

+998
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# copy
22+
23+
> Copy an input ndarray to a new ndarray having the same shape and [data type][@stdlib/ndarray/dtypes].
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 copy = require( '@stdlib/ndarray/base/copy' );
41+
```
42+
43+
#### copy( x )
44+
45+
Copies an input ndarray to a new ndarray having the same shape and [data type][@stdlib/ndarray/dtypes].
46+
47+
```javascript
48+
var getShape = require( '@stdlib/ndarray/shape' );
49+
var zeros = require( '@stdlib/ndarray/base/zeros' );
50+
51+
var x = zeros( 'float64', [ 2, 2 ], 'row-major' );
52+
// returns <ndarray>
53+
54+
var y = copy( x );
55+
// returns <ndarray>
56+
57+
var sh = getShape( y );
58+
// returns [ 2, 2 ]
59+
```
60+
61+
</section>
62+
63+
<!-- /.usage -->
64+
65+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
66+
67+
<section class="notes">
68+
69+
## Notes
70+
71+
- The function performs a full copy in which an ndarray's underlying data is copied to a new underlying data buffer.
72+
- Along with data type, shape, and order, the function infers the "class" of the returned ndarray from the provided ndarray. For example, if provided a "base" [ndarray][@stdlib/ndarray/base/ctor], the function returns a base [ndarray][@stdlib/ndarray/base/ctor]. If provided a non-base [ndarray][@stdlib/ndarray/ctor], the function returns a non-base [ndarray][@stdlib/ndarray/ctor].
73+
74+
</section>
75+
76+
<!-- /.notes -->
77+
78+
<!-- Package usage examples. -->
79+
80+
<section class="examples">
81+
82+
## Examples
83+
84+
<!-- eslint no-undef: "error" -->
85+
86+
```javascript
87+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
88+
var array = require( '@stdlib/ndarray/array' );
89+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
90+
var copy = require( '@stdlib/ndarray/base/copy' );
91+
92+
var xbuf = bernoulli( 10, 0.9, {
93+
'dtype': 'generic'
94+
});
95+
var x = array({
96+
'dtype': 'generic',
97+
'data': xbuf,
98+
'shape': [ 5, 2 ],
99+
'strides': [ 2, 1 ],
100+
'offset': 0,
101+
'order': 'row-major'
102+
});
103+
console.log( ndarray2array( x ) );
104+
105+
var o = copy( x );
106+
console.log( ndarray2array( o ) );
107+
```
108+
109+
</section>
110+
111+
<!-- /.examples -->
112+
113+
<!-- 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. -->
114+
115+
<section class="references">
116+
117+
</section>
118+
119+
<!-- /.references -->
120+
121+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
122+
123+
<section class="related">
124+
125+
</section>
126+
127+
<!-- /.related -->
128+
129+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
130+
131+
<section class="links">
132+
133+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/@stdlib/ndarray/dtypes
134+
135+
[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/@stdlib/ndarray/base/ctor
136+
137+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/@stdlib/ndarray/ctor
138+
139+
</section>
140+
141+
<!-- /.links -->
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 isndarray = require( '@stdlib/assert/is-ndarray-like' );
25+
var zeros = require( '@stdlib/ndarray/base/zeros' );
26+
var pkg = require( './../package.json' ).name;
27+
var copy = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var values;
34+
var out;
35+
var i;
36+
37+
values = [
38+
zeros( 'float64', [ 10 ], 'row-major' ),
39+
zeros( 'float32', [ 5, 5 ], 'row-major' ),
40+
zeros( 'int32', [ 3, 3, 3 ], 'row-major' ),
41+
zeros( 'uint32', [ 2, 2, 2, 2 ], 'row-major' )
42+
];
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
out = copy( values[ i%values.length ] );
47+
if ( typeof out !== 'object' ) {
48+
b.fail( 'should return an ndarray' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isndarray( out ) ) {
53+
b.fail( 'should return an ndarray' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
{{alias}}( x )
3+
Copies an input ndarray to a new ndarray having the same shape and data
4+
type.
5+
6+
The function performs a full copy in which an ndarray's underlying data is
7+
copied to a new underlying data buffer.
8+
9+
Along with data type, shape, and order, the function infers the "class" of
10+
the returned ndarray from the provided ndarray. For example, if provided a
11+
"base" ndarray, the function returns a base ndarray. If provided a non-base
12+
ndarray, the function returns a non-base ndarray.
13+
14+
Parameters
15+
----------
16+
x: ndarray
17+
Input array.
18+
19+
Returns
20+
-------
21+
out: ndarray
22+
Output array.
23+
24+
Examples
25+
--------
26+
> var x = {{alias:@stdlib/ndarray/base/zeros}}( 'float64', [ 2, 2 ], 'row-major' )
27+
<ndarray>
28+
> var sh = {{alias:@stdlib/ndarray/shape}}( x )
29+
[ 2, 2 ]
30+
> var y = {{alias}}( x )
31+
<ndarray>
32+
> sh = {{alias:@stdlib/ndarray/shape}}( y )
33+
[ 2, 2 ]
34+
35+
See Also
36+
--------
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 { ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Copies an input ndarray to a new ndarray having the same shape and data type.
27+
*
28+
* ## Notes
29+
*
30+
* - The function performs a full copy in which an ndarray's underlying data is copied to a new underlying data buffer.
31+
*
32+
* @param x - input array
33+
* @returns output array
34+
*
35+
* @example
36+
* var getShape = require( '@stdlib/ndarray/shape' );
37+
* var getDType = require( '@stdlib/ndarray/dtype' );
38+
* var zeros = require( '@stdlib/ndarray/base/zeros' );
39+
*
40+
* var x = zeros( 'float64', [ 2, 2 ], 'row-major' );
41+
* // returns <ndarray>
42+
*
43+
* var sh = getShape( x );
44+
* // returns [ 2, 2 ]
45+
*
46+
* var y = copy( x );
47+
* // returns <ndarray>
48+
*
49+
* sh = getShape( y );
50+
* // returns [ 2, 2 ]
51+
*/
52+
declare function copy<T extends ndarray = ndarray>( x: T ): T;
53+
54+
55+
// EXPORTS //
56+
57+
export = copy;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
import zeros = require( '@stdlib/ndarray/base/zeros' );
20+
import empty = require( '@stdlib/ndarray/base/empty' );
21+
import copy = require( './index' );
22+
23+
24+
// TESTS //
25+
26+
// The function returns an ndarray...
27+
{
28+
const sh = [ 2, 2 ];
29+
const ord = 'row-major';
30+
31+
copy( zeros( 'float64', sh, ord ) ); // $ExpectType float64ndarray
32+
copy( zeros( 'float32', sh, ord ) ); // $ExpectType float32ndarray
33+
copy( zeros( 'complex128', sh, ord ) ); // $ExpectType complex128ndarray
34+
copy( zeros( 'complex64', sh, ord ) ); // $ExpectType complex64ndarray
35+
copy( zeros( 'int32', sh, ord ) ); // $ExpectType int32ndarray
36+
copy( zeros( 'int16', sh, ord ) ); // $ExpectType int16ndarray
37+
copy( zeros( 'int8', sh, ord ) ); // $ExpectType int8ndarray
38+
copy( zeros( 'uint32', sh, ord ) ); // $ExpectType uint32ndarray
39+
copy( zeros( 'uint16', sh, ord ) ); // $ExpectType uint16ndarray
40+
copy( zeros( 'uint8', sh, ord ) ); // $ExpectType uint8ndarray
41+
copy( zeros( 'uint8c', sh, ord ) ); // $ExpectType uint8cndarray
42+
copy( empty( 'bool', sh, ord ) ); // $ExpectType boolndarray
43+
copy( zeros( 'generic', sh, ord ) ); // $ExpectType genericndarray<number>
44+
}
45+
46+
// The compiler throws an error if the function is provided a first argument is not an ndarray...
47+
{
48+
copy( '10' ); // $ExpectError
49+
copy( 10 ); // $ExpectError
50+
copy( false ); // $ExpectError
51+
copy( true ); // $ExpectError
52+
copy( null ); // $ExpectError
53+
copy( [] ); // $ExpectError
54+
copy( {} ); // $ExpectError
55+
copy( ( x: number ): number => x ); // $ExpectError
56+
}
57+
58+
// The compiler throws an error if the function is provided an unsupported number of arguments...
59+
{
60+
copy(); // $ExpectError
61+
copy( zeros( 'float64', [ 2, 2 ], 'row-major' ), 1 ); // $ExpectError
62+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
22+
var array = require( '@stdlib/ndarray/array' );
23+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
24+
var copy = require( './../lib' );
25+
26+
var xbuf = bernoulli( 10, 0.9, {
27+
'dtype': 'generic'
28+
});
29+
var x = array({
30+
'dtype': 'generic',
31+
'data': xbuf,
32+
'shape': [ 5, 2 ],
33+
'strides': [ 2, 1 ],
34+
'offset': 0,
35+
'order': 'row-major'
36+
});
37+
console.log( ndarray2array( x ) );
38+
39+
var o = copy( x );
40+
console.log( ndarray2array( o ) );

0 commit comments

Comments
 (0)