Skip to content

Commit 98f0d2b

Browse files
committed
Merge branch 'develop' into feat/heaviside
2 parents 3de8d33 + f633319 commit 98f0d2b

File tree

333 files changed

+33648
-572
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

333 files changed

+33648
-572
lines changed

docs/user-guides/recipes/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# Recipes
22+
23+
> Bite-sized, actionable examples of how to use specific stdlib features.
24+
25+
Welcome to stdlib recipes!
26+
27+
To help you accomplish specific tasks using stdlib, we've authored a number of guided examples which are laser-focused on achieving specific goals. These guides are in contrast to more extensive how-to guides and tutorials, which aim to provide more expansive guidance to accomplish larger tasks and to promote learning, respectively.
28+
29+
If you have specific recipes that you'd like to see or that you'd like to contribute, please feel free to open a discussion on our [issue tracker][stdlib-issue-tracker].
30+
31+
<!-- links -->
32+
33+
<section class="links">
34+
35+
[stdlib-issue-tracker]: https://github.com/stdlib-js/stdlib/issues?q=sort%3Aupdated-desc+is%3Aissue+is%3Aopen
36+
37+
</section>
38+
39+
<!-- /.links -->
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# How to create an ndarray filled with random numbers
22+
23+
> Create an ndarray of uniformly-distributed pseudorandom numbers and then regenerate the exact same ndarray using a seed or saved PRNG state.
24+
25+
## Introduction
26+
27+
stdlib provides APIs and packages to help you create [ndarrays][@stdlib/ndarray/ctor] (a.k.a., multi-dimensional arrays) containing pseudorandom numbers drawn from statistical distributions. These APIs allow you to specify seeds in order to generate reproducible sequences and to specify the output array shape and precision.
28+
29+
In this recipe, we'll use [`@stdlib/random/uniform`][@stdlib/random/uniform] to create ndarrays containing pseudorandom numbers drawn from a uniform distribution.
30+
31+
## Setup
32+
33+
Before we begin, we need to install `@stdlib/stdlib`, if it isn't already available.
34+
35+
```bash
36+
$ npm install @stdlib/stdlib
37+
```
38+
39+
## Steps
40+
41+
1. Import all necessary packages for generating pseudorandom numbers.
42+
2. Generate an ndarray.
43+
3. \[Optional] Regenerate a sequence by using a seed.
44+
45+
### 1. Import required packages
46+
47+
For this recipe, we'll use `@stdlib/stdlib` and its package [`@stdlib/random/uniform`][@stdlib/random/uniform]. We'll also use the package [`@stdlib/ndarray/to-array`][@stdlib/ndarray/to-array] to convert an ndarray to a conventional array-of-arrays format.
48+
49+
```javascript
50+
const uniform = require( '@stdlib/random/uniform' );
51+
const ndarray2array = require( '@stdlib/ndarray/to-array' );
52+
```
53+
54+
### 2. Generate an ndarray
55+
56+
Every stdlib interface for creating ndarrays of pseudorandom numbers drawn from a statistical distribution has two requirements. The first is defining the desired output shape. The second is defining the distribution parameters which control the shape of the sampled distribution.
57+
58+
For this recipe, the uniform distribution has two parameters:
59+
60+
- **a**: minimum support (inclusive).
61+
- **b**: maximum support (exclusive).
62+
63+
In the following code snippet, we generate a 3×3 matrix of pseudorandom numbers sampled from a uniform distribution with minimum support `-10` and maximum support `+10`.
64+
65+
```javascript
66+
const shape = [ 3, 3 ]; // 3×3
67+
const a = -10.0; // minimum support
68+
const b = 10.0; // maximum support
69+
70+
const x = uniform( shape, a, b );
71+
// returns <ndarray>
72+
```
73+
74+
ndarrays are multi-dimensional views atop linear memory. While boosting performance and improving memory efficiency, especially when compared to nested arrays, this makes ndarrays somewhat opaque data structures. Accordingly, to convert an ndarray to a conventional array-of-arrays format, we can use [`@stdlib/ndarray/to-array`][@stdlib/ndarray/to-array], as done in the following code snippet.
75+
76+
```javascript
77+
const xa = ndarray2array( x );
78+
// e.g., returns [ [ ~-2.41, ~3.08, ~5.09 ], [ ~5.73, ~-8.12, ~-8.99 ], [ ~0.11, ~-6.69, ~4.79 ] ]
79+
```
80+
81+
The output displayed in the above example is representative, and your values are likely to differ. However, notice that the values always reside on the half-open interval `[-10,10)`.
82+
83+
> 💡 Tip
84+
>
85+
> By default, `uniform` returns an ndarray having a `float64` data type (i.e., an ndarray of double-precision floating-point numbers). To return an ndarray having either a `float32` or `generic` data type, you can provide a `dtype` option when calling `uniform`, as displayed in the following code snippet.
86+
>
87+
> ```javascript
88+
> const x32 = uniform( shape, a, b, {
89+
> 'dtype': 'float32'
90+
> });
91+
> ```
92+
93+
At this point, we've finished creating an ndarray of pseudorandom numbers, and now we'll reproduce the same sequence of numbers by using a seed.
94+
95+
### 3. \[Optional] Regenerate a sequence by using a seed
96+
97+
A **seed** is a value which initializes a pseudorandom number generator, and a pseudorandom number generator's sequence is completely determined by that seed. Accordingly, if another pseudorandom number generator instance is initialized with the same seed, that instance will produce the same sequence of numbers.
98+
99+
In the following code snippet, we pass the seed from the `uniform` pseudorandom number generator used above to a function which returns a new `uniform` instance. When calling that instance with the same distribution parameters `a` and `b` that we used above, we generate an ndarray containing the same set of pseudorandomly generated numbers.
100+
101+
```javascript
102+
const r1 = uniform.factory({
103+
'seed': uniform.seed
104+
});
105+
106+
const y = r1( shape, a, b );
107+
// returns <ndarray>
108+
109+
const ya = ndarray2array( y );
110+
// e.g., returns [ [ ~-2.41, ~3.08, ~5.09 ], [ ~5.73, ~-8.12, ~-8.99 ], [ ~0.11, ~-6.69, ~4.79 ] ]
111+
```
112+
113+
Need to configure a `uniform` instance to continue from the current generator state, instead of starting over? Pass a state when creating a new `uniform` instance, as done in the following code snippet.
114+
115+
```javascript
116+
const r2 = uniform.factory({
117+
'state': uniform.state,
118+
'copy': true // set to `false` to share state
119+
});
120+
121+
// Generate the same sequence of values as if calling `uniform` a second time:
122+
const z = r2( shape, a, b );
123+
// returns <ndarray>
124+
```
125+
126+
Congratulations! You have successfully used stdlib to generate (and re-generate) an ndarray of pseudorandom numbers.
127+
128+
## Learn More
129+
130+
Check out our other [recipes][stdlib-user-guides-recipes] to continue your learning!
131+
132+
Sampling other distributions? See [`@stdlib/random`][@stdlib/random] for all of what stdlib has to offer.
133+
134+
<!-- links -->
135+
136+
<section class="links">
137+
138+
[@stdlib/random]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random
139+
140+
[@stdlib/random/uniform]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/uniform
141+
142+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
143+
144+
[@stdlib/ndarray/to-array]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/to-array
145+
146+
[stdlib-user-guides-recipes]: https://github.com/stdlib-js/stdlib/tree/develop/docs/user-guides/recipes
147+
148+
</section>
149+
150+
<!-- /.links -->
Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
11
{
2-
"Array": "require( '@stdlib/assert/is-array' );",
3-
"ArrayArray": "require( '@stdlib/assert/is-array-array' );",
4-
"ArrayLike": "require( '@stdlib/assert/is-array-like' );",
5-
"ArrayLikeObject": "require( '@stdlib/assert/is-array-like-object' );",
6-
"BinaryString": "require( '@stdlib/assert/is-binary-string' );",
7-
"boolean": "require( '@stdlib/assert/is-boolean' ).isPrimitive;",
8-
"BooleanArray": "require( '@stdlib/assert/is-boolean-array' ).primitives;",
9-
"Buffer": "require( '@stdlib/assert/is-buffer' );",
10-
"Collection": "require( '@stdlib/assert/is-collection' );",
11-
"Complex": "require( '@stdlib/assert/is-complex' );",
12-
"Complex64": "require( '@stdlib/assert/is-complex64' );",
13-
"Complex128": "require( '@stdlib/assert/is-complex128' );",
14-
"Date": "a `Date` object",
15-
"Function": "require( '@stdlib/assert/is-function' );",
16-
"FunctionArray": "require( '@stdlib/assert/is-function-array' );",
17-
"Integer": "require( '@stdlib/assert/is-integer' );",
18-
"IntegerArray": "require( '@stdlib/assert/is-integer-array' );",
19-
"JSON": "require( '@stdlib/assert/is-json' );",
20-
"NegativeInteger": "require( '@stdlib/assert/is-negative-integer' );",
21-
"NegativeIntegerArray": "require( '@stdlib/assert/is-negative-integer-array' );",
22-
"NegativeNumber": "require( '@stdlib/assert/is-negative-number' );",
23-
"NonNegativeInteger": "require( '@stdlib/assert/is-nonnegative-integer' );",
24-
"NonNegativeIntegerArray": "require( '@stdlib/assert/is-nonnegative-integer-array' );",
25-
"NonPositiveInteger": "require( '@stdlib/assert/is-nonpositive-integer' );",
26-
"NonPositiveIntegerArray": "require( '@stdlib/assert/is-nonpositive-integer-array' );",
27-
"NonNegativeNumber": "require( '@stdlib/assert/is-nonnegative-number' );",
28-
"NonPositiveNumber": "require( '@stdlib/assert/is-nonpositive-number' );",
29-
"number": "require( '@stdlib/assert/is-number' ).isPrimitive;",
30-
"NumberArray": "require( '@stdlib/assert/is-number-array' ).primitives;",
31-
"NumericArray": "require( '@stdlib/assert/is-numeric-array' );",
32-
"Object": "require( '@stdlib/assert/is-object' );",
33-
"ObjectArray": "require( '@stdlib/assert/is-object-array' );",
34-
"ObjectLike": "require( '@stdlib/assert/is-object-like' );",
35-
"PlainObject": "require( '@stdlib/assert/is-plain-object' );",
36-
"PositiveInteger": "require( '@stdlib/assert/is-positive-integer' );",
37-
"PositiveIntegerArray": "require( '@stdlib/assert/is-positive-integer-array' ).primitives;",
38-
"PositiveNumber": "require( '@stdlib/assert/is-positive-number' );",
39-
"Probability": "require( '@stdlib/assert/is-probability' );",
40-
"ProbabilityArray": "require( '@stdlib/assert/is-probability-array' );",
41-
"UnityProbabilityArray": "require( '@stdlib/assert/is-union-probability-array' );",
42-
"RegExp": "require( '@stdlib/assert/is-regexp' );",
43-
"string": "require( '@stdlib/assert/is-string' ).isPrimitive;",
44-
"StringArray": "require( '@stdlib/assert/is-string-array' ).primitives;",
45-
"TypedArray": "require( '@stdlib/assert/is-typed-array' );",
46-
"URI": "require( '@stdlib/assert/is-uri' );"
2+
"Array": "require( '@stdlib/assert/is-array' );",
3+
"ArrayArray": "require( '@stdlib/assert/is-array-array' );",
4+
"ArrayLike": "require( '@stdlib/assert/is-array-like' );",
5+
"ArrayLikeObject": "require( '@stdlib/assert/is-array-like-object' );",
6+
"BinaryString": "require( '@stdlib/assert/is-binary-string' );",
7+
"boolean": "require( '@stdlib/assert/is-boolean' ).isPrimitive;",
8+
"BooleanArray": "require( '@stdlib/assert/is-boolean-array' ).primitives;",
9+
"Buffer": "require( '@stdlib/assert/is-buffer' );",
10+
"Collection": "require( '@stdlib/assert/is-collection' );",
11+
"Complex": "require( '@stdlib/assert/is-complex' );",
12+
"Complex64": "require( '@stdlib/assert/is-complex64' );",
13+
"Complex128": "require( '@stdlib/assert/is-complex128' );",
14+
"Date": "a `Date` object",
15+
"Function": "require( '@stdlib/assert/is-function' );",
16+
"FunctionArray": "require( '@stdlib/assert/is-function-array' );",
17+
"Integer": "require( '@stdlib/assert/is-integer' );",
18+
"IntegerArray": "require( '@stdlib/assert/is-integer-array' );",
19+
"JSON": "require( '@stdlib/assert/is-json' );",
20+
"NegativeInteger": "require( '@stdlib/assert/is-negative-integer' );",
21+
"NegativeIntegerArray": "require( '@stdlib/assert/is-negative-integer-array' );",
22+
"NegativeNumber": "require( '@stdlib/assert/is-negative-number' );",
23+
"NonNegativeInteger": "require( '@stdlib/assert/is-nonnegative-integer' );",
24+
"NonNegativeIntegerArray": "require( '@stdlib/assert/is-nonnegative-integer-array' );",
25+
"NonPositiveInteger": "require( '@stdlib/assert/is-nonpositive-integer' );",
26+
"NonPositiveIntegerArray": "require( '@stdlib/assert/is-nonpositive-integer-array' );",
27+
"NonNegativeNumber": "require( '@stdlib/assert/is-nonnegative-number' );",
28+
"NonPositiveNumber": "require( '@stdlib/assert/is-nonpositive-number' );",
29+
"number": "require( '@stdlib/assert/is-number' ).isPrimitive;",
30+
"NumberArray": "require( '@stdlib/assert/is-number-array' ).primitives;",
31+
"NumericArray": "require( '@stdlib/assert/is-numeric-array' );",
32+
"Object": "require( '@stdlib/assert/is-object' );",
33+
"ObjectArray": "require( '@stdlib/assert/is-object-array' );",
34+
"ObjectLike": "require( '@stdlib/assert/is-object-like' );",
35+
"PlainObject": "require( '@stdlib/assert/is-plain-object' );",
36+
"PositiveInteger": "require( '@stdlib/assert/is-positive-integer' );",
37+
"PositiveIntegerArray": "require( '@stdlib/assert/is-positive-integer-array' ).primitives;",
38+
"PositiveNumber": "require( '@stdlib/assert/is-positive-number' );",
39+
"Probability": "require( '@stdlib/assert/is-probability' );",
40+
"ProbabilityArray": "require( '@stdlib/assert/is-probability-array' );",
41+
"UnityProbabilityArray": "require( '@stdlib/assert/is-union-probability-array' );",
42+
"RegExp": "require( '@stdlib/assert/is-regexp' );",
43+
"string": "require( '@stdlib/assert/is-string' ).isPrimitive;",
44+
"StringArray": "require( '@stdlib/assert/is-string-array' ).primitives;",
45+
"TypedArray": "require( '@stdlib/assert/is-typed-array' );",
46+
"URI": "require( '@stdlib/assert/is-uri' );"
4747
}
4848

