Transpose of sparse matrices

Source: Internet
Author: User

Sparse matrix: M*n Matrix, the number of valid values in the matrix is much smaller than the number of invalid values, and the distribution of these data is not regular

As shown in the following:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7F/F4/wKioL1cy2Eni9GIzAAAVBBaVJqE589.png "title=" image% 20[1].png "alt=" Wkiol1cy2eni9gizaaavbbavjqe589.png "/>

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

In general, we will think as long as the exchange of the corresponding row and column, but this practice is a waste of time and space, so we can use ternary group storage, compressed storage of very few valid data, using {Row,col,value} ternary group to store each valid data, ternary group according to the original matrix position, Store in order of priority.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7F/F4/wKioL1cy2mjiVU-gAABCJ30TEvM602.png "title=" image% 20[3].png "alt=" Wkiol1cy2mjivu-gaabcj30tevm602.png "/>

#define  _CRT_SECURE_NO_WARNINGS 1#pragma once#include<vector> #include <iostream> using namespace std;template<class t>struct triple  //Define ternary Group {int _row; int _col; t  _value; Triple (Int row, int col, t& value): _row (Row),  _col (col),  _value (value) { }triple (): _row (0),  _col (0),  _value (0) {}};template<class t>class sparsematrix{public: Sparsematrix (T* a, int m, int n, const t& invalid)//invalid is an illegal value: _ Rowsize (M),  _colsize (n),  _invaild (invalid) {for  (int i = 0; i <  m; ++i) {for  (int j = 0; j < n; j++) {if  (a[i*n  + j] != invalid) {triple<t> tmp (i, j, a[i*n + j]); _a.push_back ( TMP);}}} Sparsematrix (size_t rowsize, size_t colsize, t invaild): _rowsize(rowsize), _colsize (colsize), _invaild (invaild) {}void display (t* a, int m, int n,  const t& invalid)   //Print sparse matrix {int p = 0;for  (int i =  0; i < m; ++i) {for  (int j = 0; j < n;  J + +) {if  (p < _a.size ()  && _a[p]._row == i&&_a[p]._col &NBSP;==&NBSP;J) {cout << _a[p]._value <<  " ";p + +;} else{cout << invalid <<  " ";}} Cout << endl;}} Sparsematrix<t> transport ()   //reversal matrix { //Be sure to keep line precedence sparsematrix<t> sm (_colsize ,  _rowsize, _invaild);for  (size_t i = 0; i < _colsize; i+ +) {size_t index = 0;while  (Index < _a.size ()) {if  (_a[index]._col ==  i) {TRIPLE&LT;T&GT;&NBSP;MM;MM._COL&NBSP;=&NBsp;_a[index]._row;mm._row = _a[index]._col;mm._value = _a[index]._value;sm._a.push_back ( mm);} ++index;}} RETURN&NBSP;SM;} Sparsematrix<t> fasttransport ()    //quick Transpose {sparsematrix<t> temp;temp._ A.resize (_a.size ()); int* rowcounts = new int[_col];int* rowstarts = new  int[_col];memset (rowcounts, 0, sizeof ((int) *_col)) memset (rowstarts, 0, sizeof (int) *_col));size_t index = 0;while  (Index < _a.size ()) {rowcounts[_a[index]._col]+ +;++index;} rowstarts[0] = 0;for  (size_t i = 0; i < _col; ++i) { ROWSTARTS[I]&NBSP;=&NBSP;ROWSTARTS[I&NBSP;-&NBSP;1]&NBSP;+&NBSP;ROWCOUNTS[I&NBSP;-&NBSP;1];} while  (Index < _a.size ()) {size_t& begin = rowstarts[_a[index]._col]; triple<t> tp;tp._row = _a[index]._col;tp._col = _a[index]._row;tp._value =  _A[index]._value;tmp._a[rowstarts++] = tp;++index;} Delete[] _a;return tmp;}  protected:size_t _rowsize;size_t _colsize; t _invaild;vector<triple<t>> _a;}; The test code is as follows: Void test () {int a[6][5] ={{ 1, 0, 3, 0, 5 },{ 0,  0, 0, 0, 0 },{ 0, 0, 0, 0, 0 },{ 2, 0,  4, 0, 6 },{ 0, 0, 0, 0, 0 },{ 0, 0, 0, 0,  0 }}; Sparsematrix<int> d ((int*) a, 6, 5, 0); Sparsematrix<int> tmp = d.transport ();cout <<  "before transpose:"  <<  Endl;d.display ((int*) a, 6, 5, 0);cout << endl;cout <<  "after transpose:"  << endl;tmp.display ((int*) a, 5, 6, 0);} Int main () {test (); System ("pause"); return 0;}

The results of the operation are as follows:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7F/F7/wKiom1cy2rHwz2L3AAAj8DubTRA220.png "title=") s0t% BN_IPCDDIM6@UEYIEL.png "alt=" Wkiom1cy2rhwz2l3aaaj8dubtra220.png "/>

This article is from the "0-point Time" blog, please make sure to keep this source http://10741764.blog.51cto.com/10731764/1772214

Transpose of sparse matrices

Related Article

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.