• Main Page
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

Utilities/extra.h

Go to the documentation of this file.
00001 /************************************************************************************
00002     Copyright (C) 2005-2008 Assefaw H. Gebremedhin, Arijit Tarafdar, Duc Nguyen,
00003     Alex Pothen
00004 
00005     This file is part of ColPack.
00006 
00007     ColPack is free software: you can redistribute it and/or modify
00008     it under the terms of the GNU Lesser General Public License as published
00009     by the Free Software Foundation, either version 3 of the License, or
00010     (at your option) any later version.
00011 
00012     ColPack is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public License
00018     along with ColPack.  If not, see <http://www.gnu.org/licenses/>.
00019 ************************************************************************************/
00020 
00021 #ifndef EXTRA_H
00022 #define EXTRA_H
00023 
00024 #include <iostream>
00025 #include <fstream>
00026 #include <sstream>
00027 #include <string>
00028 #include <iomanip>
00029 #include <ctime>
00030 #include <cstdlib>
00031 //#include <cctype> //for toupper()
00032 
00033 #include <list>
00034 #include <map>
00035 #include <string>
00036 #include <vector>
00037 
00038 using namespace std;
00039 
00040 /*
00041 #include "Definitions.h"
00042 #include "Pause.h"
00043 */
00044 
00046 
00050 int ReadRowCompressedFormat(string s_InputFile, unsigned int *** uip3_SparsityPattern, int& rowCount, int& columnCount);
00051 
00053 
00057 bool isValidOrdering(vector<int> & ordering, int offset = 0);
00058 
00059 //Re-order the values randomly
00060 void randomOrdering(vector<int>& ordering);
00061 
00063 string toUpper(string input);
00064 
00066 
00079 int RowCompressedFormat_2_SparseSolversFormat_StructureOnly(unsigned int ** uip2_HessianSparsityPattern, unsigned int ui_rowCount, unsigned int** ip2_RowIndex, unsigned int** ip2_ColumnIndex);
00080 
00082 
00089 void ConvertDIMACSFormat2MatrixMarketFormat(string fileNameNoExt);
00090 
00092 
00096 int ConvertMatrixMarketFormatToRowCompressedFormat(string s_InputFile, unsigned int *** uip3_SparsityPattern, double*** dp3_Value, int &rowCount, int &columnCount);
00097 
00099 
00101 int MatrixMultiplication_VxS(unsigned int ** uip3_SparsityPattern, double** dp3_Value, int rowCount, int columnCount, double** dp2_seed, int colorCount, double*** dp3_CompressedMatrix);
00102 
00104 
00106 int MatrixMultiplication_SxV(unsigned int ** uip3_SparsityPattern, double** dp3_Value, int rowCount, int columnCount, double** dp2_seed, int colorCount, double*** dp3_CompressedMatrix);
00107 
00109 
00113 bool CompressedRowMatricesREqual(double** dp3_Value, double** dp3_NewValue, int rowCount, bool compare_exact = 1, bool print_all = 0);
00114 
00116 int Times2Plus1point5(double** dp2_Values, int i_RowCount, int i_ColumnCount);
00117 
00119 int Times2(double** dp2_Values, int i_RowCount, int i_ColumnCount);
00120 
00122 int GenerateValues(unsigned int ** uip2_SparsityPattern, int rowCount, double*** dp3_Value);
00123 
00125 int GenerateValuesForSymmetricMatrix(unsigned int ** uip2_SparsityPattern, int rowCount, double*** dp3_Value);
00126 
00127 #ifndef EXTRA_H_TEMPLATE_FUNCTIONS
00128 #define EXTRA_H_TEMPLATE_FUNCTIONS
00129 
00131 template<class T>
00132 int diffArrays(T* array1, T* array2, int rowCount, bool compare_exact = 1, bool print_all = 0) {
00133         double ratio = 0.;
00134         int none_equal_count = 0;
00135         for(int i = 0; i < rowCount; i++) {
00136           if (compare_exact) {
00137             if(array1[i]!=array2[i]) { // found a difference
00138               cout<<"At index i="<<i<<"\t array1[] = "<<array1[i]<";\t array2[] = "<<array2[i]<<endl;
00139               none_equal_count++;
00140               if(!print_all) return 1;
00141             }
00142           }
00143           else {
00144             ratio = array1[i] / array2[i];
00145             if(ratio < .99 || ratio > 1.02) { // found a difference
00146               cout<<"At index i="<<i<<"\t array1[] = "<<array1[i]<";\t array2[] = "<<array2[i]<<endl;
00147               none_equal_count++;
00148               if(!print_all) return 1;
00149             }
00150           }
00151         }
00152         
00153         return none_equal_count;
00154 }
00155 
00157 template<class T>
00158 int diffVectors(vector<T> array1, vector<T> array2, bool compare_exact = 1, bool print_all = 0) {
00159         double ratio = 0.;
00160         int none_equal_count = 0;
00161         
00162         if(array1.size() != array2.size()) {
00163           cout<<"array1.size() "<<array1.size()<<" != array2.size()"<<array2.size()<<endl;
00164           none_equal_count++;
00165         }
00166         
00167         int min_array_size = (array1.size() < array2.size())?array1.size():array2.size();
00168         
00169         for(int i = 0; i < min_array_size; i++) {
00170           if (compare_exact) {
00171             if(array1[i]!=array2[i]) { // found a difference
00172               cout<<"At index i="<<i<<"\t array1[] = "<<array1[i]<<";\t array2[] = "<<array2[i]<<endl;
00173               none_equal_count++;
00174               if(!print_all) return none_equal_count;
00175             }
00176           }
00177           else {
00178             ratio = array1[i] / array2[i];
00179             if(ratio < .99 || ratio > 1.02) { // found a difference
00180               cout<<"At index i="<<i<<"\t array1[] = "<<array1[i]<<";\t array2[] = "<<array2[i]<<endl;
00181               none_equal_count++;
00182               if(!print_all) return none_equal_count;
00183             }
00184           }
00185         }
00186         
00187         return none_equal_count;
00188 }
00189 
00190 template<class T>
00191 int deleteMatrix(T** xp2_matrix, int rowCount) {
00192 //cout<<"IN deleteM 2"<<endl<<flush;
00193 //printf("* deleteMatrix rowCount=%d \n",rowCount);
00194 //Pause();
00195         for(int i = 0; i < rowCount; i++) {
00196 //printf("delete xp2_matrix[%d][0] = %7.2f \n", i, (float) xp2_matrix[i][0]);
00197                 delete xp2_matrix[i];
00198         }
00199 //cout<<"MID deleteM 2"<<endl<<flush;
00200         delete xp2_matrix;
00201 //cout<<"OUT deleteM 2"<<endl<<flush;
00202         return 0;
00203 }
00204 
00205 template<class T>
00206 int deleteMatrix(T*** xp3_matrix, int rowCount) {
00207 //cout<<"IN deleteM 3"<<endl<<flush;
00208         deleteMatrix(*xp3_matrix,rowCount);
00209 //cout<<"MID deleteM 3"<<endl<<flush;
00210         delete xp3_matrix;
00211 //cout<<"OUT deleteM 3"<<endl<<flush;
00212         return 0;
00213 }
00214 template<class T>
00215 void displayCompressedRowMatrix(T** xp2_Value, int rowCount, bool structureOnly = false) {
00216         unsigned int estimateColumnCount = 30;
00217         cout<<setw(4)<<"["<<setw(3)<<"\\"<<"]";
00218         if(structureOnly) {
00219                 for(unsigned int j=0; j < estimateColumnCount; j++) cout<<setw(4)<<j;
00220         }
00221         else {
00222                 for(unsigned int j=0; j < estimateColumnCount; j++) cout<<setw(9)<<j;
00223         }
00224         cout<<endl;
00225 
00226         for(unsigned int i=0; i < (unsigned int)rowCount; i++) {
00227                 cout<<setw(4)<<"["<<setw(3)<<i<<"]";
00228                 unsigned int numOfNonZeros = (unsigned int)xp2_Value[i][0];
00229                 if(structureOnly) {
00230                         for(unsigned int j=0; j <= numOfNonZeros; j++) {
00231                           if (j==0) printf("  (%d)",(int)xp2_Value[i][j]);
00232                           else printf("  %d",(int)xp2_Value[i][j]);
00233                         }
00234                 }
00235                 else {
00236                         //for(unsigned int j=0; j <= numOfNonZeros; j++) cout<<setw(8)<<xp2_Value[i][j];
00237                         for(unsigned int j=0; j <= numOfNonZeros; j++) {
00238                           if(j==0) printf("  (%7.2f)",(float)xp2_Value[i][j]);
00239                           else printf("  %7.2f",(float)xp2_Value[i][j]);
00240                         }
00241                 }
00242                 cout<<endl<<flush;
00243         }
00244         cout<<endl<<endl;
00245 }
00246 
00247 template<class T>
00248 void displayMatrix(T** xp2_Value, int rowCount, int columnCount, bool structureOnly = false) {
00249         cout<<setw(4)<<"["<<setw(3)<<"\\"<<"]";
00250         if(structureOnly) {
00251                 for(unsigned int j=0; j < (unsigned int)columnCount; j++) cout<<setw(3)<<j;
00252         }
00253         else {
00254                 for(unsigned int j=0; j < (unsigned int)columnCount; j++) cout<<setw(9)<<j;
00255         }
00256         cout<<endl;
00257 
00258         for(unsigned int i=0; i < (unsigned int)rowCount; i++) {
00259                 cout<<setw(4)<<"["<<setw(3)<<i<<"]";
00260                 if(structureOnly) {
00261                         for(unsigned int j=0; j < (unsigned int)columnCount; j++) cout<<setw(3)<<(bool)xp2_Value[i][j];
00262                 }
00263                 else {
00264                         for(unsigned int j=0; j < (unsigned int)columnCount; j++) printf("  %7.2f",(float)xp2_Value[i][j]);
00265                         //for(unsigned int j=0; j < (unsigned int)columnCount; j++) cout<<setw(8)<<xp2_Value[i][j];
00266                 }
00267                 cout<<endl<<flush;
00268         }
00269         cout<<endl<<endl;
00270 }
00271 
00272 template<class T>
00273 void displayVector(T* xp2_Value, int size, bool structureOnly = false) {
00274         if(structureOnly) {
00275                 for(unsigned int i=0; i < (unsigned int)size; i++) {
00276                         cout<<setw(4)<<"["<<setw(3)<<i<<"]";
00277                         cout<<setw(3)<<(bool)xp2_Value[i];
00278                         cout<<endl<<flush;
00279                 }
00280         }
00281         else {
00282                 for(unsigned int i=0; i < (unsigned int)size; i++) {
00283                         cout<<setw(4)<<"["<<setw(3)<<i<<"]";
00284                         printf("  %7.2f",(float)xp2_Value[i]);
00285                         //cout<<setw(8)<<xp2_Value[i];
00286                         cout<<endl<<flush;
00287                 }
00288         }
00289         cout<<endl<<endl;
00290 }
00291 
00293 template<class T>
00294 void displayAdjacencyMatrix(vector< vector<T> > &xp2_Value, bool structureOnly = false) {
00295         unsigned int estimateColumnCount = 30;
00296         cout<<setw(4)<<"["<<setw(3)<<"\\"<<"]";
00297         if(structureOnly) {
00298                 for(unsigned int j=0; j < estimateColumnCount; j++) cout<<setw(3)<<j;
00299         }
00300         else {
00301                 for(unsigned int j=0; j < estimateColumnCount; j++) cout<<setw(9)<<j;
00302         }
00303         cout<<endl;
00304 
00305         unsigned int rowCount = xp2_Value.size();
00306         for(unsigned int i=0; i < rowCount; i++) {
00307                 cout<<setw(4)<<"["<<setw(3)<<i<<"]";
00308                 unsigned int numOfNonZeros = (int)xp2_Value[i].size();
00309                 cout<<"("<<setw(5)<<numOfNonZeros<<")";
00310                 if(structureOnly) {
00311                         for(unsigned int j=0; j < numOfNonZeros; j++) cout<<setw(3)<<(bool)xp2_Value[i][j];
00312                 }
00313                 else {
00314                         for(unsigned int j=0; j < numOfNonZeros; j++) cout<<setw(9)<<xp2_Value[i][j];
00315                 }
00316                 cout<<endl<<flush;
00317         }
00318         cout<<endl<<endl;
00319 }
00320 
00321 
00322 #endif //EXTRA_H_TEMPLATE_FUNCTIONS
00323 
00324 #endif //EXTRA_H
00325 
00326 
00327 

Generated on Tue Sep 7 2010 15:28:13 for ColPack by  doxygen 1.7.1