Sparse Matrix and transpose

Source: Internet
Author: User

**************************************** * ******* Sparsematrix. h *************************************** ********

# Include <iostream>

Using namespace STD;

Const int maxnum= 100;

Template <class T>
Class truple
{
Public:
Int row;
Int Col;
T data;
};

Template <class T>
Class sparsematrix
{
Friend istream & operator> (istream & in, sparsematrix <t> & M) // overload the input Operator
{
Cout <"Enter the number of rows, number of columns, and number of non-zero elements:"; // initialize the number of rows, columns, and non-zero elements in the sparse matrix.
In> M. Row> M. Col> M. terms;
If (M. Terms> M. maxterms)
{
Cout <"error! Non-zero element overflow! ";
}
Else
{
For (INT I = 0; I <M. terms; I ++)
{
Cout <"Enter the" <I + 1 <"Number of non-zero elements, number of columns, and value of non-zero elements:"; // initialize the matrix by initializing the triplet
In> m. t [I]. Row> m. t [I]. Col> m. t [I]. Data;
While (M. T [I]. row> = m. row) | (M. T [I]. col> = m. COL) // matrix element subscript check
{
Cout <"error! The input matrix element subscript is out of bounds! Enter :";
In> m. t [I]. Row> m. t [I]. Col> m. t [I]. Data;
}
}
}
Return in;
}
Friend ostream & operator <(ostream & out, sparsematrix <t> & M) // reload the output Operator
{
Int I, j, k = 0;
Out <"row:" <M. Row <"column:" <M. Col <"Number of non-zero elements:" <M. terms <Endl;
/* For (INT I = 0; I <M. terms; I ++)
{
Out <"m [" <m. T [I]. row <"] [" <m. T [I]. col <"] =" <m. T [I]. data <Endl; // output the sparse matrix in the form of M [row] [column]
}*/
For (I = 0; I <M. Row; I ++)
{
For (j = 0; j <M. Col; j ++)
{
If (M. T [K]. Row = I) & (M. T [K]. Col = J ))
{
Out <m. t [k ++]. Data <"";
}
Else
{
Cout <'0' <"";
}
}
Out <Endl;
}

Return out;
}

PRIVATE:
Int row;
Int Col;
Int terms;
Truple <t> * t; // triple Array
Int maxterms; // The maximum number of elements.
Public:
Sparsematrix (INT maxsize = maxnum) // Constructor
{
Maxterms = maxsize;
Row = Col = terms = 0;
T = new truple <t> [maxsize];
}
~ Sparsematrix () // destructor
{
Delete [] T;
}
/* Sparsematrix <t> transposedmatrix () // matrix transpose function in the textbook. The logic is correct, but the runtime Memory reports an error.
{
Int * rowsize = new int [col]; // Number of non-zero elements in each row of the transpose matrix
Int * rowstart = new int [col]; // The storage location where the non-zero elements in each row of the transpose matrix start.
Sparsematrix <t> TP (maxterms); // stores the transposed Matrix
TP. Row = Col;
TP. Col = row;
TP. Terms = terms;
If (terms> 0)
{
Int I = 0;
Int J = 0;
For (I = 0; I <Col; I ++) // initialize rowsize
Rowsize = 0;
For (I = 0; I <terms; I ++)
Rowsize [T [I]. Col] ++;
Rowstart [0] = 0;
For (I = 1; I <maxterms; I ++) // initialize rowstart
Rowstart [I] = rowsize [I] + rowstart [I-1];
//************************************** ***
For (I = 0; I <Col; I ++)
Cout <rowsize [I] <"";
Cout <Endl;
For (I = 0; I <Col; I ++)
Cout <rowstart [I] <"";
Cout <Endl;
//************************************** ****
For (I = 0; I <terms; I ++)
{
J = rowstart [T [I]. Col]; // the row of the triple in the J transpose matrix
// Rowstart [T [I]. Col] ++;
TP. t [J]. Row = T [I]. Col;
TP. t [J]. Col = T [I]. row;
TP. t [J]. Data = T [I]. Data;
Rowstart [T [I]. Col] ++;
}
}
Delete [] rowsize;
Delete [] rowstart;
Return TP;
}*/
Void transposedmatrix (sparsematrix & TP) // self-compiled transpose matrix function
{
Int I, J;
If (terms> 0)
{
Int ROW = 0;
TP. Col = row; // initialize the number of rows, columns, and non-zero elements in the transpose matrix.
TP. Row = Col;
TP. Terms = terms;
For (j = 0; j <Col; j ++) // check from column 0th
{
For (I = 0; I <terms; I ++)
{
If (T [I]. Col = J) // check the columns of the original three tuples. If they are the same, the exchange rows and columns are assigned to the transpose matrix.
{
TP. t [row]. Row = T [I]. Col;
TP. t [row]. Col = T [I]. row;
TP. t [row]. Data = T [I]. Data;
Row ++;
}
}
}
}
}
/* Void displaytruple () // output the triple
{
For (INT I = 0; I <terms; I ++)
{
Cout <t [I];
}
Cout <Endl;
}*/
};

**************************************** * ** Sparsematrixmain. CPP *************************************** **********

# Include "stdafx. H"
# Include <iostream>
// # Include "truple. H"
# Include "sparsematrix. H"

Using namespace STD;

Int _ tmain (INT argc, _ tchar * argv [])
{
Sparsematrix <int> OBJ;
Sparsematrix <int> obj1;
Cin> OBJ;
// Obj. displaytruple (); // function used for testing to output the triple
Cout <"********************** initial matrix ************* * ******** "<Endl;
Cout <OBJ;
OBJ. transposedmatrix (obj1); // call your own transpose Function
// Obj. transposedmatrix (); // call the transpose function of the textbook.
Cout <"********************** transpose matrix ************* * ******** "<Endl;
Cout <obj1;
// Cout <OBJ;
Return 0;
}

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.