Skip to content

Commit 8cc3ae9

Browse files
committed
Add ndarray utility to create a zero-filled ndarray
1 parent 68ebfa4 commit 8cc3ae9

22 files changed

Lines changed: 3350 additions & 0 deletions
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 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+
# zeros
22+
23+
> Create a zero-filled [ndarray][@stdlib/ndarray/ctor] having a specified 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 zeros = require( '@stdlib/ndarray/zeros' );
41+
```
42+
43+
#### zeros( shape\[, options] )
44+
45+
Creates a zero-filled [ndarray][@stdlib/ndarray/ctor] having a specified shape and [data type][@stdlib/ndarray/dtypes].
46+
47+
```javascript
48+
var arr = zeros( [ 2, 2 ] );
49+
// returns <ndarray>
50+
51+
var sh = arr.shape;
52+
// returns [ 2, 2 ]
53+
54+
var dt = arr.dtype;
55+
// returns 'float64'
56+
```
57+
58+
The specified output [ndarray][@stdlib/ndarray/ctor] `shape` may be either an array-like object or an integer value.
59+
60+
```javascript
61+
var arr = zeros( 2 );
62+
// returns <ndarray>
63+
64+
var sh = arr.shape;
65+
// returns [ 2 ]
66+
67+
var dt = arr.dtype;
68+
// returns 'float64'
69+
```
70+
71+
The function accepts the following `options`:
72+
73+
- **dtype**: underlying [data type][@stdlib/ndarray/dtypes]. Default: `'float64'`.
74+
- **order**: specifies whether an [ndarray][@stdlib/ndarray/ctor] is `'row-major'` (C-style) or `'column-major'` (Fortran-style). Default: `'row-major'`.
75+
76+
By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [`float64`][@stdlib/ndarray/dtypes] data type. To specify an alternative [data type][@stdlib/ndarray/dtypes], provide a `dtype` option.
77+
78+
```javascript
79+
var arr = zeros( [ 2, 2 ], {
80+
'dtype': 'float32'
81+
});
82+
// returns <ndarray>
83+
84+
var sh = arr.shape;
85+
// returns [ 2, 2 ]
86+
87+
var dt = arr.dtype;
88+
// returns 'float32'
89+
```
90+
91+
</section>
92+
93+
<!-- /.usage -->
94+
95+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
96+
97+
<section class="notes">
98+
99+
</section>
100+
101+
<!-- /.notes -->
102+
103+
<!-- Package usage examples. -->
104+
105+
<section class="examples">
106+
107+
## Examples
108+
109+
<!-- eslint no-undef: "error" -->
110+
111+
```javascript
112+
var dtypes = require( '@stdlib/ndarray/dtypes' );
113+
var zeros = require( '@stdlib/ndarray/zeros' );
114+
115+
// Get a list of data types:
116+
var dt = dtypes();
117+
118+
// Generate zero-filled arrays...
119+
var arr;
120+
var i;
121+
for ( i = 0; i < dt.length; i++ ) {
122+
arr = zeros( [ 2, 2 ], {
123+
'dtype': dt[ i ]
124+
});
125+
console.log( arr.data );
126+
}
127+
```
128+
129+
</section>
130+
131+
<!-- /.examples -->
132+
133+
<!-- 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. -->
134+
135+
<section class="references">
136+
137+
</section>
138+
139+
<!-- /.references -->
140+
141+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
142+
143+
<section class="related">
144+
145+
</section>
146+
147+
<!-- /.related -->
148+
149+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
150+
151+
<section class="links">
152+
153+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib
154+
155+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib
156+
157+
</section>
158+
159+
<!-- /.links -->

0 commit comments

Comments
 (0)