time-to-botec

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

main.js (2331B)


      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 isSquareMatrix = require( './../../is-square-matrix' );
     24 var floor = require( '@stdlib/math/base/special/floor' );
     25 var isOdd = require( '@stdlib/math/base/assert/is-odd' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Tests if a value is a centrosymmetric matrix.
     32 *
     33 * ## Notes
     34 *
     35 * -   The implementation must rely on manually checking that \\(M_{ij} = M_{N-i-1,N-j-1}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass.
     36 * -   Worst case scenario: O(N^2).
     37 *
     38 * @param {*} v - value to test
     39 * @returns {boolean} boolean indicating if a value is a centrosymmetric matrix
     40 *
     41 * @example
     42 * var ndarray = require( '@stdlib/ndarray/ctor' );
     43 *
     44 * var arr = ndarray( 'generic', [ 2, 1, 1, 2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' );
     45 *
     46 * var bool = isCentrosymmetricMatrix( arr );
     47 * // returns true
     48 *
     49 * bool = isCentrosymmetricMatrix( [] );
     50 * // returns false
     51 */
     52 function isCentrosymmetricMatrix( v ) {
     53 	var m1;
     54 	var M;
     55 	var N;
     56 	var n;
     57 	var i;
     58 	var j;
     59 	if ( !isSquareMatrix( v ) ) {
     60 		return false;
     61 	}
     62 	M = v.shape[ 0 ];
     63 	N = floor( M/2.0 ); // corresponds to a row index + 1
     64 	m1 = M - 1;
     65 	for ( i = 0; i < N; i++ ) {
     66 		n = m1 - i;
     67 		for ( j = 0; j < M; j++ ) {
     68 			if ( v.get( i, j ) !== v.get( n, m1-j ) ) {
     69 				return false;
     70 			}
     71 		}
     72 	}
     73 	if ( isOdd( M ) ) {
     74 		// Only need to examine the first half of the row (up until the center element) due to symmetry...
     75 		for ( j = 0; j < N; j++ ) {
     76 			if ( v.get( i, j ) !== v.get( N, m1-j ) ) {
     77 				return false;
     78 			}
     79 		}
     80 	}
     81 	return true;
     82 }
     83 
     84 
     85 // EXPORTS //
     86 
     87 module.exports = isCentrosymmetricMatrix;