C++程序:查找给定矩阵的迹和范数
许多应用程序都受益于二维数组或矩阵的使用。矩阵将数字存储在行和列中。在C++中,我们也可以使用多维数组来定义二维矩阵。在这篇文章中,我们将学习如何使用C++来找到给定矩阵的范数和迹。
范数是矩阵中所有元素个数的平方根。迹是所有主对角线元素的和。让我们看看C++代码中算法的表示。
矩阵迹
$\begin{bmatrix} 8 & 5& 3\newline 6 & 7& 1\newline 2 & 4& 9\newline \end{bmatrix},$
主对角线所有元素之和:(8 + 7 + 9) = 24,这是给定矩阵的迹
在前面的例子中,我们使用了 3x3 矩阵,结果是主对角线上元素的总和。迹可以通过求和得到。让我们看看算法来帮助我们理解。
算法
- 输入矩阵 M
- 假设M有n行n列
- sum := 0
- 对于 i 从 1 到 n,执行:
- sum := sum + M[ i ][ i ]
- 循环结束
- 返回 sum
示例
#include <iostream>
#include <cmath>
#define N 7
using namespace std;
float solve( int M[ N ][ N ] ){
int sum = 0;
// read elements through major diagonal, where row index and column index are same, both are i
for ( int i = 0; i < N; i++ ) {
sum = sum + M[ i ][ i ];
}
return sum;
}
int main(){
int mat1[ N ][ N ] = {
{5, 8, 74, 21, 69, 78, 25},
{48, 2, 98, 6, 63, 52, 3},
{85, 12, 10, 6, 9, 47, 21},
{6, 12, 18, 32, 5, 10, 32},
{8, 45, 74, 69, 1, 14, 56},
{7, 69, 17, 25, 89, 23, 47},
{98, 23, 15, 20, 63, 21, 56},
};
cout << "The Trace of the first matrix is: " << solve( mat1 ) << endl;
int mat2[ N ][ N ] = {
{6, 8, 35, 21, 87, 8, 26},
{99, 2, 36, 326, 25, 24, 56},
{15, 215, 3, 157, 8, 41, 23},
{96, 115, 17, 5, 3, 10, 18},
{56, 4, 78, 5, 10, 22, 58},
{85, 41, 29, 65, 47, 36, 78},
{12, 23, 87, 45, 69, 96, 12}
};
cout << "The Trace of the second matrix is: " << solve( mat2 ) << endl;
}
输出
The Trace of the first matrix is: 129 The Trace of the second matrix is: 74
矩阵范数
$\begin{bmatrix} 8 & 5& 3\newline 6 & 7& 1\newline 2 & 4& 9\newline \end{bmatrix},$
所有元素之和:(8 + 5 + 3 + 6 + 7 + 1 + 2 + 4 + 9) = 45
范数:(所有元素之和的平方根) = √45 = 6.708
在最后一个例子中,我们使用了一个 3x3 矩阵。我们首先计算所有元素的和,然后取其平方根。让我们看看算法来帮助我们理解。
算法
- 输入矩阵 M
- 假设M有n行n列
- sum 初始化为 0
- 对于 i 从 1 到 n,执行:
- 对于 j 从 1 到 n,执行:
- sum := sum + M[ i ][ j ]
- 循环结束
- 对于 j 从 1 到 n,执行:
- 循环结束
- res := sum 的平方根
- 返回 res
示例
#include <iostream>
#include <cmath>
#define N 7
using namespace std;
float solve( int M[ N ][ N ] ){
int sum = 0;
// go through each element. Using outer loop, access ith row, using inner loop access column. For cell (i, j) read the element and add it to the sum
for ( int i = 0; i < N; i++ ) {
for ( int j = 0; j < N; j++ ) {
sum = sum + M[ i ][ j ];
}
}
return sqrt( sum );
}
int main(){
int mat1[ N ][ N ] = {
{5, 8, 74, 21, 69, 78, 25},
{48, 2, 98, 6, 63, 52, 3},
{85, 12, 10, 6, 9, 47, 21},
{6, 12, 18, 32, 5, 10, 32},
{8, 45, 74, 69, 1, 14, 56},
{7, 69, 17, 25, 89, 23, 47},
{98, 23, 15, 20, 63, 21, 56},
};
cout << "The Normal of the first matrix is: " << solve( mat1 ) <<
endl;
int mat2[ N ][ N ] = {
{6, 8, 35, 21, 87, 8, 26},
{99, 2, 36, 326, 25, 24, 56},
{15, 215, 3, 157, 8, 41, 23},
{96, 115, 17, 5, 3, 10, 18},
{56, 4, 78, 5, 10, 22, 58},
{85, 41, 29, 65, 47, 36, 78},
{12, 23, 87, 45, 69, 96, 12}
};
cout << "The Normal of the second matrix is: " << solve( mat2 ) <<
endl;
}
输出
The Normal of the first matrix is: 41.1947 The Normal of the second matrix is: 49.4267
结论
范数和迹是矩阵运算。这两个运算都需要一个方阵(迹运算需要方阵)。迹是矩阵主对角线元素的总和,而范数只是矩阵中所有元素个数的平方根。在C++中,可以使用二维数组来表示矩阵。这里,我们以两个 5x5 的矩阵为例(总共 25 个元素)。需要通过循环语句和索引操作访问矩阵。由于要进行范数计算需要遍历每个元素,所以需要两个嵌套循环。这个程序的复杂度是 O(n2)。而迹运算只需要主对角线元素,因此行索引和列索引相同,所以只需要一个for循环。它的时间复杂度是 O(n)。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP