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