Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Filter and map elements in an input ndarray to elements in a new output ndarray according to a callback function.

License

NotificationsYou must be signed in to change notification settings

stdlib-js/ndarray-filter-map

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 onGitHub, and please considerfinancially supporting stdlib. We greatly appreciate your continued support!

filterMap

NPM versionBuild StatusCoverage Status

Filter and map elements in an inputndarray to elements in a new outputndarray according to a callback function.

Installation

npm install @stdlib/ndarray-filter-map

Alternatively,

  • To load the package in a website via ascript tag without installation and bundlers, use theES Module available on theesm branch (seeREADME).
  • If you are using Deno, visit thedeno branch (seeREADME for usage intructions).
  • For use in Observable, or in browser/node environments, use theUniversal Module Definition (UMD) build available on theumd branch (seeREADME).

Thebranches.md file summarizes the available branches and displays a diagram illustrating their relationships.

To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.

Usage

varfilterMap=require('@stdlib/ndarray-filter-map');

filterMap( x[, options], fcn[, thisArg] )

Filters and maps elements in an inputndarray to elements in a new outputndarray according to a callback function.

varFloat64Array=require('@stdlib/array-float64');varndarray=require('@stdlib/ndarray-ctor');varndarray2array=require('@stdlib/ndarray-to-array');functionfcn(z){if(z>5.0){returnz*10.0;}}varbuffer=newFloat64Array([1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0]);varshape=[2,3];varstrides=[6,1];varoffset=1;varx=ndarray('float64',buffer,shape,strides,offset,'row-major');// returns <ndarray>vary=filterMap(x,fcn);// returns <ndarray>vararr=ndarray2array(y);// returns [ 80.0, 90.0, 100.0 ]

The function accepts the following arguments:

  • x: inputndarray.
  • options: function options(optional).
  • fcn: callback function.
  • thisArg: callback function execution context(optional).

The function accepts the following options:

  • dtype: output ndarraydata type. If not specified, the output ndarraydata type is inferred from the inputndarray.
  • order: index iteration order. By default, the function iterates over elements according to thelayout order of the providedndarray. Accordingly, for row-major inputndarrays, the last dimension indices increment fastest. For column-major inputndarrays, the first dimension indices increment fastest. To override the inferred order and ensure that indices increment in a specific manner, regardless of the inputndarray's layout order, explicitly set the iteration order. Note, however, that iterating according to an order which does not match that of the inputndarray may, in some circumstances, result in performance degradation due to cache misses. Must be either'row-major' or'column-major'.

By default, the output ndarraydata type is inferred from the inputndarray. To return an ndarray with a differentdata type, specify thedtype option.

varFloat64Array=require('@stdlib/array-float64');varndarray=require('@stdlib/ndarray-ctor');vardtype=require('@stdlib/ndarray-dtype');varndarray2array=require('@stdlib/ndarray-to-array');functionfcn(z){if(z>5.0){returnz*10.0;}}varbuffer=newFloat64Array([1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0]);varshape=[2,3];varstrides=[6,1];varoffset=1;varx=ndarray('float64',buffer,shape,strides,offset,'row-major');// returns <ndarray>varopts={'dtype':'float32'};vary=filterMap(x,opts,fcn);// returns <ndarray>vardt=dtype(y);// returns 'float32'vararr=ndarray2array(y);// returns [ 80.0, 90.0, 100.0 ]

To set the callback function execution context, provide athisArg.

varFloat64Array=require('@stdlib/array-float64');varndarray=require('@stdlib/ndarray-ctor');varndarray2array=require('@stdlib/ndarray-to-array');functionfcn(z){this.count+=1;if(z>5.0){returnz*10.0;}}varbuffer=newFloat64Array([1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0]);varshape=[2,3];varstrides=[6,1];varoffset=1;varx=ndarray('float64',buffer,shape,strides,offset,'row-major');// returns <ndarray>varctx={'count':0};vary=filterMap(x,fcn,ctx);// returns <ndarray>vararr=ndarray2array(y);// returns [ 80.0, 90.0, 100.0 ]varcount=ctx.count;// returns 6

The callback function is provided the following arguments:

  • value: current array element.
  • indices: current array element indices.
  • arr: the inputndarray.

Notes

  • The function doesnot perform explicit casting (e.g., from a real-valued floating-point number to a complex floating-point number). Any such casting should be performed by a provided callback function.

    varFloat64Array=require('@stdlib/array-float64');varndarray=require('@stdlib/ndarray-ctor');varComplex128=require('@stdlib/complex-float64-ctor');varndarray2array=require('@stdlib/ndarray-to-array');functionfcn(z){if(z>5.0){returnnewComplex128(z,0.0);}}varbuffer=newFloat64Array([1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0]);varshape=[2,3];varstrides=[6,1];varoffset=1;varx=ndarray('float64',buffer,shape,strides,offset,'row-major');// returns <ndarray>varopts={'dtype':'complex128'};vary=filterMap(x,opts,fcn);// returns <ndarray>
  • If a provided callback function returnsundefined, the function skips the respectivendarray element. If the callback function returns a value other thanundefined, the function stores the callback's return value in the outputndarray.

  • The functionalways returns a one-dimensionalndarray.

Examples

vardiscreteUniform=require('@stdlib/random-array-discrete-uniform');varndarray2array=require('@stdlib/ndarray-to-array');vararray=require('@stdlib/ndarray-array');varfilterMap=require('@stdlib/ndarray-filter-map');functionfcn(v){if(v>0){returnv*100;}}varbuffer=discreteUniform(10,-100,100,{'dtype':'generic'});varx=array(buffer,{'shape':[5,2],'dtype':'generic'});console.log(ndarray2array(x));vary=filterMap(x,fcn);console.log(ndarray2array(y));

See Also

  • @stdlib/ndarray-filter:return a shallow copy of an ndarray containing only those elements which pass a test implemented by a predicate function.
  • @stdlib/ndarray-map:apply a callback to elements in an input ndarray and assign results to elements in a new output ndarray.
  • @stdlib/ndarray-reject:return a shallow copy of an ndarray containing only those elements which fail a test implemented by a predicate function.
  • @stdlib/ndarray-slice:return a read-only view of an input ndarray.

Notice

This package is part ofstdlib, a standard library for JavaScript and Node.js, 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 developstdlib, see the main projectrepository.

Community

Chat


License

SeeLICENSE.

Copyright

Copyright © 2016-2025. The StdlibAuthors.

About

Filter and map elements in an input ndarray to elements in a new output ndarray according to a callback function.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp