README.md (5207B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2021 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 # Serialize Meta Data 22 23 > Serialize [ndarray][@stdlib/ndarray/ctor] meta data. 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 serialize = require( '@stdlib/ndarray/base/serialize-meta-data' ); 41 ``` 42 43 #### serialize( x ) 44 45 Serializes [ndarray][@stdlib/ndarray/ctor] meta data. 46 47 ```javascript 48 var array = require( '@stdlib/ndarray/array' ); 49 50 var arr = array( [ [ 1, 2 ], [ 3, 4 ] ] ); 51 var dv = serialize( arr ); 52 // returns <DataView> 53 ``` 54 55 </section> 56 57 <!-- /.usage --> 58 59 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 60 61 <section class="notes"> 62 63 ## Notes 64 65 - Serialization is performed according to host byte order (endianness). 66 67 - Meta data format: 68 69 ```text 70 | endianness (1 byte) | <dtype> (2 bytes) | <ndims> (8 bytes) | <shape> (ndims*8 bytes) | <strides> (ndims*8 bytes) | <offset> (8 bytes) | <order> (1 byte) | <mode> (1 byte) | <nsubmodes> (8 bytes) | <submodes> (nsubmodes*1 bytes) | 71 ``` 72 73 which translates to the following `ArrayBuffer` layout: 74 75 ```text 76 ArrayBuffer[ 77 <endianness>[int8], 78 <dtype>[int16], 79 <ndims>[int64], 80 <shape>[ndims*int64], 81 <strides>[ndims*int64], 82 <offset>[int64], 83 <order>[int8], 84 <mode>[int8], 85 <nsubmodes>[int64], 86 <submodes>[nsubmodes*int8] 87 ] 88 ``` 89 90 where `strides` and `offset` are in units of bytes. 91 92 - If the endianness is `1`, the byte order is little endian. If the endianness is `0`, the byte order is big endian. 93 94 - Buffer length: 95 96 ```text 97 1 + 2 + 8 + (ndims*8) + (ndims*8) + 8 + 1 + 1 + 8 + (nsubmodes*1) = 29 + (ndims*16) + nsubmodes 98 ``` 99 100 For example, consider a three-dimensional [ndarray][@stdlib/ndarray/ctor] with one subscript index mode (submode): 101 102 ```text 103 29 + (3*16) + 1 = 78 bytes 104 ``` 105 106 </section> 107 108 <!-- /.notes --> 109 110 <!-- Package usage examples. --> 111 112 <section class="examples"> 113 114 ## Examples 115 116 <!-- eslint no-undef: "error" --> 117 118 ```javascript 119 var IS_LITTLE_ENDIAN = require( '@stdlib/assert/is-little-endian' ); 120 var array = require( '@stdlib/ndarray/array' ); 121 var Uint8Array = require( '@stdlib/array/uint8' ); 122 var fromInt64Bytes = require( '@stdlib/number/float64/base/from-int64-bytes' ); 123 var serialize = require( '@stdlib/ndarray/base/serialize-meta-data' ); 124 125 // Create an ndarray: 126 var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); 127 128 // Print various properties: 129 console.log( 'dtype: %s', x.dtype ); 130 console.log( 'ndims: %d', x.ndims ); 131 console.log( 'shape: [ %s ]', x.shape.join( ', ' ) ); 132 console.log( 'strides: [ %s ]', x.strides.join( ', ' ) ); 133 console.log( 'offset: %d', x.offset ); 134 console.log( 'order: %s', x.order ); 135 136 // Serialize ndarray meta data to a DataView: 137 var dv = serialize( x ); 138 // returns <DataView> 139 140 // Create a Uint8Array view: 141 var bytes = new Uint8Array( dv.buffer ); 142 143 // Extract the data type (enum): 144 var dtype = dv.getInt16( 1, IS_LITTLE_ENDIAN ); 145 console.log( 'dtype (enum): %d', dtype ); 146 147 // Extract the number of dimensions: 148 var ndims = fromInt64Bytes( bytes, 1, 3 ); 149 console.log( 'ndims: %d', ndims ); 150 151 // Extract the shape: 152 var shape = []; 153 var i; 154 for ( i = 0; i < ndims; i++ ) { 155 shape.push( fromInt64Bytes( bytes, 1, 11+(i*8) ) ); 156 } 157 console.log( 'shape: [ %s ]', shape.join( ', ' ) ); 158 159 // Extract the strides (in units of bytes): 160 var strides = []; 161 for ( i = 0; i < ndims; i++ ) { 162 strides.push( fromInt64Bytes( bytes, 1, 11+(ndims*8)+(i*8) ) ); 163 } 164 console.log( 'strides (bytes): [ %s ]', strides.join( ', ' ) ); 165 166 // Extract the index offset (in bytes): 167 var offset = fromInt64Bytes( bytes, 1, 11+(ndims*16) ); 168 console.log( 'offset (bytes): %d', offset ); 169 170 // Extract the order (enum): 171 var order = dv.getInt8( 19+(ndims*16) ); 172 console.log( 'order (enum): %d', order ); 173 ``` 174 175 </section> 176 177 <!-- /.examples --> 178 179 <!-- 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. --> 180 181 <section class="references"> 182 183 </section> 184 185 <!-- /.references --> 186 187 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 188 189 <section class="links"> 190 191 [@stdlib/ndarray/ctor]: https://www.npmjs.com/package/@stdlib/ndarray/tree/main/ctor 192 193 </section> 194 195 <!-- /.links -->