Go to the documentation of this file.00001 #ifndef SPARSEOWRAPPER_HPP_
00002 #define SPARSEOWRAPPER_HPP_
00003
00004 #include <cstring>
00005 #include <vector>
00006
00007 #include "operator.hh"
00008 #include "basics.hh"
00009 #include <complex>
00010
00011
00012 using namespace matfile;
00013
00014 namespace concepts {
00015
00023 class SparseOWrapper {
00024
00030 public:
00031
00032
00033
00034
00035 typedef ArraySparseWrapper Type;
00036
00037
00038
00039
00040
00041 template<class T>
00042 SparseOWrapper(SparseMatrix<T>& sparse);
00043
00044
00045
00046
00047 virtual ~SparseOWrapper() {
00048 delete (valReal_);
00049 delete (conceptsCol_);
00050 delete (conceptsRow_);
00051
00052 if (isComplex_)
00053 delete (valCmplx_);
00054 }
00055 ;
00056
00057
00058
00059
00060
00061 bool isComplex() const {
00062 return isComplex_;
00063 }
00064
00065
00066
00067
00068
00069 miINT32_t nnz() const {
00070 return nnz_;
00071 }
00072
00073
00074
00075
00076
00077 const char *ir() const {
00078 return ir_;
00079 }
00080
00081
00082
00083
00084
00085 const char *jc() const {
00086 return jc_;
00087 }
00088
00089
00090
00091
00092
00093
00094 void size(miINT32_t& rows, miINT32_t& cols) const {
00095 rows = rows_;
00096 cols = cols_;
00097 }
00098
00099 const MATtype realType() const {
00100 return type_;
00101 }
00102
00103 const MATtype imagType() const {
00104 return type_;
00105 }
00106
00107 const char *pr() const {
00108 return pr_;
00109 }
00110
00111 const char *pi() const {
00112 return pi_;
00113 }
00114
00115 private:
00116
00117 bool isComplex_;
00118
00119
00120 miINT32_t rows_, cols_, nnz_;
00121
00122
00123 const char *ir_, *jc_;
00124
00125
00126 const char *pr_, *pi_;
00127
00128
00129 double *valReal_, *valCmplx_;
00130 int *conceptsCol_, *conceptsRow_;
00131
00132 MATtype type_;
00133 };
00134
00141 template<class T>
00142 SparseOWrapper::SparseOWrapper(SparseMatrix<T>& matrix) {
00143 isComplex_ = (NumTraits<T>::isComplex == 1);
00144 rows_ = matrix.dimX();
00145 cols_ = matrix.dimY();
00146 nnz_ = 0;
00147
00148
00149 typename concepts::SparseMatrix<T>::iterator iter = matrix.begin();
00150 for (; iter != matrix.end(); ++iter)
00151 ++nnz_;
00152
00153 type_ = NumTraits<T>::MatType;
00154
00155 if (!isComplex_) {
00156
00157 const SparseMatrix<Real>* mat =
00158 dynamic_cast<const SparseMatrix<Real>*> (&matrix);
00159
00160
00161
00162 if (nnz_ == 0) {
00163 nnz_ = 1;
00164 valReal_ = new double[nnz_];
00165 valReal_[0] = 0;
00166 valCmplx_ = NULL;
00167 conceptsRow_ = new int[nnz_];
00168 conceptsRow_[0] = 0;
00169 conceptsCol_ = new int[cols_ + 1];
00170 conceptsCol_[0] = 0;
00171 for (int i = 1; i <= cols_; ++i)
00172 conceptsCol_[i] = 1;
00173 } else {
00174
00175 valReal_ = new double[nnz_];
00176 valCmplx_ = NULL;
00177 conceptsRow_ = new int[nnz_];
00178 conceptsCol_ = new int[cols_ + 1];
00179
00180
00181 mat->convertCCS(valReal_, conceptsRow_, conceptsCol_);
00182 }
00183 ir_ = reinterpret_cast<const char*> (conceptsRow_);
00184 jc_ = reinterpret_cast<const char*> (conceptsCol_);
00185 pr_ = reinterpret_cast<const char*> (valReal_);
00186 pi_ = NULL;
00187
00188 } else {
00189
00190 const SparseMatrix<Cmplx>* mat =
00191 dynamic_cast<const SparseMatrix<Cmplx>*> (&matrix);
00192
00193 if (nnz_ == 0) {
00194 nnz_ = 1;
00195 valReal_ = new double[nnz_];
00196 valReal_[0] = 0;
00197 valCmplx_ = new double[nnz_];
00198 valCmplx_[0] = 0;
00199 conceptsRow_ = new int[nnz_];
00200 conceptsRow_[0] = 0;
00201 conceptsCol_ = new int[cols_ + 1];
00202 conceptsCol_[0] = 0;
00203 for (int i = 1; i <= cols_; ++i)
00204 conceptsCol_[i] = 1;
00205 } else {
00206
00207
00208 std::complex<double> *val = new std::complex<double>[nnz_];
00209 conceptsCol_ = new int[cols_ + 1];
00210 conceptsRow_ = new int[nnz_];
00211 valReal_ = new double[nnz_];
00212 valCmplx_ = new double[nnz_];
00213
00214 mat->convertCCS(val, conceptsRow_, conceptsCol_);
00215
00216
00217 for (int i = 0; i < nnz_; ++i) {
00218 valReal_[i] = val[i].real();
00219 valCmplx_[i] = val[i].imag();
00220 }
00221
00222
00223 delete (val);
00224 }
00225
00226 ir_ = reinterpret_cast<const char*> (conceptsRow_);
00227 jc_ = reinterpret_cast<const char*> (conceptsCol_);
00228 pr_ = reinterpret_cast<const char*> (valReal_);
00229 pi_ = reinterpret_cast<const char*> (valCmplx_);
00230 }
00231
00232 }
00233
00239
00240
00241
00242
00243
00244 }
00245 #endif