README.md (4203B)
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 # Unary 22 23 > Apply a unary callback to elements in a input ndarray and assign results to elements in an output ndarray. 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 unary = require( '@stdlib/ndarray/base/unary' ); 41 ``` 42 43 #### unary( arrays, fcn ) 44 45 Applies a unary callback to elements in a input ndarray and assign results to elements in an output ndarray. 46 47 ```javascript 48 var Float64Array = require( '@stdlib/array/float64' ); 49 var unary = require( '@stdlib/ndarray/base/unary' ); 50 51 function scale( x ) { 52 return x * 10.0; 53 } 54 55 // Create data buffers: 56 var arr = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; 57 var xbuf = new Float64Array( arr); 58 var ybuf = new Float64Array( 6 ); 59 60 // Define the shape of the input and output arrays: 61 var shape = [ 3, 1, 2 ]; 62 63 // Define the array strides: 64 var sx = [ 4, 4, 1 ]; 65 var sy = [ 2, 2, 1 ]; 66 67 // Define the index offsets: 68 var ox = 1; 69 var oy = 0; 70 71 // Create the input and output ndarray-like objects: 72 var x = { 73 'dtype': 'float64', 74 'data': xbuf, 75 'shape': shape, 76 'strides': sx, 77 'offset': ox, 78 'order': 'row-major' 79 }; 80 var y = { 81 'dtype': 'float64', 82 'data': ybuf, 83 'shape': shape, 84 'strides': sy, 85 'offset': oy, 86 'order': 'row-major' 87 }; 88 89 // Apply the unary function: 90 unary( [ x, y ], scale ); 91 92 console.log( y.data ); 93 // => <Float64Array>[ 20.0, 30.0, 60.0, 70.0, 100.0, 110.0 ] 94 ``` 95 96 <!-- TODO: Finish documentation --> 97 98 </section> 99 100 <!-- /.usage --> 101 102 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 103 104 <section class="notes"> 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 discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; 120 var filledarray = require( '@stdlib/array/filled' ); 121 var gfillBy = require( '@stdlib/blas/ext/base/gfill-by' ); 122 var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); 123 var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); 124 var unary = require( '@stdlib/ndarray/base/unary' ); 125 126 function scale( x ) { 127 return x * 10; 128 } 129 130 var N = 10; 131 132 var xbuf = filledarray( 0, N, 'generic' ); 133 gfillBy( xbuf.length, xbuf, 1, discreteUniform( -100, 100 ) ); 134 135 var x = { 136 'dtype': 'generic', 137 'data': xbuf, 138 'shape': [ 5, 2 ], 139 'strides': [ 2, 1 ], 140 'offset': 0, 141 'order': 'row-major' 142 }; 143 var y = { 144 'dtype': 'generic', 145 'data': filledarray( 0, N, 'generic' ), 146 'shape': x.shape.slice(), 147 'strides': shape2strides( x.shape, 'column-major' ), 148 'offset': 0, 149 'order': 'column-major' 150 }; 151 152 unary( [ x, y ], scale ); 153 console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) ); 154 console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) ); 155 ``` 156 157 </section> 158 159 <!-- /.examples --> 160 161 <!-- 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. --> 162 163 <section class="references"> 164 165 </section> 166 167 <!-- /.references --> 168 169 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 170 171 <section class="links"> 172 173 </section> 174 175 <!-- /.links -->