|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2026 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# descriptor |
| 22 | + |
| 23 | +> Create a plain object describing how to interpret a data buffer as an n-dimensional array. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var descriptor = require( '@stdlib/ndarray/base/descriptor' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### descriptor( dtype, buffer, shape, strides, offset, order ) |
| 44 | + |
| 45 | +Returns a plain object describing how to interpret a data buffer as an n-dimensional array. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var buffer = [ 1, 2, 3, 4, 5, 6 ]; |
| 49 | +var shape = [ 3, 2 ]; |
| 50 | +var strides = [ 2, 1 ]; |
| 51 | +var offset = 0; |
| 52 | + |
| 53 | +var out = descriptor( 'generic', buffer, shape, strides, offset, 'row-major' ); |
| 54 | +// returns {...} |
| 55 | +``` |
| 56 | + |
| 57 | +The function accepts the following arguments: |
| 58 | + |
| 59 | +- **dtype**: [data type][@stdlib/ndarray/dtypes]. |
| 60 | +- **buffer**: data buffer. |
| 61 | +- **shape**: array shape (dimensions). |
| 62 | +- **strides**: array strides which are index offsets specifying how to access along corresponding dimensions. |
| 63 | +- **offset**: index offset specifying the location of the first indexed element in the data buffer. |
| 64 | +- **order**: array [order][@stdlib/ndarray/orders]. Must be either `row-major` (C-style) or `column-major` (Fortran-style). |
| 65 | + |
| 66 | +</section> |
| 67 | + |
| 68 | +<!-- /.usage --> |
| 69 | + |
| 70 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 71 | + |
| 72 | +<section class="notes"> |
| 73 | + |
| 74 | +## Notes |
| 75 | + |
| 76 | +- The returned object has the following properties: |
| 77 | + |
| 78 | + - **dtype**: [data type][@stdlib/ndarray/dtypes]. |
| 79 | + - **buffer**: data buffer. |
| 80 | + - **shape**: array shape. |
| 81 | + - **strides**: array strides. |
| 82 | + - **offset**: index offset. |
| 83 | + - **order**: array [order][@stdlib/ndarray/orders]. |
| 84 | + |
| 85 | +- This function is intended as a potential performance optimization. In V8, for example, even if two objects share common properties, if those properties were added in different orders or if one object has additional properties not shared by the other object, then those objects will have different "hidden" classes. If a function is provided many objects having different "shapes", some JavaScript VMs (e.g., V8) will consider the function "megamorphic" and fail to perform various runtime optimizations. Accordingly, the intent of this function is to create an object containing the minimal amount of meta data necessary to interpret a linear data buffer as an n-dimensional array and to ensure that internal functions operating on ndarrays are provided consistent argument "shapes". |
| 86 | + |
| 87 | +</section> |
| 88 | + |
| 89 | +<!-- /.notes --> |
| 90 | + |
| 91 | +<!-- Package usage examples. --> |
| 92 | + |
| 93 | +<section class="examples"> |
| 94 | + |
| 95 | +## Examples |
| 96 | + |
| 97 | +<!-- eslint-disable max-len --> |
| 98 | + |
| 99 | +<!-- eslint no-undef: "error" --> |
| 100 | + |
| 101 | +```javascript |
| 102 | +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); |
| 103 | +var getDType = require( '@stdlib/ndarray/dtype' ); |
| 104 | +var getData = require( '@stdlib/ndarray/data-buffer' ); |
| 105 | +var getShape = require( '@stdlib/ndarray/shape' ); |
| 106 | +var getStrides = require( '@stdlib/ndarray/strides' ); |
| 107 | +var getOffset = require( '@stdlib/ndarray/offset' ); |
| 108 | +var getOrder = require( '@stdlib/ndarray/order' ); |
| 109 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 110 | +var descriptor = require( '@stdlib/ndarray/base/descriptor' ); |
| 111 | + |
| 112 | +var x = discreteUniform( [ 3, 3 ], 0, 10, { |
| 113 | + 'dtype': 'generic' |
| 114 | +}); |
| 115 | +console.log( ndarray2array( x ) ); |
| 116 | + |
| 117 | +var obj = descriptor( getDType( x ), getData( x ), getShape( x ), getStrides( x ), getOffset( x ), getOrder( x ) ); |
| 118 | +console.log( obj ); |
| 119 | +``` |
| 120 | + |
| 121 | +</section> |
| 122 | + |
| 123 | +<!-- /.examples --> |
| 124 | + |
| 125 | +<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 126 | + |
| 127 | +<section class="references"> |
| 128 | + |
| 129 | +</section> |
| 130 | + |
| 131 | +<!-- /.references --> |
| 132 | + |
| 133 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 134 | + |
| 135 | +<section class="related"> |
| 136 | + |
| 137 | +</section> |
| 138 | + |
| 139 | +<!-- /.related --> |
| 140 | + |
| 141 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 142 | + |
| 143 | +<section class="links"> |
| 144 | + |
| 145 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/ndarray/tree/main/dtypes |
| 146 | + |
| 147 | +[@stdlib/ndarray/orders]: https://github.com/stdlib-js/ndarray/tree/main/orders |
| 148 | + |
| 149 | +</section> |
| 150 | + |
| 151 | +<!-- /.links --> |
0 commit comments