Skip to content

Commit 432b571

Browse files
committed
feat: add array/one-to
1 parent f8eebba commit 432b571

22 files changed

Lines changed: 2537 additions & 0 deletions
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# oneTo
22+
23+
> Generate a linearly spaced numeric array whose elements increment by `1` starting from one.
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 oneTo = require( '@stdlib/array/one-to' );
41+
```
42+
43+
#### oneTo( n\[, dtype] )
44+
45+
Generates a linearly spaced numeric array whose elements increment by `1` starting from one.
46+
47+
```javascript
48+
var arr = oneTo( 5 );
49+
// returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
50+
```
51+
52+
If `n == 0`, the function returns an empty array.
53+
54+
```javascript
55+
var arr = oneTo( 0 );
56+
// returns <Float64Array>[]
57+
```
58+
59+
The function recognizes the following data types:
60+
61+
- `float64`: double-precision floating-point numbers (IEEE 754)
62+
- `float32`: single-precision floating-point numbers (IEEE 754)
63+
- `complex128`: double-precision complex floating-point numbers
64+
- `complex64`: single-precision complex floating-point numbers
65+
- `int32`: 32-bit two's complement signed integers
66+
- `uint32`: 32-bit unsigned integers
67+
- `int16`: 16-bit two's complement signed integers
68+
- `uint16`: 16-bit unsigned integers
69+
- `int8`: 8-bit two's complement signed integers
70+
- `uint8`: 8-bit unsigned integers
71+
- `uint8c`: 8-bit unsigned integers clamped to `0-255`
72+
- `generic`: generic JavaScript values
73+
74+
By default, the output array data type is `float64` (i.e., a [typed array][mdn-typed-array]). To specify an alternative data type, provide a `dtype` argument.
75+
76+
```javascript
77+
var arr = oneTo( 5, 'int32' );
78+
// returns <Int32Array>[ 1, 2, 3, 4, 5 ]
79+
```
80+
81+
</section>
82+
83+
<!-- /.usage -->
84+
85+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
86+
87+
<section class="notes">
88+
89+
## Notes
90+
91+
- For complex number arrays, each element of the returned array has an imaginary component equal to `0`.
92+
93+
</section>
94+
95+
<!-- /.notes -->
96+
97+
<!-- Package usage examples. -->
98+
99+
<section class="examples">
100+
101+
## Examples
102+
103+
<!-- eslint no-undef: "error" -->
104+
105+
```javascript
106+
var sort2hp = require( '@stdlib/blas/ext/base/gsort2hp' );
107+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
108+
var oneTo = require( '@stdlib/array/one-to' );
109+
110+
// Generate an array of random numbers:
111+
var opts = {
112+
'dtype': 'generic'
113+
};
114+
var x = discreteUniform( 10, 100, 200, opts );
115+
116+
// Generate a linearly-spaced array:
117+
var y = oneTo( x.length, opts.dtype );
118+
119+
// Create a temporary array to avoid mutation:
120+
var tmp = x.slice();
121+
122+
// Sort `y` according to the sort order of `x`:
123+
sort2hp( x.length, 1, tmp, 1, y, 1 );
124+
125+
console.log( x );
126+
console.log( y );
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+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
154+
155+
</section>
156+
157+
<!-- /.links -->

0 commit comments

Comments
 (0)