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

Multidimensional arrays.

License

NotificationsYou must be signed in to change notification settings

stdlib-js/ndarray-array

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!

Multidimensional Arrays

NPM versionBuild StatusCoverage Status

Create a multidimensional array.

Installation

npm install @stdlib/ndarray-array

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

vararray=require('@stdlib/ndarray-array');

array( [buffer,] [options] )

Returns a multidimensional array.

// Create a 2x2 matrix:vararr=array([[1.0,2.0],[3.0,4.0]]);// returns <ndarray>

To initialize multidimensional array data, provide abuffer argument, which may be ageneric array,typed array,Buffer, orndarray.

varFloat64Array=require('@stdlib/array-float64');varallocUnsafe=require('@stdlib/buffer-alloc-unsafe');// Create an ndarray from a generic array linear data buffer:vararr=array([1.0,2.0,3.0,4.0],{'shape':[2,2]});// returns <ndarray>// Create an ndarray from a typed array linear data buffer:arr=array(newFloat64Array([1.0,2.0,3.0,4.0]),{'shape':[2,2]});// returns <ndarray>// Create an ndarray as a view over a Buffer:arr=array(allocUnsafe(4),{'shape':[2,2]});// returns <ndarray>// Create an ndarray from another ndarray:arr=array(array([[1.0,2.0],[3.0,4.0]]));// returns <ndarray>

The function accepts the followingoptions:

  • buffer: data source. If provided along with abuffer argument, the argument takes precedence.

  • dtype: underlying storagedata type. If not specified and a data source is provided, the data type is inferred from the provided data source. If an input data source is not of the same type, this option specifies the data type to which to cast the input data. For non-ndarray generic array data sources, the function casts generic array data elements to the default data type. In order to prevent this cast, thedtype optionmust be explicitly set to'generic'. Any time a cast is required, thecopy option is set totrue, as memory must be copied from the data source to an output data buffer. Default:'float64'.

  • order: specifies the memory layout of the data source as either row-major (C-style) or column-major (Fortran-style). The option may be one of the following values:

    • row-major: the order of the returned array is row-major.
    • column-major: the order of the returned array is column-major.
    • any: if a data source is column-major and not row-major, the order of the returned array is column-major; otherwise, the order of the returned array is row-major.
    • same: the order of the returned array matches the order of an input data source.

    Note that specifying an order which differs from the order of a provided data source doesnot entail a conversion from one memory layout to another. In short, this option is descriptive, not prescriptive. Default:'row-major'.

  • shape: array shape (dimensions). If a shape is not specified, the function attempts to infer a shape based on a provided data source. For example, if provided a nested array, the function resolves nested array dimensions. If provided a multidimensional array data source, the function uses the array's associated shape. For most use cases, such inference suffices. For the remaining use cases, specifying a shape is necessary. For example, provide a shape to create a multidimensional array view over a linear data buffer, ignoring any existing shape meta data associated with a provided data source.

  • flatten:boolean indicating whether to automatically flatten generic array data sources. If an array shape is not specified, the shape is inferred from the dimensions of nested arrays prior to flattening. If a use case requires partial flattening, partially flattenprior to invoking this function and set the option value tofalse to prevent further flattening during invocation. Default:true.

  • copy:boolean indicating whether to (shallow) copy source data to a new data buffer. The function doesnot perform a deep copy. To prevent undesired shared changes in state for generic arrays containing objects, perform a deep copyprior to invoking this function. Default:false.

  • ndmin: specifies the minimum number of dimensions. If an array shape has fewer dimensions than required byndmin, the functionprepends singleton dimensions to the array shape in order to satisfy the dimensions requirement. Default:0.

  • casting: specifies the casting rule used to determine acceptable casts. The option may be one of the following values:

    • none: only allow casting between identical types.
    • equiv: allow casting between identical and byte swapped types.
    • safe: only allow "safe" casts.
    • mostly-safe: allow "safe" casts and, for floating-point data types, downcasts.
    • same-kind: allow "safe" casts and casts within the same kind (e.g., between signed integers or between floats).
    • unsafe: allow casting between all types (including between integers and floats).

    Default:'safe'.

  • mode: specifies how to handle indices which exceed array dimensions.

    • throw: specifies that anndarray instance should throw an error when an index exceeds array dimensions.
    • normalize: specifies that anndarray instance should normalize negative indices and throw an error when an index exceeds array dimensions.
    • wrap: specifies that anndarray instance should wrap around an index exceeding array dimensions using modulo arithmetic.
    • clamp: specifies that anndarray instance should set an index exceeding array dimensions to either0 (minimum index) or the maximum index.

    Default:'throw'.

  • submode: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions. If provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. Default:[ options.mode ].

  • readonly:boolean indicating whether anndarray instance should beread-only. Default:false.

By default, anndarray instancethrows when provided an index which exceeds array dimensions. To support alternative indexing behavior, set themode option, which will affect all public methods for getting and setting array elements.

varopts={'mode':'clamp'};vararr=array([[1.0,2.0],[3.0,4.0]],opts);// returns <ndarray>// Attempt to access an out-of-bounds linear index (clamped):varv=arr.iget(10);// returns 4.0

By default, themode option is applied to subscripts which exceed array dimensions. To specify behavior for each dimension, set thesubmode option.

varopts={'submode':['wrap','clamp']};vararr=array([[[1.0,2.0],[3.0,4.0]],[[5.0,6.0],[7.0,8.0]]],opts);// returns <ndarray>// Attempt to access out-of-bounds subscripts:varv=arr.get(-2,10,-1);// linear index: 3// returns 4.0

By default, the function automatically flattensgeneric array data sources. To prevent flattening, set theflatten option tofalse.

varopts={'flatten':false,'dtype':'generic'};// Create a generic array which will serve as our ndarray data source:varbuf=[[1.0,2.0],[3.0,4.0]];// Create a 2-element vector:vararr=array(buf,opts);// returns <ndarray>// Retrieve the first vector element:varv=arr.get(0);// returns [ 1.0, 2.0 ]varbool=(v===buf[0]);// returns true

Notes

  • The number of elements in a data sourcebuffermust agree with a specified arrayshape (i.e., the function assumes a single-segment contiguousndarray). To create arbitrary multidimensional views over linear data buffers, use alower-level constructor.
  • The function supports arbitrary casting between data types. Note, however, that casting from a larger data type to a smaller data type (e.g.,int32 toint8) and between signed and unsigned types of the same size should be consideredunsafe.

Examples

vararray=require('@stdlib/ndarray-array');// Create a 4-dimensional array containing single-precision floating-point numbers:vararr=array({'dtype':'float32','shape':[3,3,3,3]});// Retrieve an array value:varv=arr.get(1,2,1,2);// returns 0.0// Set an array value:arr.set(1,2,1,2,10.0);// Retrieve the array value:v=arr.get(1,2,1,2);// returns 10.0// Serialize the array as a string:varstr=arr.toString();// returns "ndarray( 'float32', new Float32Array( [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ), [ 3, 3, 3, 3 ], [ 27, 9, 3, 1 ], 0, 'row-major' )"// Serialize the array as JSON:str=JSON.stringify(arr.toJSON());// e.g., returns '{"type":"ndarray","dtype":"float32","flags":{},"order":"row-major","shape":[3,3,3,3],"strides":[27,9,3,1],"data":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}'

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.

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp