00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <fstream>
00023 using namespace std;
00024
00025
00026 #include <SawMatrix.h>
00027 #include <SawVector.h>
00028 #include <SawVectorList.h>
00029 #include <SawUtils.h>
00030
00031 int main(int argc, char *argv[]){
00032
00033 cout << "****************************************************" << endl ;
00034 cout << "* *" << endl ;
00035 cout << "* MATRIX MANIPULATION *" << endl ;
00036 cout << "* *" << endl ;
00037 cout << "* Nestor Aguirre 2006 *" << endl ;
00038 cout << "* nfaguirrec@unal.edu.co *" << endl ;
00039 cout << "* *" << endl ;
00040 cout << "****************************************************" << endl ;
00041 cout << endl ;
00042
00043 cout << "---------------------------------------" << endl ;
00044 cout << " Initializing to constant value " << endl ;
00045 cout << "---------------------------------------" << endl ;
00046 SawMatrix matrix1(4, 4, 2.36) ;
00047 cout << "A = " << matrix1 << endl ;
00048 cout << endl ;
00049
00050 cout << "-------------------------------" << endl ;
00051 cout << " Getting data of C array " << endl ;
00052 cout << "-------------------------------" << endl ;
00053 double mat[] = { 2.350, 0.156, 4.521, 2.534,
00054 0.789, 5.423, 6.156, 5.123,
00055 5.489, 6.464, 1.453, 6.156,
00056 5.156, 2.456, 6.156, 9.156 } ;
00057 SawMatrix matrix2( mat, 4, 4 ) ;
00058 cout << "B = " << matrix2 << endl ;
00059 cout << endl ;
00060
00061 cout << "---------------------------------------" << endl ;
00062 cout << " Probando el constructor de copia " << endl ;
00063 cout << "---------------------------------------" << endl ;
00064 SawMatrix matrix3( matrix2 ) ;
00065 cout << "C = "<< matrix3 << endl ;
00066 cout << endl ;
00067
00068 cout << "----------------------------" << endl ;
00069 cout << " Probando el operador == " << endl ;
00070 cout << "----------------------------" << endl ;
00071 cout << "C == B ? " << ( (matrix3 == matrix2) ? "TRUE" : "FALSE" ) << endl ;
00072 cout << "A == B ? " << ( (matrix1 == matrix2) ? "TRUE" : "FALSE" ) << endl ;
00073 cout << endl ;
00074
00075 cout << "-------------------------------------------" << endl ;
00076 cout << " Obteniendo el elemento maximo y minimo " << endl ;
00077 cout << "-------------------------------------------" << endl ;
00078 uint i, j ;
00079 double max = matrix3.getMax( &i, &j ) ;
00080 cout << "MAX(C): vec[" << i << "][" << j << "] = " << max << endl ;
00081 double min = matrix3.getMin( &i, &j ) ;
00082 cout << "MIN(C): vec[" << i << "][" << j << "] = " << min << endl ;
00083 cout << endl ;
00084
00085 cout << "------------------------------------------" << endl ;
00086 cout << " Probando el operador de asignacion = " << endl ;
00087 cout << "------------------------------------------" << endl ;
00088 SawMatrix matrix4( 4, 4 ) ;
00089 matrix4 = matrix3 ;
00090 cout << "D = " << matrix4 << endl ;
00091 cout << endl ;
00092
00093 cout << "--------------------------------------------" << endl ;
00094 cout << " Probando intercambiar columnas 1 <--> 3 " << endl ;
00095 cout << "--------------------------------------------" << endl ;
00096 matrix4.swapColumns( 1, 3 ) ;
00097 cout << "D_{1c,3c} = " << matrix4 << endl ;
00098 cout << endl ;
00099
00100 cout << "-----------------------------------------" << endl ;
00101 cout << " Probando intercambiar filas 1 <--> 3 " << endl ;
00102 cout << "-----------------------------------------" << endl ;
00103 matrix4.swapRows( 1, 3 ) ;
00104 cout << "D_{1r,3r} = " << matrix4 << endl ;
00105 cout << endl ;
00106
00107 cout << "-------------------------------" << endl ;
00108 cout << " Probando sumar dos matrices " << endl ;
00109 cout << "-------------------------------" << endl ;
00110 cout << "A = " << matrix3 << endl ;
00111 cout << "B = " << matrix4 << endl ;
00112 cout << "A+B = " << matrix3 + matrix4 << endl ;
00113 cout << endl ;
00114
00115 cout << "--------------------------------" << endl ;
00116 cout << " Probando restar dos matrices " << endl ;
00117 cout << "--------------------------------" << endl ;
00118 cout << "A = " << matrix3 << endl ;
00119 cout << "B = " << matrix4 << endl ;
00120 cout << "A-B = " << matrix3 - matrix4 << endl ;
00121 cout << endl ;
00122
00123 cout << "--------------------------------------------------" << endl ;
00124 cout << " Probando multiplicar un escalar con una matriz " << endl ;
00125 cout << "--------------------------------------------------" << endl ;
00126 cout << "A = " << matrix4 << endl ;
00127 cout << "B = " << 0.456 << endl << endl ;
00128 cout << "A*B = " << matrix4*0.456 << endl ;
00129 cout << "B*A = " << 0.456*matrix4 << endl ;
00130 cout << endl ;
00131
00132 cout << "------------------------------------" << endl ;
00133 cout << " Obteniendo la matriz transpuesta " << endl ;
00134 cout << "------------------------------------" << endl ;
00135 cout << "A^t = " << matrix4.getTransPose() << endl ;
00136 cout << endl ;
00137
00138 cout << "-----------------------------------------" << endl ;
00139 cout << " Obteniendo los datos desde un stream " << endl ;
00140 cout << "-----------------------------------------" << endl ;
00141 SawMatrix matrix5( 4, 4 ) ;
00142 ifstream file("matrix.data") ;
00143 file >> matrix5 ;
00144 file.close() ;
00145 cout << matrix5 << endl ;
00146
00147 cout << "-------------------------------" << endl ;
00148 cout << " Multiplicando dos matrices " << endl ;
00149 cout << "-------------------------------" << endl ;
00150
00151 double dataA[] = { 0.116, 0.126, 0.138,
00152 0.213, 0.223, 0.235,
00153 0.566, 6.456, 8.321,
00154 6.546, 9.156, 0.456 } ;
00155
00156 double dataB[] = { 1.011, 7.012,
00157 6.216, 8.022,
00158 2.031, 5.032 } ;
00159
00160 SawMatrix A( dataA, 4, 3 ) ;
00161 SawMatrix B( dataB, 3, 2 ) ;
00162
00163 cout << "A = " << A << endl ;
00164 cout << "B = " << B << endl ;
00165 cout << "A*B = " << A*B << endl ;
00166
00167 cout << "------------------------------------------------------------" << endl ;
00168 cout << " Obteniendo los valores y vectores propios de una matriz " << endl ;
00169 cout << "------------------------------------------------------------" << endl ;
00170
00171 SawMatrix matrix6( mat, 4, 4 ) ;
00172 ifstream file2("matrix.data") ;
00173 file2 >> matrix6 ;
00174 file2.close() ;
00175
00176 SawVector eigenValues(4) ;
00177 SawVectorList eigenVectors(4, 4) ;
00178 eigenValues = matrix6.getEigenValues( &eigenVectors ) ;
00179
00180 cout << "A = " << matrix6 << endl ;
00181
00182 cout << " Valores propios " << endl ;
00183 cout << "-------------------" << endl ;
00184 SawUtils::writeWithFormatMapStyle( cout, eigenValues, eigenVectors, 10 ) ;
00185 cout << endl ;
00186
00187 cout << "---------------------------------------------------" << endl ;
00188 cout << " Calculando la matriz inversa y el determinante " << endl ;
00189 cout << "---------------------------------------------------" << endl ;
00190 cout << "A = " << matrix6 << endl ;
00191 SawMatrix invers(4,4) ;
00192 invers = matrix6.getInverse() ;
00193 cout << "A^(-1) = " << invers << endl ;
00194 cout << "A*A^(-1) = " << matrix6*invers << endl ;
00195 cout << "Det(A) = " << matrix6.getDeterminant() << endl ;
00196
00197 return EXIT_SUCCESS;
00198 }