GMSparse2.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #include "GMSparse2.h"
  2. #include <assert.h>
  3. using namespace OBJREC;
  4. using namespace NICE;
  5. using namespace std;
  6. GMSparse2::GMSparse2 ( uint _rows, uint _cols ) : m_rows(_rows), m_cols(_cols)
  7. {
  8. resize(_rows, _cols);
  9. }
  10. GMSparse2::GMSparse2 ( const NICE::Matrix & iceA, double epsilon)
  11. {
  12. m_rows = iceA.rows();
  13. m_cols = iceA.cols();
  14. newvectors = true;
  15. for(uint r = 0; r < m_rows; r++)
  16. {
  17. SparseVector *tmp = new SparseVector(m_cols);
  18. for(uint c = 0; c < m_cols; c++)
  19. {
  20. if(fabs(iceA(r ,c)) > epsilon)
  21. (*tmp)[c] = iceA(r, c);
  22. }
  23. A.push_back(tmp);
  24. }
  25. }
  26. GMSparse2::~GMSparse2()
  27. {
  28. clear();
  29. }
  30. SparseVector & GMSparse2::operator[](int i) const
  31. {
  32. return *A[i];
  33. }
  34. void GMSparse2::addRow(const NICE::Vector &x)
  35. {
  36. SparseVector *v = new SparseVector(x);
  37. m_cols = x.size();
  38. v->setDim(m_cols);
  39. A.push_back(v);
  40. ++m_rows;
  41. }
  42. void GMSparse2::addRow(SparseVector *x)
  43. {
  44. newvectors = false;
  45. A.push_back(x);
  46. ++m_rows;
  47. m_cols = x->getDim();
  48. }
  49. void GMSparse2::clear()
  50. {
  51. if(newvectors)
  52. {
  53. for(uint i = 0; i < A.size(); i++)
  54. {
  55. delete A[i];
  56. }
  57. }
  58. A.clear();
  59. }
  60. void GMSparse2::resize( int _rows, int _cols)
  61. {
  62. m_rows = _rows;
  63. m_cols = _cols;
  64. newvectors = true;
  65. for(uint i = 0; i < m_rows; i++)
  66. {
  67. SparseVector *tmp = new SparseVector(m_cols);
  68. A.push_back(tmp);
  69. }
  70. }
  71. void GMSparse2::mult2(GMSparse2 &y, GMSparse2 &out, bool transpx, bool transpy)
  72. {
  73. int rowsx, colsx, rowsy, colsy;
  74. if(transpx)
  75. {
  76. colsx = m_rows;
  77. rowsx = m_cols;
  78. }
  79. else
  80. {
  81. rowsx = m_rows;
  82. colsx = m_cols;
  83. }
  84. if(transpy)
  85. {
  86. colsy = y.rows();
  87. rowsy = y.cols();
  88. }
  89. else
  90. {
  91. rowsy = y.rows();
  92. colsy = y.cols();
  93. }
  94. if(rowsy != colsx)
  95. {
  96. cout << "GMSparse::mult matrix sizes missmatched" << endl;
  97. exit(1);
  98. }
  99. out.resize(rowsx,colsy);
  100. double xval, yval, val, val2;
  101. for(int c = 0; c < colsy; c++)
  102. {
  103. for(int i = 0; i < rowsy; i++)
  104. {
  105. if(transpy)
  106. yval = y[c].get(i);
  107. else
  108. yval = y[i].get(c);
  109. if(yval != 0.0)
  110. {
  111. for(int r = 0; r < rowsx; r++)
  112. {
  113. if(transpx)
  114. xval = (*A[i]).get(r);
  115. else
  116. xval = (*A[r]).get(i);
  117. val = out[r].get(c);
  118. val2 = val+xval*yval;
  119. if(fabs(val2) > 10e-10)
  120. out[r][c] = val2;
  121. else if(val != 0.0)
  122. out[r].erase(c);
  123. }
  124. }
  125. }
  126. }
  127. }
  128. void GMSparse2::mult(GMSparse2 &y, GMSparse2 &out, bool transpx, bool transpy)
  129. {
  130. int rowsx, colsx, rowsy, colsy;
  131. if(transpx)
  132. {
  133. colsx = m_rows;
  134. rowsx = m_cols;
  135. }
  136. else
  137. {
  138. rowsx = m_rows;
  139. colsx = m_cols;
  140. }
  141. if(transpy)
  142. {
  143. colsy = y.rows();
  144. rowsy = y.cols();
  145. }
  146. else
  147. {
  148. rowsy = y.rows();
  149. colsy = y.cols();
  150. }
  151. if(rowsy != colsx)
  152. {
  153. cout << "GMSparse::mult matrix sizes missmatched" << endl;
  154. exit(1);
  155. }
  156. out.resize(rowsx,colsy);
  157. double xval, yval, val;
  158. for(int r = 0; r < rowsx; r++)
  159. {
  160. for(int c = 0; c < colsy; c++)
  161. {
  162. val = 0.0;
  163. for(int i = 0; i < rowsy; i++)
  164. {
  165. if(transpx)
  166. xval = (*A[i]).get(r);
  167. else
  168. xval = (*A[r]).get(i);
  169. if(transpy)
  170. yval = y[c].get(i);
  171. else
  172. yval = y[i].get(c);
  173. val+=xval * yval;
  174. }
  175. if(fabs(val) > 10e-7)
  176. out[r][c] = val;
  177. if(!finite(val*val))
  178. {
  179. cout << "val " << val << endl;
  180. for(int i = 0; i < rowsy; i++)
  181. {
  182. if(transpx)
  183. xval = (*A[i]).get(r);
  184. else
  185. xval = (*A[r]).get(i);
  186. if(transpy)
  187. yval = y[c].get(i);
  188. else
  189. yval = y[i].get(c);
  190. val+=xval * yval;
  191. cout << " xval: " << xval << " yval " << yval << endl;
  192. }
  193. getchar();
  194. }
  195. }
  196. }
  197. }
  198. void GMSparse2::mult(SparseVector &y, GMSparse2 &out, bool transpx, bool transpy)
  199. {
  200. int rowsx, colsx, rowsy, colsy;
  201. if(transpx)
  202. {
  203. colsx = m_rows;
  204. rowsx = m_cols;
  205. }
  206. else
  207. {
  208. rowsx = m_rows;
  209. colsx = m_cols;
  210. }
  211. if(transpy)
  212. {
  213. colsy = 1;
  214. rowsy = y.getDim();
  215. }
  216. else
  217. {
  218. rowsy = 1;
  219. colsy = y.getDim();
  220. }
  221. if(rowsy != colsx)
  222. {
  223. cout << "GMSparse::mult matrix sizes missmatched" << endl;
  224. exit(1);
  225. }
  226. out.resize(rowsx,colsy);
  227. double xval, yval, val;
  228. for(int r = 0; r < rowsx; r++)
  229. {
  230. for(int c = 0; c < colsy; c++)
  231. {
  232. val = 0.0;
  233. for(int i = 0; i < rowsy; i++)
  234. {
  235. if(transpx)
  236. xval = (*A[i]).get(r);
  237. else
  238. xval = (*A[r]).get(i);
  239. if(transpy)
  240. yval = y.get(i);
  241. else
  242. yval = y.get(c);
  243. val += xval * yval;
  244. }
  245. if(fabs(val) > 10e-7)
  246. out[r][c] = val;
  247. }
  248. }
  249. }
  250. void GMSparse2::multiply ( NICE::Vector & y, const NICE::Vector & x ) const
  251. {
  252. //FIXME: einbauen
  253. }
  254. void GMSparse2::store (ostream & os, int format) const
  255. {
  256. os << m_rows << " " << m_cols << " " << A.size() << endl;
  257. for(int i = 0; i < (int) A.size(); i++)
  258. {
  259. A[i]->store(os, format);
  260. }
  261. }
  262. void GMSparse2::restore (istream & is, int format)
  263. {
  264. A.clear();
  265. is >> m_rows;
  266. is >> m_cols;
  267. int size;
  268. is >> size;
  269. for(int i = 0; i < size; i++)
  270. {
  271. SparseVector *s = new SparseVector(m_cols);
  272. s->restore(is, format);
  273. A.push_back(s);
  274. }
  275. }
  276. void GMSparse2::setDel(bool del)
  277. {
  278. newvectors = del;
  279. }