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< /c10>), then matrix A is a symmetric matrix. Separated by the diagonal of the matrix, it is divided into upper and lower triangles.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7E/ED/wKiom1cMz5zzQr_rAAAb3l_RgBs093.png "title=" QQ picture 20160412183656.png "alt=" Wkiom1cmz5zzqr_raaab3l_rgbs093.png "/>

For example, symmetric matrix compression storage storage only need to store the upper triangle/lower triangle of data, under normal circumstances with the lower triangle storage, so up to 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]

#define  _CRT_SECURE_NO_WARNINGS 1#include<iostream> #include <assert.h>using  Namespace std;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 the upper triangle, using the symmetry principle, the row and column of the position can be exchanged if  (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 <  _size; 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*_sizesize_t _arraysize;   //The total size of the compression matrix};int main () {int matrix[][5] =     {{ 0, 1, 2, 3, 4 },{ 1, 0, 1, 2,  3 },{ 2, 1, 0, 1, 2 },{ 3, 2, 1, 0, 1 } ,{ 4, 3, 2, 1, 0 },}; Symmetricmatrix<int> mat (int *) Matrix,  5);   //establishes a compression matrix and initializes the Mat.display () Cout << mat with matrix matrices. GetPos (1, 2)  << endl;system ("pause"); return 0;}

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7E/ED/wKiom1cM0iOQjcDHAAANmmpQlQU829.png "title=" QQ picture 20160412184807.png "alt=" Wkiom1cm0ioqjcdhaaanmmpqlqu829.png "/>

This article from "Yan Anyang" blog, declined reproduced!

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.