time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

nd.js (3790B)


      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 // MODULES //
     22 
     23 var numel = require( './../../../base/numel' );
     24 var vind2bind = require( './../../../base/vind2bind' );
     25 
     26 
     27 // VARIABLES //
     28 
     29 var MODE = 'throw';
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Applies a unary callback to elements in an n-dimensional input ndarray and assigns results to elements in an equivalently shaped output ndarray.
     36 *
     37 * @private
     38 * @param {Object} x - object containing input ndarray meta data
     39 * @param {string} x.dtype - data type
     40 * @param {Collection} x.data - data buffer
     41 * @param {NonNegativeIntegerArray} x.shape - dimensions
     42 * @param {IntegerArray} x.strides - stride lengths
     43 * @param {NonNegativeInteger} x.offset - index offset
     44 * @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
     45 * @param {Object} y - object containing output ndarray meta data
     46 * @param {string} y.dtype - data type
     47 * @param {Collection} y.data - data buffer
     48 * @param {NonNegativeIntegerArray} y.shape - dimensions
     49 * @param {IntegerArray} y.strides - stride lengths
     50 * @param {NonNegativeInteger} y.offset - index offset
     51 * @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style)
     52 * @param {Callback} fcn - unary callback
     53 * @returns {void}
     54 *
     55 * @example
     56 * var Float64Array = require( '@stdlib/array/float64' );
     57 *
     58 * function scale( x ) {
     59 *     return x * 10.0;
     60 * }
     61 *
     62 * // Create data buffers:
     63 * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
     64 * var ybuf = new Float64Array( 4 );
     65 *
     66 * // Define the shape of the input and output arrays:
     67 * var shape = [ 2, 2 ];
     68 *
     69 * // Define the array strides:
     70 * var sx = [ 4, 1 ];
     71 * var sy = [ 2, 1 ];
     72 *
     73 * // Define the index offsets:
     74 * var ox = 1;
     75 * var oy = 0;
     76 *
     77 * // Create the input and output ndarray-like objects:
     78 * var x = {
     79 *     'dtype': 'float64',
     80 *     'data': xbuf,
     81 *     'shape': shape,
     82 *     'strides': sx,
     83 *     'offset': ox,
     84 *     'order': 'row-major'
     85 * };
     86 * var y = {
     87 *     'dtype': 'float64',
     88 *     'data': ybuf,
     89 *     'shape': shape,
     90 *     'strides': sy,
     91 *     'offset': oy,
     92 *     'order': 'row-major'
     93 * };
     94 *
     95 * // Apply the unary function:
     96 * unarynd( x, y, scale );
     97 *
     98 * console.log( y.data );
     99 * // => <Float64Array>[ 20.0, 30.0, 60.0, 70.0 ]
    100 */
    101 function unarynd( x, y, fcn ) {
    102 	var xbuf;
    103 	var ybuf;
    104 	var ordx;
    105 	var ordy;
    106 	var len;
    107 	var sh;
    108 	var sx;
    109 	var sy;
    110 	var ox;
    111 	var oy;
    112 	var ix;
    113 	var iy;
    114 	var i;
    115 
    116 	sh = x.shape;
    117 
    118 	// Compute the total number of elements over which to iterate:
    119 	len = numel( sh );
    120 
    121 	// Cache references to the input and output ndarray data buffers:
    122 	xbuf = x.data;
    123 	ybuf = y.data;
    124 
    125 	// Cache references the respective stride arrays:
    126 	sx = x.strides;
    127 	sy = y.strides;
    128 
    129 	// Cache the indices of the first indexed elements in the respective ndarrays:
    130 	ox = x.offset;
    131 	oy = y.offset;
    132 
    133 	// Cache the respective array orders:
    134 	ordx = x.order;
    135 	ordy = y.order;
    136 
    137 	// Iterate over each element based on the linear **view** index, regardless as to how the data is stored in memory...
    138 	for ( i = 0; i < len; i++ ) {
    139 		ix = vind2bind( sh, sx, ox, ordx, i, MODE );
    140 		iy = vind2bind( sh, sy, oy, ordy, i, MODE );
    141 		ybuf[ iy ] = fcn( xbuf[ ix ] );
    142 	}
    143 }
    144 
    145 
    146 // EXPORTS //
    147 
    148 module.exports = unarynd;