Symmetric matrix
Mainly share the compressed storage of the symmetric matrix in the special matrix. If there are some errors, please bear with me.
1>What is a symmetric matrix
A symmetric matrix is a special matrix : its data is symmetric along the diagonal (one by one symmetric along the diagonal)
This involves a concept of linear algebra. If the transposed matrix of a matrix is symmetrical to itself
At the same time, the symmetrical triangle symmetric matrix can be divided into upper triangle and lower triangle with the diagonal as the dividing line .
2>The necessity of compressed storage
The connection between matrix and array:
The matrix in linear algebra is equivalent to the array in the data structure, for example, A=[1,2,3,4,5,6] This is a 1x6 matrix and also a one-dimensional array. Similarly, there will be two-dimensional arrays and three-dimensional arrays up to n-dimensional arrays.
The symmetric matrix must satisfy the row=column, otherwise the symmetry condition cannot be satisfied
Storage method
In the computer, we will open up a one-dimensional array to store the first-order matrix (relatively simple and convenient), but as the order increases, if we open up the address space of the same dimension, the cost of system storage will be very high. . So we have to compress the matrix to store (compression here is not to lose data)
So in summary, we can get as much as possible to reduce the memory occupation and improve the utilization of the memory as much as possible, then for this special matrix can be processed to achieve the least memory to store (take the diagonal as the dividing line Storing half of the data can store the data of the entire matrix. According to the symmetry, the data of each position can be stored)
3>Analysis steps
Array occupied address = number of data * data type size (you can get the size of the data type through the sizeof function)
There are two ways to arrange a symmetric matrix: 1 Arrange according to row order (one row is full and go to the next row) 2 Arrange according to columns (one column is full and go to the next column)
Arrange according to rows ( assuming there is a matrix Dnxn, where the subscripts start from 1 )// The subscripts in the matrix mostly start from 1, and the arrays start from 0
Regardless of the data correspondence problem, we assume that all the xia triangle data are now placed in a one-dimensional array. So three factors need to be considered: the length of the array, how to locate the data (such as Aij), and how to express the lower triangle elements
Array length:
Need to count the number of all data in the upper triangle,
The first line a11 (1 data)
The second line a21-->a22 (2 data)
.....
N line an1--->ann (n data)
In summary, the total number of data in the lower triangle: 1+2+3.....+n=[n(n+1)/2]-1=maxsize, which is the total length of the one-dimensional array data we developed data[maxsize]
In the same way, the position of any point can be calculated
Positioning data (such as Aij)
The first line a11 (1 data)
The second line a21-->a22 (2 data)
.....
Line i-1 a(i-1)1--->a(i-1)n (i-1 data)
Position: 1 + 2 + 3 ..... + i-1 + j = [i (i-1) / 2] + j-1
So the positioning of the array maxsize and any position is completed
Lower triangle element expression
This can be based on the symmetry principle: Aij=Aji, then the expression and preservation of the lower triangle data can be completed
Similarly, if it is to store by column, just replace i in the formula with j.
4>Code implementation
The implementation here is the case where the rows are stored in the lower triangle
Define a 4th order matrix
int array[4][4] = {
{1,2,3,4},
{2,9,4,5},
{3,4,0,6},
{4,5,6,3},
};
printf("对称矩阵如下\n");
for (int i = 1; i <=4; i++)
{
for (int j = 1; j <=4; j++)
{
printf("a[%d][%d]=%d ", i, j, array[i-1][j-1]);
if (j == 4)
printf("\n");
}
}
Assign and output a one-dimensional array:
for (int k = 0; k < maxsize ; k++)
{
for (int i = 1; i <=4; i++)
{
for (int j = 1; j <=4; j++)
{
if (i*(i - 1) / 2 + j - 1 == k)
{
array1[k] = array[i - 1][j - 1];
}
}
}
}
printf("一维数组:");
for (int i = 0; i < maxsize; i++)
{
printf("%d ",array1[i]);
}