Skip to content

Create a filled two-dimensional symmetric nested array according to a provided callback function.

License

Notifications You must be signed in to change notification settings

stdlib-js/array-base-symmetric-filled2d-by

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About stdlib...

We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.

The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.

When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.

To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!

filled2dBy

NPM version Build Status Coverage Status

Create a filled two-dimensional symmetric nested array according to a provided callback function.

Usage

import filled2dBy from 'https://cdn.jsdelivr.net/gh/stdlib-js/array-base-symmetric-filled2d-by@esm/index.mjs';

filled2dBy( N, clbk[, thisArg] )

Returns a filled two-dimensional symmetric nested array according to a provided callback function.

function clbk( idx ) {
    return idx[ 0 ] + idx[ 1 ];
}

var out = filled2dBy( 2, clbk );
// returns [ [ 0, 1 ], [ 1, 2 ] ]

The function accepts the following arguments:

  • N: number of rows and columns.
  • clbk: callback function.
  • thisArg: callback function execution context (optional).

When invoked, a callback function is provided a single argument:

  • indices: current array element indices.

To set the callback execution context, provide a thisArg.

function clbk() {
    this.count += 1;
    return 1;
}

var ctx = {
    'count': 0
};

var out = filled2dBy( 2, clbk, ctx );
// returns [ [ 1, 1 ], [ 1, 1 ] ];

var cnt = ctx.count;
// returns 3

Notes

  • As the output array is symmetric, the callback function is only invoked for the elements residing in the upper triangle of the output array (i.e., indices[1] >= indices[0]).

Examples

<!DOCTYPE html>
<html lang="en">
<body>
<script type="module">

import constantFunction from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-constant-function@esm/index.mjs';
import filled2dBy from 'https://cdn.jsdelivr.net/gh/stdlib-js/array-base-symmetric-filled2d-by@esm/index.mjs';

var out = filled2dBy( 2, constantFunction( 0.0 ) );
// returns [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]

out = filled2dBy( 2, constantFunction( 'beep' ) );
// returns [ [ 'beep', 'beep' ], [ 'beep', 'beep' ] ]

out = filled2dBy( 2, constantFunction( null ) );
// returns [ [ null, null ], [ null, null ] ]

out = filled2dBy( 2, constantFunction( true ) );
// returns [ [ true, true ], [ true, true ] ]

function clbk( indices ) {
    return indices[ 0 ] + indices[ 1 ];
}
out = filled2dBy( 3, clbk );
// returns [ [ 0, 1, 2 ], [ 1, 2, 3 ], [ 2, 3, 4 ] ]

</script>
</body>
</html>

Notice

This package is part of stdlib, a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2025. The Stdlib Authors.

Releases

No releases published

Packages

No packages published