-
-
Notifications
You must be signed in to change notification settings - Fork 844
docs: improve examples of ndarray/iter
namespace
#1686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 2 commits
b36dc4a
aa1b0e4
440c045
9e73fd3
e5311a0
0fa2bb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2018 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. | ||
|
@@ -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 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ES5. |
||
console.log( 'Index:', entry[ 0 ], 'Value:', entry[ 1 ] ); | ||
} | ||
``` | ||
|
||
```javascript | ||
// Example: Iterate over each row of the ndarray | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These examples seem to just be pulling from the READMEs of the child packages. I think we would prefer something fresh, which ideally combines the functionality of multiple packages to demonstrate how they can all work together. |
||
|
||
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 --> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer modifying the existing example code. Project conventions are to not create multiple separate Markdown code blocks. Prefer instead a single Markdown code block with various example usage.