Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/copy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<!--

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

-->

# copy

> Copy an input ndarray to a new ndarray having the same shape and [data type][@stdlib/ndarray/dtypes].

<!-- 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 copy = require( '@stdlib/ndarray/base/copy' );
```

#### copy( x )

Copies an input ndarray to a new ndarray having the same shape and [data type][@stdlib/ndarray/dtypes].

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

var x = zeros( 'float64', [ 2, 2 ], 'row-major' );
// returns <ndarray>

var y = copy( x );
// returns <ndarray>

var sh = getShape( y );
// returns [ 2, 2 ]
```

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

- The function performs a full copy in which an ndarray's underlying data is copied to a new underlying data buffer.
- 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].

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

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

```javascript
var bernoulli = require( '@stdlib/random/array/bernoulli' );
var array = require( '@stdlib/ndarray/array' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var copy = require( '@stdlib/ndarray/base/copy' );

var xbuf = bernoulli( 10, 0.9, {
'dtype': 'generic'
});
var x = array({
'dtype': 'generic',
'data': xbuf,
'shape': [ 5, 2 ],
'strides': [ 2, 1 ],
'offset': 0,
'order': 'row-major'
});
console.log( ndarray2array( x ) );

var o = copy( x );
console.log( ndarray2array( o ) );
```

</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/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/@stdlib/ndarray/dtypes

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

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

</section>

<!-- /.links -->
57 changes: 57 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/copy/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @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 isndarray = require( '@stdlib/assert/is-ndarray-like' );
var zeros = require( '@stdlib/ndarray/base/zeros' );
var pkg = require( './../package.json' ).name;
var copy = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var values;
var out;
var i;

values = [
zeros( 'float64', [ 10 ], 'row-major' ),
zeros( 'float32', [ 5, 5 ], 'row-major' ),
zeros( 'int32', [ 3, 3, 3 ], 'row-major' ),
zeros( 'uint32', [ 2, 2, 2, 2 ], 'row-major' )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = copy( values[ i%values.length ] );
if ( typeof out !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarray( out ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});
36 changes: 36 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/copy/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

{{alias}}( x )
Copies an input ndarray to a new ndarray having the same shape and data
type.

The function performs a full copy in which an ndarray's underlying data is
copied to a new underlying data buffer.

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, the function returns a base ndarray. If provided a non-base
ndarray, the function returns a non-base ndarray.

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

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

Examples
--------
> var x = {{alias:@stdlib/ndarray/base/zeros}}( 'float64', [ 2, 2 ], 'row-major' )
<ndarray>
> var sh = {{alias:@stdlib/ndarray/shape}}( x )
[ 2, 2 ]
> var y = {{alias}}( x )
<ndarray>
> sh = {{alias:@stdlib/ndarray/shape}}( y )
[ 2, 2 ]

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

/**
* Copies an input ndarray to a new ndarray having the same shape and data type.
*
* ## Notes
*
* - The function performs a full copy in which an ndarray's underlying data is copied to a new underlying data buffer.
*
* @param x - input array
* @returns output array
*
* @example
* var getShape = require( '@stdlib/ndarray/shape' );
* var getDType = require( '@stdlib/ndarray/dtype' );
* var zeros = require( '@stdlib/ndarray/base/zeros' );
*
* var x = zeros( 'float64', [ 2, 2 ], 'row-major' );
* // returns <ndarray>
*
* var sh = getShape( x );
* // returns [ 2, 2 ]
*
* var y = copy( x );
* // returns <ndarray>
*
* sh = getShape( y );
* // returns [ 2, 2 ]
*/
declare function copy<T extends ndarray = ndarray>( x: T ): T;


// EXPORTS //

export = copy;
62 changes: 62 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/copy/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* @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.
*/

import zeros = require( '@stdlib/ndarray/base/zeros' );
import empty = require( '@stdlib/ndarray/base/empty' );
import copy = require( './index' );


// TESTS //

// The function returns an ndarray...
{
const sh = [ 2, 2 ];
const ord = 'row-major';

copy( zeros( 'float64', sh, ord ) ); // $ExpectType float64ndarray
copy( zeros( 'float32', sh, ord ) ); // $ExpectType float32ndarray
copy( zeros( 'complex128', sh, ord ) ); // $ExpectType complex128ndarray
copy( zeros( 'complex64', sh, ord ) ); // $ExpectType complex64ndarray
copy( zeros( 'int32', sh, ord ) ); // $ExpectType int32ndarray
copy( zeros( 'int16', sh, ord ) ); // $ExpectType int16ndarray
copy( zeros( 'int8', sh, ord ) ); // $ExpectType int8ndarray
copy( zeros( 'uint32', sh, ord ) ); // $ExpectType uint32ndarray
copy( zeros( 'uint16', sh, ord ) ); // $ExpectType uint16ndarray
copy( zeros( 'uint8', sh, ord ) ); // $ExpectType uint8ndarray
copy( zeros( 'uint8c', sh, ord ) ); // $ExpectType uint8cndarray
copy( empty( 'bool', sh, ord ) ); // $ExpectType boolndarray
copy( zeros( 'generic', sh, ord ) ); // $ExpectType genericndarray<number>
}

// The compiler throws an error if the function is provided a first argument is not an ndarray...
{
copy( '10' ); // $ExpectError
copy( 10 ); // $ExpectError
copy( false ); // $ExpectError
copy( true ); // $ExpectError
copy( null ); // $ExpectError
copy( [] ); // $ExpectError
copy( {} ); // $ExpectError
copy( ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
copy(); // $ExpectError
copy( zeros( 'float64', [ 2, 2 ], 'row-major' ), 1 ); // $ExpectError
}
40 changes: 40 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/copy/examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @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';

var bernoulli = require( '@stdlib/random/array/bernoulli' );
var array = require( '@stdlib/ndarray/array' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var copy = require( './../lib' );

var xbuf = bernoulli( 10, 0.9, {
'dtype': 'generic'
});
var x = array({
'dtype': 'generic',
'data': xbuf,
'shape': [ 5, 2 ],
'strides': [ 2, 1 ],
'offset': 0,
'order': 'row-major'
});
console.log( ndarray2array( x ) );

var o = copy( x );
console.log( ndarray2array( o ) );
Loading
Loading