Compression storage of symmetric matrices and symmetric matrices

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.