diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/README.md b/lib/node_modules/@stdlib/ndarray/base/eye/README.md new file mode 100644 index 000000000000..59c82b14eecb --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/README.md @@ -0,0 +1,136 @@ + + +# eye + +> Create a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var eye = require( '@stdlib/ndarray/base/eye' ); +``` + +#### eye( dtype, rows, cols, k, order ) + +Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. + +```javascript +var getShape = require( '@stdlib/ndarray/shape' ); +var getDType = require( '@stdlib/ndarray/dtype' ); + +var arr = eye( 'float64', 3, 3, 0, 'row-major'); +// returns [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] + +var sh = getShape( arr ); +// returns [ 3, 3 ] + +var dt = String( getDType( arr ) ); +// returns 'float64' +``` + +The function accepts the following arguments: + +- **dtype**: underlying [data type][@stdlib/ndarray/dtypes]. +- **rows**: number of rows. +- **cols**: number of columns. +- **k**: diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal. +- **order**: specifies whether an [ndarray][@stdlib/ndarray/base/ctor] is `'row-major'` (C-style) or `'column-major'` (Fortran-style). + + +
+ + + + + +
+ +## Notes + +- If the provided [data type][@stdlib/ndarray/dtypes] is complex, the function returns an ndarray whose kth diagonal is filled with complex numbers whose real component equals one and imaginary component equals zero. +- If `k` is greater than the number of columns or less than the number of negative rows, the function returns an ndarray filled with zeros. + +
+ + + + + +
+ +## Examples + + + +```javascript +var eye = require( '@stdlib/ndarray/base/eye' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +var arr = eye( 'float64', 3, 3, 1, 'row-major'); +console.log( ndarray2array( arr ) ); + +arr = eye( 'complex64', 3, 3, 1, 'row-major'); +console.log( ndarray2array( arr ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.js new file mode 100644 index 000000000000..d338adbdafbe --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.js @@ -0,0 +1,264 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var eye = require( './../lib' ); + + +// MAIN // + +bench( format( '%s:dtype=float64', pkg ), function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'float64', 0, 0, 0, 'row-major' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=float32', pkg ), function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'float32', 0, 0, 0, 'row-major' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=complex128', pkg ), function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'complex128', 0, 0, 0, 'row-major' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=complex64', pkg ), function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'complex64', 0, 0, 0, 'row-major' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=int32', pkg ), function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'int32', 0, 0, 0, 'row-major' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=uint32', pkg ), function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'uint32', 0, 0, 0, 'row-major' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=int16', pkg ), function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'int16', 0, 0, 0, 'row-major' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=uint16', pkg ), function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'uint16', 0, 0, 0, 'row-major' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=int8', pkg ), function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'int8', 0, 0, 0, 'row-major' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=uint8', pkg ), function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'uint8', 0, 0, 0, 'row-major' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=uint8c', pkg ), function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'uint8c', 0, 0, 0, 'row-major' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=bool', pkg ), function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'bool', 0, 0, 0, 'row-major' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=generic', pkg ), function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'generic', 0, 0, 0, 'row-major' ); + if ( arr.length !== 0 ) { + b.fail( 'should have length 0' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.bool.js b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.bool.js new file mode 100644 index 000000000000..0b02a54e9569 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.bool.js @@ -0,0 +1,94 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var eye = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'bool', len, len, 0, 'row-major' ); + if ( arr.length !== pow( len, 2 ) ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + 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:dtype=bool,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.complex128.js b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.complex128.js new file mode 100644 index 000000000000..d63043b15e78 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.complex128.js @@ -0,0 +1,94 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var eye = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'complex128', len, len, 0, 'row-major' ); + if ( arr.length !== pow( len, 2 ) ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + 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:dtype=complex128,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.complex64.js b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.complex64.js new file mode 100644 index 000000000000..c98aca90ae8c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.complex64.js @@ -0,0 +1,94 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var eye = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'complex64', len, len, 0, 'row-major' ); + if ( arr.length !== pow( len, 2 ) ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + 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:dtype=complex64,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.float64.js b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.float64.js new file mode 100644 index 000000000000..69b02e500a9a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.float64.js @@ -0,0 +1,94 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var eye = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'float64', len, len, 0, 'row-major' ); + if ( arr.length !== pow( len, 2 ) ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + 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:dtype=float64,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.generic.js b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.generic.js new file mode 100644 index 000000000000..aecbb7cef427 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.generic.js @@ -0,0 +1,94 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var eye = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'generic', len, len, 0, 'row-major' ); + if ( arr.length !== pow( len, 2 ) ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + 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:dtype=generic,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.int16.js b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.int16.js new file mode 100644 index 000000000000..c723ddbcb8f8 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.int16.js @@ -0,0 +1,94 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var eye = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'int16', len, len, 0, 'row-major' ); + if ( arr.length !== pow( len, 2 ) ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + 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:dtype=int16,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.int32.js b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.int32.js new file mode 100644 index 000000000000..3e2b7c8ab996 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.int32.js @@ -0,0 +1,94 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var eye = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'int32', len, len, 0, 'row-major' ); + if ( arr.length !== pow( len, 2 ) ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + 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:dtype=int32,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.int8.js b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.int8.js new file mode 100644 index 000000000000..6bcab14734be --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.int8.js @@ -0,0 +1,94 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var eye = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'int8', len, len, 0, 'row-major' ); + if ( arr.length !== pow( len, 2 ) ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + 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:dtype=int8,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.uint16.js b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.uint16.js new file mode 100644 index 000000000000..fd87f11a7e21 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.uint16.js @@ -0,0 +1,94 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var eye = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'uint16', len, len, 0, 'row-major' ); + if ( arr.length !== pow( len, 2 ) ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + 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:dtype=uint16,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.uint32.js b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.uint32.js new file mode 100644 index 000000000000..129e26eb380c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.uint32.js @@ -0,0 +1,94 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var eye = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'uint32', len, len, 0, 'row-major' ); + if ( arr.length !== pow( len, 2 ) ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + 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:dtype=uint32,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.uint8.js b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.uint8.js new file mode 100644 index 000000000000..187c1c87d8ad --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.uint8.js @@ -0,0 +1,94 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var eye = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'uint8', len, len, 0, 'row-major' ); + if ( arr.length !== pow( len, 2 ) ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + 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:dtype=uint8,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.uint8c.js b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.uint8c.js new file mode 100644 index 000000000000..bf10208dfd84 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmark.size.uint8c.js @@ -0,0 +1,94 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var eye = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'uint8c', len, len, 0, 'row-major' ); + if ( arr.length !== pow( len, 2 ) ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + 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:dtype=uint8c,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmkar.size.float32.js b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmkar.size.float32.js new file mode 100644 index 000000000000..84c11308ed09 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/benchmark/benchmkar.size.float32.js @@ -0,0 +1,94 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var eye = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = eye( 'float32', len, len, 0, 'row-major' ); + if ( arr.length !== pow( len, 2 ) ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + 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:dtype=float32,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/eye/docs/repl.txt new file mode 100644 index 000000000000..cd246221cd35 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/docs/repl.txt @@ -0,0 +1,43 @@ + +{{alias}}( dtype, rows, cols, k, order ) + Returns a two-dimensional ndarray with ones on the kth diagonal and zeros + elsewhere. + + If the provided data type is complex, the function returns an ndarray + whose kth diagonal is filled with complex numbers whose real component + equals one and imaginary component equals zero. + + Parameters + ---------- + dtype: string|DataType + Underlying data type. + + rows: integer + Number of rows. + + cols: integer + Number of columns. + + k: integer + Diagonal index. Positive refers to upper diagonal, and negative refers + to lower diagonal. + + order: string + Memory layout (either row-major or column-major). + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var arr = {{alias}}( 'float64', 3, 3, 0, 'row-major' ) + [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] + > var sh = {{alias:@stdlib/ndarray/shape}}( arr ) + [ 3, 3 ] + > var dt = String( {{alias:@stdlib/ndarray/dtype}}( arr ) ) + 'float64' + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/eye/docs/types/index.d.ts new file mode 100644 index 000000000000..e7483bdaa02d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/docs/types/index.d.ts @@ -0,0 +1,389 @@ +/* +* @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 { Order, typedndarray, float64ndarray, float32ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, boolndarray, complex128ndarray, complex64ndarray, genericndarray, DataType, Float64DataType, Float32DataType, Complex128DataType, Complex64DataType, Int32DataType, Int16DataType, Int8DataType, Uint32DataType, Uint16DataType, Uint8DataType, Uint8cDataType, BooleanDataType, GenericDataType } from '@stdlib/types/ndarray'; + +/** +* Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. +* +* @param dtype - underlying data type +* @param rows - number of rows +* @param cols - number of columns +* @param k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal +* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @returns output two-dimensional array +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var arr = eye( 'float64', 3, 3, 0, 'row-major'); +* // returns [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] +* +* var sh = getShape( arr ); +* // returns [ 3, 3 ] +* +* var dt = String( getDType( arr ) ); +* // returns 'float64' +*/ +declare function eye( dtype: Float64DataType, rows: number, cols: number, k: number, order: Order ): float64ndarray; + +/** +* Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. +* +* @param dtype - underlying data type +* @param rows - number of rows +* @param cols - number of columns +* @param k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal +* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @returns output two-dimensional array +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var arr = eye( 'float32', 3, 3, 0, 'row-major'); +* // returns [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] +* +* var sh = getShape( arr ); +* // returns [ 3, 3 ] +* +* var dt = String( getDType( arr ) ); +* // returns 'float32' +*/ +declare function eye( dtype: Float32DataType, rows: number, cols: number, k: number, order: Order ): float32ndarray; + +/** +* Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. +* +* ## Notes +* +* - The function returns an ndarray whose kth diagonal is filled with complex numbers whose real component equals one and imaginary component equals zero +* +* @param dtype - underlying data type +* @param rows - number of rows +* @param cols - number of columns +* @param k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal +* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @returns output two-dimensional array +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var arr = eye( 'complex128', 3, 3, 0, 'row-major'); +* // returns +* +* var sh = getShape( arr ); +* // returns [ 3, 3 ] +* +* var dt = String( getDType( arr ) ); +* // returns 'complex128' +* +* var v = arr.get( 0, 0 ); +* // returns [ 1.0, 0.0 ] +*/ +declare function eye( dtype: Complex128DataType, rows: number, cols: number, k: number, order: Order ): complex128ndarray; + +/** +* Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. +* +* ## Notes +* +* - The function returns an ndarray whose kth diagonal is filled with complex numbers whose real component equals one and imaginary component equals zero +* +* @param dtype - underlying data type +* @param rows - number of rows +* @param cols - number of columns +* @param k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal +* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @returns output two-dimensional array +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var arr = eye( 'complex64', 3, 3, 0, 'row-major'); +* // returns +* +* var sh = getShape( arr ); +* // returns [ 3, 3 ] +* +* var dt = String( getDType( arr ) ); +* // returns 'complex64' +* +* var v = arr.get( 0, 0 ); +* // returns [ 1.0, 0.0 ] +*/ +declare function eye( dtype: Complex64DataType, rows: number, cols: number, k: number, order: Order ): complex64ndarray; + +/** +* Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. +* +* @param dtype - underlying data type +* @param rows - number of rows +* @param cols - number of columns +* @param k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal +* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @returns output two-dimensional array +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var arr = eye( 'int32', 3, 3, 0, 'row-major'); +* // returns [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ] +* +* var sh = getShape( arr ); +* // returns [ 3, 3 ] +* +* var dt = String( getDType( arr ) ); +* // returns 'int32' +*/ +declare function eye( dtype: Int32DataType, rows: number, cols: number, k: number, order: Order ): int32ndarray; + +/** +* Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. +* +* @param dtype - underlying data type +* @param rows - number of rows +* @param cols - number of columns +* @param k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal +* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @returns output two-dimensional array +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var arr = eye( 'int16', 3, 3, 0, 'row-major'); +* // returns [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ] +* +* var sh = getShape( arr ); +* // returns [ 3, 3 ] +* +* var dt = String( getDType( arr ) ); +* // returns 'int16' +*/ +declare function eye( dtype: Int16DataType, rows: number, cols: number, k: number, order: Order ): int16ndarray; + +/** +* Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. +* +* @param dtype - underlying data type +* @param rows - number of rows +* @param cols - number of columns +* @param k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal +* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @returns output two-dimensional array +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var arr = eye( 'int8', 3, 3, 0, 'row-major'); +* // returns [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ] +* +* var sh = getShape( arr ); +* // returns [ 3, 3 ] +* +* var dt = String( getDType( arr ) ); +* // returns 'int8' +*/ +declare function eye( dtype: Int8DataType, rows: number, cols: number, k: number, order: Order ): int8ndarray; + +/** +* Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. +* +* @param dtype - underlying data type +* @param rows - number of rows +* @param cols - number of columns +* @param k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal +* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @returns output two-dimensional array +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var arr = eye( 'uint32', 3, 3, 0, 'row-major'); +* // returns [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ] +* +* var sh = getShape( arr ); +* // returns [ 3, 3 ] +* +* var dt = String( getDType( arr ) ); +* // returns 'uint32' +*/ +declare function eye( dtype: Uint32DataType, rows: number, cols: number, k: number, order: Order ): uint32ndarray; + +/** +* Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. +* +* @param dtype - underlying data type +* @param rows - number of rows +* @param cols - number of columns +* @param k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal +* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @returns output two-dimensional array +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var arr = eye( 'uint16', 3, 3, 0, 'row-major'); +* // returns [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ] +* +* var sh = getShape( arr ); +* // returns [ 3, 3 ] +* +* var dt = String( getDType( arr ) ); +* // returns 'uint16' +*/ +declare function eye( dtype: Uint16DataType, rows: number, cols: number, k: number, order: Order ): uint16ndarray; + +/** +* Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. +* +* @param dtype - underlying data type +* @param rows - number of rows +* @param cols - number of columns +* @param k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal +* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @returns output two-dimensional array +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var arr = eye( 'uint8', 3, 3, 0, 'row-major'); +* // returns [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ] +* +* var sh = getShape( arr ); +* // returns [ 3, 3 ] +* +* var dt = String( getDType( arr ) ); +* // returns 'uint8' +*/ +declare function eye( dtype: Uint8DataType, rows: number, cols: number, k: number, order: Order ): uint8ndarray; + +/** +* Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. +* +* @param dtype - underlying data type +* @param rows - number of rows +* @param cols - number of columns +* @param k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal +* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @returns output two-dimensional array +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var arr = eye( 'uint8c', 3, 3, 0, 'row-major'); +* // returns [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ] +* +* var sh = getShape( arr ); +* // returns [ 3, 3 ] +* +* var dt = String( getDType( arr ) ); +* // returns 'uint8c' +*/ +declare function eye( dtype: Uint8cDataType, rows: number, cols: number, k: number, order: Order ): uint8cndarray; + +/** +* Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. +* +* @param dtype - underlying data type +* @param rows - number of rows +* @param cols - number of columns +* @param k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal +* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @returns output two-dimensional array +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var arr = eye( 'bool', 3, 3, 0, 'row-major'); +* // returns [ [ true, false, false ], [ false, true, false ], [ false, false, true ] ] +* +* var sh = getShape( arr ); +* // returns [ 3, 3 ] +* +* var dt = String( getDType( arr ) ); +* // returns 'bool' +*/ +declare function eye( dtype: BooleanDataType, rows: number, cols: number, k: number, order: Order ): boolndarray; + +/** +* Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. +* +* @param dtype - underlying data type +* @param rows - number of rows +* @param cols - number of columns +* @param k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal +* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @returns output two-dimensional array +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var arr = eye( 'generic', 3, 3, 0, 'row-major'); +* // returns [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] +* +* var sh = getShape( arr ); +* // returns [ 3, 3 ] +* +* var dt = String( getDType( arr ) ); +* // returns 'generic' +*/ +declare function eye( dtype: GenericDataType, rows: number, cols: number, k: number, order: Order ): genericndarray; + +/** +* Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. +* +* @param dtype - underlying data type +* @param rows - number of rows +* @param cols - number of columns +* @param k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal +* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @returns output two-dimensional array +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var arr = eye( 'float32', 3, 3, 0, 'row-major'); +* // returns [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] +* +* var sh = getShape( arr ); +* // returns [ 3, 3 ] +* +* var dt = String( getDType( arr ) ); +* // returns 'float32' +*/ +declare function eye( dtype: DataType, rows: number, cols: number, k: number, order: Order ): typedndarray; + +export = eye; diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/eye/docs/types/test.ts new file mode 100644 index 000000000000..cddc1e0cfb0d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/docs/types/test.ts @@ -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. +*/ + +import eye = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + eye( 'float64', 2, 2, 0, 'row-major' ); // $ExpectType float64ndarray + eye( 'float32', 2, 2, 0, 'row-major' ); // $ExpectType float32ndarray + eye( 'complex128', 2, 2, 0, 'row-major' ); // $ExpectType complex128ndarray + eye( 'complex64', 2, 2, 0, 'row-major' ); // $ExpectType complex64ndarray + eye( 'int32', 2, 2, 0, 'row-major' ); // $ExpectType int32ndarray + eye( 'int16', 2, 2, 0, 'row-major' ); // $ExpectType int16ndarray + eye( 'int8', 2, 2, 0, 'row-major' ); // $ExpectType int8ndarray + eye( 'uint32', 2, 2, 0, 'row-major' ); // $ExpectType uint32ndarray + eye( 'uint16', 2, 2, 0, 'row-major' ); // $ExpectType uint16ndarray + eye( 'uint8', 2, 2, 0, 'row-major' ); // $ExpectType uint8ndarray + eye( 'uint8c', 2, 2, 0, 'row-major' ); // $ExpectType uint8cndarray + eye( 'bool', 2, 2, 0, 'row-major' ); // $ExpectType boolndarray + eye( 'generic', 2, 2, 0, 'row-major' ); // $ExpectType genericndarray + + eye( 'float64', 2, 2, 0, 'column-major' ); // $ExpectType float64ndarray + eye( 'float32', 2, 2, 0, 'column-major' ); // $ExpectType float32ndarray + eye( 'complex128', 2, 2, 0, 'column-major' ); // $ExpectType complex128ndarray + eye( 'complex64', 2, 2, 0, 'column-major' ); // $ExpectType complex64ndarray + eye( 'int32', 2, 2, 0, 'column-major' ); // $ExpectType int32ndarray + eye( 'int16', 2, 2, 0, 'column-major' ); // $ExpectType int16ndarray + eye( 'int8', 2, 2, 0, 'column-major' ); // $ExpectType int8ndarray + eye( 'uint32', 2, 2, 0, 'column-major' ); // $ExpectType uint32ndarray + eye( 'uint16', 2, 2, 0, 'column-major' ); // $ExpectType uint16ndarray + eye( 'uint8', 2, 2, 0, 'column-major' ); // $ExpectType uint8ndarray + eye( 'uint8c', 2, 2, 0, 'column-major' ); // $ExpectType uint8cndarray + eye( 'bool', 2, 2, 0, 'column-major' ); // $ExpectType boolndarray + eye( 'generic', 2, 2, 0, 'column-major' ); // $ExpectType genericndarray +} + +// The compiler throws an error if the function is provided a first argument which is an unrecognized/unsupported data type... +{ + eye( '10', 2, 2, 0, 'row-major' ); // $ExpectError + eye( 10, 2, 2, 0, 'row-major' ); // $ExpectError + eye( false, 2, 2, 0, 'row-major' ); // $ExpectError + eye( true, 2, 2, 0, 'row-major' ); // $ExpectError + eye( null, 2, 2, 0, 'row-major' ); // $ExpectError + eye( [], 2, 2, 0, 'row-major' ); // $ExpectError + eye( {}, 2, 2, 0, 'row-major' ); // $ExpectError + eye( ( x: number ): number => x, 2, 2, 0, 'row-major' ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a valid row for the second argument... +{ + eye( 'float32', '5', 2, 0, 'row-major' ); // $ExpectError + eye( 'float32', false, 2, 0, 'row-major' ); // $ExpectError + eye( 'float32', true, 2, 0, 'row-major' ); // $ExpectError + eye( 'float32', null, 2, 0, 'row-major' ); // $ExpectError + eye( 'float32', undefined, 2, 0, 'row-major' ); // $ExpectError + eye( 'float32', [ '5' ], 2, 0, 'row-major' ); // $ExpectError + eye( 'float32', {}, 2, 0, 'row-major' ); // $ExpectError + eye( 'float32', ( x: number ): number => x, 2, 0, 'row-major' ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a valid column for the third argument... +{ + eye( 'float32', 2, '5', 0, 'row-major' ); // $ExpectError + eye( 'float32', 2, false, 0, 'row-major' ); // $ExpectError + eye( 'float32', 2, true, 0, 'row-major' ); // $ExpectError + eye( 'float32', 2, null, 0, 'row-major' ); // $ExpectError + eye( 'float32', 2, undefined, 0, 'row-major' ); // $ExpectError + eye( 'float32', 2, [ '5' ], 0, 'row-major' ); // $ExpectError + eye( 'float32', 2, {}, 0, 'row-major' ); // $ExpectError + eye( 'float32', 2, ( x: number ): number => x, 0, 'row-major' ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a valid k for the fourth argument... +{ + eye( 'float32', 2 , 2, '5', 'row-major' ); // $ExpectError + eye( 'float32', 2 , 2, false, 'row-major' ); // $ExpectError + eye( 'float32', 2 , 2, true, 'row-major' ); // $ExpectError + eye( 'float32', 2 , 2, null, 'row-major' ); // $ExpectError + eye( 'float32', 2 , 2, undefined, 'row-major' ); // $ExpectError + eye( 'float32', 2 , 2, [ '5' ], 'row-major' ); // $ExpectError + eye( 'float32', 2 , 2, {}, 'row-major' ); // $ExpectError + eye( 'float32', 2 , 2, ( x: number ): number => x, 'row-major' ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a valid order for the fifth argument... +{ + eye( 'float32', 2 , 2, 0, '5' ); // $ExpectError + eye( 'float32', 2 , 2, 0, false ); // $ExpectError + eye( 'float32', 2 , 2, 0, true ); // $ExpectError + eye( 'float32', 2 , 2, 0, null ); // $ExpectError + eye( 'float32', 2 , 2, 0, undefined ); // $ExpectError + eye( 'float32', 2 , 2, 0, [ '5' ] ); // $ExpectError + eye( 'float32', 2 , 2, 0, {} ); // $ExpectError + eye( 'float32', 2 , 2, 0, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + eye( 'float64' ); // $ExpectError + eye( 'float64', 2 , 2 ); // $ExpectError + eye( 'float64', 2 , 2, 0 ); // $ExpectError + eye( 'float64', 2 , 2, 0, 'row-major', 1 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/eye/examples/index.js new file mode 100644 index 000000000000..5cabec628648 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/examples/index.js @@ -0,0 +1,28 @@ +/** +* @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 ndarray2array = require( '@stdlib/ndarray/to-array' ); +var eye = require( './../lib' ); + +var x = eye( 'float64', 3, 3, 0, 'row-major'); +console.log( ndarray2array( x ) ); + +x = eye( 'complex64', 3, 3, 0, 'row-major'); +console.log( ndarray2array( x ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/eye/lib/index.js new file mode 100644 index 000000000000..1e38d0945394 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/lib/index.js @@ -0,0 +1,48 @@ +/** +* @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'; + +/** +* Create a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. +* +* @module @stdlib/ndarray/base/eye +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var eye = require( '@stdlib/ndarray/base/eye' ); +* +* var arr = eye( 'float64', 3, 3, 0, 'row-major'); +* // returns [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] +* +* var sh = getShape( arr ); +* // returns [ 3, 3 ] +* +* var dt = String( getDType( arr ) ); +* // returns 'float64' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/eye/lib/main.js new file mode 100644 index 000000000000..f6884b131fa3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/lib/main.js @@ -0,0 +1,139 @@ +/** +* @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 isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' ); +var accessorSetter = require( '@stdlib/array/base/accessor-setter' ); +var isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' ); +var isInteger = require( '@stdlib/assert/is-integer' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ); +var ctors = require( '@stdlib/complex/ctors' ); +var max = require( '@stdlib/math/base/special/max' ); +var min = require( '@stdlib/math/base/special/min' ); +var format = require( '@stdlib/string/format' ); +var zeros = require( '@stdlib/ndarray/base/zeros' ); + + +// FUNCTIONS // + +/** +* Default buffer setter function. +* +* @private +* @param {ArrayBufferLike} buf - array buffer +* @param {Number} idx - array index +* @param {Number|ComplexLike} v - value +*/ +function setter( buf, idx, v ) { + buf[ idx ] = v; +} + + +// MAIN // + +/** +* Returns a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere. +* +* @param {*} dtype - output array data type +* @param {NonNegativeInteger} rows - output array number of rows +* @param {NonNegativeInteger} cols - output array number of columns +* @param {IntegerNumber} k - diagonal index. positive refers to upper diagonal, and negative refers to lower diagonal +* @param {string} order - memory layout (either row-major or column-major) +* @throws {TypeError} first argument must be a recognized data type +* @throws {TypeError} second and third arguments must be non-negative integers +* @throws {TypeError} fourth argument must be an integer +* @returns {ndarray} ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var arr = eye( 'float64', 3, 3, 0, 'row-major'); +* // returns [ [ 1.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ], [ 0.0, 0.0, 1.0 ] ] +* +* var sh = getShape( arr ); +* // returns [ 3, 3 ] +* +* var dt = String( getDType( arr ) ); +* // returns 'float64' +*/ +function eye( dtype, rows, cols, k, order ) { + var ctor; + var out; + var len; + var buf; + var set; + var sr; + var sc; + var ix; + var dx; + var i; + var v; + var r; + var c; + + if ( !isNonNegativeInteger( rows ) || !isNonNegativeInteger( cols ) ) { + throw new TypeError( format('invalid arguments. Second and third arguments must be non-negative integers. Rows: %s, Columns: %s.', rows, cols) ); + } + + if ( !isInteger( k ) ) { + throw new TypeError( format('invalid arguments. The fourth argument must be an integer. Diagonal index: %s.', k) ); + } + + if ( k < 0 ) { + r = -k; + c = 0; + } else { + r = 0; + c = k; + } + + out = zeros( dtype, [ rows, cols ], order ); + + sr = out.strides[ 0 ]; + sc = out.strides[ 1 ]; + ix = out.offset + (r * sr) + (c * sc); // offset + (row * row_stride) + (col * col_stride) + dx = sr + sc; + + if ( isAccessorArray( out.data ) ) { + set = accessorSetter( dtype ); + } else { + set = setter; + } + buf = out.data; + len = max( 0, min( rows - r, cols - c ) ); + + if ( isComplexDataType( dtype ) ) { + ctor = ctors( dtype ); + v = new ctor( 1.0, 0.0 ); + } else { + v = 1.0; + } + + for ( i = 0; i < len; i++ ) { + set( buf, ix, v ); + ix += dx; + } + + return out; +} + +module.exports = eye; diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/package.json b/lib/node_modules/@stdlib/ndarray/base/eye/package.json new file mode 100644 index 000000000000..351279385201 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/package.json @@ -0,0 +1,62 @@ +{ + "name": "@stdlib/ndarray/base/eye", + "version": "0.0.0", + "description": "Create a two-dimensional ndarray with ones on the kth diagonal and zeros elsewhere.", + "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" + }, + "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", + "stdtypes", + "types", + "base", + "data", + "structure", + "ndarray", + "matrix", + "identity", + "diag" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/base/eye/test/test.js b/lib/node_modules/@stdlib/ndarray/base/eye/test/test.js new file mode 100644 index 000000000000..ce343ae028b3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/eye/test/test.js @@ -0,0 +1,663 @@ +/** +* @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 isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' ); +var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var Float64Array = require( '@stdlib/array/float64' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var eye = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof eye, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided an invalid data type as the first argument', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.14, + NaN, + 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 ) { + return function badValue() { + eye( value, 2, 2, 0, 'row-major' ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid diagonal index as the fourth argument', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.14, + NaN, + 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 ) { + return function badValue() { + eye( 'float64', 2, 2, value, 'row-major' ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid rows or columns as the second or third argument', function test( t ) { + var values; + var i; + + values = [ + '5', + -5, + -3.14, + 3.14, + NaN, + 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 ) { + return function badValue() { + eye( 'float64', value, value, 0, 'row-major' ); + }; + } +}); + +tape( 'the function returns a zero-filled ndarray when provided zero rows and columns', function test( t ) { + var out; + + out = eye( 'float64', 0, 0, 1, 'row-major' ); + + t.strictEqual( String( getDType( out ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 0, 0 ], 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( out ), new Float64Array( 0 ) ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an empty array when provided zero rows and columns (accessors)', function test( t ) { + var out; + + out = eye( 'complex128', 0, 0, 1, 'row-major' ); + + t.strictEqual( String( getDType( out ) ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 0, 0 ], 'returns expected value' ); + t.strictEqual( isSameComplex128Array( getData( out ), new Complex128Array( 0 ) ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-filled array when provided k greater than columns', function test( t ) { + var out; + + out = eye( 'float64', 3, 6, 7, 'row-major' ); + + t.strictEqual( String( getDType( out ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 3, 6 ], 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( out ), new Float64Array( 18 ) ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-filled array when provided k less than negative rows', function test( t ) { + var out; + + out = eye( 'float64', 3, 6, -4, 'row-major' ); + + t.strictEqual( String( getDType( out ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 3, 6 ], 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( out ), new Float64Array( 18 ) ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a square identity matrix (row-major)', function test( t ) { + var expected; + var out; + + out = eye( 'float64', 2, 2, 0, 'row-major' ); + + expected = new Float64Array([ + 1.0, + 0.0, + 0.0, + 1.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a square identity matrix (row-major, accessors)', function test( t ) { + var expected; + var out; + + out = eye( 'complex128', 2, 2, 0, 'row-major' ); + + expected = new Complex128Array([ + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( isSameComplex128Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a square identity matrix (column-major)', function test( t ) { + var expected; + var out; + + out = eye( 'float64', 3, 3, 0, 'column-major' ); + + expected = new Float64Array([ + 1.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 1.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 3, 3 ], 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a square identity matrix (column-major, accessors)', function test( t ) { + var expected; + var out; + + out = eye( 'complex128', 2, 2, 0, 'column-major' ); + + expected = new Complex128Array([ + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( isSameComplex128Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a square matrix with ones on a positive diagonal offset (row-major)', function test( t ) { + var expected; + var out; + + out = eye( 'float64', 3, 3, 1, 'row-major' ); + + expected = new Float64Array([ + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 3, 3 ], 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a square matrix with ones on a positive diagonal offset (row-major, accessors)', function test( t ) { + var expected; + var out; + + out = eye( 'complex128', 2, 2, 1, 'row-major' ); + + expected = new Complex128Array([ + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( isSameComplex128Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a square matrix with ones on a positive diagonal offset (column-major)', function test( t ) { + var expected; + var out; + + out = eye( 'float64', 3, 3, 1, 'column-major' ); + + expected = new Float64Array([ + 0.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 3, 3 ], 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a square matrix with ones on a positive diagonal offset (column-major, accessors)', function test( t ) { + var expected; + var out; + + out = eye( 'complex128', 2, 2, 1, 'column-major' ); + + expected = new Complex128Array([ + 0.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( isSameComplex128Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a square matrix with ones on a negative diagonal offset (row-major)', function test( t ) { + var expected; + var out; + + out = eye( 'float64', 3, 3, -1, 'row-major' ); + + expected = new Float64Array([ + 0.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 3, 3 ], 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a square matrix with ones on a negative diagonal offset (row-major, accessors)', function test( t ) { + var expected; + var out; + + out = eye( 'complex128', 2, 2, -1, 'row-major' ); + + expected = new Complex128Array([ + 0.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( isSameComplex128Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a square matrix with ones on a negative diagonal offset (column-major)', function test( t ) { + var expected; + var out; + + out = eye( 'float64', 3, 3, -1, 'column-major' ); + + expected = new Float64Array([ + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 3, 3 ], 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a square matrix with ones on a positive diagonal offset (column-major, accessors)', function test( t ) { + var expected; + var out; + + out = eye( 'complex128', 2, 2, -1, 'column-major' ); + + expected = new Complex128Array([ + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( isSameComplex128Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a rectangular matrix with ones on a positive diagonal offset (row-major)', function test( t ) { + var expected; + var out; + + out = eye( 'float64', 2, 3, 1, 'row-major' ); + + expected = new Float64Array([ + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 1.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 2, 3 ], 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a rectangular matrix with ones on a positive diagonal offset (row-major, accessors)', function test( t ) { + var expected; + var out; + + out = eye( 'complex128', 2, 3, 1, 'row-major' ); + + expected = new Complex128Array([ + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 2, 3 ], 'returns expected value' ); + t.strictEqual( isSameComplex128Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a rectangular matrix with ones on a positive diagonal offset (column-major)', function test( t ) { + var expected; + var out; + + out = eye( 'float64', 2, 3, 1, 'column-major' ); + + expected = new Float64Array([ + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 1.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 2, 3 ], 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a rectangular matrix with ones on a positive diagonal offset (column-major, accessors)', function test( t ) { + var expected; + var out; + + out = eye( 'complex128', 2, 3, 1, 'column-major' ); + + expected = new Complex128Array([ + 0.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 2, 3 ], 'returns expected value' ); + t.strictEqual( isSameComplex128Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a rectangular matrix with ones on a negative diagonal offset (row-major)', function test( t ) { + var expected; + var out; + + out = eye( 'float64', 2, 3, -1, 'row-major' ); + + expected = new Float64Array([ + 0.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 2, 3 ], 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a rectangular matrix with ones on a negative diagonal offset (row-major, accessors)', function test( t ) { + var expected; + var out; + + out = eye( 'complex128', 2, 3, -1, 'row-major' ); + + expected = new Complex128Array([ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 2, 3 ], 'returns expected value' ); + t.strictEqual( isSameComplex128Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a rectangular matrix with ones on a negative diagonal offset (column-major)', function test( t ) { + var expected; + var out; + + out = eye( 'float64', 2, 3, -1, 'column-major' ); + + expected = new Float64Array([ + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 0.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 2, 3 ], 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a rectangular matrix with ones on a negative diagonal offset (column-major, accessors)', function test( t ) { + var expected; + var out; + + out = eye( 'complex128', 2, 3, -1, 'column-major' ); + + expected = new Complex128Array([ + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ]); + + t.strictEqual( String( getDType( out ) ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 2, 3 ], 'returns expected value' ); + t.strictEqual( isSameComplex128Array( getData( out ), expected ), true, 'returns expected value' ); + + t.end(); +});