time-to-botec

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

absdiff.js (1337B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2020 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 Float64Array = require( '@stdlib/array/float64' );
     24 var abs = require( '@stdlib/math/base/special/abs' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Computes an element-wise absolute difference of two matrices and stores the results in a typed array.
     31 *
     32 * @param {Matrix} x - first input matrix
     33 * @param {Matrix} y - second input matrix
     34 * @returns {Float64Array} output array
     35 */
     36 function absdiff( x, y ) {
     37 	var out;
     38 	var i;
     39 	var j;
     40 	var M;
     41 	var N;
     42 
     43 	out = new Float64Array( x.length );
     44 	M = x.shape[ 0 ];
     45 	N = x.shape[ 1 ];
     46 	for ( i = 0; i < M; i++ ) {
     47 		for ( j = 0; j < N; j++ ) {
     48 			out[ ( i*M ) + j ] = abs( x.get( i, j ) - y.get( i, j ) );
     49 		}
     50 	}
     51 	return out;
     52 }
     53 
     54 
     55 // EXPORTS //
     56 
     57 module.exports = absdiff;