time-to-botec

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

ndarray_like.js (2073B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 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 isNumericArray = require( '@stdlib/assert/is-numeric-array' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Converts two arrays .
     30 *
     31 * @private
     32 * @param {NumericArray} x - array of x values
     33 * @param {NumericArray} y - array of y values
     34 * @throws {TypeError} first argument must be a numeric array
     35 * @throws {TypeError} second argument must be a numeric array
     36 * @throws {Error} first and second arguments must be of the same length
     37 * @returns {Object} object that mirrors an `ndarray`
     38 *
     39 * @example
     40 * var x = [ 0.6333, 0.8643, 1.0952, 1.3262, 1.5571, 1.7881, 2.019, 2.25, 2.481, 2.7119 ];
     41 * var y = [ -0.0468, 0.8012, 1.6492, 2.4973, 3.3454, 4.1934, 5.0415, 5.8896, 6.7376, 7.5857 ];
     42 * var out = ndarrayLike( x, y );
     43 */
     44 function ndarrayLike( x, y ) {
     45 	if ( !isNumericArray(x) ) {
     46 		throw new TypeError( 'First argument must be a numeric array' );
     47 	}
     48 
     49 	if ( !isNumericArray(y) ) {
     50 		throw new TypeError( 'Second argument must be a numeric array' );
     51 	}
     52 
     53 	if ( x.length !== y.length ) {
     54 		throw new Error( 'First and second argument must be of same length' );
     55 	}
     56 
     57 	return {
     58 		'get': get,
     59 		'shape': [ x.length, 2 ]
     60 	};
     61 
     62 	/**
     63 	* Gets an element of an nd-array-like object .
     64 	*
     65 	* @private
     66 	* @param {number} i - row index
     67 	* @param {number} j - column index
     68 	* @returns {number} number stored in row i and column j
     69 	*/
     70 	function get( i, j ) {
     71 		if ( j === 0 ) {
     72 			return x[ i ];
     73 		}
     74 		return y[ i ];
     75 	}
     76 }
     77 
     78 
     79 // EXPORTS //
     80 
     81 module.exports = ndarrayLike;