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

Convert an array to an object supporting fancy indexing.

License

NotificationsYou must be signed in to change notification settings

stdlib-js/array-to-fancy

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!

array2fancy

NPM versionBuild StatusCoverage Status

Convert an array to an object supporting fancy indexing.

An array supportingfancy indexing is an array which supports slicing via indexing expressions for both retrieval and assignment.

vararray2fancy=require('@stdlib/array-to-fancy');// Create a plain array:varx=[1,2,3,4,5,6,7,8];// Turn the plain array into a "fancy" array:vary=array2fancy(x);// Select the first 3 elements:varv=y[':3'];// returns [ 1, 2, 3 ]// Select every other element, starting from the second element:v=y['1::2'];// returns [ 2, 4, 6, 8 ]// Select every other element, in reverse order, starting with the least element:v=y['::-2'];// returns [ 8, 6, 4, 2 ]// Set all elements to the same value:y[':']=9;// Create a shallow copy by selecting all elements:v=y[':'];// returns [ 9, 9, 9, 9, 9, 9, 9, 9 ]

Installation

npm install @stdlib/array-to-fancy

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

vararray2fancy=require('@stdlib/array-to-fancy');

array2fancy( x[, options] )

Converts an array to an object supporting fancy indexing.

varSlice=require('@stdlib/slice-ctor');varx=[1,2,3,4];vary=array2fancy(x);// returns <Array>// Normal element access:varv=y[0];// returns 1v=y[1];// returns 2// Using negative integers:v=y[-1];// returns 4v=y[-2];// returns 3// Using subsequence expressions:v=y['1::2'];// returns [ 2, 4 ]// Using Slice objects:v=y[newSlice(1,null,2)];// returns [ 2, 4 ]// Assignment:y['1:3']=5;v=y[':'];// returns [ 1, 5, 5, 4 ]

The function supports the following options:

  • strict: boolean indicating whether to enforce strict bounds checking. Default:false.

By default, the function returns a fancy array which doesnot enforce strict bounds checking. For example,

vary=array2fancy([1,2,3,4]);varv=y[10];// returns undefined

To enforce strict bounds checking, set thestrict option totrue.

vary=array2fancy([1,2,3,4],{'strict':true});varv=y[10];// throws <RangeError>

Notes

  • A fancy array shares thesame data as the provided input array. Hence, any mutations to the returned array will affect the underlying input array.
  • A fancy array supports indexing using positive and negative integers (both numeric literals and strings),Slice instances, andsubsequence expressions.
  • A fancy array supports all properties and methods of the input array, and, thus, a fancy array can be consumed by any API which supports array-like objects.
  • Indexing expressions provide a convenient and powerful means for creating and operating on array views; 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 supporting array-like objects.
  • In older JavaScript environments which donot supportProxy objects, the use of indexing expressions isnot supported.

Broadcasting

Fancy arrays supportbroadcasting in which assigned scalars and single-element arrays are repeated (without additional memory allocation) to match the length of a target array instance.

vary=array2fancy([1,2,3,4]);// Broadcast a scalar:y[':']=5;varv=y[':'];// returns [ 5, 5, 5, 5 ]// Broadcast a single-element array:y[':']=[6];v=y[':'];// returns [ 6, 6, 6, 6 ]

Fancy array broadcasting follows thesame rules as forndarrays. Consequently, when assigning arrays to slices, the array on the right-hand-side must be broadcast-compatible with number of elements in the slice. For example,

vary=array2fancy([1,2,3,4]);y[':']=[5,6,7,8];varv=y[':'];// returns [ 5, 6, 7, 8 ]y['1::2']=[9,10];v=y[':'];// returns [ 5, 9, 7, 10 ]y['1::2']=[11];v=y[':'];// returns [ 5, 11, 7, 11 ]y['1::2']=12;v=y[':'];// returns [ 5, 12, 7, 12 ]// Out-of-bounds slices (i.e., slices with zero elements):y['10:20']=[13];v=y[':'];// returns [ 5, 12, 7, 12 ]y['10:20']=13;v=y[':'];// returns [ 5, 12, 7, 12 ]y['10:20']=[];v=y[':'];// returns [ 5, 12, 7, 12 ]

are all valid. However,

vary=array2fancy([1,2,3,4]);y[':']=[5,6];// throws <Error>// Out-of-bounds slice (i.e., a slice with zero elements):y['10:20']=[8,9,10,11];// throws <Error>

Casting

Fancy arrays support(mostly) safe casts (i.e., any cast which can be performed without overflow or loss of precision, with the exception of floating-point arrays which are also allowed to downcast from higher precision to lower precision).

varUint8Array=require('@stdlib/array-uint8');varInt32Array=require('@stdlib/array-int32');varx=newInt32Array([1,2,3,4]);vary=array2fancy(x);// 8-bit unsigned integer values can be safely cast to 32-bit signed integer values:y[':']=newUint8Array([5,6,7,8]);

When attempting to perform an unsafe cast, fancy arrays will raise an exception.

varUint8Array=require('@stdlib/array-uint8');varx=newUint8Array([1,2,3,4]);vary=array2fancy(x);// Attempt to assign a non-integer value:y[':']=3.14;// throws <TypeError>// Attempt to assign a negative value:y[':']=-3;// throws <TypeError>

When assigning a real-valued scalar to a complex number array (e.g.,Complex128Array orComplex64Array), a fancy array will cast the real-valued scalar to a complex number argument having an imaginary component equal to zero.

varComplex128Array=require('@stdlib/array-complex128');varreal=require('@stdlib/complex-real');varimag=require('@stdlib/complex-imag');varx=newComplex128Array([1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0]);vary=array2fancy(x);// Retrieve the first element:varv=y[0];// returns <Complex128>varre=real(v);// returns 1.0varim=imag(v);// returns 2.0// Assign a real-valued scalar to the first element:y[0]=9.0;v=y[0];// returns <Complex128>re=real(v);// returns 9.0im=imag(v);// returns 0.0

Note, however, that attempting to assign a real-valued array to a complex number array slice isnot supported due to the ambiguity of whether the real-valued array is a collection of real components (with implied imaginary components equal to zero) or an array of interleaved real and imaginary components.

varFloat64Array=require('@stdlib/array-float64');varComplex128Array=require('@stdlib/array-complex128');varx=newComplex128Array([1.0,2.0,3.0,4.0]);vary=array2fancy(x);// Attempt to assign a real-valued array:y[':']=newFloat64Array([5.0,6.0]);// is this a single complex number which should be broadcast or a list of real components with implied imaginary components?// throws <Error>

Examples

vararray2fancy=require('@stdlib/array-to-fancy');varx=[1,2,3,4,5,6];vary=array2fancy(x);// returns <Array>varz=y['1::2'];// returns [ 2, 4, 6 ]z=y['-2::-2'];// returns [ 5, 3, 1 ]z=y['1:4'];// returns [ 2, 3, 4 ]y['4:1:-1']=10;z=y[':'];// returns [ 1, 2, 10, 10, 10, 6 ]y['2:5']=[-10,-9,-8];z=y[':'];// returns [ 1, 2, -10, -9, -8, 6 ]

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-2024. The StdlibAuthors.

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp