|
| 1 | +/* |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 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 | +// TypeScript Version: 4.1 |
| 20 | + |
| 21 | +/// <reference types="@stdlib/types"/> |
| 22 | + |
| 23 | +import { DataType, Order, Mode, Shape, OutputPolicy, typedndarray } from '@stdlib/types/ndarray'; |
| 24 | +import { ArrayLike } from '@stdlib/types/array'; |
| 25 | +import * as random from '@stdlib/types/random'; |
| 26 | + |
| 27 | +/** |
| 28 | +* Interface defining factory options. |
| 29 | +*/ |
| 30 | +interface FactoryOptions { |
| 31 | + /** |
| 32 | + * Default memory layout. |
| 33 | + */ |
| 34 | + order?: Order; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | +* Interface defining PRNG options. |
| 39 | +*/ |
| 40 | +interface PRNGOptions { |
| 41 | + /** |
| 42 | + * Pseudorandom number generator which generates uniformly distributed pseudorandom numbers. |
| 43 | + */ |
| 44 | + prng?: random.PRNG; |
| 45 | + |
| 46 | + /** |
| 47 | + * Pseudorandom number generator seed. |
| 48 | + */ |
| 49 | + seed?: random.PRNGSeedMT19937; |
| 50 | + |
| 51 | + /** |
| 52 | + * Pseudorandom number generator state. |
| 53 | + */ |
| 54 | + state?: random.PRNGStateMT19937; |
| 55 | + |
| 56 | + /** |
| 57 | + * Specifies whether to copy a provided pseudorandom number generator state. Default: true. |
| 58 | + */ |
| 59 | + copy?: boolean; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | +* Interface defining options when generating pseudorandom values. |
| 64 | +*/ |
| 65 | +interface Options { |
| 66 | + /** |
| 67 | + * Output ndarray data type. |
| 68 | + * |
| 69 | + * ## Notes |
| 70 | + * |
| 71 | + * - Setting this option overrides the output data type policy. |
| 72 | + */ |
| 73 | + dtype?: DataType; |
| 74 | + |
| 75 | + /** |
| 76 | + * Memory layout. |
| 77 | + * |
| 78 | + * ## Notes |
| 79 | + * |
| 80 | + * - Setting this option overrides the default memory layout. |
| 81 | + */ |
| 82 | + order?: Order; |
| 83 | + |
| 84 | + /** |
| 85 | + * Specifies how to handle indices which exceed ndarray dimensions. |
| 86 | + */ |
| 87 | + mode?: Mode; |
| 88 | + |
| 89 | + /** |
| 90 | + * Specifies how to handle subscripts which exceed ndarray dimensions on a per dimension basis. |
| 91 | + */ |
| 92 | + submode?: ArrayLike<Mode>; |
| 93 | + |
| 94 | + /** |
| 95 | + * Boolean indicating whether an ndarray should be read-only. |
| 96 | + */ |
| 97 | + readonly?: boolean; |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | +* Interface defining class policies. |
| 102 | +*/ |
| 103 | +interface Policies { |
| 104 | + /** |
| 105 | + * Output data type policy. |
| 106 | + */ |
| 107 | + output: OutputPolicy; |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | +* Interface for PRNG properties and methods. |
| 112 | +*/ |
| 113 | +interface PRNG { |
| 114 | + /** |
| 115 | + * Underlying pseudorandom number generator. |
| 116 | + */ |
| 117 | + readonly PRNG: random.PRNG; |
| 118 | + |
| 119 | + /** |
| 120 | + * PRNG seed. |
| 121 | + */ |
| 122 | + readonly seed: random.PRNGSeedMT19937 | null; |
| 123 | + |
| 124 | + /** |
| 125 | + * PRNG seed length. |
| 126 | + */ |
| 127 | + readonly seedLength: number | null; |
| 128 | + |
| 129 | + /** |
| 130 | + * PRNG state. |
| 131 | + */ |
| 132 | + state: random.PRNGStateMT19937 | null; |
| 133 | + |
| 134 | + /** |
| 135 | + * PRNG state length. |
| 136 | + */ |
| 137 | + readonly stateLength: number | null; |
| 138 | + |
| 139 | + /** |
| 140 | + * PRNG state size (in bytes). |
| 141 | + */ |
| 142 | + readonly byteLength: number | null; |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | +* Interface for generating pseudorandom values drawn from a unary PRNG. |
| 147 | +*/ |
| 148 | +interface UnaryPRNG<T, U> extends random.PRNG { |
| 149 | + /** |
| 150 | + * Returns a pseudorandom value. |
| 151 | + * |
| 152 | + * @param param1 - PRNG parameter |
| 153 | + * @returns pseudorandom value |
| 154 | + */ |
| 155 | + ( param1: T ): U; |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | +* Interface for creating ndarrays filled with pseudorandom values drawn from a unary PRNG. |
| 160 | +*/ |
| 161 | +interface Random<T, U> extends PRNG { |
| 162 | + /** |
| 163 | + * Returns an ndarray filled with pseudorandom values drawn from a unary PRNG. |
| 164 | + * |
| 165 | + * @param shape - output shape |
| 166 | + * @param param1 - PRNG parameter |
| 167 | + * @param options - function options |
| 168 | + * @returns output ndarray |
| 169 | + * |
| 170 | + * @example |
| 171 | + * var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 172 | + * var exponential = require( '@stdlib/random/base/exponential' ); |
| 173 | + * |
| 174 | + * var idt = dtypes( 'real_and_generic' ); |
| 175 | + * var odt = dtypes( 'real_floating_point_and_generic' ); |
| 176 | + * |
| 177 | + * var policies = { |
| 178 | + * 'output': 'real_floating_point_and_generic' |
| 179 | + * }; |
| 180 | + * var options = { |
| 181 | + * 'order': 'row-major' |
| 182 | + * }; |
| 183 | + * |
| 184 | + * var rand = new Random( exponential, idt, odt, policies, options ); |
| 185 | + * |
| 186 | + * var v = rand( [ 2, 2 ], 2.0 ); |
| 187 | + * // returns <ndarray> |
| 188 | + */ |
| 189 | + ( shape: Shape, param1: T | typedndarray<T>, options?: Options ): typedndarray<U>; |
| 190 | + |
| 191 | + /** |
| 192 | + * Fills an ndarray with pseudorandom values drawn from a unary PRNG. |
| 193 | + * |
| 194 | + * @param param1 - PRNG parameter |
| 195 | + * @param out - output ndarray |
| 196 | + * @returns output ndarray |
| 197 | + * |
| 198 | + * @example |
| 199 | + * var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 200 | + * var ndzeros = require( '@stdlib/ndarray/zeros' ); |
| 201 | + * var exponential = require( '@stdlib/random/base/exponential' ); |
| 202 | + * |
| 203 | + * var idt = dtypes( 'real_and_generic' ); |
| 204 | + * var odt = dtypes( 'real_floating_point_and_generic' ); |
| 205 | + * |
| 206 | + * var policies = { |
| 207 | + * 'output': 'real_floating_point_and_generic' |
| 208 | + * }; |
| 209 | + * var options = { |
| 210 | + * 'order': 'row-major' |
| 211 | + * }; |
| 212 | + * |
| 213 | + * var rand = new Random( exponential, idt, odt, policies, options ); |
| 214 | + * |
| 215 | + * var out = ndzeros( [ 2, 2 ] ); |
| 216 | + * var v = rand.assign( 2.0, out ); |
| 217 | + * // returns <ndarray> |
| 218 | + * |
| 219 | + * var bool = ( v === out ); |
| 220 | + * // returns true |
| 221 | + */ |
| 222 | + assign( param1: T | typedndarray<T>, out: typedndarray<U> ): typedndarray<U>; |
| 223 | +} |
| 224 | + |
| 225 | +/** |
| 226 | +* Returns a function for generating pseudorandom values drawn from a unary PRNG. |
| 227 | +* |
| 228 | +* @param options - function options |
| 229 | +* @returns function for creating ndarrays |
| 230 | +*/ |
| 231 | +type Factory<T, U> = ( options?: PRNGOptions ) => Random<T, U>; |
| 232 | + |
| 233 | +/** |
| 234 | +* Returns a function for generating pseudorandom values drawn from a unary PRNG. |
| 235 | +* |
| 236 | +* @param prng - unary pseudorandom value generator |
| 237 | +* @param idtypes - list of supported input data types |
| 238 | +* @param odtypes - list of supported output data types |
| 239 | +* @param policies - dispatch policies |
| 240 | +* @param options - function options |
| 241 | +* @returns instance |
| 242 | +* |
| 243 | +* @example |
| 244 | +* var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 245 | +* var exponential = require( '@stdlib/random/base/exponential' ); |
| 246 | +* |
| 247 | +* var idt = dtypes( 'real_and_generic' ); |
| 248 | +* var odt = dtypes( 'real_floating_point_and_generic' ); |
| 249 | +* |
| 250 | +* var policies = { |
| 251 | +* 'output': 'real_floating_point_and_generic' |
| 252 | +* }; |
| 253 | +* var options = { |
| 254 | +* 'order': 'row-major' |
| 255 | +* }; |
| 256 | +* |
| 257 | +* var factory = createFactory( exponential, idt, odt, policies, options ); |
| 258 | +* |
| 259 | +* var rand = factory(); |
| 260 | +* // returns <Function> |
| 261 | +* |
| 262 | +* var v = rand( [ 2, 2 ], 2.0 ); |
| 263 | +* // returns <ndarray> |
| 264 | +* |
| 265 | +* @example |
| 266 | +* var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 267 | +* var ndzeros = require( '@stdlib/ndarray/zeros' ); |
| 268 | +* var exponential = require( '@stdlib/random/base/exponential' ); |
| 269 | +* |
| 270 | +* var idt = dtypes( 'real_and_generic' ); |
| 271 | +* var odt = dtypes( 'real_floating_point_and_generic' ); |
| 272 | +* |
| 273 | +* var policies = { |
| 274 | +* 'output': 'real_floating_point_and_generic' |
| 275 | +* }; |
| 276 | +* var options = { |
| 277 | +* 'order': 'row-major' |
| 278 | +* }; |
| 279 | +* |
| 280 | +* var factory = createFactory( exponential, idt, odt, policies, options ); |
| 281 | +* |
| 282 | +* var rand = factory(); |
| 283 | +* // returns <Function> |
| 284 | +* |
| 285 | +* var out = ndzeros( [ 2, 2 ] ); |
| 286 | +* var v = rand.assign( 2.0, out ); |
| 287 | +* // returns <ndarray> |
| 288 | +* |
| 289 | +* var bool = ( v === out ); |
| 290 | +* // returns true |
| 291 | +*/ |
| 292 | +declare function createFactory<T = unknown, U = unknown>( prng: UnaryPRNG<T, U>, idtypes: ArrayLike<DataType>, odtypes: ArrayLike<DataType>, policies: Policies, options?: FactoryOptions ): Factory<T, U>; |
| 293 | + |
| 294 | + |
| 295 | +// EXPORTS // |
| 296 | + |
| 297 | +export = createFactory; |
0 commit comments