Skip to content

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

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions lib/node_modules/@stdlib/ndarray/iter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -75,10 +75,51 @@ var o = ns;
<!-- eslint no-undef: "error" -->

```javascript
var objectKeys = require( '@stdlib/utils/keys' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var ns = require( '@stdlib/ndarray/iter' );

console.log( objectKeys( ns ) );
var mean = require( '@stdlib/stats/base/mean' );
var variance = require( '@stdlib/stats/base/variance' );
var randu = require( '@stdlib/random/base/randu' );

var rowVariance;
var iterRows;
var rowMean;
var rowData;
var data;
var row;
var x;
var j;
var i;

data = new Array( 25 );
for ( i = 0; i < data.length; i++ ) {
data[ i ] = parseFloat( ( randu() * 100 ).toFixed( 2 ) );
}

console.log( data );

// Logs an array of 25 elements generated randomly as above.

x = ndarray( 'generic', data, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' );
iterRows = ns.nditerRows( x );

while ( true ) {
row = iterRows.next();
if ( row.done ) {
break;
}
rowData = [];
for ( j = 0; j < row.value.shape[ 0 ]; j++ ) {
rowData.push( row.value.get( j ) );
}
rowMean = parseFloat( ( mean( rowData.length, rowData, 1 ) ).toFixed( 2 ) );
rowVariance = parseFloat( variance( rowData.length, 1, rowData, 1 )
.toFixed( 2 ) );

console.log( rowMean, rowVariance );

// Logs mean and variance of each row in the 5x5 array.
}
```

</section>
Expand Down
47 changes: 44 additions & 3 deletions 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 @@ -18,7 +18,48 @@

'use strict';

var objectKeys = require( '@stdlib/utils/keys' );
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' );

console.log( objectKeys( ns ) );
var rowVariance;
var iterRows;
var rowMean;
var rowData;
var data;
var row;
var x;
var j;
var i;

data = new Array( 25 );
for ( i = 0; i < data.length; i++ ) {
data[ i ] = parseFloat( ( randu() * 100 ).toFixed( 2 ) );
}

console.log( data );

// Logs an array of 25 elements generated randomly as above.

x = ndarray( 'generic', data, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' );
iterRows = ns.nditerRows( x );

while ( true ) {
row = iterRows.next();
if ( row.done ) {
break;
}
rowData = [];
for ( j = 0; j < row.value.shape[ 0 ]; j++ ) {
rowData.push( row.value.get( j ) );
}
rowMean = parseFloat( ( mean( rowData.length, rowData, 1 ) ).toFixed( 2 ) );
rowVariance = parseFloat( variance( rowData.length, 1, rowData, 1 )
.toFixed( 2 ) );

console.log( rowMean, rowVariance );

// Logs mean and variance of each row in the 5x5 array.
}