lib/node_modules/@stdlib/array/promotion-rules/README.md

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,28 +94,23 @@ var out = promotionRules( 'foo', 'generic' );
9494
<!-- eslint no-undef: "error" -->
9595

9696
```javascript
97+
var cartesianProduct = require( '@stdlib/array/cartesian-product' );
9798
var dtypes = require( '@stdlib/array/dtypes' );
99+
var unzip = require( '@stdlib/utils/unzip' );
100+
var logEachMap = require( '@stdlib/console/log-each-map' );
98101
var promotionRules = require( '@stdlib/array/promotion-rules' );
99102

100-
var DTYPES;
101-
var dt1;
102-
var dt2;
103-
var dt;
104-
var i;
105-
var j;
106-
107103
// Get the list of supported array data types:
108-
DTYPES = dtypes();
109-
110-
// Print the promotion rule for each pair of array data types...
111-
for ( i = 0; i < DTYPES.length; i++ ) {
112-
dt1 = DTYPES[ i ];
113-
for ( j = 0; j < DTYPES.length; j++ ) {
114-
dt2 = DTYPES[ j ];
115-
dt = promotionRules( dt1, dt2 );
116-
console.log( '(%s, %s) => %s', dt1, dt2, dt );
117-
}
118-
}
104+
var dt = dtypes();
105+
106+
// Generate a list of data type pairs:
107+
var pairs = cartesianProduct( dt, dt );
108+
109+
// Split the pairs into separate arrays:
110+
var args = unzip( pairs );
111+
112+
// Print the promotion rule for each pair of array data types:
113+
logEachMap( '(%s, %s) => %s', args[ 0 ], args[ 1 ], promotionRules );
119114
```
120115

