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

Apply a callback to elements in an input ndarray and assign results to elements in a new output ndarray.

License

NotificationsYou must be signed in to change notification settings

stdlib-js/ndarray-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!

map

NPM versionBuild StatusCoverage Status

Apply a callback function to elements in an inputndarray and assign results to elements in a new outputndarray.

Installation

npm install @stdlib/ndarray-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

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

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

Applies a callback function to elements in an inputndarray and assigns results to elements in a new outputndarray.

varFloat64Array=require('@stdlib/array-float64');varndarray=require('@stdlib/ndarray-ctor');varndarray2array=require('@stdlib/ndarray-to-array');functionscale(z){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=map(x,scale);// returns <ndarray>vararr=ndarray2array(y);// returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ]

The function accepts the following arguments:

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

The function accepts the following options:

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');functionscale(z){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=map(x,opts,scale);// returns <ndarray>vardt=dtype(y);// returns 'float32'vararr=ndarray2array(y);// returns [ [ 20.0, 30.0, 40.0 ], [ 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');functionscale(z){this.count+=1;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=map(x,scale,ctx);// returns <ndarray>vararr=ndarray2array(y);// returns [ [ 20.0, 30.0, 40.0 ], [ 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');functiontoComplex(z){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=map(x,opts,toComplex);// returns <ndarray>
  • The functionalways returns anndarray having the same shape andorder as the inputndarray.

  • For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before applying a callback function in order to achieve better performance.

Examples

vardiscreteUniform=require('@stdlib/random-array-discrete-uniform');varabs=require('@stdlib/math-base-special-abs');varndarray2array=require('@stdlib/ndarray-to-array');varnaryFunction=require('@stdlib/utils-nary-function');varndarray=require('@stdlib/ndarray-ctor');varmap=require('@stdlib/ndarray-map');varbuffer=discreteUniform(10,-100,100,{'dtype':'generic'});varshape=[5,2];varstrides=[2,1];varoffset=0;varx=ndarray('generic',buffer,shape,strides,offset,'row-major');console.log(ndarray2array(x));vary=map(x,naryFunction(abs,1));console.log(ndarray2array(y));

See Also


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

Apply a callback to elements in an input ndarray and assign results to elements in a new output ndarray.

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