From 8156dc14a12208537483372257930672aba0e6c3 Mon Sep 17 00:00:00 2001 From: Siddhram Date: Sat, 7 Nov 2026 23:57:17 +0530 Subject: [PATCH 1/2] feat: add blas/ext/base/ndarray/dreplicate --- .../ext/base/ndarray/dreplicate/README.md | 136 ++++++ .../ndarray/dreplicate/benchmark/benchmark.js | 115 +++++ .../ext/base/ndarray/dreplicate/docs/repl.txt | 37 ++ .../ndarray/dreplicate/docs/types/index.d.ts | 62 +++ .../ndarray/dreplicate/docs/types/test.ts | 71 +++ .../base/ndarray/dreplicate/examples/index.js | 43 ++ .../ext/base/ndarray/dreplicate/lib/index.js | 56 +++ .../ext/base/ndarray/dreplicate/lib/main.js | 84 ++++ .../ext/base/ndarray/dreplicate/package.json | 72 +++ .../ext/base/ndarray/dreplicate/test/test.js | 434 ++++++++++++++++++ 10 files changed, 1110 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/test/test.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/README.md new file mode 100644 index 000000000000..54d5934793f5 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/README.md @@ -0,0 +1,136 @@ + + +# dreplicate + +> Replicate each element in a one-dimensional double-precision floating-point ndarray a specified number of times. + +
+ +
+ + + +
+ +## Usage + +```javascript +var dreplicate = require( '@stdlib/blas/ext/base/ndarray/dreplicate' ); +``` + +#### dreplicate( arrays ) + +Replicates each element in a one-dimensional double-precision floating-point ndarray a specified number of times. + +```javascript +var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var zeros = require( '@stdlib/ndarray/zeros' ); + +var x = new Float64Vector( [ 1.0, 2.0, 3.0 ] ); +var out = zeros( [ 6 ], { + 'dtype': 'float64' +}); + +var k = scalar2ndarray( 2, { + 'dtype': 'generic' +}); + +var v = dreplicate( [ x, out, k ] ); +// returns [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] + +var bool = ( v === out ); +// returns true +``` + +The function has the following parameters: + +- **arrays**: array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a one-dimensional output ndarray. Must have `N * k` elements, where `N` is the number of elements in the input ndarray, and `k` is the number of times to replicate each element. + - a zero-dimensional ndarray specifying the number of times to replicate each element. + +
+ + + +
+ +## Notes + +- If the input ndarray is empty, or if `k` is less than or equal to zero, the function returns the output ndarray unchanged. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var dreplicate = require( '@stdlib/blas/ext/base/ndarray/dreplicate' ); + +var opts = { + 'dtype': 'float64' +}; + +var x = discreteUniform( [ 5 ], -100, 100, opts ); +console.log( ndarray2array( x ) ); + +var k = scalar2ndarray( 3, { + 'dtype': 'generic' +}); + +var out = zeros( [ 15 ], opts ); +console.log( ndarray2array( out ) ); + +var v = dreplicate( [ x, out, k ] ); +console.log( ndarray2array( v ) ); +``` + +
+ + + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/benchmark/benchmark.js new file mode 100644 index 000000000000..a61d3440f60e --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/benchmark/benchmark.js @@ -0,0 +1,115 @@ +/** +* @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 uniform = require( '@stdlib/random/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var dreplicate = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out; + var x; + var k; + + x = uniform( [ len ], 0.0, 100.0, options ); + out = zeros( [ len*2 ], options ); + k = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = dreplicate( [ x, out, k ] ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get( i%( len*2 ) ) ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:len=%d', pkg, len ), f ); + } +} + +main(); + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/repl.txt new file mode 100644 index 000000000000..619b073fae47 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/repl.txt @@ -0,0 +1,37 @@ + +{{alias}}( arrays ) + Replicates each element in a one-dimensional double-precision floating-point + ndarray a specified number of times. + + If the input ndarray is empty, or if `k` is less than or equal to zero, the + function returns the output ndarray unchanged. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a one-dimensional output ndarray. Must have `N * k` elements, where + `N` is the number of elements in the input ndarray, and `k` is the + number of times to replicate each element. + - a zero-dimensional ndarray specifying the number of times to replicate + each element. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/vector/float64}}( [ 1.0, 2.0, 3.0 ] ); + > var opts = { 'dtype': 'float64' }; + > var out = {{alias:@stdlib/ndarray/zeros}}( [ 6 ], opts ); + > var k = {{alias:@stdlib/ndarray/from-scalar}}( 2, { 'dtype': 'generic' } ); + > {{alias}}( [ x, out, k ] ) + [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/index.d.ts new file mode 100644 index 000000000000..deb973869a33 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/index.d.ts @@ -0,0 +1,62 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { float64ndarray, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Replicates each element in a one-dimensional double-precision floating-point ndarray a specified number of times. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a one-dimensional output ndarray. Must have `N * k` elements, where `N` is the number of elements in the input ndarray, and `k` is the number of times to replicate each element. +* - a zero-dimensional ndarray specifying the number of times to replicate each element. +* +* @param arrays - array-like object containing ndarrays +* @returns output ndarray +* +* @example +* var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = new Float64Vector( [ 1.0, 2.0, 3.0 ] ); +* var out = zeros( [ 6 ], { +* 'dtype': 'float64' +* }); +* +* var k = scalar2ndarray( 2, { +* 'dtype': 'generic' +* }); +* +* var v = dreplicate( [ x, out, k ] ); +* // returns [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] +*/ +declare function dreplicate( arrays: [ float64ndarray, float64ndarray, typedndarray ] ): float64ndarray; + + +// EXPORTS // + +export = dreplicate; + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/test.ts new file mode 100644 index 000000000000..4a2a827c0445 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/test.ts @@ -0,0 +1,71 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +import dreplicate = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 3 ], { + 'dtype': 'float64' + }); + const out = zeros( [ 6 ], { + 'dtype': 'float64' + }); + const k = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + + dreplicate( [ x, out, k ] ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + dreplicate( '10' ); // $ExpectError + dreplicate( 10 ); // $ExpectError + dreplicate( true ); // $ExpectError + dreplicate( false ); // $ExpectError + dreplicate( null ); // $ExpectError + dreplicate( undefined ); // $ExpectError + dreplicate( [] ); // $ExpectError + dreplicate( {} ); // $ExpectError + dreplicate( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 3 ], { + 'dtype': 'float64' + }); + const out = zeros( [ 6 ], { + 'dtype': 'float64' + }); + const k = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + + dreplicate(); // $ExpectError + dreplicate( [ x, out, k ], {} ); // $ExpectError +} + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/examples/index.js new file mode 100644 index 000000000000..e6d320a77321 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/examples/index.js @@ -0,0 +1,43 @@ +/** +* @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'; + +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var dreplicate = require( './../lib' ); + +var opts = { + 'dtype': 'float64' +}; + +var x = discreteUniform( [ 5 ], -100, 100, opts ); +console.log( ndarray2array( x ) ); + +var k = scalar2ndarray( 3, { + 'dtype': 'generic' +}); + +var out = zeros( [ 15 ], opts ); +console.log( ndarray2array( out ) ); + +var v = dreplicate( [ x, out, k ] ); +console.log( ndarray2array( v ) ); + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/lib/index.js new file mode 100644 index 000000000000..912daa57d751 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/lib/index.js @@ -0,0 +1,56 @@ +/** +* @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'; + +/** +* Replicate each element in a one-dimensional double-precision floating-point ndarray a specified number of times. +* +* @module @stdlib/blas/ext/base/ndarray/dreplicate +* +* @example +* var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* var dreplicate = require( '@stdlib/blas/ext/base/ndarray/dreplicate' ); +* +* var x = new Float64Vector( [ 1.0, 2.0, 3.0 ] ); +* var out = zeros( [ 6 ], { +* 'dtype': 'float64' +* }); +* +* var k = scalar2ndarray( 2, { +* 'dtype': 'generic' +* }); +* +* var v = dreplicate( [ x, out, k ] ); +* // returns [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] +* +* var bool = ( v === out ); +* // returns true +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/lib/main.js new file mode 100644 index 000000000000..9daec824d7e4 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/lib/main.js @@ -0,0 +1,84 @@ +/** +* @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 numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); +var strided = require( '@stdlib/blas/ext/base/dreplicate' ).ndarray; + + +// MAIN // + +/** +* Replicates each element in a one-dimensional double-precision floating-point ndarray a specified number of times. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a one-dimensional output ndarray. Must have `N * k` elements, where `N` is the number of elements in the input ndarray, and `k` is the number of times to replicate each element. +* - a zero-dimensional ndarray specifying the number of times to replicate each element. +* +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @returns {ndarray} output ndarray +* +* @example +* var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = new Float64Vector( [ 1.0, 2.0, 3.0 ] ); +* var out = zeros( [ 6 ], { +* 'dtype': 'float64' +* }); +* +* var k = scalar2ndarray( 2, { +* 'dtype': 'generic' +* }); +* +* var v = dreplicate( [ x, out, k ] ); +* // returns [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] +* +* var bool = ( v === out ); +* // returns true +*/ +function dreplicate( arrays ) { + var out; + var x; + var k; + + x = arrays[ 0 ]; + out = arrays[ 1 ]; + k = ndarraylike2scalar( arrays[ 2 ] ); + + strided( numelDimension( x, 0 ), k, getData( x ), getStride( x, 0 ), getOffset( x ), getData( out ), getStride( out, 0 ), getOffset( out ) ); // eslint-disable-line max-len + return out; +} + + +// EXPORTS // + +module.exports = dreplicate; + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/package.json new file mode 100644 index 000000000000..9e1a25509ae1 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/dreplicate", + "version": "0.0.0", + "description": "Replicate each element in a one-dimensional double-precision floating-point ndarray a specified number of times.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "blas", + "extended", + "ext", + "dreplicate", + "replicate", + "repeat", + "copy", + "vector", + "strided", + "array", + "ndarray", + "float64", + "double" + ], + "__stdlib__": {} +} + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/test/test.js new file mode 100644 index 000000000000..2fd507d78f5c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/test/test.js @@ -0,0 +1,434 @@ +/** +* @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 tape = require( 'tape' ); +var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); +var Float64Array = require( '@stdlib/array/float64' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var dreplicate = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Float64Array} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float64', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + +/** +* Returns a zero-dimensional ndarray. +* +* @private +* @param {number} v - scalar value +* @returns {ndarray} zero-dimensional ndarray +*/ +function scalar( v ) { + return scalar2ndarray( v, { + 'dtype': 'generic' + }); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dreplicate, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( dreplicate.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function replicates each strided array element', function test( t ) { + var expected; + var actual; + var xbuf; + var ybuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + ybuf = new Float64Array( 6 ); + x = vector( xbuf, 3, 1, 0 ); + out = vector( ybuf, 6, 1, 0 ); + + actual = dreplicate( [ x, out, scalar( 2 ) ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float64Array( [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] ); + t.strictEqual( isSameFloat64Array( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + ybuf = new Float64Array( 12 ); + x = vector( xbuf, 4, 1, 0 ); + out = vector( ybuf, 12, 1, 0 ); + + actual = dreplicate( [ x, out, scalar( 3 ) ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float64Array( [ 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0 ] ); + t.strictEqual( isSameFloat64Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function replicates each strided array element (k=1)', function test( t ) { + var expected; + var actual; + var xbuf; + var ybuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + ybuf = new Float64Array( 3 ); + x = vector( xbuf, 3, 1, 0 ); + out = vector( ybuf, 3, 1, 0 ); + + actual = dreplicate( [ x, out, scalar( 1 ) ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + t.strictEqual( isSameFloat64Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function replicates each strided array element (N=1)', function test( t ) { + var expected; + var actual; + var xbuf; + var ybuf; + var out; + var x; + + xbuf = new Float64Array( [ 5.0 ] ); + ybuf = new Float64Array( 3 ); + x = vector( xbuf, 1, 1, 0 ); + out = vector( ybuf, 3, 1, 0 ); + + actual = dreplicate( [ x, out, scalar( 3 ) ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float64Array( [ 5.0, 5.0, 5.0 ] ); + t.strictEqual( isSameFloat64Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a reference to the output ndarray', function test( t ) { + var actual; + var xbuf; + var ybuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + ybuf = new Float64Array( 6 ); + x = vector( xbuf, 3, 1, 0 ); + out = vector( ybuf, 6, 1, 0 ); + + actual = dreplicate( [ x, out, scalar( 2 ) ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + t.end(); +}); + +tape( 'if the input ndarray is empty, the function returns the output ndarray unchanged', function test( t ) { + var expected; + var actual; + var ybuf; + var out; + var x; + + ybuf = new Float64Array( [ 6.0, 7.0, 8.0 ] ); + x = vector( new Float64Array( 0 ), 0, 1, 0 ); + out = vector( ybuf, 3, 1, 0 ); + + actual = dreplicate( [ x, out, scalar( 2 ) ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float64Array( [ 6.0, 7.0, 8.0 ] ); + t.strictEqual( isSameFloat64Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `k` parameter less than or equal to 0, the function returns the output ndarray unchanged', function test( t ) { + var expected; + var actual; + var xbuf; + var ybuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + ybuf = new Float64Array( [ 6.0, 7.0, 8.0 ] ); + x = vector( xbuf, 3, 1, 0 ); + out = vector( ybuf, 3, 1, 0 ); + + actual = dreplicate( [ x, out, scalar( 0 ) ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float64Array( [ 6.0, 7.0, 8.0 ] ); + t.strictEqual( isSameFloat64Array( getData( actual ), expected ), true, 'returns expected value' ); + + actual = dreplicate( [ x, out, scalar( -1 ) ] ); + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports input ndarrays having non-unit strides', function test( t ) { + var expected; + var actual; + var xbuf; + var ybuf; + var out; + var x; + + xbuf = new Float64Array([ + 1.0, // 0 + 99.0, + 2.0, // 1 + 99.0, + 3.0 // 2 + ]); + ybuf = new Float64Array( 6 ); + x = vector( xbuf, 3, 2, 0 ); + out = vector( ybuf, 6, 1, 0 ); + + actual = dreplicate( [ x, out, scalar( 2 ) ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float64Array( [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] ); + t.strictEqual( isSameFloat64Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports output ndarrays having non-unit strides', function test( t ) { + var expected; + var actual; + var xbuf; + var ybuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + ybuf = new Float64Array([ + 0.0, // 0 + 99.0, + 0.0, // 1 + 99.0, + 0.0, // 2 + 99.0, + 0.0, // 3 + 99.0, + 0.0, // 4 + 99.0, + 0.0, // 5 + 99.0 + ]); + x = vector( xbuf, 3, 1, 0 ); + out = vector( ybuf, 6, 2, 0 ); + + actual = dreplicate( [ x, out, scalar( 2 ) ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float64Array( [ 1.0, 99.0, 1.0, 99.0, 2.0, 99.0, 2.0, 99.0, 3.0, 99.0, 3.0, 99.0 ] ); + t.strictEqual( isSameFloat64Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative strides', function test( t ) { + var expected; + var actual; + var xbuf; + var ybuf; + var out; + var x; + + xbuf = new Float64Array([ + 1.0, // 2 + 2.0, // 1 + 3.0 // 0 + ]); + ybuf = new Float64Array([ + 0.0, // 5 + 0.0, // 4 + 0.0, // 3 + 0.0, // 2 + 0.0, // 1 + 0.0 // 0 + ]); + x = vector( xbuf, 3, -1, 2 ); + out = vector( ybuf, 6, -1, 5 ); + + actual = dreplicate( [ x, out, scalar( 2 ) ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float64Array( [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] ); + t.strictEqual( isSameFloat64Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports input ndarrays having non-zero offsets', function test( t ) { + var expected; + var actual; + var xbuf; + var ybuf; + var out; + var x; + + xbuf = new Float64Array([ + 99.0, + 99.0, + 1.0, // 0 + 2.0, // 1 + 3.0 // 2 + ]); + ybuf = new Float64Array( 6 ); + x = vector( xbuf, 3, 1, 2 ); + out = vector( ybuf, 6, 1, 0 ); + + actual = dreplicate( [ x, out, scalar( 2 ) ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float64Array( [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] ); + t.strictEqual( isSameFloat64Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports output ndarrays having non-zero offsets', function test( t ) { + var expected; + var actual; + var xbuf; + var ybuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + ybuf = new Float64Array([ + 99.0, + 99.0, + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0, // 3 + 0.0, // 4 + 0.0 // 5 + ]); + x = vector( xbuf, 3, 1, 0 ); + out = vector( ybuf, 6, 1, 2 ); + + actual = dreplicate( [ x, out, scalar( 2 ) ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float64Array( [ 99.0, 99.0, 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ] ); + t.strictEqual( isSameFloat64Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a zero input stride', function test( t ) { + var expected; + var actual; + var xbuf; + var ybuf; + var out; + var x; + + xbuf = new Float64Array( [ 5.0, 99.0 ] ); + ybuf = new Float64Array( 6 ); + x = vector( xbuf, 3, 0, 0 ); + out = vector( ybuf, 6, 1, 0 ); + + actual = dreplicate( [ x, out, scalar( 2 ) ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float64Array( [ 5.0, 5.0, 5.0, 5.0, 5.0, 5.0 ] ); + t.strictEqual( isSameFloat64Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports complex access patterns', function test( t ) { + var expected; + var actual; + var xbuf; + var ybuf; + var out; + var x; + + xbuf = new Float64Array([ + 10.0, + 1.0, // 0 + 20.0, + 2.0, // 1 + 30.0, + 3.0 // 2 + ]); + ybuf = new Float64Array([ + 0.0, // 5 + 0.0, + 0.0, // 4 + 0.0, + 0.0, // 3 + 0.0, + 0.0, // 2 + 0.0, + 0.0, // 1 + 0.0, + 0.0, // 0 + 0.0 + ]); + x = vector( xbuf, 3, 2, 1 ); + out = vector( ybuf, 6, -2, 10 ); + + actual = dreplicate( [ x, out, scalar( 2 ) ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float64Array( [ 3.0, 0.0, 3.0, 0.0, 2.0, 0.0, 2.0, 0.0, 1.0, 0.0, 1.0, 0.0 ] ); + t.strictEqual( isSameFloat64Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + From ff52dc32143da75e7d37cb7955d7f391e8cd1a31 Mon Sep 17 00:00:00 2001 From: Siddhram Date: Sun, 8 Nov 2026 00:24:52 +0530 Subject: [PATCH 2/2] feat: add blas/ext/base/ndarray/dreplicate --- .../@stdlib/blas/ext/base/ndarray/dreplicate/README.md | 1 - .../blas/ext/base/ndarray/dreplicate/benchmark/benchmark.js | 1 - .../@stdlib/blas/ext/base/ndarray/dreplicate/docs/repl.txt | 1 - .../blas/ext/base/ndarray/dreplicate/docs/types/index.d.ts | 1 - .../@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/test.ts | 1 - .../@stdlib/blas/ext/base/ndarray/dreplicate/examples/index.js | 1 - .../@stdlib/blas/ext/base/ndarray/dreplicate/lib/index.js | 1 - .../@stdlib/blas/ext/base/ndarray/dreplicate/lib/main.js | 1 - .../@stdlib/blas/ext/base/ndarray/dreplicate/package.json | 1 - .../@stdlib/blas/ext/base/ndarray/dreplicate/test/test.js | 1 - 10 files changed, 10 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/README.md index 54d5934793f5..b6f80b3d7d4a 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/README.md @@ -133,4 +133,3 @@ console.log( ndarray2array( v ) ); - diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/benchmark/benchmark.js index a61d3440f60e..fe8dabc9d130 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/benchmark/benchmark.js @@ -112,4 +112,3 @@ function main() { } main(); - diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/repl.txt index 619b073fae47..16245df46f79 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/repl.txt @@ -34,4 +34,3 @@ See Also -------- - diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/index.d.ts index deb973869a33..f93dba820e63 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/index.d.ts @@ -59,4 +59,3 @@ declare function dreplicate( arrays: [ float64ndarray, float64ndarray, typedndar // EXPORTS // export = dreplicate; - diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/test.ts index 4a2a827c0445..a805f06380d1 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/test.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/docs/types/test.ts @@ -68,4 +68,3 @@ import dreplicate = require( './index' ); dreplicate(); // $ExpectError dreplicate( [ x, out, k ], {} ); // $ExpectError } - diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/examples/index.js index e6d320a77321..41b9b68a12a9 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/examples/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/examples/index.js @@ -40,4 +40,3 @@ console.log( ndarray2array( out ) ); var v = dreplicate( [ x, out, k ] ); console.log( ndarray2array( v ) ); - diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/lib/index.js index 912daa57d751..7bacdbba35d7 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/lib/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/lib/index.js @@ -53,4 +53,3 @@ var main = require( './main.js' ); // EXPORTS // module.exports = main; - diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/lib/main.js index 9daec824d7e4..4a79551daadd 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/lib/main.js @@ -81,4 +81,3 @@ function dreplicate( arrays ) { // EXPORTS // module.exports = dreplicate; - diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/package.json index 9e1a25509ae1..db0ee679cdbd 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/package.json +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/package.json @@ -69,4 +69,3 @@ ], "__stdlib__": {} } - diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/test/test.js index 2fd507d78f5c..5d1aed9a521f 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/test/test.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dreplicate/test/test.js @@ -431,4 +431,3 @@ tape( 'the function supports complex access patterns', function test( t ) { t.end(); }); -