From b36dc4a2759e851a9e65eaee6383f0546b619bc6 Mon Sep 17 00:00:00 2001 From: Rutam Kathale Date: Mon, 4 Mar 2024 14:03:15 +0530 Subject: [PATCH 1/3] docs: improve README examples of ndarray/iter namespace Updated examples to span over the functionalities implemented in the namespace. Addresses: #1589 --- .../@stdlib/ndarray/iter/README.md | 139 ++++++++++++++++++ .../@stdlib/ndarray/iter/examples/index.js | 109 +++++++++++++- 2 files changed, 247 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/iter/README.md b/lib/node_modules/@stdlib/ndarray/iter/README.md index ee6a6de9d848..ecfd267dd4a6 100644 --- a/lib/node_modules/@stdlib/ndarray/iter/README.md +++ b/lib/node_modules/@stdlib/ndarray/iter/README.md @@ -71,12 +71,151 @@ var o = ns; ```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 ); +} +``` + diff --git a/lib/node_modules/@stdlib/ndarray/iter/examples/index.js b/lib/node_modules/@stdlib/ndarray/iter/examples/index.js index c876063831b1..6188465aa426 100644 --- a/lib/node_modules/@stdlib/ndarray/iter/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/iter/examples/index.js @@ -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. @@ -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 ); +} From aa1b0e47f42da544c3c9593ca3e65d71798dccae Mon Sep 17 00:00:00 2001 From: Rutam <138517416+performant23@users.noreply.github.com> Date: Mon, 4 Mar 2024 14:16:22 +0530 Subject: [PATCH 2/3] Update README.md Update the copyright year Signed-off-by: Rutam <138517416+performant23@users.noreply.github.com> --- lib/node_modules/@stdlib/ndarray/iter/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/iter/README.md b/lib/node_modules/@stdlib/ndarray/iter/README.md index ecfd267dd4a6..608f5e3f3a4a 100644 --- a/lib/node_modules/@stdlib/ndarray/iter/README.md +++ b/lib/node_modules/@stdlib/ndarray/iter/README.md @@ -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. From 440c045b99079025a03859549b0ecff0f0d7695c Mon Sep 17 00:00:00 2001 From: Rutam Kathale Date: Mon, 4 Mar 2024 23:10:24 +0530 Subject: [PATCH 3/3] docs: apply suggestions from code review Updated example for the namespace using functionalities from stats, random, and ndarray Fixes: #1589 --- .../@stdlib/ndarray/iter/README.md | 154 ++++-------------- .../@stdlib/ndarray/iter/examples/index.js | 122 ++++---------- 2 files changed, 56 insertions(+), 220 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/iter/README.md b/lib/node_modules/@stdlib/ndarray/iter/README.md index ecfd267dd4a6..405c4a1ef404 100644 --- a/lib/node_modules/@stdlib/ndarray/iter/README.md +++ b/lib/node_modules/@stdlib/ndarray/iter/README.md @@ -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,148 +71,50 @@ var o = ns; ```javascript -// Example: Get an array consisting of keys/properties inside ns. -var objectKeys = require( '@stdlib/utils/keys' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); var ns = require( '@stdlib/ndarray/iter' ); +var mean = require( '@stdlib/stats/base/mean' ); +var variance = require( '@stdlib/stats/base/variance' ); +var randu = require( '@stdlib/random/base/randu' ); -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 rowVariance; +var iterRows; +var rowMean; var rowData; +var data; var row; +var x; +var j; 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 ); +data = new Array( 25 ); +for ( i = 0; i < data.length; i++ ) { + data[ i ] = parseFloat( ( randu() * 100 ).toFixed( 2 ) ); } -``` - -```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; +console.log( data ); -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 ); -} -``` +// Logs an array of 25 elements generated randomly as above. -```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; +x = ndarray( 'generic', data, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' ); +iterRows = ns.nditerRows( x ); while ( true ) { - matrix = iterMatrices.next(); - if ( matrix.done ) { + row = iterRows.next(); + if ( row.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 ); + rowData = []; + for ( j = 0; j < row.value.shape[ 0 ]; j++ ) { + rowData.push( row.value.get( j ) ); } - 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' ); + rowMean = parseFloat( ( mean( rowData.length, rowData, 1 ) ).toFixed( 2 ) ); + rowVariance = parseFloat( variance( rowData.length, 1, rowData, 1 ) + .toFixed( 2 ) ); -var array2D = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]; -var data = [].concat.apply( [], array2D ); + console.log( rowMean, rowVariance ); -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 ); + // Logs mean and variance of each row in the 5x5 array. } ``` diff --git a/lib/node_modules/@stdlib/ndarray/iter/examples/index.js b/lib/node_modules/@stdlib/ndarray/iter/examples/index.js index 6188465aa426..1ad48acd7a1a 100644 --- a/lib/node_modules/@stdlib/ndarray/iter/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/iter/examples/index.js @@ -18,114 +18,48 @@ '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 ndarray = require( '@stdlib/ndarray/base/ctor' ); +var mean = require( '@stdlib/stats/base/mean' ); +var variance = require( '@stdlib/stats/base/variance' ); +var randu = require( '@stdlib/random/base/randu' ); 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 rowVariance; +var iterRows; +var rowMean; +var rowData; +var data; var row; +var x; +var j; 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 ); +data = new Array( 25 ); +for ( i = 0; i < data.length; i++ ) { + data[ i ] = parseFloat( ( randu() * 100 ).toFixed( 2 ) ); } -// Example: Iterate over each column of the ndarray -var iterColumns = ns.nditerColumns( x ); -var column; -var k; -var columnData; +console.log( data ); -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 ); -} +// Logs an array of 25 elements generated randomly as above. -// 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; +x = ndarray( 'generic', data, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' ); +iterRows = ns.nditerRows( x ); while ( true ) { - matrix = iterMatrices.next(); - if ( matrix.done ) { + row = iterRows.next(); + if ( row.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 ); + rowData = []; + for ( j = 0; j < row.value.shape[ 0 ]; j++ ) { + rowData.push( row.value.get( j ) ); } - 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; + rowMean = parseFloat( ( mean( rowData.length, rowData, 1 ) ).toFixed( 2 ) ); + rowVariance = parseFloat( variance( rowData.length, 1, rowData, 1 ) + .toFixed( 2 ) ); -console.log( 'Original ndarray:', ndMatrix.toString() ); + console.log( rowMean, rowVariance ); -while ( true ) { - v = it.next(); - if ( v.done ) { - break; - } - console.log( v.value ); + // Logs mean and variance of each row in the 5x5 array. }