From 349919e7f68a45d0f47ae6457815498d900cc4ca Mon Sep 17 00:00:00 2001 From: Samarth Kolarkar Date: Tue, 22 Sep 2026 23:42:10 +0530 Subject: [PATCH] feat: add float16 dtype support to ndarray/base/full-by --- .../base/full-by/benchmark/benchmark.js | 22 ++++ .../benchmark/benchmark.size.float16.js | 104 ++++++++++++++++++ .../base/full-by/docs/types/index.d.ts | 27 ++++- .../ndarray/base/full-by/docs/types/test.ts | 3 + .../@stdlib/ndarray/base/full-by/test/test.js | 53 +++++++++ 5 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 lib/node_modules/@stdlib/ndarray/base/full-by/benchmark/benchmark.size.float16.js diff --git a/lib/node_modules/@stdlib/ndarray/base/full-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/full-by/benchmark/benchmark.js index ef2f00396dbf..3ff02f0f413b 100644 --- a/lib/node_modules/@stdlib/ndarray/base/full-by/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/ndarray/base/full-by/benchmark/benchmark.js @@ -75,6 +75,28 @@ bench( format( '%s:dtype=float32', pkg ), function benchmark( b ) { } }); +bench( format( '%s:dtype=float16', pkg ), function benchmark( b ) { + var arr; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = fullBy( 'float16', [ 0 ], 'row-major', clbk ); + 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(); + + function clbk() { + return 10.0; + } +}); + bench( format( '%s:dtype=complex128', pkg ), function benchmark( b ) { var arr; var i; diff --git a/lib/node_modules/@stdlib/ndarray/base/full-by/benchmark/benchmark.size.float16.js b/lib/node_modules/@stdlib/ndarray/base/full-by/benchmark/benchmark.size.float16.js new file mode 100644 index 000000000000..25801850353c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/full-by/benchmark/benchmark.size.float16.js @@ -0,0 +1,104 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var 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 fullBy = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @returns {*} fill value +*/ +function clbk() { + return 10.0; +} + +/** +* 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 = fullBy( 'float16', [ len ], 'row-major', clbk ); + if ( arr.length !== len ) { + 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 = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:dtype=float16,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/full-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/full-by/docs/types/index.d.ts index 078000bf0d64..55353dbbf67e 100644 --- a/lib/node_modules/@stdlib/ndarray/base/full-by/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/base/full-by/docs/types/index.d.ts @@ -20,7 +20,7 @@ /// -import { Shape, 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'; +import { Shape, Order, typedndarray, float64ndarray, float32ndarray, float16ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, boolndarray, complex128ndarray, complex64ndarray, genericndarray, DataType, Float64DataType, Float32DataType, Float16DataType, Complex128DataType, Complex64DataType, Int32DataType, Int16DataType, Int8DataType, Uint32DataType, Uint16DataType, Uint8DataType, Uint8cDataType, BooleanDataType, GenericDataType } from '@stdlib/types/ndarray'; import { ComplexLike } from '@stdlib/types/complex'; /** @@ -96,6 +96,31 @@ declare function fullBy( dtype: Float64DataType, shape: Shape */ declare function fullBy( dtype: Float32DataType, shape: Shape, order: Order, clbk: Callback, thisArg?: ThisArg ): float32ndarray; +/** +* Returns an ndarray filled according to a callback function and having a specified shape and data type. +* +* @param dtype - underlying data type +* @param shape - array shape +* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @param clbk - callback function +* @param thisArg - callback function execution context +* @returns output array +* +* @example +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* function clbk() { +* return 10.0; +* } +* +* var arr = fullBy( 'float16', [ 2, 2 ], 'row-major', clbk ); +* // returns [ [ 10.0, 10.0 ], [ 10.0, 10.0 ] ] +* +* var dt = String( getDType( arr ) ); +* // returns 'float16' +*/ +declare function fullBy( dtype: Float16DataType, shape: Shape, order: Order, clbk: Callback, thisArg?: ThisArg ): float16ndarray; + /** * Returns an ndarray filled according to a callback function and having a specified shape and data type. * diff --git a/lib/node_modules/@stdlib/ndarray/base/full-by/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/full-by/docs/types/test.ts index 4e82a5e77d0d..b7c4cc7fa167 100644 --- a/lib/node_modules/@stdlib/ndarray/base/full-by/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/base/full-by/docs/types/test.ts @@ -25,6 +25,7 @@ import fullBy = require( './index' ); { fullBy( 'float64', [ 2, 2 ], 'row-major', () => 10.0 ); // $ExpectType float64ndarray fullBy( 'float32', [ 2, 2 ], 'row-major', () => 10.0 ); // $ExpectType float32ndarray + fullBy( 'float16', [ 2, 2 ], 'row-major', () => 10.0 ); // $ExpectType float16ndarray fullBy( 'complex128', [ 2, 2 ], 'row-major', () => 10.0 ); // $ExpectType complex128ndarray fullBy( 'complex64', [ 2, 2 ], 'row-major', () => 10.0 ); // $ExpectType complex64ndarray fullBy( 'int32', [ 2, 2 ], 'row-major', () => 10 ); // $ExpectType int32ndarray @@ -39,6 +40,7 @@ import fullBy = require( './index' ); fullBy( 'float64', [ 2, 2 ], 'column-major', () => 10.0 ); // $ExpectType float64ndarray fullBy( 'float32', [ 2, 2 ], 'column-major', () => 10.0 ); // $ExpectType float32ndarray + fullBy( 'float16', [ 2, 2 ], 'column-major', () => 10.0 ); // $ExpectType float16ndarray fullBy( 'complex128', [ 2, 2 ], 'column-major', () => 10.0 ); // $ExpectType complex128ndarray fullBy( 'complex64', [ 2, 2 ], 'column-major', () => 10.0 ); // $ExpectType complex64ndarray fullBy( 'int32', [ 2, 2 ], 'column-major', () => 10 ); // $ExpectType int32ndarray @@ -53,6 +55,7 @@ import fullBy = require( './index' ); fullBy( 'float64', [ 2, 2 ], 'row-major', () => 10.0, {} ); // $ExpectType float64ndarray fullBy( 'float32', [ 2, 2 ], 'row-major', () => 10.0, {} ); // $ExpectType float32ndarray + fullBy( 'float16', [ 2, 2 ], 'row-major', () => 10.0, {} ); // $ExpectType float16ndarray fullBy( 'complex128', [ 2, 2 ], 'row-major', () => 10.0, {} ); // $ExpectType complex128ndarray fullBy( 'complex64', [ 2, 2 ], 'row-major', () => 10.0, {} ); // $ExpectType complex64ndarray fullBy( 'int32', [ 2, 2 ], 'row-major', () => 10, {} ); // $ExpectType int32ndarray diff --git a/lib/node_modules/@stdlib/ndarray/base/full-by/test/test.js b/lib/node_modules/@stdlib/ndarray/base/full-by/test/test.js index 924f4b5302a2..3d39a4f465bb 100644 --- a/lib/node_modules/@stdlib/ndarray/base/full-by/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/base/full-by/test/test.js @@ -23,6 +23,7 @@ var tape = require( 'tape' ); var Float64Array = require( '@stdlib/array/float64' ); var Float32Array = require( '@stdlib/array/float32' ); +var Float16Array = require( '@stdlib/array/float16' ); var Int32Array = require( '@stdlib/array/int32' ); var Uint32Array = require( '@stdlib/array/uint32' ); var Int16Array = require( '@stdlib/array/int16' ); @@ -199,6 +200,58 @@ tape( 'the function returns a filled array (dtype=float32, order=column-major)', } }); +tape( 'the function returns a filled array (dtype=float16, order=row-major)', function test( t ) { + var expected; + var arr; + + expected = new Float16Array([ + 10.0, + 10.0, + 10.0, + 10.0 + ]); + + arr = fullBy( 'float16', [ 2, 2 ], 'row-major', clbk ); + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'float16', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Float16Array ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + + t.end(); + + function clbk() { + return 10.0; + } +}); + +tape( 'the function returns a filled array (dtype=float16, order=column-major)', function test( t ) { + var expected; + var arr; + + expected = new Float16Array([ + 10.0, + 10.0, + 10.0, + 10.0 + ]); + + arr = fullBy( 'float16', [ 2, 2 ], 'column-major', clbk ); + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'float16', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Float16Array ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'column-major', 'returns expected value' ); + + t.end(); + + function clbk() { + return 10.0; + } +}); + tape( 'the function returns a filled array (dtype=int32, order=row-major)', function test( t ) { var expected; var arr;