0d.js (2740B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2021 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MAIN // 22 23 /** 24 * Applies a unary callback to elements in a zero-dimensional input ndarray and assigns results to elements in an equivalently shaped output ndarray. 25 * 26 * @private 27 * @param {Object} x - object containing input ndarray meta data 28 * @param {string} x.dtype - data type 29 * @param {Collection} x.data - data buffer 30 * @param {NonNegativeIntegerArray} x.shape - dimensions 31 * @param {IntegerArray} x.strides - stride lengths 32 * @param {NonNegativeInteger} x.offset - index offset 33 * @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style) 34 * @param {Object} y - object containing output ndarray meta data 35 * @param {string} y.dtype - data type 36 * @param {Collection} y.data - data buffer 37 * @param {NonNegativeIntegerArray} y.shape - dimensions 38 * @param {IntegerArray} y.strides - stride lengths 39 * @param {NonNegativeInteger} y.offset - index offset 40 * @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style) 41 * @param {Callback} fcn - unary callback 42 * @returns {void} 43 * 44 * @example 45 * var Float64Array = require( '@stdlib/array/float64' ); 46 * 47 * function scale( x ) { 48 * return x * 10.0; 49 * } 50 * 51 * // Create data buffers: 52 * var xbuf = new Float64Array( [ 1.0, 2.0 ] ); 53 * var ybuf = new Float64Array( 1 ); 54 * 55 * // Define the shape of the input and output arrays: 56 * var shape = []; 57 * 58 * // Define the array strides: 59 * var sx = [ 0 ]; 60 * var sy = [ 0 ]; 61 * 62 * // Define the index offsets: 63 * var ox = 1; 64 * var oy = 0; 65 * 66 * // Create the input and output ndarray-like objects: 67 * var x = { 68 * 'dtype': 'float64', 69 * 'data': xbuf, 70 * 'shape': shape, 71 * 'strides': sx, 72 * 'offset': ox, 73 * 'order': 'row-major' 74 * }; 75 * var y = { 76 * 'dtype': 'float64', 77 * 'data': ybuf, 78 * 'shape': shape, 79 * 'strides': sy, 80 * 'offset': oy, 81 * 'order': 'row-major' 82 * }; 83 * 84 * // Apply the unary function: 85 * unary0d( x, y, scale ); 86 * 87 * console.log( y.data ); 88 * // => <Float64Array>[ 20.0 ] 89 */ 90 function unary0d( x, y, fcn ) { 91 y.data[ y.offset ] = fcn( x.data[ x.offset ] ); 92 } 93 94 95 // EXPORTS // 96 97 module.exports = unary0d;