00001 // written by png shao wei 00002 00003 #include "PreCMatrix.h" 00004 00005 PreSparseMatrix::PreSparseMatrix() { 00006 value_a = 0; 00007 value_b = 0; 00008 } 00009 00010 PreSparseMatrix::PreSparseMatrix(int a, int b) { 00011 value_a = a; 00012 value_b = b; 00013 } 00014 00015 void PreSparseMatrix::addEntries(int a, int b, double c) { 00016 PreCEntry entry = PreCEntry(a, b, c); 00017 preCEntries.push_back(entry); 00018 } 00019 00020 void PreSparseMatrix::sortEntries() { 00021 stable_sort(preCEntries.begin(), preCEntries.end()); 00022 } 00023 00024 SharedPointer<SparseMatrix> PreSparseMatrix::convertSparseMatrix() { 00025 sortEntries(); 00026 removeDuplicates(); 00027 SharedPointer<SparseMatrix> temp (new SparseMatrix(value_a, value_b)); 00028 for (unsigned int i = 0; i < preCEntries.size(); i++) 00029 { 00030 temp->push_back(preCEntries[i].first, preCEntries[i].second,preCEntries[i].third); 00031 } 00032 temp->canonicalize(); // important to call this , if not printout will not work 00033 return temp; 00034 } 00035 00036 void PreSparseMatrix::removeDuplicates() { 00037 00038 if (preCEntries.size() > 0) { 00039 vector<PreCEntry> newCEntries; 00040 00041 for (unsigned int i = 0; i < preCEntries.size() - 1; i++) { 00042 if (!(preCEntries[i] == preCEntries[i + 1])) { 00043 newCEntries.push_back(preCEntries[i]); 00044 } 00045 } 00046 newCEntries.push_back(preCEntries[preCEntries.size() - 1]); 00047 00048 preCEntries = newCEntries; 00049 }//end of if 00050 00051 } 00052 00053 std::ostream& PreSparseMatrix::write(std::ostream& out) { 00054 out << value_a << " " << value_b << endl; 00055 for (unsigned int i = 0; i < preCEntries.size(); i++) 00056 out << preCEntries[i].first << " " << preCEntries[i].second << " " 00057 << preCEntries[i].third << endl; 00058 return out; 00059 }