README.md (4108B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # ndarray2array 22 23 > Convert an ndarray buffer to a generic 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 ndarray2array = require( '@stdlib/ndarray/base/to-array' ); 41 ``` 42 43 #### ndarray2array( buffer, shape, strides, offset, order ) 44 45 Converts an ndarray buffer to a generic array (which may include nested arrays). 46 47 ```javascript 48 var buffer = [ 1, 2, 3, 4 ]; 49 var shape = [ 2, 2 ]; 50 var order = 'row-major'; 51 var strides = [ 2, 1 ]; 52 var offset = 0; 53 54 var arr = ndarray2array( buffer, shape, strides, offset, order ); 55 // returns [ [ 1, 2 ], [ 3, 4 ] ] 56 ``` 57 58 The `order` parameter specifies whether an array is `row-major` (C-style) or `column-major` (Fortran-style). 59 60 ```javascript 61 var buffer = [ 1, 2, 3, 4 ]; 62 var shape = [ 2, 2 ]; 63 var order = 'column-major'; 64 var strides = [ 1, 2 ]; 65 var offset = 0; 66 67 var arr = ndarray2array( buffer, shape, strides, offset, order ); 68 // returns [ [ 1, 3 ], [ 2, 4 ] ] 69 ``` 70 71 The `offset` parameter specifies the location of the first indexed element based on the `strides` array. 72 73 ```javascript 74 var buffer = [ 1, 2, 3, 4 ]; 75 var shape = [ 2, 2 ]; 76 var order = 'row-major'; 77 var strides = [ -2, -1 ]; 78 var offset = 3; 79 80 var arr = ndarray2array( buffer, shape, strides, offset, order ); 81 // returns [ [ 4, 3 ], [ 2, 1 ] ] 82 ``` 83 84 </section> 85 86 <!-- /.usage --> 87 88 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 89 90 <section class="notes"> 91 92 </section> 93 94 <!-- /.notes --> 95 96 <!-- Package usage examples. --> 97 98 <section class="examples"> 99 100 ## Examples 101 102 <!-- eslint no-undef: "error" --> 103 104 ```javascript 105 var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); 106 var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); 107 var dicreteUniform = require( '@stdlib/random/base/discrete-uniform' ); 108 var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); 109 110 // Create a data buffer: 111 var buffer = []; 112 var i; 113 for ( i = 0; i < 27; i++ ) { 114 buffer.push( i ); 115 } 116 117 // Specify array meta data: 118 var shape = [ 3, 3, 3 ]; 119 var order = 'column-major'; 120 var ndims = shape.length; 121 122 // Compute array meta data: 123 var strides = shape2strides( shape, order ); 124 var offset = strides2offset( shape, strides ); 125 126 // Print array information: 127 console.log( '' ); 128 console.log( 'Dims: %s', shape.join( 'x' ) ); 129 130 // Random flip strides and convert an ndarray to a nested array... 131 var arr; 132 var j; 133 for ( i = 0; i < 20; i++ ) { 134 j = dicreteUniform( 0, ndims-1 ); 135 strides[ j ] *= -1; 136 offset = strides2offset( shape, strides ); 137 138 console.log( '' ); 139 console.log( 'Strides: %s', strides.join( ',' ) ); 140 console.log( 'Offset: %d', offset ); 141 142 arr = ndarray2array( buffer, shape, strides, offset, order ); 143 console.log( JSON.stringify( arr ) ); 144 } 145 ``` 146 147 </section> 148 149 <!-- /.examples --> 150 151 <!-- 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. --> 152 153 <section class="references"> 154 155 </section> 156 157 <!-- /.references --> 158 159 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 160 161 <section class="links"> 162 163 </section> 164 165 <!-- /.links -->