检查矩阵是否对称的 JavaScript 程序

WBOY
WBOY 转载
2023-08-27 15:37:08 221浏览

检查矩阵是否对称的 JavaScript 程序

对称矩阵是矩阵的特殊情况,其中矩阵和矩阵的转置都相同。矩阵是一组以矩形形式存储的整数或数字,相当于二维数组,矩阵的转置也是将所有行替换为列得到的矩阵。我们将得到一个矩阵,并且必须打印它是否是对称矩阵。

输入

Mat = [[1, 2, 3],
	   [2, 3, 8],
	   [3, 8, 0]]

输出

Yes, the given matrix is the symmetric matrix. 

说明

众所周知,转置矩阵是将列替换为行、将行替换为列的矩阵,因此这里第一行与第一列相同,第二行与列相同,第三行与列相同也是一样的。

输入

Mat = [[1, 2, 3],
	   [2, 3, 9],
	   [3, 8, 0]]

输出

No, the given matrix is not a symmetric matrix. 

说明

在给定的矩阵中,转置矩阵将是 -

Trans: [[1, 2, 3],
	    [2, 3, 8],
	    [3, 9, 0]]

我们可以看到第二行和第三行或者第二列和第三列不一样。

注意 - 正如我们所见,给定矩阵的转置可以通过交换行和列来形成,这意味着如果矩阵的维度为 N*M,则矩阵的维度为转置矩阵将为 M*N。这意味着要使矩阵成为对称矩阵,N 必须等于 M,从而得到方阵。

天真的方法

在这种方法中,我们首先通过创建一个新矩阵并按行列存储元素来获取转置矩阵。然后我们将简单地遍历两个矩阵并比较它们。如果它们在任何索引处不匹配,那么我们将返回 false,否则我们将返回 true。

示例

// function to find the transpose of the given matrix
function getTranspose(mat){   
 
   // getting the number of rows present in the given matrix. 
   var n = mat.length; 
   
   // getting the number of columns present in the given matrix. 
   var m = mat.length;   
   
   // creating a new array to store the transpose matrix 
   
   // new array will have m rows and n columns 
   var transP = new Array(m) 
   
   // traversing over the given matrix column-wise 
   for(var i = 0;i < m; i++){
      var cur = new Array(n);
      for(var j = 0; j<n; j++){
         cur[j] = mat[j][i];
      }
      transP[i] = cur;
   }
   
   // returing tranpose of the given matrix 
   return transP;
}

// function to check if the given matrix is symmetric or not
function check(mat){
   var n = mat.length;
   var m = mat[0].length;
   
   // matrix must be a square matrix 
   if(n != m){
      return false;
   }    
   
   // getting tranpose of the given matrix 
   var transP = getTranspose(mat);  
   
   // checking if both matrices are equal
   for(var i = 0; i<n ;i++){
      for(var j = 0; j<n ;j++){
         if(mat[i][j] != transP[i][j]){
            return false;
         }
      }
   }
   return true;
}

// defining the matrix 
var mat = [[1, 2, 3],
           [2, 3, 8],
           [3, 8, 0]]
console.log("The given matrix is: ")
console.log(mat);
if(check(mat)){
   console.log("The given matrix is a symmetric matrix")
}
else{
   console.log("The given matrix is not a symmetric matrix")
}

时间和空间复杂度

上述代码的时间复杂度为 O(N*N),其中 N 是给定矩阵的大小。

上述代码的空间复杂度为 O(N*N),因为我们使用额外的空间来存储转置矩阵元素。

高效的方法

转置矩阵可以通过交换行和列得到,即每一列等于对应的行。因此,任何索引 (i,j) 处的值都将等于给定矩阵中 (j,i) 处的值。

示例

// function to check if the given matrix is symmetric or not
function check(mat){
   var n = mat.length;
   var m = mat[0].length;  
   
   // matrix must be a square matrix 
   if(n != m){
      return false;
   }  
   
   // checking if mat[i][j] is equal to mat[j][i] or not
   for(var i = 0; i<n ;i++){
      for(var j = 0; j<i ;j++){
         if(mat[i][j] != mat[j][i]){
            return false;
         }
      }
   }
   return true;
}

// defining the matrix 
var mat = [[1, 2, 3],
           [2, 3, 8],
           [3, 9, 0]]
console.log("The given matrix is: ")
console.log(mat);
if(check(mat)){
   console.log("The given matrix is a symmetric matrix")
}
else{
   console.log("The given matrix is not a symmetric matrix")
}

时间和空间复杂度

上述代码的时间复杂度为 O(N*N),其中 N 是给定矩阵的大小。

上述代码的空间复杂度为 O(1),因为我们没有使用任何额外的空间。

结论

在上面的教程中,我们实现了一段 JavaScript 代码来查找给定的矩阵是否是对称矩阵。对称矩阵是矩阵的特殊情况,其中矩阵和矩阵的转置都相同,并且矩阵的转置可以通过交换行和列来获得。矩阵必须是方阵才能成为对称矩阵。我们实现了两种方法,时间复杂度为 O(N*N),空间复杂度为 O(N*N),空间复杂度为 O(1)。

以上就是检查矩阵是否对称的 JavaScript 程序的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除