From 210544cdb6c0a0611d3ed3164d0bb95dca0aa248 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Wed, 14 Oct 2026 12:21:17 +0500 Subject: [PATCH 1/2] feat: add blas/ext/base/ndarray/scartesian-power --- .../base/ndarray/scartesian-power/README.md | 136 +++++++ .../scartesian-power/benchmark/benchmark.js | 116 ++++++ .../ndarray/scartesian-power/docs/repl.txt | 39 ++ .../scartesian-power/docs/types/index.d.ts | 64 +++ .../scartesian-power/docs/types/test.ts | 70 ++++ .../scartesian-power/examples/index.js | 41 ++ .../ndarray/scartesian-power/lib/index.js | 55 +++ .../base/ndarray/scartesian-power/lib/main.js | 87 +++++ .../ndarray/scartesian-power/package.json | 69 ++++ .../ndarray/scartesian-power/test/test.js | 368 ++++++++++++++++++ 10 files changed, 1045 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/test/test.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/README.md new file mode 100644 index 000000000000..201aae2db307 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/README.md @@ -0,0 +1,136 @@ + + +# scartesianPower + +> Compute the Cartesian power for a single-precision floating-point ndarray. + +
+ +
+ + + +
+ +## Usage + +```javascript +var scartesianPower = require( '@stdlib/blas/ext/base/ndarray/scartesian-power' ); +``` + +#### scartesianPower( arrays ) + +Computes the Cartesian power for a single-precision floating-point ndarray. + +```javascript +var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); + +var x = new Float32Vector( [ 1.0, 2.0 ] ); +var out = zeros( [ 4, 2 ], { + 'dtype': 'float32' +}); + +var k = scalar2ndarray( 2, { + 'dtype': 'generic' +}); + +var v = scartesianPower( [ x, out, k ] ); +// returns [ [ 1.0, 1.0 ], [ 1.0, 2.0 ], [ 2.0, 1.0 ], [ 2.0, 2.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 two-dimensional output ndarray. + - a zero-dimensional ndarray specifying the power. + +
+ + + +
+ +## Notes + +- `k`-tuples are stored as rows in the output matrix, where the `j`-th column contains the `j`-th element of each tuple. +- For an input array of length `N`, the output array should have shape `[N^k, k]`, where `N^k` is the number of rows and `k` is the number of columns. +- If `N <= 0` or `k <= 0`, the function returns the output ndarray unchanged. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var scartesianPower = require( '@stdlib/blas/ext/base/ndarray/scartesian-power' ); + +var opts = { + 'dtype': 'float32' +}; + +var x = discreteUniform( [ 3 ], -50, 50, opts ); +console.log( ndarray2array( x ) ); + +var out = zeros( [ 9, 2 ], opts ); + +var k = scalar2ndarray( 2, { + 'dtype': 'generic' +}); + +var v = scartesianPower( [ x, out, k ] ); +console.log( ndarray2array( v ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/benchmark/benchmark.js new file mode 100644 index 000000000000..724f982b7298 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/benchmark/benchmark.js @@ -0,0 +1,116 @@ +/** +* @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 zeros = require( '@stdlib/ndarray/zeros' ); +var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var pkg = require( './../package.json' ).name; +var scartesianPower = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float32' +}; +var K = 2; + + +// 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 ], -100.0, 100.0, options ); + out = zeros( [ pow( len, K ), K ], options ); + + k = scalar2ndarray( K, { + 'dtype': 'generic' + }); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = scartesianPower( [ x, out, k ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnanf( v.get( i%pow( len, K ), i%K ) ) ) { + 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 = 3; // 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/scartesian-power/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/docs/repl.txt new file mode 100644 index 000000000000..4c02768eed54 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/docs/repl.txt @@ -0,0 +1,39 @@ + +{{alias}}( arrays ) + Computes the Cartesian power for a single-precision floating-point ndarray. + + `k`-tuples are stored as rows in the output matrix, where the `j`-th column + contains the `j`-th element of each tuple. + + For an input array of length `N`, the output array should have shape + [N^k, k], where `N^k` is the number of rows and `k` is the number of + columns. + + If `N <= 0` or `k <= 0`, the function returns the output ndarray unchanged. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a two-dimensional output ndarray. + - a zero-dimensional ndarray specifying the power. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var x = new {{alias:@stdlib/ndarray/vector/float32}}( [ 1.0, 2.0 ] ); + > var out = {{alias:@stdlib/ndarray/zeros}}( [ 4, 2 ], { 'dtype': 'float32' } ); + > var k = {{alias:@stdlib/ndarray/from-scalar}}( 2, { 'dtype': 'generic' } ); + > {{alias}}( [ x, out, k ] ); + > out + [ [ 1.0, 1.0 ], [ 1.0, 2.0 ], [ 2.0, 1.0 ], [ 2.0, 2.0 ] ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/docs/types/index.d.ts new file mode 100644 index 000000000000..9194f7bf2e66 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/docs/types/index.d.ts @@ -0,0 +1,64 @@ +/* +* @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 { float32ndarray, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Computes the Cartesian power for a single-precision floating-point ndarray. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a two-dimensional output ndarray. +* - a zero-dimensional ndarray specifying the power. +* +* @param arrays - array-like object containing ndarrays +* @returns output ndarray +* +* @example +* var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* +* var x = new Float32Vector( [ 1.0, 2.0 ] ); +* var out = zeros( [ 4, 2 ], { +* 'dtype': 'float32' +* }); +* +* var k = scalar2ndarray( 2, { +* 'dtype': 'generic' +* }); +* +* var v = scartesianPower( [ x, out, k ] ); +* // returns [ [ 1.0, 1.0 ], [ 1.0, 2.0 ], [ 2.0, 1.0 ], [ 2.0, 2.0 ] ] +* +* var bool = ( v === out ); +* // returns true +*/ +declare function scartesianPower( arrays: [ float32ndarray, float32ndarray, typedndarray ] ): float32ndarray; + + +// EXPORTS // + +export = scartesianPower; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/docs/types/test.ts new file mode 100644 index 000000000000..13d0dfdf0fad --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/docs/types/test.ts @@ -0,0 +1,70 @@ +/* +* @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 scartesianPower = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2 ], { + 'dtype': 'float32' + }); + const out = zeros( [ 4, 2 ], { + 'dtype': 'float32' + }); + const k = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + + scartesianPower( [ x, out, k ] ); // $ExpectType float32ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + scartesianPower( '10' ); // $ExpectError + scartesianPower( 10 ); // $ExpectError + scartesianPower( true ); // $ExpectError + scartesianPower( false ); // $ExpectError + scartesianPower( null ); // $ExpectError + scartesianPower( undefined ); // $ExpectError + scartesianPower( [] ); // $ExpectError + scartesianPower( {} ); // $ExpectError + scartesianPower( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2 ], { + 'dtype': 'float32' + }); + const out = zeros( [ 4, 2 ], { + 'dtype': 'float32' + }); + const k = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + + scartesianPower(); // $ExpectError + scartesianPower( [ x, out, k ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/examples/index.js new file mode 100644 index 000000000000..13e0286f4d3e --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/examples/index.js @@ -0,0 +1,41 @@ +/** +* @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 zeros = require( '@stdlib/ndarray/zeros' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var scartesianPower = require( './../lib' ); + +var opts = { + 'dtype': 'float32' +}; + +var x = discreteUniform( [ 3 ], -50, 50, opts ); +console.log( ndarray2array( x ) ); + +var out = zeros( [ 9, 2 ], opts ); + +var k = scalar2ndarray( 2, { + 'dtype': 'generic' +}); + +var v = scartesianPower( [ x, out, k ] ); +console.log( ndarray2array( v ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/lib/index.js new file mode 100644 index 000000000000..24e1bb7c8fa7 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/lib/index.js @@ -0,0 +1,55 @@ +/** +* @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'; + +/** +* Compute the Cartesian power for a single-precision floating-point ndarray. +* +* @module @stdlib/blas/ext/base/ndarray/scartesian-power +* +* @example +* var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var scartesianPower = require( '@stdlib/blas/ext/base/ndarray/scartesian-power' ); +* +* var x = new Float32Vector( [ 1.0, 2.0 ] ); +* var out = zeros( [ 4, 2 ], { +* 'dtype': 'float32' +* }); +* +* var k = scalar2ndarray( 2, { +* 'dtype': 'generic' +* }); +* +* var v = scartesianPower( [ x, out, k ] ); +* // returns [ [ 1.0, 1.0 ], [ 1.0, 2.0 ], [ 2.0, 1.0 ], [ 2.0, 2.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/scartesian-power/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/lib/main.js new file mode 100644 index 000000000000..af15191f0e21 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/lib/main.js @@ -0,0 +1,87 @@ +/** +* @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 ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); +var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStrides = require( '@stdlib/ndarray/base/strides' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var strided = require( '@stdlib/blas/ext/base/scartesian-power' ).ndarray; + + +// MAIN // + +/** +* Computes the Cartesian power for a single-precision floating-point ndarray. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a two-dimensional output ndarray. +* - a zero-dimensional ndarray specifying the power. +* +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @returns {Object} output ndarray +* +* @example +* var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* +* var x = new Float32Vector( [ 1.0, 2.0 ] ); +* var out = zeros( [ 4, 2 ], { +* 'dtype': 'float32' +* }); +* +* var k = scalar2ndarray( 2, { +* 'dtype': 'generic' +* }); +* +* var v = scartesianPower( [ x, out, k ] ); +* // returns [ [ 1.0, 1.0 ], [ 1.0, 2.0 ], [ 2.0, 1.0 ], [ 2.0, 2.0 ] ] +* +* var bool = ( v === out ); +* // returns true +*/ +function scartesianPower( arrays ) { + var out; + var so; + var x; + var k; + + x = arrays[ 0 ]; + out = arrays[ 1 ]; + k = ndarraylike2scalar( arrays[ 2 ] ); + + so = getStrides( out, false ); + + strided( numelDimension( x, 0 ), k, getData( x ), getStride( x, 0 ), getOffset( x ), getData( out ), so[ 0 ], so[ 1 ], getOffset( out ) ); // eslint-disable-line max-len + return out; +} + + +// EXPORTS // + +module.exports = scartesianPower; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/package.json new file mode 100644 index 000000000000..ced238ebd862 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/scartesian-power", + "version": "0.0.0", + "description": "Compute the Cartesian power for a single-precision floating-point ndarray.", + "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", + "blas", + "extended", + "ext", + "linear", + "algebra", + "cartesian", + "power", + "tuples", + "product", + "array", + "ndarray", + "float32", + "single", + "float32array" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/test/test.js new file mode 100644 index 000000000000..157a69f005d9 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/test/test.js @@ -0,0 +1,368 @@ +/** +* @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 isSameFloat32Array = require( '@stdlib/assert/is-same-float32array' ); +var Float32Array = require( '@stdlib/array/float32' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var scartesianPower = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Collection} buffer - underlying data buffer +* @param {NonNegativeInteger} N - number of elements +* @param {integer} stride - stride +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, N, stride, offset ) { + return new ndarray( 'float32', buffer, [ N ], [ stride ], offset, 'row-major' ); +} + +/** +* Returns a two-dimensional ndarray. +* +* @private +* @param {Collection} buffer - underlying data buffer +* @param {NonNegativeInteger} M - number of rows +* @param {NonNegativeInteger} N - number of columns +* @param {integer} stride0 - stride of the first dimension +* @param {integer} stride1 - stride of the second dimension +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} two-dimensional ndarray +*/ +function matrix( buffer, M, N, stride0, stride1, offset ) { + return new ndarray( 'float32', buffer, [ M, N ], [ stride0, stride1 ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof scartesianPower, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( scartesianPower.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function computes the Cartesian power (row-major, k=2)', function test( t ) { + var expected; + var out; + var x; + var k; + var v; + + x = vector( new Float32Array( [ 1.0, 2.0 ] ), 2, 1, 0 ); + out = matrix( new Float32Array( 8 ), 4, 2, 2, 1, 0 ); + + k = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + v = scartesianPower( [ x, out, k ] ); + + expected = new Float32Array( [ 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0 ] ); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the Cartesian power (row-major, k=3)', function test( t ) { + var expected; + var out; + var x; + var k; + var v; + + x = vector( new Float32Array( [ 1.0, 2.0 ] ), 2, 1, 0 ); + out = matrix( new Float32Array( 24 ), 8, 3, 3, 1, 0 ); + + k = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + v = scartesianPower( [ x, out, k ] ); + + expected = new Float32Array([ + 1.0, + 1.0, + 1.0, + 1.0, + 1.0, + 2.0, + 1.0, + 2.0, + 1.0, + 1.0, + 2.0, + 2.0, + 2.0, + 1.0, + 1.0, + 2.0, + 1.0, + 2.0, + 2.0, + 2.0, + 1.0, + 2.0, + 2.0, + 2.0 + ]); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the Cartesian power (row-major, N=3, k=2)', function test( t ) { + var expected; + var out; + var x; + var k; + var v; + + x = vector( new Float32Array( [ 1.0, 2.0, 3.0 ] ), 3, 1, 0 ); + out = matrix( new Float32Array( 18 ), 9, 2, 2, 1, 0 ); + + k = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + v = scartesianPower( [ x, out, k ] ); + + expected = new Float32Array([ + 1.0, + 1.0, + 1.0, + 2.0, + 1.0, + 3.0, + 2.0, + 1.0, + 2.0, + 2.0, + 2.0, + 3.0, + 3.0, + 1.0, + 3.0, + 2.0, + 3.0, + 3.0 + ]); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the Cartesian power (row-major, N=1, k=1)', function test( t ) { + var expected; + var out; + var x; + var k; + var v; + + x = vector( new Float32Array( [ 5.0 ] ), 1, 1, 0 ); + out = matrix( new Float32Array( 1 ), 1, 1, 1, 1, 0 ); + + k = scalar2ndarray( 1, { + 'dtype': 'generic' + }); + v = scartesianPower( [ x, out, k ] ); + + expected = new Float32Array( [ 5.0 ] ); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the Cartesian power (column-major)', function test( t ) { + var expected; + var out; + var x; + var k; + var v; + + x = vector( new Float32Array( [ 1.0, 2.0 ] ), 2, 1, 0 ); + out = matrix( new Float32Array( 8 ), 4, 2, 1, 4, 0 ); + + k = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + v = scartesianPower( [ x, out, k ] ); + + expected = new Float32Array( [ 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 1.0, 2.0 ] ); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a reference to the output ndarray', function test( t ) { + var out; + var x; + var k; + var v; + + x = vector( new Float32Array( [ 1.0, 2.0 ] ), 2, 1, 0 ); + out = matrix( new Float32Array( 8 ), 4, 2, 2, 1, 0 ); + + k = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + v = scartesianPower( [ x, out, k ] ); + + t.strictEqual( v, out, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided `N` or `k` less than or equal to `0`, the function returns the output ndarray unchanged', function test( t ) { + var expected; + var out; + var x; + var k; + var v; + + out = matrix( new Float32Array( [ 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 ] ), 4, 2, 2, 1, 0 ); + expected = new Float32Array( [ 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + + x = vector( new Float32Array( [ 1.0, 2.0 ] ), 0, 1, 0 ); + + k = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + v = scartesianPower( [ x, out, k ] ); + + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + + x = vector( new Float32Array( [ 1.0, 2.0 ] ), 2, 1, 0 ); + + k = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + v = scartesianPower( [ x, out, k ] ); + + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + + k = scalar2ndarray( -1, { + 'dtype': 'generic' + }); + v = scartesianPower( [ x, out, k ] ); + + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports input ndarrays with strides', function test( t ) { + var expected; + var out; + var x; + var k; + var v; + + x = vector( new Float32Array( [ 1.0, 0.0, 2.0, 0.0 ] ), 2, 2, 0 ); + out = matrix( new Float32Array( 8 ), 4, 2, 2, 1, 0 ); + + k = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + v = scartesianPower( [ x, out, k ] ); + + expected = new Float32Array( [ 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0 ] ); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports input ndarrays with negative strides', function test( t ) { + var expected; + var out; + var x; + var k; + var v; + + x = vector( new Float32Array( [ 2.0, 1.0 ] ), 2, -1, 1 ); + out = matrix( new Float32Array( 8 ), 4, 2, 2, 1, 0 ); + + k = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + v = scartesianPower( [ x, out, k ] ); + + expected = new Float32Array( [ 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0 ] ); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports ndarrays having non-zero offsets', function test( t ) { + var expected; + var out; + var x; + var k; + var v; + + x = vector( new Float32Array( [ 99.0, 1.0, 2.0 ] ), 2, 1, 1 ); + out = matrix( new Float32Array( 10 ), 4, 2, 2, 1, 2 ); + + k = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + v = scartesianPower( [ x, out, k ] ); + + expected = new Float32Array( [ 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0 ] ); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports output ndarrays with negative strides', function test( t ) { + var expected; + var out; + var x; + var k; + var v; + + x = vector( new Float32Array( [ 1.0, 2.0 ] ), 2, 1, 0 ); + out = matrix( new Float32Array( 8 ), 4, 2, -2, 1, 6 ); + + k = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + v = scartesianPower( [ x, out, k ] ); + + expected = new Float32Array( [ 2.0, 2.0, 2.0, 1.0, 1.0, 2.0, 1.0, 1.0 ] ); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat32Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); From 88068c196d054056782d1d3ceacae3565dd6244c Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 14 Oct 2026 13:43:55 +0200 Subject: [PATCH 2/2] Apply batched suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../@stdlib/blas/ext/base/ndarray/scartesian-power/README.md | 2 +- .../blas/ext/base/ndarray/scartesian-power/docs/repl.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/README.md index 201aae2db307..45c462fe1355 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/README.md @@ -77,7 +77,7 @@ The function has the following parameters: ## Notes -- `k`-tuples are stored as rows in the output matrix, where the `j`-th column contains the `j`-th element of each tuple. +- Tuples are stored as rows in the output matrix, where the `j`-th column contains the `j`-th element of each tuple. - For an input array of length `N`, the output array should have shape `[N^k, k]`, where `N^k` is the number of rows and `k` is the number of columns. - If `N <= 0` or `k <= 0`, the function returns the output ndarray unchanged. diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/docs/repl.txt index 4c02768eed54..481862cd1289 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/scartesian-power/docs/repl.txt @@ -2,7 +2,7 @@ {{alias}}( arrays ) Computes the Cartesian power for a single-precision floating-point ndarray. - `k`-tuples are stored as rows in the output matrix, where the `j`-th column + Tuples are stored as rows in the output matrix, where the `j`-th column contains the `j`-th element of each tuple. For an input array of length `N`, the output array should have shape