From fc30f3b7fc91e39d6e517d555e87031a643fa363 Mon Sep 17 00:00:00 2001 From: kaustubh Date: Tue, 7 Jul 2026 02:01:08 +0530 Subject: [PATCH] feat: add blas/dscal --- 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_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - 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 --- --- lib/node_modules/@stdlib/blas/dscal/README.md | 155 +++++ .../@stdlib/blas/dscal/benchmark/benchmark.js | 104 ++++ .../blas/dscal/benchmark/benchmark.stacks.js | 121 ++++ .../@stdlib/blas/dscal/docs/repl.txt | 38 ++ .../@stdlib/blas/dscal/docs/types/index.d.ts | 57 ++ .../@stdlib/blas/dscal/docs/types/test.ts | 96 ++++ .../@stdlib/blas/dscal/examples/index.js | 36 ++ .../@stdlib/blas/dscal/lib/index.js | 46 ++ .../@stdlib/blas/dscal/lib/main.js | 134 +++++ .../@stdlib/blas/dscal/package.json | 74 +++ .../@stdlib/blas/dscal/test/test.js | 528 ++++++++++++++++++ 11 files changed, 1389 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/dscal/README.md create mode 100644 lib/node_modules/@stdlib/blas/dscal/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/dscal/benchmark/benchmark.stacks.js create mode 100644 lib/node_modules/@stdlib/blas/dscal/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/dscal/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/dscal/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/dscal/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/dscal/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/dscal/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/dscal/package.json create mode 100644 lib/node_modules/@stdlib/blas/dscal/test/test.js diff --git a/lib/node_modules/@stdlib/blas/dscal/README.md b/lib/node_modules/@stdlib/blas/dscal/README.md new file mode 100644 index 000000000000..bfe70f85011a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/dscal/README.md @@ -0,0 +1,155 @@ + + +# dscal + +> Multiply a double-precision floating-point vector by a scalar `alpha`. + +
+ +
+ + + +
+ +## Usage + +```javascript +var dscal = require( '@stdlib/blas/dscal' ); +``` + +#### dscal( alpha, x\[, dim] ) + +Multiplies a double-precision floating-point vector `x` by a scalar `alpha`. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ) ); + +dscal( 5.0, x ); + +var xbuf = x.data; +// returns [ 5.0, 10.0, 15.0, 20.0, 25.0 ] +``` + +The function has the following parameters: + +- **alpha**: scalar constant. +- **x**: a non-zero-dimensional [`ndarray`][@stdlib/ndarray/ctor] whose underlying data type is `float64`. Must not be read-only. +- **dim**: dimension along which to scale vectors. Must be a negative integer. Negative indices are resolved relative to the last array dimension, with the last dimension corresponding to `-1`. Default: `-1`. + +For multi-dimensional input [`ndarrays`][@stdlib/ndarray/ctor], the function performs batched computation, such that the function scales each vector in `x` according to the specified dimension index. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var array = require( '@stdlib/ndarray/array' ); + +var opts = { + 'shape': [ 2, 3 ] +}; +var x = array( new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ), opts ); + +var v = x.get( 0, 0 ); +// returns 1.0 + +dscal( 5.0, x ); + +v = x.get( 0, 0 ); +// returns 5.0 + +v = x.get( 1, 2 ); +// returns 30.0 +``` + +
+ + + +
+ +## Notes + +- Negative indices are resolved relative to the last [`ndarray`][@stdlib/ndarray/ctor] dimension, with the last dimension corresponding to `-1`. +- For multi-dimensional [`ndarrays`][@stdlib/ndarray/ctor], batched computation effectively means scaling all elements of `x`; however, the choice of `dim` will significantly affect performance. For best performance, specify a `dim` which best aligns with the [memory layout][@stdlib/ndarray/orders] of provided [`ndarrays`][@stdlib/ndarray/ctor]. +- `dscal()` provides a higher-level interface to the [BLAS][blas] level 1 function [`dscal`][@stdlib/blas/base/dscal]. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); +var dscal = require( '@stdlib/blas/dscal' ); + +var opts = { + 'dtype': 'float64' +}; + +var x = array( discreteUniform( 10, 0, 100, opts ), { + 'shape': [ 5, 2 ] +}); +console.log( ndarray2array( x ) ); + +dscal( 5.0, x ); +console.log( ndarray2array( x ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/dscal/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/dscal/benchmark/benchmark.js new file mode 100644 index 000000000000..e68b729386f3 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/dscal/benchmark/benchmark.js @@ -0,0 +1,104 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var array = require( '@stdlib/ndarray/array' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var dscal = require( './../lib/main.js' ); + + +// VARIABLES // + +var opts = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = array( uniform( len, -100.0, 100.0, opts ) ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var d; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + d = dscal( 1.0, x ); + if ( typeof d !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( d.get( i%len ) ) ) { + 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/dscal/benchmark/benchmark.stacks.js b/lib/node_modules/@stdlib/blas/dscal/benchmark/benchmark.stacks.js new file mode 100644 index 000000000000..051e4b91432c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/dscal/benchmark/benchmark.stacks.js @@ -0,0 +1,121 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var array = require( '@stdlib/ndarray/array' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var dscal = require( './../lib/main.js' ); + + +// VARIABLES // + +var OPTS = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveIntegerArray} shape - array shape +* @returns {Function} benchmark function +*/ +function createBenchmark( shape ) { + var x; + var N; + var o; + + N = numel( shape ); + o = { + 'shape': shape + }; + x = array( uniform( N, -100.0, 100.0, OPTS ), o ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var d; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + d = dscal( 1.0, x ); + if ( typeof d !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( d.iget( i%N ) ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var shape; + var min; + var max; + var N; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + N = pow( 10, i ); + + shape = [ 2, N/2 ]; + f = createBenchmark( shape ); + bench( format( '%s::stacks:size=%d,ndims=%d,shape=(%s)', pkg, N, shape.length, shape.join( ',' ) ), f ); + + shape = [ N/2, 2 ]; + f = createBenchmark( shape ); + bench( format( '%s::stacks:size=%d,ndims=%d,shape=(%s)', pkg, N, shape.length, shape.join( ',' ) ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/dscal/docs/repl.txt b/lib/node_modules/@stdlib/blas/dscal/docs/repl.txt new file mode 100644 index 000000000000..d640863eb3fd --- /dev/null +++ b/lib/node_modules/@stdlib/blas/dscal/docs/repl.txt @@ -0,0 +1,38 @@ + +{{alias}}( alpha, x[, dim] ) + Multiplies a double-precision floating-point vector `x` by a scalar + `alpha`. + + For multi-dimensional input arrays, the function performs batched + computation, such that the function scales each vector in `x` according to + the specified dimension index. + + Parameters + ---------- + alpha: number + Scalar constant. + + x: ndarray + Input array. Must have a 'float64' data type. Must have at least one + dimension and must not be read-only. + + dim: integer (optional) + Dimension index along which to scale vectors. Must be a negative + integer. Negative indices are resolved relative to the last array + dimension, with the last dimension corresponding to `-1`. Default: -1. + + Returns + ------- + x: ndarray + The input array `x`. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ) ); + > {{alias}}( 5.0, x ); + > x.data + [ 5.0, 10.0, 15.0, 20.0, 25.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/dscal/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/dscal/docs/types/index.d.ts new file mode 100644 index 000000000000..1a35c4dce7e0 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/dscal/docs/types/index.d.ts @@ -0,0 +1,57 @@ +/* +* @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 } from '@stdlib/types/ndarray'; + +/** +* Multiplies a double-precision floating-point vector `x` by a scalar `alpha`. +* +* ## Notes +* +* - For multi-dimensional input arrays, the function performs batched computation, such that the function scales each vector in `x` according to the specified dimension index. +* - Negative indices are resolved relative to the last array dimension, with the last dimension corresponding to `-1`. +* +* @param alpha - scalar constant +* @param x - input array +* @param dim - dimension along which to scale vectors (default: -1) +* @throws first argument must be a number +* @throws second argument must be a non-zero-dimensional ndarray containing double-precision floating-point numbers +* @throws cannot write to read-only array +* @returns `x` +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ) ); +* +* dscal( 5.0, x ); +* +* var xbuf = x.data; +* // returns [ 5.0, 10.0, 15.0, 20.0, 25.0 ] +*/ +declare function dscal( alpha: number, x: float64ndarray, dim?: number ): float64ndarray; + + +// EXPORTS // + +export = dscal; diff --git a/lib/node_modules/@stdlib/blas/dscal/docs/types/test.ts b/lib/node_modules/@stdlib/blas/dscal/docs/types/test.ts new file mode 100644 index 000000000000..af0a4e5267d4 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/dscal/docs/types/test.ts @@ -0,0 +1,96 @@ +/* +* @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. +*/ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import dscal = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + dscal( 5.0, zeros( [ 10 ], { 'dtype': 'float64' } ) ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not a number... +{ + const x = zeros( [ 10 ], { 'dtype': 'float64' } ); + + dscal( '10', x ); // $ExpectError + dscal( true, x ); // $ExpectError + dscal( false, x ); // $ExpectError + dscal( null, x ); // $ExpectError + dscal( undefined, x ); // $ExpectError + dscal( {}, x ); // $ExpectError + dscal( [], x ); // $ExpectError + dscal( ( x: number ): number => x, x ); // $ExpectError + + dscal( '10', x, -1 ); // $ExpectError + dscal( true, x, -1 ); // $ExpectError + dscal( false, x, -1 ); // $ExpectError + dscal( null, x, -1 ); // $ExpectError + dscal( undefined, x, -1 ); // $ExpectError + dscal( {}, x, -1 ); // $ExpectError + dscal( [], x, -1 ); // $ExpectError + dscal( ( x: number ): number => x, x, -1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an ndarray... +{ + dscal( 5.0, 10 ); // $ExpectError + dscal( 5.0, '10' ); // $ExpectError + dscal( 5.0, true ); // $ExpectError + dscal( 5.0, false ); // $ExpectError + dscal( 5.0, null ); // $ExpectError + dscal( 5.0, undefined ); // $ExpectError + dscal( 5.0, {} ); // $ExpectError + dscal( 5.0, [] ); // $ExpectError + dscal( 5.0, ( x: number ): number => x ); // $ExpectError + + dscal( 5.0, 10, -1 ); // $ExpectError + dscal( 5.0, '10', -1 ); // $ExpectError + dscal( 5.0, true, -1 ); // $ExpectError + dscal( 5.0, false, -1 ); // $ExpectError + dscal( 5.0, null, -1 ); // $ExpectError + dscal( 5.0, undefined, -1 ); // $ExpectError + dscal( 5.0, {}, -1 ); // $ExpectError + dscal( 5.0, [], -1 ); // $ExpectError + dscal( 5.0, ( x: number ): number => x, -1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a number... +{ + const x = zeros( [ 10 ], { 'dtype': 'float64' } ); + + dscal( 5.0, x, '10' ); // $ExpectError + dscal( 5.0, x, true ); // $ExpectError + dscal( 5.0, x, false ); // $ExpectError + dscal( 5.0, x, null ); // $ExpectError + dscal( 5.0, x, {} ); // $ExpectError + dscal( 5.0, x, [] ); // $ExpectError + dscal( 5.0, x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { 'dtype': 'float64' } ); + + dscal(); // $ExpectError + dscal( 5.0 ); // $ExpectError + dscal( 5.0, x, -1, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/dscal/examples/index.js b/lib/node_modules/@stdlib/blas/dscal/examples/index.js new file mode 100644 index 000000000000..cac7bbebd3fa --- /dev/null +++ b/lib/node_modules/@stdlib/blas/dscal/examples/index.js @@ -0,0 +1,36 @@ +/** +* @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/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); +var dscal = require( './../lib' ); + +var opts = { + 'dtype': 'float64' +}; + +var x = array( discreteUniform( 10, 0, 100, opts ), { + 'shape': [ 5, 2 ] +}); +console.log( ndarray2array( x ) ); + +dscal( 5.0, x ); +console.log( ndarray2array( x ) ); diff --git a/lib/node_modules/@stdlib/blas/dscal/lib/index.js b/lib/node_modules/@stdlib/blas/dscal/lib/index.js new file mode 100644 index 000000000000..470fac8bd31e --- /dev/null +++ b/lib/node_modules/@stdlib/blas/dscal/lib/index.js @@ -0,0 +1,46 @@ +/** +* @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'; + +/** +* BLAS level 1 routine to multiply a double-precision floating-point vector by a scalar `alpha`. +* +* @module @stdlib/blas/dscal +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var array = require( '@stdlib/ndarray/array' ); +* var dscal = require( '@stdlib/blas/dscal' ); +* +* var x = array( new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ) ); +* +* dscal( 5.0, x ); +* +* var xbuf = x.data; +* // returns [ 5.0, 10.0, 15.0, 20.0, 25.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/dscal/lib/main.js b/lib/node_modules/@stdlib/blas/dscal/lib/main.js new file mode 100644 index 000000000000..8a642914903c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/dscal/lib/main.js @@ -0,0 +1,134 @@ +/** +* @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 isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isFloat64ndarrayLike = require( '@stdlib/assert/is-float64ndarray-like' ); +var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive; +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var without = require( '@stdlib/array/base/without' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' ); +var nditerStacks = require( '@stdlib/ndarray/iter/stacks' ); +var base = require( '@stdlib/blas/base/dscal' ).ndarray; +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Multiplies a double-precision floating-point vector `x` by a scalar `alpha`. +* +* @param {number} alpha - scalar constant +* @param {ndarrayLike} x - input array +* @param {NegativeInteger} [dim=-1] - dimension along which to scale elements +* @throws {TypeError} first argument must be a number +* @throws {TypeError} second argument must be an ndarray containing double-precision floating-point numbers +* @throws {TypeError} second argument must have at least one dimension +* @throws {TypeError} third argument must be a negative integer +* @throws {RangeError} third argument is out-of-bounds +* @throws {Error} cannot write to read-only array +* @returns {ndarrayLike} `x` +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ) ); +* +* dscal( 5.0, x ); +* +* var xbuf = x.data; +* // returns [ 5.0, 10.0, 15.0, 20.0, 25.0 ] +*/ +function dscal( alpha, x ) { + var dim; + var xsh; + var xit; + var xc; + var vx; + var dm; + var S; + var N; + var i; + + if ( !isNumber( alpha ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a number. Value: `%s`.', alpha ) ); + } + if ( !isFloat64ndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an ndarray containing double-precision floating-point numbers. Value: `%s`.', x ) ); + } + // Validate that the input array is not read-only... + if ( isReadOnly( x ) ) { + throw new Error( 'invalid argument. Cannot write to read-only array.' ); + } + // Convert the input array to a "base" ndarray: + xc = ndarraylike2ndarray( x ); + + // Resolve the input array shape: + xsh = xc.shape; + + // Validate that we've been provided a non-zero-dimensional array... + if ( xsh.length < 1 ) { + throw new TypeError( format( 'invalid argument. Second argument must have at least one dimension.' ) ); + } + // Validate that the dimension argument is a negative integer... + if ( arguments.length > 2 ) { + dim = arguments[ 2 ]; + if ( !isNegativeInteger( dim ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be a negative integer. Value: `%s`.', dim ) ); + } + } else { + dim = -1; + } + // Validate that a provided dimension index is within bounds... + dm = xsh.length - 1; + dim = normalizeIndex( dim, dm ); + if ( dim === -1 ) { + throw new RangeError( format( 'invalid argument. Third argument must be a value on the interval: [%d,%d]. Value: `%d`.', -( dm+1 ), -1, arguments[ 2 ] ) ); + } + // Resolve the size of the dimension over which to scale: + S = xsh[ dim ]; + + // If we are only provided a one-dimensional input array, we can skip iterating over stacks... + if ( xsh.length === 1 ) { + base( S, alpha, xc.data, xc.strides[0], xc.offset ); + return x; + } + // Resolve the number of stacks: + N = numel( without( xsh, dim ) ); + + // Create an iterator for iterating over stacks of vectors: + xit = nditerStacks( xc, [ dim ] ); + + // Scale each vector... + for ( i = 0; i < N; i++ ) { + vx = xit.next().value; + base( S, alpha, vx.data, vx.strides[0], vx.offset ); + } + return x; +} + + +// EXPORTS // + +module.exports = dscal; diff --git a/lib/node_modules/@stdlib/blas/dscal/package.json b/lib/node_modules/@stdlib/blas/dscal/package.json new file mode 100644 index 000000000000..b7c8170e7fb1 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/dscal/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/blas/dscal", + "version": "0.0.0", + "description": "Multiply a double-precision floating-point vector by a scalar `alpha`.", + "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", + "browser": "./lib/main.js", + "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", + "level 1", + "linear", + "algebra", + "subroutines", + "dscal", + "scale", + "multiply", + "scalar", + "vector", + "array", + "ndarray", + "float64", + "double", + "float64array", + "typed array" + ] +} diff --git a/lib/node_modules/@stdlib/blas/dscal/test/test.js b/lib/node_modules/@stdlib/blas/dscal/test/test.js new file mode 100644 index 000000000000..ed399b8934ee --- /dev/null +++ b/lib/node_modules/@stdlib/blas/dscal/test/test.js @@ -0,0 +1,528 @@ +/** +* @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 Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var array = require( '@stdlib/ndarray/array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndims = require( '@stdlib/ndarray/ndims' ); +var dscal = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dscal, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 2', function test( t ) { + t.strictEqual( dscal.length, 2, 'has expected arity' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not a number', function test( t ) { + var values; + var i; + + values = [ + '5', + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var x = array( new Float64Array( 10 ) ); + + return function badValue() { + dscal( value, x ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not a number (dimension)', function test( t ) { + var values; + var i; + + values = [ + '5', + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var x = array( new Float64Array( 10 ) ); + + return function badValue() { + dscal( value, x, -1 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a non-zero-dimensional ndarray containing double-precision floating-point numbers', function test( t ) { + var values; + var i; + + values = [ + 5, + '5', + true, + false, + null, + void 0, + {}, + [], + function noop() {}, + zeros( [], { + 'dtype': 'float64' + }), + array( new Float32Array( 10 ) ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dscal( 5.0, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a non-zero-dimensional ndarray containing double-precision floating-point numbers (dimension)', function test( t ) { + var values; + var i; + + values = [ + 5, + '5', + true, + false, + null, + void 0, + {}, + [], + function noop() {}, + zeros( [], { + 'dtype': 'float64' + }), + array( new Float32Array( 10 ) ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dscal( 5.0, value, -1 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is a read-only ndarray', function test( t ) { + var values; + var opts; + var i; + + opts = { + 'readonly': true + }; + + values = [ + array( new Float64Array( 10 ), opts ), + array( new Float64Array( 5 ), opts ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + dscal( 5.0, value ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not a negative integer (vector)', function test( t ) { + var values; + var i; + + values = [ + '5', + 0, + 5, + NaN, + -3.14, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var x = array( new Float64Array( 10 ) ); + + return function badValue() { + dscal( 5.0, x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not a negative integer (multi-dimensional array)', function test( t ) { + var values; + var i; + + values = [ + '5', + 0, + 5, + NaN, + -3.14, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var opts = { + 'shape': [ 2, 5 ] + }; + var x = array( new Float64Array( 10 ), opts ); + + return function badValue() { + dscal( 5.0, x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is out-of-bounds (vector)', function test( t ) { + var values; + var i; + + values = [ + -2, + -3, + -4, + -5 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var x = array( new Float64Array( 10 ) ); + + return function badValue() { + dscal( 5.0, x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is out-of-bounds (multi-dimensional array)', function test( t ) { + var values; + var i; + + values = [ + -3, + -4, + -5, + -10 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var opts = { + 'shape': [ 2, 5 ] + }; + var x = array( new Float64Array( 10 ), opts ); + + return function badValue() { + dscal( 5.0, x, value ); + }; + } +}); + +tape( 'the function scales a vector `x` by a scalar `alpha`', function test( t ) { + var xe; + var x; + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + xe = new Float64Array( [ 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0 ] ); + + x = array( x ); + + dscal( 5.0, x ); + + t.strictEqual( ndims( x ), 1, 'returns expected value' ); + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'different references' ); + + t.end(); +}); + +tape( 'the function supports operating on stacks of vectors (default)', function test( t ) { + var opts; + var xe; + var x; + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + xe = new Float64Array( [ 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0 ] ); + + opts = { + 'shape': [ 4, 2 ] + }; + x = array( x, opts ); + + dscal( 5.0, x ); + + t.strictEqual( ndims( x ), 2, 'returns expected value' ); + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'different references' ); + + t.end(); +}); + +tape( 'the function supports operating on stacks of vectors (dim=-1)', function test( t ) { + var opts; + var xe; + var x; + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + xe = new Float64Array( [ 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0 ] ); + + opts = { + 'shape': [ 4, 2 ] + }; + x = array( x, opts ); + + dscal( 5.0, x, -1 ); + + t.strictEqual( ndims( x ), 2, 'returns expected value' ); + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'different references' ); + + t.end(); +}); + +tape( 'the function supports operating on stacks of vectors (dim=-2)', function test( t ) { + var opts; + var xe; + var x; + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + xe = new Float64Array( [ 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0 ] ); + + opts = { + 'shape': [ 4, 2 ] + }; + x = array( x, opts ); + + dscal( 5.0, x, -2 ); + + t.strictEqual( ndims( x ), 2, 'returns expected value' ); + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'different references' ); + + t.end(); +}); + +tape( 'if provided an `alpha` equal to `1`, the function returns `x` unchanged', function test( t ) { + var xe; + var x; + + x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + xe = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + + x = array( x ); + + dscal( 1.0, x ); + + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'different references' ); + + t.end(); +}); + +tape( 'the function supports a strided vector', function test( t ) { + var xe; + var x; + + x = new Float64Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 2 + ]); + + xe = new Float64Array( [ 5.0, 2.0, 15.0, 4.0, 25.0 ] ); + + x = ndarray( 'float64', x, [ 3 ], [ 2 ], 0, 'row-major' ); + + dscal( 5.0, x ); + + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'different references' ); + + t.end(); +}); + +tape( 'the function supports negative strides', function test( t ) { + var xe; + var x; + + x = new Float64Array([ + 1.0, // 2 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 0 + ]); + + xe = new Float64Array( [ 5.0, 2.0, 15.0, 4.0, 25.0 ] ); + + x = ndarray( 'float64', x, [ 3 ], [ -2 ], x.length-1, 'row-major' ); + + dscal( 5.0, x ); + + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'different references' ); + + t.end(); +}); + +tape( 'the function supports strided vectors having offsets', function test( t ) { + var xe; + var x; + + x = new Float64Array([ + 1.0, + 2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 2 + ]); + + xe = new Float64Array( [ 1.0, 10.0, 3.0, 20.0, 5.0, 30.0 ] ); + + x = ndarray( 'float64', x, [ 3 ], [ 2 ], 1, 'row-major' ); + + dscal( 5.0, x ); + + t.deepEqual( x.data, xe, 'returns expected value' ); + t.notEqual( x.data, xe, 'different references' ); + + t.end(); +}); + +tape( 'the function supports underlying data buffers with view offsets', function test( t ) { + var x0; + var x1; + var xe; + + x0 = new Float64Array([ + 1.0, + 2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 2 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); + + xe = new Float64Array( [ 1.0, 10.0, 3.0, 20.0, 5.0, 30.0 ] ); + + x1 = ndarray( 'float64', x1, [ 3 ], [ 2 ], 0, 'row-major' ); + + dscal( 5.0, x1 ); + + t.deepEqual( x0, xe, 'returns expected value' ); + t.notEqual( x0, xe, 'different references' ); + + t.end(); +}); + +tape( 'the function returns a reference to the input array', function test( t ) { + var out; + var x; + + x = array( new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ) ); + + out = dscal( 5.0, x ); + + t.strictEqual( out, x, 'same reference' ); + t.end(); +});