Skip to content

Commit

Permalink
docs: improve README examples of ndarray/iter namespace
Browse files Browse the repository at this point in the history
Updated examples to span over the functionalities implemented in the namespace.

Addresses: stdlib-js#1589
  • Loading branch information
performant23 committed Mar 4, 2024
1 parent 3163201 commit b36dc4a
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 1 deletion.
139 changes: 139 additions & 0 deletions lib/node_modules/@stdlib/ndarray/iter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,151 @@ var o = ns;
<!-- eslint no-undef: "error" -->

```javascript
// Example: Get an array consisting of keys/properties inside ns.
var objectKeys = require( '@stdlib/utils/keys' );
var ns = require( '@stdlib/ndarray/iter' );

console.log( objectKeys( ns ) );
```

```javascript
// Example: Iterate over [index, column] pairs for each column in a matrix
var ndarray = require( '@stdlib/ndarray/ctor' );
var ns = require( '@stdlib/ndarray/iter' );

var array2D = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];
var data = [].concat.apply( [], array2D );
var x = ndarray( 'generic', data, [ 3, 3 ], [ 3, 1 ], 0, 'row-major' );
var iterEntries = ns.nditerEntries( x );
var entry;

for ( entry of iterEntries ) {
console.log( 'Index:', entry[ 0 ], 'Value:', entry[ 1 ] );
}
```

```javascript
// Example: Iterate over each row of the ndarray

var ndarray = require( '@stdlib/ndarray/ctor' );
var ns = require( '@stdlib/ndarray/iter' );

var array2D = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];
var data = [].concat.apply( [], array2D );
var x = ndarray( 'generic', data, [ 3, 3 ], [ 3, 1 ], 0, 'row-major' );
var iterRows = ns.nditerRows( x );
var rowData;
var row;
var i;

while ( true ) {
row = iterRows.next();
if ( row.done ) {
break;
}
rowData = [];
for ( i = 0; i < row.value.shape[ 0 ]; i++ ) {
rowData.push( row.value.get( i ) );
}
console.log( rowData );
}
```

```javascript
// Example: Iterate over each column of the ndarray
var ns = require( '@stdlib/ndarray/iter' );
var ndarray = require( '@stdlib/ndarray/ctor' );

var array2D = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];
var data = [].concat.apply( [], array2D );
var x = ndarray( 'generic', data, [ 3, 3 ], [ 3, 1 ], 0, 'row-major' );
var iterColumns = ns.nditerColumns( x );
var column;
var k;
var columnData;

while ( true ) {
column = iterColumns.next();
if ( column.done ) {
break;
}
columnData = [];
for ( k = 0; k < column.value.shape[ 0 ]; k++ ) {
columnData.push( column.value.get( k ) );
}
console.log( columnData );
}
```

```javascript
// Example: Iterate over each matrix in the stack
var ns = require( '@stdlib/ndarray/iter' );
var ndarray = require( '@stdlib/ndarray/ctor' );

var matricesData = [
1,
2,
3,
4,
5,
6,
7,
8
];
var dataMatrix = ndarray( 'generic', matricesData, [ 2, 2, 2 ], [ 4, 2, 1 ], 0, 'row-major' );
var iterMatrices = ns.nditerMatrices( dataMatrix );
var matrix;
var rowIndex;
var j;
var matrixData;
var currentRow;

while ( true ) {
matrix = iterMatrices.next();
if ( matrix.done ) {
break;
}
matrixData = [];
for ( rowIndex = 0; rowIndex < matrix.value.shape[ 0 ]; rowIndex++ ) {
currentRow = [];
for ( j = 0; j < matrix.value.shape[ 1 ]; j++ ) {
currentRow.push( matrix.value.get( rowIndex, j ) );
}
matrixData.push( currentRow );
}
console.log( matrixData );
}
```

```javascript
// Example: Converting each iterated ndarray to a generic array
var array = require( '@stdlib/ndarray/array' );
var iter = require( '@stdlib/ndarray/iter' );

var array2D = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];
var data = [].concat.apply( [], array2D );

var ndMatrix;
ndMatrix = array( data, {
'shape': [ 3, 3 ],
'dtype': 'generic'
} );
var rowIterator;
rowIterator = iter.nditerRows( ndMatrix );
var it = iter.nditer2arrayEach( rowIterator );
var v;

console.log( 'Original ndarray:', ndMatrix.toString() );

while ( true ) {
v = it.next();
if ( v.done ) {
break;
}
console.log( v.value );
}
```

</section>

<!-- /.examples -->
Expand Down
109 changes: 108 additions & 1 deletion lib/node_modules/@stdlib/ndarray/iter/examples/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
* Copyright (c) 2024 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.
Expand All @@ -19,6 +19,113 @@
'use strict';

var objectKeys = require( '@stdlib/utils/keys' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var array = require( '@stdlib/ndarray/array' );
var iter = require( '@stdlib/ndarray/iter' );
var ns = require( './../lib' );

// Example: Get an array consisting of keys/properties inside ns.
console.log( objectKeys( ns ) );

// Example: Iterate over [index, column] pairs for each column in a matrix
var array2D = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];
var data = [].concat.apply( [], array2D );
var x = ndarray( 'generic', data, [ 3, 3 ], [ 3, 1 ], 0, 'row-major' );
var iterEntries = ns.nditerEntries( x );
var entry;

for ( entry of iterEntries ) {
console.log( 'Index:', entry[ 0 ], 'Value:', entry[ 1 ] );
}

// Example: Iterate over each row of the ndarray
var iterRows = ns.nditerRows( x );
var row;
var i;
var rowData;

while ( true ) {
row = iterRows.next();
if ( row.done ) {
break;
}
rowData = [];
for ( i = 0; i < row.value.shape[ 0 ]; i++ ) {
rowData.push( row.value.get( i ) );
}
console.log( rowData );
}

// Example: Iterate over each column of the ndarray
var iterColumns = ns.nditerColumns( x );
var column;
var k;
var columnData;

while ( true ) {
column = iterColumns.next();
if ( column.done ) {
break;
}
columnData = [];
for ( k = 0; k < column.value.shape[ 0 ]; k++ ) {
columnData.push( column.value.get( k ) );
}
console.log( columnData );
}

// Example: Iterate over each matrix in the stack
var matricesData = [
1,
2,
3,
4,
5,
6,
7,
8
];
var dataMatrix = ndarray( 'generic', matricesData, [ 2, 2, 2 ], [ 4, 2, 1 ], 0, 'row-major' );
var iterMatrices = ns.nditerMatrices( dataMatrix );
var matrix;
var rowIndex;
var j;
var matrixData;
var currentRow;

while ( true ) {
matrix = iterMatrices.next();
if ( matrix.done ) {
break;
}
matrixData = [];
for ( rowIndex = 0; rowIndex < matrix.value.shape[ 0 ]; rowIndex++ ) {
currentRow = [];
for ( j = 0; j < matrix.value.shape[ 1 ]; j++ ) {
currentRow.push( matrix.value.get( rowIndex, j ) );
}
matrixData.push( currentRow );
}
console.log( matrixData );
}

// Example: Converting each iterated ndarray to a generic array
var ndMatrix;
ndMatrix = array( data, {
'shape': [ 3, 3 ],
'dtype': 'generic'
} );
var rowIterator;
rowIterator = iter.nditerRows( ndMatrix );
var it = iter.nditer2arrayEach( rowIterator );
var v;

console.log( 'Original ndarray:', ndMatrix.toString() );

while ( true ) {
v = it.next();
if ( v.done ) {
break;
}
console.log( v.value );
}

0 comments on commit b36dc4a

Please sign in to comment.