Skip to content

Commit 8e18e43

Browse files
committed
feat: add support for normalize index mode
1 parent e5fe0ce commit 8e18e43

File tree

10 files changed

+554
-69
lines changed

10 files changed

+554
-69
lines changed

lib/node_modules/@stdlib/ndarray/base/ind2sub/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ var subscripts = ind2sub( shape, strides, offset, order, 1, 'throw' );
5454
// returns [ 0, 1 ]
5555
```
5656

57-
The function supports the following `modes`:
57+
The function supports the following modes:
5858

59-
- `throw`: specifies that the function should throw an error when a linear index exceeds array dimensions.
60-
- `wrap`: specifies that the function should wrap around a linear index exceeding array dimensions using modulo arithmetic.
61-
- `clamp`: specifies that the function should set a linear index exceeding array dimensions to either `0` (minimum linear index) or the maximum linear index.
59+
- **throw**: specifies that the function should throw an error when a linear index exceeds array dimensions.
60+
- **normalize**: specifies that the function should normalize negative indices and throw an error when a linear index exceeds array dimensions.
61+
- **wrap**: specifies that the function should wrap around a linear index exceeding array dimensions using modulo arithmetic.
62+
- **clamp**: specifies that the function should set a linear index exceeding array dimensions to either `0` (minimum linear index) or the maximum linear index.
6263

6364
```javascript
6465
var shape = [ 2, 2 ];
@@ -337,7 +338,7 @@ The function accepts the following arguments:
337338
- **out**: `[out] int64_t*` output array.
338339

339340
```c
340-
int8_t stdlib_ndarray_ind2sub( int64_t ndims, int64_t *shape, int64_t *strides, int64_t offset, enum STDLIB_NDARRAY_ORDER order, int64_t idx, enum STDLIB_NDARRAY_INDEX_MODE mode, int64_t *out );
341+
int8_t stdlib_ndarray_ind2sub( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const enum STDLIB_NDARRAY_ORDER order, const int64_t idx, const enum STDLIB_NDARRAY_INDEX_MODE mode, int64_t *out );
341342
```
342343
343344
</section>

