Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
Fancy multidimensional array constructor.
License
stdlib-js/ndarray-fancy
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
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 onGitHub, and please considerfinancially supporting stdlib. We greatly appreciate your continued support!
Fancy multidimensional array constructor.
AFancyArray is anndarray which supports slicing via indexing expressions.
importndarray2arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-to-array@deno/mod.js';importFancyArrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-fancy@deno/mod.js';varbuffer=[1,2,3,4,5,6];varx=newFancyArray('generic',buffer,[6],[1],0,'row-major');// returns <FancyArray>// Select the first 3 elements:vary=x[':3'];// returns <FancyArray>vararr=ndarray2array(y);// returns [ 1, 2, 3 ]// Select every other element, starting with the second element:y=x['1::2'];// returns <FancyArray>arr=ndarray2array(y);// returns [ 2, 4, 6 ]// Reverse the array, starting with last element and skipping every other element:y=x['::-2'];// returns <FancyArray>arr=ndarray2array(y);// returns [ 6, 4, 2 ]
importFancyArrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-fancy@deno/mod.js';
Returns aFancyArray instance.
// Specify the array configuration:varbuffer=[1.0,2.0,3.0,4.0];varshape=[2,2];varorder='row-major';varstrides=[2,1];varoffset=0;// Create a new array:vararr=newFancyArray('generic',buffer,shape,strides,offset,order);// returns <FancyArray>
The constructor expects the following arguments:
- dtype: underlyingdata type.
- buffer: data buffer.
- shape: array shape (dimensions).
- strides: array strides which are index offsets specifying how to access along corresponding dimensions.
- offset: index offset specifying the location of the first indexed element in the data buffer.
- order: array order, which is either
row-major(C-style) orcolumn-major(Fortran-style).
The constructor accepts the followingoptions:
- mode: specifies how to handle indices which exceed array dimensions. 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 constructor recycles modes using modulo arithmetic. Default:
[ options.mode ]. - readonly: boolean indicating whether an array should beread-only. Default:
false.
The constructor supports the followingmodes:
- throw: specifies that a
FancyArrayinstance should throw an error when an index exceeds array dimensions. - normalize: specifies that a
FancyArrayinstance should normalize negative indices and throw an error when an index exceeds array dimensions. - wrap: specifies that a
FancyArrayinstance should wrap around an index exceeding array dimensions using modulo arithmetic. - clamp: specifies that a
FancyArrayinstance should set an index exceeding array dimensions to either0(minimum index) or the maximum index.
By default, aFancyArray instancethrows when provided an index which exceeds array dimensions. To support alternative indexing behavior, set themode option, which will affect all publicmethods (butnot slicing semantics) for getting and setting array elements.
varopts={'mode':'clamp'};// Specify the array configuration:varbuffer=[1.0,2.0,3.0,4.0];varshape=[2,2];varorder='row-major';varstrides=[2,1];varoffset=0;// Create a new array:vararr=newFancyArray('generic',buffer,shape,strides,offset,order,opts);// returns <FancyArray>// 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']};// Specify the array configuration:varbuffer=[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0];varshape=[2,2,2];varorder='row-major';varstrides=[4,2,1];varoffset=0;// Create a new array:vararr=newFancyArray('generic',buffer,shape,strides,offset,order,opts);// returns <FancyArray>// Attempt to access out-of-bounds subscripts:varv=arr.get(-2,10,-1);// linear index: 3// returns 4.0
String value of the constructor name.
varstr=FancyArray.name;// returns 'ndarray'
Size (in bytes) of the array (if known).
importFloat64Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-float64@deno/mod.js';// Specify the array configuration:varbuffer=newFloat64Array([1.0,2.0,3.0,4.0]);varshape=[2,2];varorder='row-major';varstrides=[2,1];varoffset=0;// Create a new array:vararr=newFancyArray('float64',buffer,shape,strides,offset,order);// Get the byte length:varnbytes=arr.byteLength;// returns 32
If unable to determine the size of the array, the property value isnull.
// Specify the array configuration:varbuffer=[1.0,2.0,3.0,4.0];varshape=[2,2];varorder='row-major';varstrides=[2,1];varoffset=0;// Create a new array:vararr=newFancyArray('generic',buffer,shape,strides,offset,order);// Get the byte length:varnbytes=arr.byteLength;// returns null
Size (in bytes) of each array element (if known).
importFloat32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-float32@deno/mod.js';// Specify the array configuration:varbuffer=newFloat32Array([1.0,2.0,3.0,4.0]);varshape=[2,2];varorder='row-major';varstrides=[2,1];varoffset=0;// Create a new array:vararr=newFancyArray('float32',buffer,shape,strides,offset,order);// Get the number of bytes per element:varnbytes=arr.BYTES_PER_ELEMENT;// returns 4
If size of each array element is unknown, the property value isnull.
// Specify the array configuration:varbuffer=[1.0,2.0,3.0,4.0];varshape=[2,2];varorder='row-major';varstrides=[2,1];varoffset=0;// Create a new array:vararr=newFancyArray('generic',buffer,shape,strides,offset,order);// Get the number of bytes per element:varnbytes=arr.BYTES_PER_ELEMENT;// returns null
A reference to the underlying data buffer.
importInt8Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int8@deno/mod.js';// Specify the array configuration:varbuffer=newInt8Array([1,2,3,4]);varshape=[2,2];varorder='row-major';varstrides=[2,1];varoffset=0;// Create a new array:vararr=newFancyArray('int8',buffer,shape,strides,offset,order);// Get the buffer reference:vard=arr.data;// returns <Int8Array>[ 1, 2, 3, 4 ]varbool=(d===buffer);// returns true
Underlyingdata type.
importUint8Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-uint8@deno/mod.js';// Specify the array configuration:varbuffer=newUint8Array([1,2,3,4]);varshape=[2,2];varorder='row-major';varstrides=[-2,1];varoffset=2;// Create a new array:vararr=newFancyArray('uint8',buffer,shape,strides,offset,order);// Get the underlying data type:vardtype=arr.dtype;// returns 'uint8'
Meta information, such as information regarding the memory layout of the array. The returned object has the following properties:
- ROW_MAJOR_CONTIGUOUS:
booleanindicating if an array is row-major contiguous. - COLUMN_MAJOR_CONTIGUOUS:
booleanindicating if an array is column-major contiguous. - READONLY:
booleanindicating whether an array isread-only.
An array is contiguous if (1) an array is compatible with being stored in a single memory segment and (2) each array element is adjacent to the next array element. Note that an array can be both row-major contiguous and column-major contiguous at the same time (e.g., if an array is a 1-dimensional array withstrides = [1]).
importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@deno/mod.js';// Specify the array configuration:varbuffer=newInt32Array([1,2,3,4]);varshape=[2,2];varorder='column-major';varstrides=[1,2];varoffset=0;// Create a new array:vararr=newFancyArray('int32',buffer,shape,strides,offset,order);// Get the array flags:varflg=arr.flags;// returns {...}
Number of array elements.
importUint16Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-uint16@deno/mod.js';// Specify the array configuration:varbuffer=newUint16Array([1,2,3,4]);varshape=[2,2];varorder='column-major';varstrides=[-1,-2];varoffset=3;// Create a new array:vararr=newFancyArray('uint16',buffer,shape,strides,offset,order);// Get the array length:varlen=arr.length;// returns 4
Number of dimensions.
importUint8ClampedArrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-uint8c@deno/mod.js';// Specify the array configuration:varbuffer=newUint8ClampedArray([1,2,3,4]);varshape=[2,2];varorder='row-major';varstrides=[-2,-1];varoffset=3;// Create a new array:vararr=newFancyArray('uint8c',buffer,shape,strides,offset,order);// Get the number of dimensions:varndims=arr.ndims;// returns 2
Index offset which specifies thebuffer index at which to start iterating over array elements.
importInt16Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int16@deno/mod.js';// Specify the array configuration:varbuffer=newInt16Array([1,2,3,4,5,6,7,8,9,10,11,12]);varshape=[2,2];varorder='row-major';varstrides=[-2,-1];varoffset=10;// Create a new array:vararr=newFancyArray('int16',buffer,shape,strides,offset,order);// Get the index offset:varo=arr.offset;// returns 10
Array order. The array order is either row-major (C-style) or column-major (Fortran-style).
importUint32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-uint32@deno/mod.js';// Specify the array configuration:varbuffer=newUint32Array([1,2,3,4]);varshape=[2,2];varorder='row-major';varstrides=[2,1];varoffset=0;// Create a new array:vararr=newFancyArray('uint32',buffer,shape,strides,offset,order);// Get the array order:varord=arr.order;// returns 'row-major'
Returns a copy of the array shape.
// Specify the array configuration:varbuffer=[1.0,2.0,3.0,4.0,5.0,6.0];varshape=[2,2];varorder='row-major';varstrides=[2,1];varoffset=2;// Create a new array:vararr=newFancyArray('generic',buffer,shape,strides,offset,order);// Get the array shape:vardims=arr.shape;// returns [ 2, 2 ]
Returns a copy of the array strides which specify how to access data along corresponding array dimensions.
// Specify the array configuration:varbuffer=[1.0,2.0,3.0,4.0];varshape=[2,2];varorder='column-major';varstrides=[-1,2];varoffset=1;// Create a new array:vararr=newFancyArray('generic',buffer,shape,strides,offset,order);// Get the array strides:vars=arr.strides;// returns [ -1, 2 ]
Returns an array element specified according to provided subscripts. The number of provided subscripts mustequal the number of dimensions.
// Specify the array configuration:varbuffer=[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0];varshape=[2,2];varorder='row-major';varstrides=[2,1];varoffset=2;// Create a new array:vararr=newFancyArray('generic',buffer,shape,strides,offset,order);// Get the element located at (1,1):varv=arr.get(1,1);// returns 6.0
Returns an array element located at a specified linear index.
// Specify the array configuration:varbuffer=[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0];varshape=[2,2];varorder='row-major';varstrides=[2,1];varoffset=2;// Create a new array:vararr=newFancyArray('generic',buffer,shape,strides,offset,order);// Get the element located at index 3:varv=arr.iget(3);// returns 6.0
For zero-dimensional arrays, the input argument is ignored and, for clarity, shouldnot be provided.
Sets an array element specified according to provided subscripts. The number of provided subscripts mustequal the number of dimensions.
// Specify the array configuration:varbuffer=[1.0,2.0,3.0,4.0];varshape=[2,2];varorder='row-major';varstrides=[2,1];varoffset=0;// Create a new array:vararr=newFancyArray('generic',buffer,shape,strides,offset,order);// Set the element located at (1,1):arr.set(1,1,40.0);varv=arr.get(1,1);// returns 40.0// Get the underlying buffer:vard=arr.data;// returns [ 1.0, 2.0, 3.0, 40.0 ]
The method returns theFancyArray instance. If an array isread-only, the method raises an exception.
Sets an array element located at a specified linear index.
// Specify the array configuration:varbuffer=[1.0,2.0,3.0,4.0];varshape=[2,2];varorder='row-major';varstrides=[2,1];varoffset=0;// Create a new array:vararr=newFancyArray('generic',buffer,shape,strides,offset,order);// Set the element located at index 3:arr.iset(3,40.0);varv=arr.iget(3);// returns 40.0// Get the underlying buffer:vard=arr.data;// returns [ 1.0, 2.0, 3.0, 40.0 ]
For zero-dimensional arrays, the first, andonly, argument should be the valuev to set.
The method returns theFancyArray instance. If an array isread-only, the method raises an exception.
Serializes aFancyArray as a string.
// Specify the array configuration:varbuffer=[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0];varshape=[3,2];varorder='row-major';varstrides=[2,1];varoffset=2;// Create a new array:vararr=newFancyArray('generic',buffer,shape,strides,offset,order);// Serialize to a string:varstr=arr.toString();// returns "ndarray( 'generic', [ 3, 4, 5, 6, 7, 8 ], [ 3, 2 ], [ 2, 1 ], 0, 'row-major' )"
The method doesnot serialize data outside of the buffer region defined by the array configuration.
Serializes aFancyArray as aJSON object.JSON.stringify() implicitly calls this method when stringifying aFancyArray instance.
// Specify the array configuration:varbuffer=[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0];varshape=[3,2];varorder='row-major';varstrides=[2,1];varoffset=2;// Create a new array:vararr=newFancyArray('generic',buffer,shape,strides,offset,order);// Serialize to JSON:varo=arr.toJSON();// returns { 'type': 'ndarray', 'dtype': 'generic', 'flags': {...}, 'offset': 0, 'order': 'row-major', 'shape': [ 3, 2 ], 'strides': [ 2, 1 ], 'data': [ 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]}
The method doesnot serialize data outside of the buffer region defined by the array configuration.
To create a zero-dimensional array, provide an empty
shapeand a singlestrideselement equal to0. Theordercan be eitherrow-majororcolumn-majorand has no effect on data storage or access.varbuffer=[1];varshape=[];varorder='row-major';varstrides=[0];varoffset=0;// Create a new zero-dimensional array:vararr=newFancyArray('generic',buffer,shape,strides,offset,order);// returns <FancyArray>
A
FancyArrayis anndarrayinstance and supports allndarrayoptions, attributes, and methods. AFancyArraycan be consumed by any API which supportsndarrayinstances.Indexing expressions provide a convenient and powerful means for creating and operating on
ndarrayviews; however, their use does entail a performance cost. Indexing expressions are best suited for interactive use (e.g., in theREPL) and scripting. For performance critical applications, prefer equivalent functional APIs supportingndarrayinstances.In older JavaScript environments which donot support
Proxyobjects, the use of indexing expressions isnot supported.
importSfrom'https://cdn.jsdelivr.net/gh/stdlib-js/slice-ctor@deno/mod.js';importEfrom'https://cdn.jsdelivr.net/gh/stdlib-js/slice-multi@deno/mod.js';importtoArrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-to-array@deno/mod.js';importFancyArrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-fancy@deno/mod.js';varbuffer=[1,2,3,4,// 05,6,// 17,8,// 29,10];varshape=[3,2];varstrides=[2,1];varoffset=2;varx=newFancyArray('generic',buffer,shape,strides,offset,'row-major');// returns <FancyArray>// Access an ndarray property:varndims=x.ndims;// returns 2// Retrieve an ndarray element:varv=x.get(2,1);// returns 8// Set an ndarray element:x.set(2,1,20);v=x.get(2,1);// returns 20// Create an alias for `undefined` for more concise slicing expressions:var_=void0;// Create a multi-dimensional slice:vars=E(S(0,_,2),_);// returns <MultiSlice>// Use the slice to create a view on the original ndarray:vary1=x[s];console.log(toArray(y1));// => [ [ 3, 4 ], [ 7, 20 ] ]// Use alternative syntax:vary2=x[[S(0,_,2),_]];console.log(toArray(y2));// => [ [ 3, 4 ], [ 7, 20 ] ]// Use alternative syntax:vary3=x['0::2,:'];console.log(toArray(y3));// => [ [ 3, 4 ], [ 7, 20 ] ]// Flip dimensions:vary4=x[[S(_,_,-2),S(_,_,-1)]];console.log(toArray(y4));// => [ [ 20, 7 ], [ 4, 3 ] ]
@stdlib/ndarray-array:multidimensional arrays.@stdlib/ndarray-ctor:multidimensional array constructor.
This package is part ofstdlib, 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 developstdlib, see the main projectrepository.
SeeLICENSE.
Copyright © 2016-2025. The StdlibAuthors.
About
Fancy multidimensional array constructor.
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.