Compressed storage of symmetric matrices

Source: Internet
Author: User

Symmetric matrix: An element that corresponds to an equal matrix of the main diagonal axis

The symmetric matrix storage uses the upper triangle, the lower triangle storage, the lower triangle conforms to the i>=j (row is greater than equals the column), I use the lower triangle storage, the storage may store the lower triangle the element, but also needs to turn it to the symmetry matrix form printing when printing

constructor function:

Symmetricmatrix (t* a,size_t size): _a (New t[size* (size+1)/2]), _size (size* (size+1)/2), _n (size) {int index=0;for (size_ T i=0;i<size;i++) {for (size_t j=0;j<size;j++) {if (i>=j) {_a[index++]=a[i*size+j];} Elsebreak;}}}

Print matrix

void Display () {for (size_t i=0;i<_n;i++) {for (size_t j=0;j<_n;j++) {if (i>=j) {cout<<_a[i* (i+1)/2+j] << "";} else{cout<<_a[j* (j+1)/2+i]<< "";}} Cout<<endl;} Cout<<endl;}

Access to elements

t& Access (size_t i,size_t j) {if (i<j) {swap (i,j);} Return _a[i* (i+1)/2+j];}


Compressed storage of 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.