lib/node_modules/@stdlib/ndarray/base/ind2sub/benchmark/benchmark.js

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,207 @@ bench( pkg+'::negative_strides:mode=clamp,order=column-major', function benchmar
714714
b.pass( 'benchmark finished' );
715715
b.end();
716716
});
717+
718+
bench( pkg+':mode=normalize,order=row-major', function benchmark( b ) {
719+
var strides;
720+
var offset;
721+
var order;
722+
var shape;
723+
var out;
724+
var len;
725+
var idx;
726+
var i;
727+
728+
shape = [ 10, 10, 10 ];
729+
order = 'row-major';
730+
strides = shape2strides( shape, order );
731+
offset = strides2offset( shape, strides );
732+
len = numel( shape );
733+
734+
b.tic();
735+
for ( i = 0; i < b.iterations; i++ ) {
736+
idx = floor( randu()*(len*2) ) - len;
737+
out = ind2sub( shape, strides, offset, order, idx, 'normalize' );
738+
if ( out.length !== shape.length ) {
739+
b.fail( 'should have expected length' );
740+
}
741+
}
742+
b.toc();
743+
if ( !isNonNegativeIntegerArray( out ) ) {
744+
b.fail( 'should return an array' );
745+
}
746+
b.pass( 'benchmark finished' );
747+
b.end();
748+
});
749+
750+
bench( pkg+':mode=normalize,order=column-major', function benchmark( b ) {
751+
var strides;
752+
var offset;
753+
var order;
754+
var shape;
755+
var out;
756+
var len;
757+
var idx;
758+
var i;
759+
760+
shape = [ 10, 10, 10 ];
761+
order = 'column-major';
762+
strides = shape2strides( shape, order );
763+
offset = strides2offset( shape, strides );
764+
len = numel( shape );
765+
766+
b.tic();
767+
for ( i = 0; i < b.iterations; i++ ) {
768+
idx = floor( randu()*(len*2) ) - len;
769+
out = ind2sub( shape, strides, offset, order, idx, 'normalize' );
770+
if ( out.length !== shape.length ) {
771+
b.fail( 'should have expected length' );
772+
}
773+
}
774+
b.toc();
775+
if ( !isNonNegativeIntegerArray( out ) ) {
776+
b.fail( 'should return an array' );
777+
}
778+
b.pass( 'benchmark finished' );
779+
b.end();
780+
});
781+
782+
bench( pkg+'::negative_strides:offset=0,mode=normalize,order=row-major', function benchmark( b ) {
783+
var strides;
784+
var offset;
785+
var order;
786+
var shape;
787+
var out;
788+
var len;
789+
var idx;
790+
var i;
791+
792+
shape = [ 10, 10, 10 ];
793+
order = 'row-major';
794+
len = numel( shape );
795+
strides = shape2strides( shape, order );
796+
for ( i = 0; i < shape.length; i++ ) {
797+
strides[ i ] *= -1;
798+
}
799+
offset = 0;
800+
801+
b.tic();
802+
for ( i = 0; i < b.iterations; i++ ) {
803+
idx = floor( randu()*(len*2) ) - len;
804+
out = ind2sub( shape, strides, offset, order, idx, 'normalize' );
805+
if ( out.length !== shape.length ) {
806+
b.fail( 'should have expected length' );
807+
}
808+
}
809+
b.toc();
810+
if ( !isNonNegativeIntegerArray( out ) ) {
811+
b.fail( 'should return an array' );
812+
}
813+
b.pass( 'benchmark finished' );
814+
b.end();
815+
});
816+
817+
bench( pkg+'::negative_strides:offset=0,mode=normalize,order=column-major', function benchmark( b ) {
818+
var strides;
819+
var offset;
820+
var order;
821+
var shape;
822+
var out;
823+
var len;
824+
var idx;
825+
var i;
826+
827+
shape = [ 10, 10, 10 ];
828+
order = 'column-major';
829+
len = numel( shape );
830+
strides = shape2strides( shape, order );
831+
for ( i = 0; i < shape.length; i++ ) {
832+
strides[ i ] *= -1;
833+
}
834+
offset = 0;
835+
836+
b.tic();
837+
for ( i = 0; i < b.iterations; i++ ) {
838+
idx = floor( randu()*(len*2) ) - len;
839+
out = ind2sub( shape, strides, offset, order, idx, 'normalize' );
840+
if ( out.length !== shape.length ) {
841+
b.fail( 'should have expected length' );
842+
}
843+
}
844+
b.toc();
845+
if ( !isNonNegativeIntegerArray( out ) ) {
846+
b.fail( 'should return an array' );
847+
}
848+
b.pass( 'benchmark finished' );
849+
b.end();
850+
});
851+
852+
bench( pkg+'::negative_strides:mode=normalize,order=row-major', function benchmark( b ) {
853+
var strides;
854+
var offset;
855+
var order;
856+
var shape;
857+
var out;
858+
var len;
859+
var idx;
860+
var i;
861+
862+
shape = [ 10, 10, 10 ];
863+
order = 'row-major';
864+
len = numel( shape );
865+
strides = shape2strides( shape, order );
866+
for ( i = 0; i < shape.length; i++ ) {
867+
strides[ i ] *= -1;
868+
}
869+
offset = strides2offset( shape, strides );
870+
871+
b.tic();
872+
for ( i = 0; i < b.iterations; i++ ) {
873+
idx = floor( randu()*(len*2) ) - len;
874+
out = ind2sub( shape, strides, offset, order, idx, 'normalize' );
875+
if ( out.length !== shape.length ) {
876+
b.fail( 'should have expected length' );
877+
}
878+
}
879+
b.toc();
880+
if ( !isNonNegativeIntegerArray( out ) ) {
881+
b.fail( 'should return an array' );
882+
}
883+
b.pass( 'benchmark finished' );
884+
b.end();
885+
});
886+
887+
bench( pkg+'::negative_strides:mode=normalize,order=column-major', function benchmark( b ) {
888+
var strides;
889+
var offset;
890+
var order;
891+
var shape;
892+
var out;
893+
var len;
894+
var idx;
895+
var i;
896+
897+
shape = [ 10, 10, 10 ];
898+
order = 'column-major';
899+
len = numel( shape );
900+
strides = shape2strides( shape, order );
901+
for ( i = 0; i < shape.length; i++ ) {
902+
strides[ i ] *= -1;
903+
}
904+
offset = strides2offset( shape, strides );
905+
906+
b.tic();
907+
for ( i = 0; i < b.iterations; i++ ) {
908+
idx = floor( randu()*(len*2) ) - len;
909+
out = ind2sub( shape, strides, offset, order, idx, 'normalize' );
910+
if ( out.length !== shape.length ) {
911+
b.fail( 'should have expected length' );
912+
}
913+
}
914+
b.toc();
915+
if ( !isNonNegativeIntegerArray( out ) ) {
916+
b.fail( 'should return an array' );
917+
}
918+
b.pass( 'benchmark finished' );
919+
b.end();
920+
});

0 commit comments

Comments
 (0)