00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00040 #ifndef HEADER_TRANSFORM
00041 #define HEADER_TRANSFORM
00042
00043 #include <iostream>
00044 #include "matrix_typedefs.h"
00045 #include "matrix_typedefs_chtml.h"
00046
00047 typedef ergo_real real;
00048
00049
00050
00051 template<typename TYPE1, typename TYPE2>
00052 inline void transform_matrix_from_to(const TYPE1& A, TYPE2& B, const ParamsType& P)
00053 {
00054 throw std::runtime_error("Error in transform_matrix_from_to : it is not implemented for given template parameters.");
00055 }
00056
00057
00058
00059
00060 template<>
00061 inline void transform_matrix_from_to<symmMatrix, symmMatrix>
00062 (const symmMatrix& A, symmMatrix& B, const ParamsType& P)
00063 {
00064 B = A;
00065 }
00066
00067
00068 template<>
00069 inline void transform_matrix_from_to<normalMatrix, normalMatrix>
00070 (const normalMatrix& A, normalMatrix& B, const ParamsType& P)
00071 {
00072 B = A;
00073 }
00074
00075
00076 template<>
00077 inline void transform_matrix_from_to<triangMatrix, triangMatrix>
00078 (const triangMatrix& A, triangMatrix& B, const ParamsType& P)
00079 {
00080 B = A;
00081 }
00082
00083
00084 #ifdef USE_CHUNKS_AND_TASKS
00085
00086
00087
00088
00089
00090
00091 template<typename MatrixType, typename MatrixTypeWrapper>
00092 inline void get_sparse_matrix_data(const MatrixType& X,
00093 std::vector<int>& rows,
00094 std::vector<int>& cols,
00095 std::vector<real>& vals)
00096 {
00097 throw std::runtime_error("Error in transform.h : get_sparse_matrix_data is not implemented for a given template parameters.");
00098 }
00099
00100
00101 template<>
00102 inline void get_sparse_matrix_data<symmMatrix, chtml::CHTSymmMatrix<real, ParamsType> >(const symmMatrix& X,
00103 std::vector<int>& rows,
00104 std::vector<int>& cols,
00105 std::vector<real>& vals)
00106 {
00107 rows.clear();
00108 cols.clear();
00109 vals.clear();
00110 X.get_all_values(rows, cols, vals);
00111
00112 size_t count = 0;
00113 for (size_t i = 0; i < rows.size(); ++i)
00114 {
00115 if (vals[i] == 0)
00116 {
00117 continue;
00118 }
00119 rows[count] = rows[i];
00120 cols[count] = cols[i];
00121 vals[count] = vals[i];
00122 count++;
00123 }
00124
00125 rows.resize(count);
00126 cols.resize(count);
00127 vals.resize(count);
00128 }
00129
00130
00131 template<>
00132 inline void get_sparse_matrix_data<symmMatrix, chtml::CHTGeneralMatrix<real, ParamsType> >(const symmMatrix& X,
00133 std::vector<int>& rows,
00134 std::vector<int>& cols,
00135 std::vector<real>& vals)
00136 {
00137 rows.clear();
00138 cols.clear();
00139 vals.clear();
00140 X.get_all_values(rows, cols, vals);
00141
00142 size_t count = 0;
00143 for (size_t i = 0; i < rows.size(); ++i)
00144 {
00145 if (vals[i] == 0)
00146 {
00147 continue;
00148 }
00149 rows[count] = rows[i];
00150 cols[count] = cols[i];
00151 vals[count] = vals[i];
00152 count++;
00153 }
00154
00155
00156
00157
00158
00159 rows.resize(count);
00160 cols.resize(count);
00161 vals.resize(count);
00162 rows.reserve(count * 2);
00163 cols.reserve(count * 2);
00164 vals.reserve(count * 2);
00165
00166 size_t N = rows.size();
00167 for (size_t i = 0; i < N; ++i)
00168 {
00169 if (rows[i] != cols[i])
00170 {
00171 rows.push_back(cols[i]);
00172 cols.push_back(rows[i]);
00173 vals.push_back(vals[i]);
00174 count++;
00175 }
00176 }
00177 }
00178
00179
00180 template<>
00181 inline void get_sparse_matrix_data<triangMatrix, chtml::CHTTriangMatrix<real, ParamsType> >(const triangMatrix& X,
00182 std::vector<int>& rows,
00183 std::vector<int>& cols,
00184 std::vector<real>& vals)
00185 {
00186 rows.clear();
00187 cols.clear();
00188 vals.clear();
00189 X.get_all_values(rows, cols, vals);
00190
00191 size_t count = 0;
00192 for (size_t i = 0; i < rows.size(); ++i)
00193 {
00194 if (vals[i] == 0)
00195 {
00196 continue;
00197 }
00198 rows[count] = rows[i];
00199 cols[count] = cols[i];
00200 vals[count] = vals[i];
00201 count++;
00202 }
00203
00204 rows.resize(count);
00205 cols.resize(count);
00206 vals.resize(count);
00207 }
00208
00209
00210 template<>
00211 inline void get_sparse_matrix_data<normalMatrix, chtml::CHTGeneralMatrix<real, ParamsType> >(const normalMatrix& X,
00212 std::vector<int>& rows,
00213 std::vector<int>& cols,
00214 std::vector<real>& vals)
00215 {
00216 rows.clear();
00217 cols.clear();
00218 vals.clear();
00219 X.get_all_values(rows, cols, vals);
00220
00221 size_t count = 0;
00222 for (size_t i = 0; i < rows.size(); ++i)
00223 {
00224 if (vals[i] == 0)
00225 {
00226 continue;
00227 }
00228 rows[count] = rows[i];
00229 cols[count] = cols[i];
00230 vals[count] = vals[i];
00231 count++;
00232 }
00233
00234 rows.resize(count);
00235 cols.resize(count);
00236 vals.resize(count);
00237 }
00238
00239
00240 template<>
00241 inline void transform_matrix_from_to<symmMatrix, chtml::CHTSymmMatrix<real, ParamsType> >
00242 (const symmMatrix& A, chtml::CHTSymmMatrix<real, ParamsType>& B, const ParamsType& P)
00243 {
00244 B.set_matrix_params(P);
00245
00246 int n = A.get_nrows();
00247 int m = A.get_ncols();
00248
00249
00250 std::vector<int> rows;
00251 std::vector<int> cols;
00252 std::vector<real> vals;
00253 get_sparse_matrix_data<symmMatrix, chtml::CHTSymmMatrix<real, ParamsType> >(A, rows, cols, vals);
00254 B.create_CHT_matrix_from_sparse(rows, cols, vals);
00255 }
00256
00257
00258 template<>
00259 inline void transform_matrix_from_to<symmMatrix, chtml::CHTGeneralMatrix<real, ParamsType> >
00260 (const symmMatrix& A, chtml::CHTGeneralMatrix<real, ParamsType>& B, const ParamsType& P)
00261 {
00262 B.set_matrix_params(P);
00263
00264 int n = A.get_nrows();
00265 int m = A.get_ncols();
00266
00267
00268 std::vector<int> rows;
00269 std::vector<int> cols;
00270 std::vector<real> vals;
00271 get_sparse_matrix_data<symmMatrix, chtml::CHTGeneralMatrix<real, ParamsType> >(A, rows, cols, vals);
00272 B.create_CHT_matrix_from_sparse(rows, cols, vals);
00273 }
00274
00275
00276 template<>
00277 inline void transform_matrix_from_to<normalMatrix, chtml::CHTGeneralMatrix<real, ParamsType> >
00278 (const normalMatrix& A, chtml::CHTGeneralMatrix<real, ParamsType>& B, const ParamsType& P)
00279 {
00280 B.set_matrix_params(P);
00281
00282 int n = A.get_nrows();
00283 int m = A.get_ncols();
00284
00285
00286 std::vector<int> rows;
00287 std::vector<int> cols;
00288 std::vector<real> vals;
00289 get_sparse_matrix_data<normalMatrix, chtml::CHTGeneralMatrix<real, ParamsType> >(A, rows, cols, vals);
00290 B.create_CHT_matrix_from_sparse(rows, cols, vals);
00291 }
00292
00293
00294 template<>
00295 inline void transform_matrix_from_to<triangMatrix, chtml::CHTTriangMatrix<real, ParamsType> >
00296 (const triangMatrix& A, chtml::CHTTriangMatrix<real, ParamsType>& B, const ParamsType& P)
00297 {
00298 B.set_matrix_params(P);
00299
00300 int n = A.get_nrows();
00301 int m = A.get_ncols();
00302
00303
00304 std::vector<int> rows;
00305 std::vector<int> cols;
00306 std::vector<real> vals;
00307 get_sparse_matrix_data<triangMatrix, chtml::CHTTriangMatrix<real, ParamsType> >(A, rows, cols, vals);
00308 B.create_CHT_matrix_from_sparse(rows, cols, vals);
00309 }
00310
00311
00312
00313
00314
00315 template<typename MatrixTypeCHT, typename MatrixType>
00316 inline void set_sparse_matrix_from_data(MatrixType& X,
00317 const std::vector<int>& rows,
00318 const std::vector<int>& cols,
00319 const std::vector<real>& vals)
00320 {
00321 throw std::runtime_error("Error in transform_matrix_from_to : set_sparse_matrix_from_data is not implemented for a given template parameters.");
00322 }
00323
00324
00325 template<>
00326 inline void set_sparse_matrix_from_data<chtml::CHTGeneralMatrix<real, ParamsType>, symmMatrix>(symmMatrix& A,
00327 const std::vector<int>& rows,
00328 const std::vector<int>& cols,
00329 const std::vector<real>& vals)
00330 {
00331
00332 std::vector<int> rows1;
00333 rows1.resize(rows.size());
00334 std::vector<int> cols1;
00335 cols1.resize(cols.size());
00336 std::vector<real> vals1;
00337 vals1.resize(vals.size());
00338 size_t count = 0;
00339 for (size_t i = 0; i < rows.size(); ++i)
00340 {
00341 if (rows[i] > cols[i])
00342 {
00343 continue;
00344 }
00345 rows1[count] = rows[i];
00346 cols1[count] = cols[i];
00347 vals1[count] = vals[i];
00348 count++;
00349 }
00350 rows1.resize(count);
00351 cols1.resize(count);
00352 vals1.resize(count);
00353 A.assign_from_sparse(rows1, cols1, vals1);
00354 }
00355
00356
00357 template<>
00358 inline void set_sparse_matrix_from_data<chtml::CHTSymmMatrix<real, ParamsType>, symmMatrix>(symmMatrix& A,
00359 const std::vector<int>& rows,
00360 const std::vector<int>& cols,
00361 const std::vector<real>& vals)
00362 {
00363 A.assign_from_sparse(rows, cols, vals);
00364 }
00365
00366
00367 template<>
00368 inline void transform_matrix_from_to<chtml::CHTSymmMatrix<real, ParamsType>, symmMatrix>
00369 (const chtml::CHTSymmMatrix<real, ParamsType>& A, symmMatrix& B, const ParamsType& P)
00370 {
00371 int n, m, nB, mB;
00372
00373 std::vector<int> rows;
00374 std::vector<int> cols;
00375 std::vector<real> vals;
00376
00377 n = A.get_nrows();
00378 nB = B.get_nrows();
00379 assert(nB == n);
00380 m = A.get_ncols();
00381 mB = B.get_ncols();
00382 assert(mB == m);
00383 A.get_matrix(rows, cols, vals);
00384 set_sparse_matrix_from_data<chtml::CHTSymmMatrix<real, ParamsType>, symmMatrix>(B, rows, cols, vals);
00385 }
00386
00387
00388 template<>
00389 inline void transform_matrix_from_to<chtml::CHTGeneralMatrix<real, ParamsType>, symmMatrix>
00390 (const chtml::CHTGeneralMatrix<real, ParamsType>& A, symmMatrix& B, const ParamsType& P)
00391 {
00392 int n, m, nB, mB;
00393
00394 std::vector<int> rows;
00395 std::vector<int> cols;
00396 std::vector<real> vals;
00397
00398 n = A.get_nrows();
00399 nB = B.get_nrows();
00400 assert(nB == n);
00401 m = A.get_ncols();
00402 mB = B.get_ncols();
00403 assert(mB == m);
00404 A.get_matrix(rows, cols, vals);
00405 set_sparse_matrix_from_data<chtml::CHTGeneralMatrix<real, ParamsType>, symmMatrix>(B, rows, cols, vals);
00406 }
00407
00408
00409 #endif
00410
00411
00412
00413 #endif // HEADER_TRANSFORM