Skip to content

Commit bb15dcc

Browse files
committed
feat: add random/tools/ternary
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 64fa79c commit bb15dcc

14 files changed

Lines changed: 2565 additions & 0 deletions

File tree

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
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+
# Random
22+
23+
> Constructor for creating ndarrays filled with pseudorandom values drawn from a ternary PRNG.
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 Random = require( '@stdlib/random/tools/ternary' );
41+
```
42+
43+
#### Random( prng, idtypes, odtypes, policies\[, options] )
44+
45+
Returns an interface for creating ndarrays filled with pseudorandom values drawn from a ternary PRNG.
46+
47+
```javascript
48+
var dtypes = require( '@stdlib/ndarray/dtypes' );
49+
var frechet = require( '@stdlib/random/base/frechet' );
50+
51+
var idt = dtypes( 'real_and_generic' );
52+
var odt = dtypes( 'real_floating_point_and_generic' );
53+
54+
var policies = {
55+
'output': 'real_floating_point_and_generic'
56+
};
57+
var options = {
58+
'order': 'row-major'
59+
};
60+
61+
var rand = new Random( frechet, [ idt, idt, idt ], odt, policies, options );
62+
```
63+
64+
The constructor has the following parameters:
65+
66+
- **prng**: ternary pseudorandom value generator.
67+
68+
- **idtypes**: list containing a list of supported input data types for each PRNG parameter.
69+
70+
- **odtypes**: list of supported output data types.
71+
72+
- **policies**: interface policies. Must have the following properties:
73+
74+
- **output**: output data type [policy][@stdlib/ndarray/policies].
75+
76+
- **options**: function options (_optional_).
77+
78+
The constructor supports the following options:
79+
80+
- **order**: default [memory layout][@stdlib/ndarray/orders].
81+
82+
#### Random.prototype.generate( shape, param1, param2, param3\[, options] )
83+
84+
Returns an ndarray filled with pseudorandom values drawn from a ternary PRNG.
85+
86+
```javascript
87+
var dtypes = require( '@stdlib/ndarray/dtypes' );
88+
var frechet = require( '@stdlib/random/base/frechet' );
89+
90+
var idt = dtypes( 'real_and_generic' );
91+
var odt = dtypes( 'real_floating_point_and_generic' );
92+
93+
var policies = {
94+
'output': 'real_floating_point_and_generic'
95+
};
96+
var options = {
97+
'order': 'row-major'
98+
};
99+
100+
var rand = new Random( frechet, [ idt, idt, idt ], odt, policies, options );
101+
102+
var v = rand.generate( [ 2, 2 ], 2.0, 3.0, 0.0 );
103+
// returns <ndarray>
104+
```
105+
106+
The method has the following parameters:
107+
108+
- **shape**: output ndarray shape.
109+
- **param1**: first PRNG parameter. May be either a scalar or an ndarray. If an ndarray, must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the specified output ndarray shape.
110+
- **param2**: second PRNG parameter. May be either a scalar or an ndarray. If an ndarray, must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the specified output ndarray shape.
111+
- **param3**: third PRNG parameter. May be either a scalar or an ndarray. If an ndarray, must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the specified output ndarray shape.
112+
- **options**: function options (_optional_).
113+
114+
The method accepts the following options:
115+
116+
- **dtype**: output ndarray data type. Setting this option overrides the output data type policy.
117+
- **order**: memory layout. Setting this option overrides the default memory layout.
118+
- **mode**: specifies how to handle indices which exceed ndarray dimensions.
119+
- **submode**: specifies how to handle subscripts which exceed ndarray dimensions on a per dimension basis.
120+
- **readonly**: boolean indicating whether an ndarray should be read-only.
121+
122+
By default, the method returns an ndarray having a data type determined by the output data type policy. To override the default behavior, set the `dtype` option.
123+
124+
```javascript
125+
var dtypes = require( '@stdlib/ndarray/dtypes' );
126+
var getDType = require( '@stdlib/ndarray/dtype' );
127+
var frechet = require( '@stdlib/random/base/frechet' );
128+
129+
var idt = dtypes( 'real_and_generic' );
130+
var odt = dtypes( 'real_floating_point_and_generic' );
131+
132+
var policies = {
133+
'output': 'real_floating_point_and_generic'
134+
};
135+
var options = {
136+
'order': 'row-major'
137+
};
138+
139+
var rand = new Random( frechet, [ idt, idt, idt ], odt, policies, options );
140+
141+
var v = rand.generate( [ 2, 2 ], 2.0, 3.0, 0.0, {
142+
'dtype': 'generic'
143+
});
144+
// returns <ndarray>
145+
146+
var dt = String( getDType( v ) );
147+
// returns 'generic'
148+
```
149+
150+
#### Random.prototype.assign( param1, param2, param3, out )
151+
152+
Fills an ndarray with pseudorandom values drawn from a ternary PRNG.
153+
154+
```javascript
155+
var dtypes = require( '@stdlib/ndarray/dtypes' );
156+
var ndzeros = require( '@stdlib/ndarray/zeros' );
157+
var frechet = require( '@stdlib/random/base/frechet' );
158+
159+
var idt = dtypes( 'real_and_generic' );
160+
var odt = dtypes( 'real_floating_point_and_generic' );
161+
162+
var policies = {
163+
'output': 'real_floating_point_and_generic'
164+
};
165+
var options = {
166+
'order': 'row-major'
167+
};
168+
169+
var rand = new Random( frechet, [ idt, idt, idt ], odt, policies, options );
170+
171+
var out = ndzeros( [ 2, 2 ] );
172+
var v = rand.assign( 2.0, 3.0, 0.0, out );
173+
// returns <ndarray>
174+
175+
var bool = ( v === out );
176+
// returns true
177+
```
178+
179+
The method has the following parameters:
180+
181+
- **param1**: first PRNG parameter. May be either a scalar or an ndarray. If an ndarray, must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the output ndarray.
182+
- **param2**: second PRNG parameter. May be either a scalar or an ndarray. If an ndarray, must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the output ndarray.
183+
- **param3**: third PRNG parameter. May be either a scalar or an ndarray. If an ndarray, must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the output ndarray.
184+
- **out**: output ndarray.
185+
186+
</section>
187+
188+
<!-- /.usage -->
189+
190+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
191+
192+
<section class="notes">
193+
194+
## Notes
195+
196+
- The output data type policy only applies to the `generate` method. For the `assign` method, the output ndarray is allowed to have any supported output data type.
197+
198+
</section>
199+
200+
<!-- /.notes -->
201+
202+
<!-- Package usage examples. -->
203+
204+
<section class="examples">
205+
206+
## Examples
207+
208+
<!-- eslint no-undef: "error" -->
209+
210+
```javascript
211+
var frechet = require( '@stdlib/random/base/frechet' );
212+
var dtypes = require( '@stdlib/ndarray/dtypes' );
213+
var ndarray = require( '@stdlib/ndarray/ctor' );
214+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
215+
var Random = require( '@stdlib/random/tools/ternary' );
216+
217+
// Create a new PRNG instance...
218+
var idt = dtypes( 'real_and_generic' );
219+
var odt = dtypes( 'real_floating_point_and_generic' );
220+
var policies = {
221+
'output': 'real_floating_point_and_generic'
222+
};
223+
var random = new Random( frechet, [ idt, idt, idt ], odt, policies );
224+
225+
// Generate a 3x3 matrix of pseudorandom numbers:
226+
var x = random.generate( [ 3, 3 ], 2.0, 3.0, 0.0 );
227+
console.log( ndarray2array( x ) );
228+
229+
// Generate another matrix with a specified data type:
230+
x = random.generate( [ 3, 3 ], 2.0, 3.0, 0.0, {
231+
'dtype': 'float32'
232+
});
233+
console.log( ndarray2array( x ) );
234+
235+
// Define arrays of distribution parameters:
236+
var param1 = new ndarray( 'generic', [ 1.0, 10.0, 100.0 ], [ 3, 1 ], [ 1, 1 ], 0, 'row-major' );
237+
var param2 = new ndarray( 'generic', [ 2.0, 20.0, 200.0 ], [ 3, 1 ], [ 1, 1 ], 0, 'row-major' );
238+
var param3 = new ndarray( 'generic', [ 0.0, 100.0, 200.0 ], [ 3, 1 ], [ 1, 1 ], 0, 'row-major' );
239+
240+
// Broadcast the parameters to generate another 3x3 matrix of pseudorandom numbers:
241+
x = random.generate( [ 3, 3 ], param1, param2, param3 );
242+
console.log( ndarray2array( x ) );
243+
```
244+
245+
</section>
246+
247+
<!-- /.examples -->
248+
249+
<!-- 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. -->
250+
251+
<section class="references">
252+
253+
</section>
254+
255+
<!-- /.references -->
256+
257+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
258+
259+
<section class="related">
260+
261+
</section>
262+
263+
<!-- /.related -->
264+
265+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
266+
267+
<section class="links">
268+
269+
[@stdlib/ndarray/policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/policies
270+
271+
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders
272+
273+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
274+
275+
</section>
276+
277+
<!-- /.links -->
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var dtypes = require( '@stdlib/ndarray/dtypes' );
27+
var frechet = require( '@stdlib/random/base/frechet' );
28+
var zeros = require( '@stdlib/ndarray/zeros' );
29+
var format = require( '@stdlib/string/format' );
30+
var pkg = require( './../package.json' ).name;
31+
var Random = require( './../lib' );
32+
33+
34+
// FUNCTIONS //
35+
36+
/**
37+
* Creates a benchmark function.
38+
*
39+
* @private
40+
* @param {PositiveInteger} len - array length
41+
* @returns {Function} benchmark function
42+
*/
43+
function createBenchmark( len ) {
44+
var policies;
45+
var random;
46+
var out;
47+
var dt;
48+
49+
dt = dtypes( 'real_floating_point' );
50+
policies = {
51+
'output': 'real_floating_point',
52+
'casting': 'none'
53+
};
54+
random = new Random( frechet, [ dt, dt, dt ], dt, policies );
55+
56+
out = zeros( [ len ], {
57+
'dtype': 'float64'
58+
});
59+
60+
return benchmark;
61+
62+
/**
63+
* Benchmark function.
64+
*
65+
* @private
66+
* @param {Benchmark} b - benchmark instance
67+
*/
68+
function benchmark( b ) {
69+
var o;
70+
var i;
71+
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
o = random.assign( 2.0, 3.0, 0.0, out );
75+
if ( typeof o !== 'object' ) {
76+
b.fail( 'should return an ndarray' );
77+
}
78+
}
79+
b.toc();
80+
if ( isnan( o.get( i%len ) ) ) {
81+
b.fail( 'should not return NaN' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
}
86+
}
87+
88+
89+
// MAIN //
90+
91+
/**
92+
* Main execution sequence.
93+
*
94+
* @private
95+
*/
96+
function main() {
97+
var len;
98+
var min;
99+
var max;
100+
var f;
101+
var i;
102+
103+
min = 1; // 10^min
104+
max = 6; // 10^max
105+
106+
for ( i = min; i <= max; i++ ) {
107+
len = pow( 10, i );
108+
f = createBenchmark( len );
109+
bench( format( '%s:assign:len=%d', pkg, len ), f );
110+
}
111+
}
112+
113+
main();

0 commit comments

Comments
 (0)