main.js (2395B)
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 skew-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 skew-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 * var bool = isSkewCentrosymmetricMatrix( arr ); 46 * // returns true 47 * 48 * bool = isSkewCentrosymmetricMatrix( [] ); 49 * // returns false 50 */ 51 function isSkewCentrosymmetricMatrix( v ) { // eslint-disable-line id-length 52 var m1; 53 var M; 54 var N; 55 var n; 56 var i; 57 var j; 58 if ( !isSquareMatrix( v ) ) { 59 return false; 60 } 61 M = v.shape[ 0 ]; 62 N = floor( M/2.0 ); // corresponds to a row index + 1 63 m1 = M - 1; 64 for ( i = 0; i < N; i++ ) { 65 n = m1 - i; 66 for ( j = 0; j < M; j++ ) { 67 if ( v.get( i, j ) !== -v.get( n, m1-j ) ) { 68 return false; 69 } 70 } 71 } 72 if ( isOdd( M ) ) { 73 // Only need to examine the first half of the row (including the center element) due to symmetry... 74 for ( j = 0; j <= N; j++ ) { 75 if ( v.get( i, j ) !== -v.get( N, m1-j ) ) { 76 return false; 77 } 78 } 79 } 80 return true; 81 } 82 83 84 // EXPORTS // 85 86 module.exports = isSkewCentrosymmetricMatrix;