From 33a075d02f043956488637b842bc9cc7b55ced36 Mon Sep 17 00:00:00 2001 From: Samarth Kolarkar Date: Sat, 19 Sep 2026 02:45:02 +0530 Subject: [PATCH] feat: add float16 dtype support to ndarray/base/from-array --- .../base/from-array/benchmark/benchmark.js | 22 ++++++++++++++++ .../base/from-array/docs/types/index.d.ts | 25 +++++++++++++++++-- .../base/from-array/docs/types/test.ts | 1 + .../ndarray/base/from-array/test/test.js | 18 +++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/from-array/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/from-array/benchmark/benchmark.js index a7b5baf9dc99..f1c34be6a405 100644 --- a/lib/node_modules/@stdlib/ndarray/base/from-array/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/ndarray/base/from-array/benchmark/benchmark.js @@ -75,6 +75,28 @@ bench( format( '%s:dtype=float32', pkg ), function benchmark( b ) { b.end(); }); +bench( format( '%s:dtype=float16', pkg ), function benchmark( b ) { + var buf; + var x; + var i; + + buf = typedarray( 1, 'float16' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = array2ndarray( buf, 'row-major' ); + if ( x.length !== 1 ) { + b.fail( 'should have length 1' ); + } + } + b.toc(); + if ( !isndarrayLike( x ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + bench( format( '%s:dtype=complex128', pkg ), function benchmark( b ) { var buf; var x; diff --git a/lib/node_modules/@stdlib/ndarray/base/from-array/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/from-array/docs/types/index.d.ts index 28ea7f2b6abd..a8dda828cd93 100644 --- a/lib/node_modules/@stdlib/ndarray/base/from-array/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/base/from-array/docs/types/index.d.ts @@ -20,8 +20,8 @@ /// -import { Collection, AccessorArrayLike, Complex128Array, Complex64Array, BooleanArray } from '@stdlib/types/array'; -import { float64ndarray, float32ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, complex128ndarray, complex64ndarray, boolndarray, genericndarray, Order } from '@stdlib/types/ndarray'; +import { Collection, AccessorArrayLike, Complex128Array, Complex64Array, BooleanArray, Float16Array } from '@stdlib/types/array'; +import { float64ndarray, float32ndarray, float16ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, complex128ndarray, complex64ndarray, boolndarray, genericndarray, Order } from '@stdlib/types/ndarray'; /** * Converts an array to a one-dimensional ndarray. @@ -65,6 +65,27 @@ declare function array2ndarray( buf: Float64Array, order: Order ): float64ndarra */ declare function array2ndarray( buf: Float32Array, order: Order ): float32ndarray; +/** +* Converts an array to a one-dimensional ndarray. +* +* @param buf - input array +* @param order - memory layout (row-major or column-major) +* @returns one-dimensional ndarray +* +* @example +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var Float16Array = require( '@stdlib/array/float16' ); +* +* var arr = new Float16Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* +* var x = array2ndarray( arr, 'row-major' ); +* // returns +* +* var dt = String( getDType( x ) ); +* // returns 'float16' +*/ +declare function array2ndarray( buf: Float16Array, order: Order ): float16ndarray; + /** * Converts an array to a one-dimensional ndarray. * diff --git a/lib/node_modules/@stdlib/ndarray/base/from-array/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/from-array/docs/types/test.ts index 9385e625104a..787cc6ef0785 100644 --- a/lib/node_modules/@stdlib/ndarray/base/from-array/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/base/from-array/docs/types/test.ts @@ -26,6 +26,7 @@ import array2ndarray = require( './index' ); { array2ndarray( zeros( 1, 'float64' ), 'row-major' ); // $ExpectType float64ndarray array2ndarray( zeros( 1, 'float32' ), 'row-major' ); // $ExpectType float32ndarray + array2ndarray( zeros( 1, 'float16' ), 'row-major' ); // $ExpectType float16ndarray array2ndarray( zeros( 1, 'complex128' ), 'row-major' ); // $ExpectType complex128ndarray array2ndarray( zeros( 1, 'complex64' ), 'row-major' ); // $ExpectType complex64ndarray array2ndarray( zeros( 1, 'int32' ), 'row-major' ); // $ExpectType int32ndarray diff --git a/lib/node_modules/@stdlib/ndarray/base/from-array/test/test.js b/lib/node_modules/@stdlib/ndarray/base/from-array/test/test.js index 01ce040ce549..9d1c20580835 100644 --- a/lib/node_modules/@stdlib/ndarray/base/from-array/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/base/from-array/test/test.js @@ -78,6 +78,24 @@ tape( 'the function returns a one-dimensional ndarray (dtype=float32)', function t.end(); }); +tape( 'the function returns a one-dimensional ndarray (dtype=float16)', function test( t ) { + var buf; + var arr; + + buf = typedarray( 4, 'float16' ); + arr = array2ndarray( buf, 'column-major' ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'float16', 'returns expected value' ); + t.strictEqual( getData( arr ), buf, 'returns expected value' ); + t.deepEqual( getShape( arr ), [ buf.length ], 'returns expected value' ); + t.deepEqual( getStrides( arr ), [ 1 ], 'returns expected value' ); + t.strictEqual( getOffset( arr ), 0, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'column-major', 'returns expected value' ); + + t.end(); +}); + tape( 'the function returns a one-dimensional ndarray (dtype=int32)', function test( t ) { var buf; var arr;