README.md (3729B)
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 # isBufferLengthCompatible 22 23 > Determine if a buffer length is compatible with ndarray 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 isBufferLengthCompatible = require( '@stdlib/ndarray/base/assert/is-buffer-length-compatible' ); 41 ``` 42 43 #### isBufferLengthCompatible( len, shape, strides, offset ) 44 45 Returns a `boolean` indicating if a buffer `length` is compatible with provided ndarray meta data. 46 47 ```javascript 48 var shape = [ 2, 2 ]; 49 var strides = [ 2, 1 ]; 50 var offset = 25; 51 52 var bool = isBufferLengthCompatible( 30, shape, strides, offset ); 53 // returns true 54 55 bool = isBufferLengthCompatible( 4, shape, strides, offset ); 56 // returns false 57 ``` 58 59 </section> 60 61 <!-- /.usage --> 62 63 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 64 65 <section class="notes"> 66 67 </section> 68 69 <!-- /.notes --> 70 71 <!-- Package usage examples. --> 72 73 <section class="examples"> 74 75 ## Examples 76 77 <!-- eslint no-undef: "error" --> 78 79 ```javascript 80 var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); 81 var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); 82 var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); 83 var randu = require( '@stdlib/random/base/randu' ); 84 var isBufferLengthCompatible = require( '@stdlib/ndarray/base/assert/is-buffer-length-compatible' ); 85 86 var strides; 87 var offset; 88 var shape; 89 var bool; 90 var len; 91 var i; 92 var j; 93 94 len = 500; // buffer length 95 shape = [ 0, 0, 0 ]; 96 97 for ( i = 0; i < 100; i++ ) { 98 // Generate a random array shape: 99 shape[ 0 ] = discreteUniform( 1, 10 ); 100 shape[ 1 ] = discreteUniform( 1, 10 ); 101 shape[ 2 ] = discreteUniform( 1, 10 ); 102 103 // Generate strides: 104 if ( randu() < 0.5 ) { 105 strides = shape2strides( shape, 'row-major' ); 106 } else { 107 strides = shape2strides( shape, 'column-major' ); 108 } 109 j = discreteUniform( 0, shape.length-1 ); 110 strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; 111 112 // Compute the index offset: 113 offset = strides2offset( shape, strides ) + discreteUniform( 0, 200 ); 114 115 // Determine if a buffer length is compatible with generated meta data: 116 bool = isBufferLengthCompatible( len, shape, strides, offset ); 117 console.log( 'Buffer length: %d. Shape: %s. Strides: %s. Offset: %d. Compatible: %s.', len, shape.join( 'x' ), strides.join( ',' ), offset, bool ); 118 } 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 all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 134 135 <section class="links"> 136 137 </section> 138 139 <!-- /.links -->