Repository navigation
feat: add lapack/base/dlaruv
#12443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add lapack/base/dlaruv
#12443
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3a73dd7
feat: add lapack/base/dlaruv
iampratik13 f07365b
chore: suggested changes
iampratik13 bfea793
fix: update signatures and parameter naming
iampratik13 251080f
Merge branch 'develop' into lapack/dlaruv
anandkaranubc e4debd5
chore: code review
anandkaranubc 696ca0f
Apply suggestions from code review
kgryte f73013c
Apply suggestions from code review
kgryte 563c843
Apply suggestions from code review
kgryte 8a3aadc
Apply suggestions from code review
kgryte 4062d7b
docs: fix comment
kgryte 06ea6e0
docs: add reference
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,261 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2026 The Stdlib Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
|
|
||
| # dlaruv | ||
|
|
||
| > Return a vector of `N` random real numbers drawn from a uniform (0,1) distribution. | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var dlaruv = require( '@stdlib/lapack/base/dlaruv' ); | ||
| ``` | ||
|
|
||
| #### dlaruv( seed, N, x ) | ||
|
|
||
| Returns a vector of `N` random real numbers drawn from a uniform (0,1) distribution. | ||
|
|
||
| ```javascript | ||
| var Int32Array = require( '@stdlib/array/int32' ); | ||
| var Float64Array = require( '@stdlib/array/float64' ); | ||
|
|
||
| var seed = new Int32Array( [ 0, 1, 2, 3 ] ); | ||
| var x = new Float64Array( 3 ); | ||
|
|
||
| dlaruv( seed, 3, x ); | ||
| // x => <Float64Array>[ ~0.1319, ~0.2338, ~0.3216 ] | ||
| ``` | ||
|
|
||
| The function has the following parameters: | ||
|
|
||
| - **seed**: [`Int32Array`][@stdlib/array/int32] seed array of four integers. Each element must be between `0` and `4095`, and `seed[3]` must be odd. On exit, the seed is updated. | ||
| - **N**: number of random numbers to generate. Must be at most `128`. | ||
| - **x**: output [`Float64Array`][@stdlib/array/float64]. | ||
|
|
||
| Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. | ||
|
|
||
| <!-- eslint-disable stdlib/capitalized-comments --> | ||
|
|
||
| ```javascript | ||
| var Int32Array = require( '@stdlib/array/int32' ); | ||
| var Float64Array = require( '@stdlib/array/float64' ); | ||
|
|
||
| // Initial arrays... | ||
| var seed0 = new Int32Array( [ 0, 0, 1, 2, 3 ] ); | ||
| var x0 = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); | ||
|
|
||
| // Create offset views... | ||
| var seed1 = new Int32Array( seed0.buffer, seed0.BYTES_PER_ELEMENT*1 ); // start at 2nd element | ||
| var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element | ||
|
|
||
| dlaruv( seed1, 3, x1 ); | ||
| // x0 => <Float64Array>[ 0.0, ~0.1319, ~0.2338, ~0.3216 ] | ||
| ``` | ||
|
|
||
| #### dlaruv.ndarray( N, seed, strideS, offsetS, x, strideX, offsetX ) | ||
|
|
||
| Returns a vector of `N` random real numbers drawn from a uniform (0,1) distribution using alternative indexing semantics. | ||
|
|
||
| ```javascript | ||
| var Int32Array = require( '@stdlib/array/int32' ); | ||
| var Float64Array = require( '@stdlib/array/float64' ); | ||
|
|
||
| var seed = new Int32Array( [ 0, 1, 2, 3 ] ); | ||
| var x = new Float64Array( 3 ); | ||
|
|
||
| dlaruv.ndarray( 3, seed, 1, 0, x, 1, 0 ); | ||
| // x => <Float64Array>[ ~0.1319, ~0.2338, ~0.3216 ] | ||
| ``` | ||
|
|
||
| The function has the following additional parameters: | ||
|
|
||
| - **strideS**: stride length for `seed`. | ||
| - **offsetS**: starting index for `seed`. | ||
| - **strideX**: stride length for `x`. | ||
| - **offsetX**: starting index for `x`. | ||
|
|
||
| While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, | ||
|
|
||
| <!-- eslint-disable max-len --> | ||
|
|
||
| ```javascript | ||
| var Int32Array = require( '@stdlib/array/int32' ); | ||
| var Float64Array = require( '@stdlib/array/float64' ); | ||
|
|
||
| var seed = new Int32Array( [ 0, 0, 1, 2, 3 ] ); | ||
| var x = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); | ||
|
|
||
| dlaruv.ndarray( 3, seed, 1, 1, x, 1, 2 ); | ||
| // x => <Float64Array>[ 0.0, 0.0, ~0.1319, ~0.2338, ~0.3216 ] | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - `seed` is updated in-place and should be reused across invocations in order to generate a continuous stream of random numbers. | ||
| - At most `128` random numbers are generated per invocation. If `N > 128`, only the first `128` indexed elements of `x` are updated. | ||
| - If `N <= 0`, the functions return `x` unchanged and do not update `seed`. | ||
| - Generated values are always on the open interval `(0,1)`, as the largest representable output is `1 - 2^-48`. | ||
| - `dlaruv()` corresponds to the [LAPACK][LAPACK] function [`dlaruv`][lapack-dlaruv]. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var Int32Array = require( '@stdlib/array/int32' ); | ||
| var Float64Array = require( '@stdlib/array/float64' ); | ||
| var dlaruv = require( '@stdlib/lapack/base/dlaruv' ); | ||
|
|
||
| var seed = new Int32Array( [ 1, 23, 456, 3795 ] ); | ||
| var x = new Float64Array( 10 ); | ||
|
|
||
| dlaruv( seed, x.length, x ); | ||
|
|
||
| console.log( x ); | ||
| console.log( seed ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- C interface documentation. --> | ||
|
|
||
| * * * | ||
|
|
||
| <section class="c"> | ||
|
|
||
| ## C APIs | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- C usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ### Usage | ||
|
|
||
| ```c | ||
| TODO | ||
| ``` | ||
|
|
||
| #### TODO | ||
|
|
||
| TODO. | ||
|
|
||
| ```c | ||
| TODO | ||
| ``` | ||
|
|
||
| TODO | ||
|
|
||
| ```c | ||
| TODO | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- C API usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ### Examples | ||
|
|
||
| ```c | ||
| TODO | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.c --> | ||
|
|
||
| * * * | ||
|
|
||
| <section class="references"> | ||
|
|
||
| ## References | ||
|
|
||
| - Fishman, George S. 1990. "Multiplicative Congruential Random Number Generators with Modulus 2^β: An Exhaustive Analysis for β = 32 and a Partial Analysis for β = 48." _Mathematics of Computation_ 54 (189). American Mathematical Society: 331–44. doi:[10.2307/2008698][@fishman1990a]. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.references --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [lapack]: https://www.netlib.org/lapack/explore-html/ | ||
|
|
||
| [lapack-dlaruv]: https://netlib.org/lapack/explore-html/d9/d0f/group__laruv.html | ||
|
|
||
| [@stdlib/array/int32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/int32 | ||
|
|
||
| [@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64 | ||
|
|
||
| [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray | ||
|
|
||
| [@fishman1990a]: https://doi.org/10.2307/2008698 | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
97 changes: 97 additions & 0 deletions
97
lib/node_modules/@stdlib/lapack/base/dlaruv/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var Int32Array = require( '@stdlib/array/int32' ); | ||
| var Float64Array = require( '@stdlib/array/float64' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var pow = require( '@stdlib/math/base/special/pow' ); | ||
| var format = require( '@stdlib/string/format' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var dlaruv = require( './../lib/dlaruv.js' ); | ||
|
|
||
|
|
||
| // FUNCTIONS // | ||
|
|
||
| /** | ||
| * Creates a benchmark function. | ||
| * | ||
| * @private | ||
| * @param {PositiveInteger} len - array length | ||
| * @returns {Function} benchmark function | ||
| */ | ||
| function createBenchmark( len ) { | ||
| var seed = new Int32Array( [ 1619, 1381, 1011, 2459 ] ); | ||
| var x = new Float64Array( len ); | ||
| return benchmark; | ||
|
|
||
| /** | ||
| * Benchmark function. | ||
| * | ||
| * @private | ||
| * @param {Benchmark} b - benchmark instance | ||
| */ | ||
| function benchmark( b ) { | ||
| var i; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| dlaruv( seed, len, x ); | ||
| if ( isnan( x[ 0 ] ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( x[ 0 ] ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| /** | ||
| * Main execution sequence. | ||
| * | ||
| * @private | ||
| */ | ||
| function main() { | ||
| var len; | ||
| var max; | ||
| var min; | ||
| var f; | ||
| var i; | ||
|
|
||
| min = 0; // 2^min | ||
| max = 7; // 2^max (capped at 128) | ||
|
|
||
| for ( i = min; i <= max; i++ ) { | ||
| len = pow( 2, i ); | ||
| f = createBenchmark( len ); | ||
| bench( format( '%s:len=%d', pkg, len ), f ); | ||
| } | ||
| } | ||
|
|
||
| main(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is likely fine to have this be an
Int32Array, especially given the range of desired seed values. There is some mild concern that this should be the same asCBLAS_INT(which could be eitherint64orint32depending). We can revisit once we add the C implementation.