GMSparseVectorMatrix.cpp 5.2 KB

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