Storage of one-dimensional/two-dimensional arrays and ordinary/symmetric/triangular/tridiagonal/sparse matrices


1 array

1.1 One-dimensional array

ElemType a[10]; //C语言中的一维数组,数据类型是ElemType

Insert picture description here
Each element in the array has the same size and is stored continuously when stored. The storage address of the array element a[i]=Loc+i*sizeof(ElemType), where 0≤i<10.


1.2 Two-dimensional array

ElemType b[2][4]; //C语言中2行4列的二维数组,数据类型是ElemType

The logical perspective of the above array is:
Insert picture description here
two-dimensional array has two storage methods, row-first storage and column-first storage, as follows:
Insert picture description here
For a two-dimensional array with M rows and N columns b[M][N], set the initial address of the two-dimensional array b to Loc, then :

(1) If storing by row priority, b[i][j]the storage address =Loc+(i*N+j)*sizeof(ElemType).

(2) If storing first by column, b[i][j]the storage address =Loc+(j*M+i)*sizeof(ElemType).


2 Matrix

2.1 Ordinary matrix

The following figure shows an abstract m×n order matrix: it
Insert picture description here
can be stored as the two-dimensional array in the section [2.2 Two-dimensional array]-row-first storage and column-first storage. It should be noted that when describing matrix elements, the row and column numbers usually start from 1; when describing arrays, the subscript usually starts from 0.


2.2 Special matrix

Note that this section and the following sections are all about square matrices .

2.2.1 Symmetric matrix

If any element a ij in the square matrix of order n has a ij = a ji , then the matrix is ​​a symmetric matrix. According to the ordinary storage method, that is, two-dimensional array storage, storage space is wasted.

The compression storage strategy of symmetric matrix: only the main diagonal and the lower triangle or the main diagonal and the upper triangle are stored.
Insert picture description here
In the above figure, the upper triangle is ①, i<j; the main diagonal is ②, i=j; the lower triangle is ③, i>j.

2.2.1.1 Strategy 1

Storage strategy: Only store the elements of the main diagonal + lower triangle, and store each element in a one-dimensional array according to the principle of row priority, so (1+n)*n/2 elements need to be stored. Storage is for use, so the storage method should be such that when accessing an element in the matrix, it can be found quickly. A mapping function can be implemented-the a[i][j]subscript of the matrix element is mapped to the subscript of a one-dimensional array b[k], where i and j start from 1 to n, and k from 0 to (1+n)*n/2- 1. Therefore, the a[i][j]elements of the matrix correspond to a one-dimensional array: ① Yes when i≥j b[i(i-1)/2+j-1]; ② Yes when i<j b[j(j-1)/2+i-1].

2.2.1.2 Strategy 2

Storage strategy: Only store the elements of the main diagonal + lower triangle, and store each element in a one-dimensional array according to the principle of column priority, so (1+n)*n/2 elements need to be stored. Storage is for use, so the storage method should be such that when accessing an element in the matrix, it can be found quickly. A mapping function can be implemented-the a[i][j]subscript of the matrix element is mapped to the subscript of a one-dimensional array b[k], where i and j start from 1 to n, and k from 0 to (1+n)*n/2- 1. Therefore, the a[i][j]elements of the matrix correspond to a one-dimensional array: ① Yes when i≥j b[(2n-j+2)(j-1)/2+i-j]; ② Yes when i<j b[(2n-i+2)(i-1)/2+j-i].


2.2.2 Triangular matrix

2.2.2.1 Upper triangular matrix

Upper triangular matrix: Except for the main diagonal and the upper triangular area, the rest of the elements are the same, as follows:
Insert picture description here
Storage strategy: The elements of the triangular area composed of the two shaded parts are stored in a one-dimensional array according to the row-first principle, and at the end One location stores the constant c, so (1+n)*n/2+1 elements need to be stored. A mapping function can be implemented—mapping the a[i][j]subscripts of matrix elements to the subscripts of a one-dimensional array b[k], where i and j start from 1 to n, and k from 0 to (1+n)*n/2. Therefore, the a[i][j]elements of the matrix correspond to a one-dimensional array: ① Yes when i≤j b[(2n-i+2)(i-1)/2+j-i]; ② Yes when i<j b[(1+n)*n/2].

2.2.2.2 Lower triangular matrix

Lower triangular matrix: Except for the main diagonal and the lower triangular area, the rest of the elements are the same, as follows:
Insert picture description here
Storage strategy: The elements of the triangular area composed of the two shaded parts are stored in a one-dimensional array according to the row-first principle, and at the end One location stores the constant c, so (1+n)*n/2+1 elements need to be stored. A mapping function can be implemented—mapping the a[i][j]subscripts of matrix elements to the subscripts of a one-dimensional array b[k], where i and j start from 1 to n, and k from 0 to (1+n)*n/2. Therefore, the a[i][j]elements of the matrix correspond to a one-dimensional array: ① Yes when i≥j b[i(i-1)/2+j-1]; ② Yes when i<j b[(1+n)*n/2].


2.2.3 Tridiagonal matrix

Tridiagonal matrix is ​​also called band matrix: when |ij|>1, there is a[i][j]=0, where 1≤i,j≤n.
Insert picture description here
Storage strategy: According to the principle of row priority, only the strip part is stored, so 3n-2 elements need to be stored. A mapping function can be implemented—mapping the a[i][j]subscripts of matrix elements to the subscripts of a one-dimensional array b[k], where i and j start from 1 to n, and k from 0 to 3n-3. Therefore, the a[i][j]elements of the matrix correspond to: ① When |ij|>1, it is 0; ② When |ij|≤1, it is a one-dimensional array b[2i+j-3].

There is a question, if the array index k is known, how to get the j and j of the matrix? b[k] is the k+1 element in the array, corresponding to the element in the i=⌈(k+2)/3⌉row and j=k-2i+3column of the matrix , and ⌈m⌉ is to round up m.


2.2.4 Sparse matrix

A sparse matrix is ​​a matrix in which the number of non-zero elements is far less than the number of matrix elements. In short, there are very many zeros. The following is an example:
Insert picture description here
Storage strategy 1: Use order to store triples <row, column, value>, as follows:
Insert picture description here
storage strategy 2: Use chain storage of triples <row, column, value>, that is, the cross-linked list method, where the nodes of non-zero data are as follows:
Insert picture description here
all the nodes are as follows: in the
Insert picture description here
above figure: the green is the right field, pointing to the i-th The first element of the row, red is the down field, pointing to the first element of the jth column.


END

Guess you like

Origin blog.csdn.net/qq_40061206/article/details/113773511