Set a n*n square a,a any element a[i][j], when and only if a[i][j] = = A[j][i] (0 <= i <= N-1 && 0 <= J <=N-1), then matrix A is a symmetric matrix.
Separated by the diagonal of the matrix, it is divided into upper and lower triangles.
For example, symmetric matrix compression storage storage only needs to store the upper triangle/lower triangle of data, under normal circumstances, with the lower triangle storage, so the maximum storage n (n+1)/2 data.
The correspondence between symmetric matrices and compressed storage:
Lower triangle storage i>=j, Symmetricmatrix[i][j] = = array[i* (i+1)/2+j]
Template<class t>class Symmetricmatrix{public:symmetricmatrix (t* array, size_t n) {_arraysize = N (n + 1)/2; _size = n; _array = new T[_arraysize]; ASSERT (array); for (size_t i = 0, i < n; i++) {for (size_t j = 0; J < N; j + +) {_array [I* (i + 1)/2 + j] = Array[i*n + j]; }}} t& GetPos (size_t row, size_t col)//Get node {//If the position is in the upper triangle, using the symmetry principle, the row and column of the position can be exchanged I F (Row < col) {swap (row, col); } return _array[row* (row + 1)/2 + col]; } void Display ()//print {for (int i = 0; i < _size; i++) {for (int j = 0; J < _s Ize J + +) {if (I >= j) {cout << _array[i* (i + 1)/2 + j ] << ""; } else if (i<j) {cout << _array[j* (j +1)/2 + i] << ""; }} cout << Endl; } cout << Endl; }private:t *_array; Compression matrix size_t _size; Phalanx size _size*_size size_t _arraysize; The total size of the compression matrix};
Compression storage of symmetric matrices and symmetric matrices