Skip to content

Commit ded14cf

Browse files
committed
feat: add ndarray/ones
1 parent bbd8f0e commit ded14cf

22 files changed

Lines changed: 3778 additions & 0 deletions
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
# ones
22+
23+
> Create a ones-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 ones = require( '@stdlib/ndarray/ones' );
41+
```
42+
43+
#### ones( shape\[, options] )
44+
45+
Creates a ones-filled [ndarray][@stdlib/ndarray/ctor] having a specified shape and [data type][@stdlib/ndarray/dtypes].
46+
47+
```javascript
48+
var getShape = require( '@stdlib/ndarray/shape' );
49+
var getDType = require( '@stdlib/ndarray/dtype' );
50+
51+
var arr = ones( [ 2, 2 ] );
52+
// returns <ndarray>[ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]
53+
54+
var sh = getShape( arr );
55+
// returns [ 2, 2 ]
56+
57+
var dt = String( getDType( arr ) );
58+
// returns 'float64'
59+
```
60+
61+
The specified output [ndarray][@stdlib/ndarray/ctor] shape may be either an array-like object or an integer value.
62+
63+
```javascript
64+
var getShape = require( '@stdlib/ndarray/shape' );
65+
var getDType = require( '@stdlib/ndarray/dtype' );
66+
67+
var arr = ones( 2 );
68+
// returns <ndarray>[ 1.0, 1.0 ]
69+
70+
var sh = getShape( arr );
71+
// returns [ 2 ]
72+
73+
var dt = String( getDType( arr ) );
74+
// returns 'float64'
75+
```
76+
77+
The function accepts the following options:
78+
79+
- **dtype**: underlying [data type][@stdlib/ndarray/dtypes]. Must be a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. Default: `'float64'`.
80+
- **order**: specifies whether an [ndarray][@stdlib/ndarray/ctor] is `'row-major'` (C-style) or `'column-major'` (Fortran-style). Default: `'row-major'`.
81+
- **mode**: specifies how to handle indices which exceed array dimensions (see [ndarray][@stdlib/ndarray/ctor]). Default: `'throw'`.
82+
- **submode**: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions (see [ndarray][@stdlib/ndarray/ctor]). If provided fewer modes than dimensions, the constructor recycles modes using modulo arithmetic. Default: `[ options.mode ]`.
83+
- **readonly**: boolean indicating whether an array should be **read-only**. Default: `false`.
84+
85+
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.
86+
87+
```javascript
88+
var getShape = require( '@stdlib/ndarray/shape' );
89+
var getDType = require( '@stdlib/ndarray/dtype' );
90+
91+
var arr = ones( [ 2, 2 ], {
92+
'dtype': 'float32'
93+
});
94+
// returns <ndarray>[ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]
95+
96+
var sh = getShape( arr );
97+
// returns [ 2, 2 ]
98+
99+
var dt = String( getDType( arr ) );
100+
// returns 'float32'
101+
```
102+
103+
</section>
104+
105+
<!-- /.usage -->
106+
107+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
108+
109+
<section class="notes">
110+
111+
</section>
112+
113+
<!-- /.notes -->
114+
115+
<!-- Package usage examples. -->
116+
117+
<section class="examples">
118+
119+
## Examples
120+
121+
<!-- eslint no-undef: "error" -->
122+
123+
```javascript
124+
var dtypes = require( '@stdlib/ndarray/dtypes' );
125+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
126+
var ones = require( '@stdlib/ndarray/ones' );
127+
128+
// Get a list of data types:
129+
var dt = dtypes( 'integer_and_generic' );
130+
131+
// Generate ones-filled arrays...
132+
var arr;
133+
var i;
134+
for ( i = 0; i < dt.length; i++ ) {
135+
arr = ones( [ 2, 2 ], {
136+
'dtype': dt[ i ]
137+
});
138+
console.log( ndarray2array( arr ) );
139+
}
140+
```
141+
142+
</section>
143+
144+
<!-- /.examples -->
145+
146+
<!-- 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. -->
147+
148+
<section class="references">
149+
150+
</section>
151+
152+
<!-- /.references -->
153+
154+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
155+
156+
<section class="related">
157+
158+
</section>
159+
160+
<!-- /.related -->
161+
162+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
163+
164+
<section class="links">
165+
166+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
167+
168+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
169+
170+
</section>
171+
172+
<!-- /.links -->

0 commit comments

Comments
 (0)