121116
</section>

lib/node_modules/@stdlib/array/promotion-rules/examples/index.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,20 @@
1818

1919
'use strict';
2020

21+
var cartesianProduct = require( '@stdlib/array/cartesian-product' );
2122
var dtypes = require( '@stdlib/array/dtypes' );
23+
var unzip = require( '@stdlib/utils/unzip' );
24+
var logEachMap = require( '@stdlib/console/log-each-map' );
2225
var promotionRules = require( './../lib' );
2326

24-
var DTYPES;
25-
var dt1;
26-
var dt2;
27-
var dt;
28-
var i;
29-
var j;
30-
3127
// Get the list of supported array data types:
32-
DTYPES = dtypes();
28+
var dt = dtypes();
29+
30+
// Generate a list of data type pairs:
31+
var pairs = cartesianProduct( dt, dt );
32+
33+
// Split the pairs into separate arrays:
34+
var args = unzip( pairs );
3335

34-
// Print the promotion rule for each pair of array data types...
35-
for ( i = 0; i < DTYPES.length; i++ ) {
36-
dt1 = DTYPES[ i ];
37-
for ( j = 0; j < DTYPES.length; j++ ) {
38-
dt2 = DTYPES[ j ];
39-
dt = promotionRules( dt1, dt2 );
40-
console.log( '(%s, %s) => %s', dt1, dt2, dt );
41-
}
42-
}
36+
// Print the promotion rule for each pair of array data types:
37+
logEachMap( '(%s, %s) => %s', args[ 0 ], args[ 1 ], promotionRules );

lib/node_modules/@stdlib/array/typed/docs/types/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
/// <reference types="@stdlib/types"/>
2424

25-
import { RealOrComplexTypedArray, TypedDataTypeMap } from '@stdlib/types/array';
25+
import { RealOrComplexTypedArray, TypedDataTypeMap, Collection } from '@stdlib/types/array';
2626
import ArrayBuffer = require( '@stdlib/array/buffer' );
2727

2828
/**
@@ -100,7 +100,7 @@ declare function typedarray<T extends keyof TypedDataTypeMap = 'float64'>( typed
100100
* var arr = typedarray( [ 5, -3 ], 'int32' );
101101
* // returns <Int32Array>[ 5, -3 ]
102102
*/
103-
declare function typedarray<T extends keyof TypedDataTypeMap = 'float64'>( obj: ArrayLike<number> | Iterable<any>, dtype?: T ): TypedDataTypeMap[T];
103+
declare function typedarray<T extends keyof TypedDataTypeMap = 'float64'>( obj: Collection<unknown> | Iterable<unknown>, dtype?: T ): TypedDataTypeMap[T];
104104

105105
/**
106106
* Creates a typed array.

0 commit comments

Comments
 (0)