time-to-botec

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

avg_matrix.js (1400B)


      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 matrix = require( './matrix.js' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Calculate weighted average of two matrices.
     30 *
     31 * @private
     32 * @param {Matrix} A - first matrix
     33 * @param {Matrix} B - second matrix
     34 * @param {PositiveInteger} weight - relative weight of matrix A
     35 * @returns {Matrix} averaged matrix
     36 */
     37 function avgMatrix( A, B, weight ) {
     38 	var propA;
     39 	var propB;
     40 	var nrow;
     41 	var ncol;
     42 	var val;
     43 	var C;
     44 	var i;
     45 	var j;
     46 
     47 	nrow = A.shape[ 0 ];
     48 	ncol = A.shape[ 1 ];
     49 	C = matrix( [ nrow, ncol ] );
     50 	propA = ( weight - 1.0 ) / weight;
     51 	propB = 1.0 / weight;
     52 
     53 	for ( i = 0; i < nrow; i++ ) {
     54 		for ( j = 0; j < ncol; j++ ) {
     55 			val = (propA * A.get(i, j)) + (propB * B.get(i, j));
     56 			C.set( i, j, val );
     57 		}
     58 	}
     59 	return C;
     60 }
     61 
     62 
     63 // EXPORTS //
     64 
     65 module.exports = avgMatrix;