Skip to content

Commit 9f1c2f3

Browse files
committed
feat: add ndarray/base/empty-like
This commit adds support for creating an uninitialized "base" ndarray based on a provided ndarray argument.
1 parent aeca0a6 commit 9f1c2f3

22 files changed

+2887
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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+
# emptyLike
22+
23+
> Create an uninitialized ndarray having the same shape and [data type][@stdlib/ndarray/dtypes] as a provided ndarray.
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 emptyLike = require( '@stdlib/ndarray/base/empty-like' );
41+
```
42+
43+
#### emptyLike( x )
44+
45+
Creates an uninitialized ndarray having the same shape and [data type][@stdlib/ndarray/dtypes] as a provided ndarray.
46+
47+
```javascript
48+
var zeros = require( '@stdlib/ndarray/base/zeros' );
49+
50+
var x = zeros( 'float64', [ 2, 2 ], 'row-major' );
51+
// returns <ndarray>
52+
53+
var y = emptyLike( x );
54+
// returns <ndarray>
55+
56+
var sh = y.shape;
57+
// returns [ 2, 2 ]
58+
```
59+
60+
</section>
61+
62+
<!-- /.usage -->
63+
64+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
65+
66+
<section class="notes">
67+
68+
## Notes
69+
70+
- 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].
71+
- If the inferred output ndarray data type is `'generic'`, the function always returns a zero-filled ndarray.
72+
- For returned ndarrays whose underlying memory is **not** initialized, memory contents are unknown and may contain **sensitive** data.
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 dtypes = require( '@stdlib/ndarray/dtypes' );
88+
var zeros = require( '@stdlib/ndarray/base/zeros' );
89+
var emptyLike = require( '@stdlib/ndarray/base/empty-like' );
90+
91+
// Get a list of data types:
92+
var dt = dtypes();
93+
94+
// Generate uninitialized arrays...
95+
var x;
96+
var y;
97+
var i;
98+
for ( i = 0; i < dt.length; i++ ) {
99+
x = zeros( dt[ i ], [ 2, 2 ], 'row-major' );
100+
y = emptyLike( x );
101+
console.log( y.data );
102+
}
103+
```
104+
105+
</section>
106+
107+
<!-- /.examples -->
108+
109+
<!-- 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. -->
110+
111+
<section class="references">
112+
113+
</section>
114+
115+
<!-- /.references -->
116+
117+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
118+
119+
<section class="related">
120+
121+
</section>
122+
123+
<!-- /.related -->
124+
125+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
126+
127+
<section class="links">
128+
129+
[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib
130+
131+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib
132+
133+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib
134+
135+
</section>
136+
137+
<!-- /.links -->

0 commit comments

Comments
 (0)