Array.h 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554
  1. // This file is part of QuadProg++: a C++ library implementing
  2. // the algorithm of Goldfarb and Idnani for the solution of a (convex)
  3. // Quadratic Programming problem by means of an active-set dual method.
  4. // Copyright (C) 2007-2009 Luca Di Gaspero.
  5. // Copyright (C) 2009 Eric Moyer.
  6. //
  7. // QuadProg++ is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU Lesser General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // QuadProg++ is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU Lesser General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU Lesser General Public License
  18. // along with QuadProg++. If not, see <http://www.gnu.org/licenses/>.
  19. #ifndef _ARRAY_HH
  20. #define _ARRAY_HH
  21. #include <set>
  22. #include <stdexcept>
  23. #include <iostream>
  24. #include <iomanip>
  25. #include <cmath>
  26. #include <cstdlib>
  27. namespace QuadProgPP{
  28. enum MType { DIAG };
  29. template <typename T>
  30. class Vector
  31. {
  32. public:
  33. Vector();
  34. Vector(const unsigned int n);
  35. Vector(const T& a, const unsigned int n); //initialize to constant value
  36. Vector(const T* a, const unsigned int n); // Initialize to array
  37. Vector(const Vector &rhs); // copy constructor
  38. ~Vector(); // destructor
  39. inline void set(const T* a, const unsigned int n);
  40. Vector<T> extract(const std::set<unsigned int>& indexes) const;
  41. inline T& operator[](const unsigned int& i); //i-th element
  42. inline const T& operator[](const unsigned int& i) const;
  43. inline unsigned int size() const;
  44. inline void resize(const unsigned int n);
  45. inline void resize(const T& a, const unsigned int n);
  46. Vector<T>& operator=(const Vector<T>& rhs); //assignment
  47. Vector<T>& operator=(const T& a); //assign a to every element
  48. inline Vector<T>& operator+=(const Vector<T>& rhs);
  49. inline Vector<T>& operator-=(const Vector<T>& rhs);
  50. inline Vector<T>& operator*=(const Vector<T>& rhs);
  51. inline Vector<T>& operator/=(const Vector<T>& rhs);
  52. inline Vector<T>& operator^=(const Vector<T>& rhs);
  53. inline Vector<T>& operator+=(const T& a);
  54. inline Vector<T>& operator-=(const T& a);
  55. inline Vector<T>& operator*=(const T& a);
  56. inline Vector<T>& operator/=(const T& a);
  57. inline Vector<T>& operator^=(const T& a);
  58. private:
  59. unsigned int n; // size of array. upper index is n-1
  60. T* v; // storage for data
  61. };
  62. template <typename T>
  63. Vector<T>::Vector()
  64. : n(0), v(0)
  65. {}
  66. template <typename T>
  67. Vector<T>::Vector(const unsigned int n)
  68. : v(new T[n])
  69. {
  70. this->n = n;
  71. }
  72. template <typename T>
  73. Vector<T>::Vector(const T& a, const unsigned int n)
  74. : v(new T[n])
  75. {
  76. this->n = n;
  77. for (unsigned int i = 0; i < n; i++)
  78. v[i] = a;
  79. }
  80. template <typename T>
  81. Vector<T>::Vector(const T* a, const unsigned int n)
  82. : v(new T[n])
  83. {
  84. this->n = n;
  85. for (unsigned int i = 0; i < n; i++)
  86. v[i] = *a++;
  87. }
  88. template <typename T>
  89. Vector<T>::Vector(const Vector<T>& rhs)
  90. : v(new T[rhs.n])
  91. {
  92. this->n = rhs.n;
  93. for (unsigned int i = 0; i < n; i++)
  94. v[i] = rhs[i];
  95. }
  96. template <typename T>
  97. Vector<T>::~Vector()
  98. {
  99. if (v != 0)
  100. delete[] (v);
  101. }
  102. template <typename T>
  103. void Vector<T>::resize(const unsigned int n)
  104. {
  105. if (n == this->n)
  106. return;
  107. if (v != 0)
  108. delete[] (v);
  109. v = new T[n];
  110. this->n = n;
  111. }
  112. template <typename T>
  113. void Vector<T>::resize(const T& a, const unsigned int n)
  114. {
  115. resize(n);
  116. for (unsigned int i = 0; i < n; i++)
  117. v[i] = a;
  118. }
  119. template <typename T>
  120. inline Vector<T>& Vector<T>::operator=(const Vector<T>& rhs)
  121. // postcondition: normal assignment via copying has been performed;
  122. // if vector and rhs were different sizes, vector
  123. // has been resized to match the size of rhs
  124. {
  125. if (this != &rhs)
  126. {
  127. resize(rhs.n);
  128. for (unsigned int i = 0; i < n; i++)
  129. v[i] = rhs[i];
  130. }
  131. return *this;
  132. }
  133. template <typename T>
  134. inline Vector<T> & Vector<T>::operator=(const T& a) //assign a to every element
  135. {
  136. for (unsigned int i = 0; i < n; i++)
  137. v[i] = a;
  138. return *this;
  139. }
  140. template <typename T>
  141. inline T & Vector<T>::operator[](const unsigned int& i) //subscripting
  142. {
  143. return v[i];
  144. }
  145. template <typename T>
  146. inline const T& Vector<T>::operator[](const unsigned int& i) const //subscripting
  147. {
  148. return v[i];
  149. }
  150. template <typename T>
  151. inline unsigned int Vector<T>::size() const
  152. {
  153. return n;
  154. }
  155. template <typename T>
  156. inline void Vector<T>::set(const T* a, unsigned int n)
  157. {
  158. resize(n);
  159. for (unsigned int i = 0; i < n; i++)
  160. v[i] = a[i];
  161. }
  162. template <typename T>
  163. inline Vector<T> Vector<T>::extract(const std::set<unsigned int>& indexes) const
  164. {
  165. Vector<T> tmp(indexes.size());
  166. unsigned int i = 0;
  167. for (std::set<unsigned int>::const_iterator el = indexes.begin(); el != indexes.end(); el++)
  168. {
  169. if (*el >= n)
  170. throw std::logic_error("Error extracting subvector: the indexes are out of vector bounds");
  171. tmp[i++] = v[*el];
  172. }
  173. return tmp;
  174. }
  175. template <typename T>
  176. inline Vector<T>& Vector<T>::operator+=(const Vector<T>& rhs)
  177. {
  178. if (this->size() != rhs.size())
  179. throw std::logic_error("Operator+=: vectors have different sizes");
  180. for (unsigned int i = 0; i < n; i++)
  181. v[i] += rhs[i];
  182. return *this;
  183. }
  184. template <typename T>
  185. inline Vector<T>& Vector<T>::operator+=(const T& a)
  186. {
  187. for (unsigned int i = 0; i < n; i++)
  188. v[i] += a;
  189. return *this;
  190. }
  191. template <typename T>
  192. inline Vector<T> operator+(const Vector<T>& rhs)
  193. {
  194. return rhs;
  195. }
  196. template <typename T>
  197. inline Vector<T> operator+(const Vector<T>& lhs, const Vector<T>& rhs)
  198. {
  199. if (lhs.size() != rhs.size())
  200. throw std::logic_error("Operator+: vectors have different sizes");
  201. Vector<T> tmp(lhs.size());
  202. for (unsigned int i = 0; i < lhs.size(); i++)
  203. tmp[i] = lhs[i] + rhs[i];
  204. return tmp;
  205. }
  206. template <typename T>
  207. inline Vector<T> operator+(const Vector<T>& lhs, const T& a)
  208. {
  209. Vector<T> tmp(lhs.size());
  210. for (unsigned int i = 0; i < lhs.size(); i++)
  211. tmp[i] = lhs[i] + a;
  212. return tmp;
  213. }
  214. template <typename T>
  215. inline Vector<T> operator+(const T& a, const Vector<T>& rhs)
  216. {
  217. Vector<T> tmp(rhs.size());
  218. for (unsigned int i = 0; i < rhs.size(); i++)
  219. tmp[i] = a + rhs[i];
  220. return tmp;
  221. }
  222. template <typename T>
  223. inline Vector<T>& Vector<T>::operator-=(const Vector<T>& rhs)
  224. {
  225. if (this->size() != rhs.size())
  226. throw std::logic_error("Operator-=: vectors have different sizes");
  227. for (unsigned int i = 0; i < n; i++)
  228. v[i] -= rhs[i];
  229. return *this;
  230. }
  231. template <typename T>
  232. inline Vector<T>& Vector<T>::operator-=(const T& a)
  233. {
  234. for (unsigned int i = 0; i < n; i++)
  235. v[i] -= a;
  236. return *this;
  237. }
  238. template <typename T>
  239. inline Vector<T> operator-(const Vector<T>& rhs)
  240. {
  241. return (T)(-1) * rhs;
  242. }
  243. template <typename T>
  244. inline Vector<T> operator-(const Vector<T>& lhs, const Vector<T>& rhs)
  245. {
  246. if (lhs.size() != rhs.size())
  247. throw std::logic_error("Operator-: vectors have different sizes");
  248. Vector<T> tmp(lhs.size());
  249. for (unsigned int i = 0; i < lhs.size(); i++)
  250. tmp[i] = lhs[i] - rhs[i];
  251. return tmp;
  252. }
  253. template <typename T>
  254. inline Vector<T> operator-(const Vector<T>& lhs, const T& a)
  255. {
  256. Vector<T> tmp(lhs.size());
  257. for (unsigned int i = 0; i < lhs.size(); i++)
  258. tmp[i] = lhs[i] - a;
  259. return tmp;
  260. }
  261. template <typename T>
  262. inline Vector<T> operator-(const T& a, const Vector<T>& rhs)
  263. {
  264. Vector<T> tmp(rhs.size());
  265. for (unsigned int i = 0; i < rhs.size(); i++)
  266. tmp[i] = a - rhs[i];
  267. return tmp;
  268. }
  269. template <typename T>
  270. inline Vector<T>& Vector<T>::operator*=(const Vector<T>& rhs)
  271. {
  272. if (this->size() != rhs.size())
  273. throw std::logic_error("Operator*=: vectors have different sizes");
  274. for (unsigned int i = 0; i < n; i++)
  275. v[i] *= rhs[i];
  276. return *this;
  277. }
  278. template <typename T>
  279. inline Vector<T>& Vector<T>::operator*=(const T& a)
  280. {
  281. for (unsigned int i = 0; i < n; i++)
  282. v[i] *= a;
  283. return *this;
  284. }
  285. template <typename T>
  286. inline Vector<T> operator*(const Vector<T>& lhs, const Vector<T>& rhs)
  287. {
  288. if (lhs.size() != rhs.size())
  289. throw std::logic_error("Operator*: vectors have different sizes");
  290. Vector<T> tmp(lhs.size());
  291. for (unsigned int i = 0; i < lhs.size(); i++)
  292. tmp[i] = lhs[i] * rhs[i];
  293. return tmp;
  294. }
  295. template <typename T>
  296. inline Vector<T> operator*(const Vector<T>& lhs, const T& a)
  297. {
  298. Vector<T> tmp(lhs.size());
  299. for (unsigned int i = 0; i < lhs.size(); i++)
  300. tmp[i] = lhs[i] * a;
  301. return tmp;
  302. }
  303. template <typename T>
  304. inline Vector<T> operator*(const T& a, const Vector<T>& rhs)
  305. {
  306. Vector<T> tmp(rhs.size());
  307. for (unsigned int i = 0; i < rhs.size(); i++)
  308. tmp[i] = a * rhs[i];
  309. return tmp;
  310. }
  311. template <typename T>
  312. inline Vector<T>& Vector<T>::operator/=(const Vector<T>& rhs)
  313. {
  314. if (this->size() != rhs.size())
  315. throw std::logic_error("Operator/=: vectors have different sizes");
  316. for (unsigned int i = 0; i < n; i++)
  317. v[i] /= rhs[i];
  318. return *this;
  319. }
  320. template <typename T>
  321. inline Vector<T>& Vector<T>::operator/=(const T& a)
  322. {
  323. for (unsigned int i = 0; i < n; i++)
  324. v[i] /= a;
  325. return *this;
  326. }
  327. template <typename T>
  328. inline Vector<T> operator/(const Vector<T>& lhs, const Vector<T>& rhs)
  329. {
  330. if (lhs.size() != rhs.size())
  331. throw std::logic_error("Operator/: vectors have different sizes");
  332. Vector<T> tmp(lhs.size());
  333. for (unsigned int i = 0; i < lhs.size(); i++)
  334. tmp[i] = lhs[i] / rhs[i];
  335. return tmp;
  336. }
  337. template <typename T>
  338. inline Vector<T> operator/(const Vector<T>& lhs, const T& a)
  339. {
  340. Vector<T> tmp(lhs.size());
  341. for (unsigned int i = 0; i < lhs.size(); i++)
  342. tmp[i] = lhs[i] / a;
  343. return tmp;
  344. }
  345. template <typename T>
  346. inline Vector<T> operator/(const T& a, const Vector<T>& rhs)
  347. {
  348. Vector<T> tmp(rhs.size());
  349. for (unsigned int i = 0; i < rhs.size(); i++)
  350. tmp[i] = a / rhs[i];
  351. return tmp;
  352. }
  353. template <typename T>
  354. inline Vector<T> operator^(const Vector<T>& lhs, const Vector<T>& rhs)
  355. {
  356. if (lhs.size() != rhs.size())
  357. throw std::logic_error("Operator^: vectors have different sizes");
  358. Vector<T> tmp(lhs.size());
  359. for (unsigned int i = 0; i < lhs.size(); i++)
  360. tmp[i] = pow(lhs[i], rhs[i]);
  361. return tmp;
  362. }
  363. template <typename T>
  364. inline Vector<T> operator^(const Vector<T>& lhs, const T& a)
  365. {
  366. Vector<T> tmp(lhs.size());
  367. for (unsigned int i = 0; i < lhs.size(); i++)
  368. tmp[i] = pow(lhs[i], a);
  369. return tmp;
  370. }
  371. template <typename T>
  372. inline Vector<T> operator^(const T& a, const Vector<T>& rhs)
  373. {
  374. Vector<T> tmp(rhs.size());
  375. for (unsigned int i = 0; i < rhs.size(); i++)
  376. tmp[i] = pow(a, rhs[i]);
  377. return tmp;
  378. }
  379. template <typename T>
  380. inline Vector<T>& Vector<T>::operator^=(const Vector<T>& rhs)
  381. {
  382. if (this->size() != rhs.size())
  383. throw std::logic_error("Operator^=: vectors have different sizes");
  384. for (unsigned int i = 0; i < n; i++)
  385. v[i] = pow(v[i], rhs[i]);
  386. return *this;
  387. }
  388. template <typename T>
  389. inline Vector<T>& Vector<T>::operator^=(const T& a)
  390. {
  391. for (unsigned int i = 0; i < n; i++)
  392. v[i] = pow(v[i], a);
  393. return *this;
  394. }
  395. template <typename T>
  396. inline bool operator==(const Vector<T>& v, const Vector<T>& w)
  397. {
  398. if (v.size() != w.size())
  399. throw std::logic_error("Vectors of different size are not confrontable");
  400. for (unsigned i = 0; i < v.size(); i++)
  401. if (v[i] != w[i])
  402. return false;
  403. return true;
  404. }
  405. template <typename T>
  406. inline bool operator!=(const Vector<T>& v, const Vector<T>& w)
  407. {
  408. if (v.size() != w.size())
  409. throw std::logic_error("Vectors of different size are not confrontable");
  410. for (unsigned i = 0; i < v.size(); i++)
  411. if (v[i] != w[i])
  412. return true;
  413. return false;
  414. }
  415. template <typename T>
  416. inline bool operator<(const Vector<T>& v, const Vector<T>& w)
  417. {
  418. if (v.size() != w.size())
  419. throw std::logic_error("Vectors of different size are not confrontable");
  420. for (unsigned i = 0; i < v.size(); i++)
  421. if (v[i] >= w[i])
  422. return false;
  423. return true;
  424. }
  425. template <typename T>
  426. inline bool operator<=(const Vector<T>& v, const Vector<T>& w)
  427. {
  428. if (v.size() != w.size())
  429. throw std::logic_error("Vectors of different size are not confrontable");
  430. for (unsigned i = 0; i < v.size(); i++)
  431. if (v[i] > w[i])
  432. return false;
  433. return true;
  434. }
  435. template <typename T>
  436. inline bool operator>(const Vector<T>& v, const Vector<T>& w)
  437. {
  438. if (v.size() != w.size())
  439. throw std::logic_error("Vectors of different size are not confrontable");
  440. for (unsigned i = 0; i < v.size(); i++)
  441. if (v[i] <= w[i])
  442. return false;
  443. return true;
  444. }
  445. template <typename T>
  446. inline bool operator>=(const Vector<T>& v, const Vector<T>& w)
  447. {
  448. if (v.size() != w.size())
  449. throw std::logic_error("Vectors of different size are not confrontable");
  450. for (unsigned i = 0; i < v.size(); i++)
  451. if (v[i] < w[i])
  452. return false;
  453. return true;
  454. }
  455. /**
  456. Input/Output
  457. */
  458. template <typename T>
  459. inline std::ostream& operator<<(std::ostream& os, const Vector<T>& v)
  460. {
  461. os << std::endl << v.size() << std::endl;
  462. for (unsigned int i = 0; i < v.size() - 1; i++)
  463. os << std::setw(20) << std::setprecision(16) << v[i] << ", ";
  464. os << std::setw(20) << std::setprecision(16) << v[v.size() - 1] << std::endl;
  465. return os;
  466. }
  467. template <typename T>
  468. std::istream& operator>>(std::istream& is, Vector<T>& v)
  469. {
  470. int elements;
  471. char comma;
  472. is >> elements;
  473. v.resize(elements);
  474. for (unsigned int i = 0; i < elements; i++)
  475. is >> v[i] >> comma;
  476. return is;
  477. }
  478. /**
  479. Index utilities
  480. */
  481. std::set<unsigned int> seq(unsigned int s, unsigned int e);
  482. std::set<unsigned int> singleton(unsigned int i);
  483. template <typename T>
  484. class CanonicalBaseVector : public Vector<T>
  485. {
  486. public:
  487. CanonicalBaseVector(unsigned int i, unsigned int n);
  488. inline void reset(unsigned int i);
  489. private:
  490. unsigned int e;
  491. };
  492. template <typename T>
  493. CanonicalBaseVector<T>::CanonicalBaseVector(unsigned int i, unsigned int n)
  494. : Vector<T>((T)0, n), e(i)
  495. { (*this)[e] = (T)1; }
  496. template <typename T>
  497. inline void CanonicalBaseVector<T>::reset(unsigned int i)
  498. {
  499. (*this)[e] = (T)0;
  500. e = i;
  501. (*this)[e] = (T)1;
  502. }
  503. #include <stdexcept>
  504. template <typename T>
  505. inline T sum(const Vector<T>& v)
  506. {
  507. T tmp = (T)0;
  508. for (unsigned int i = 0; i < v.size(); i++)
  509. tmp += v[i];
  510. return tmp;
  511. }
  512. template <typename T>
  513. inline T prod(const Vector<T>& v)
  514. {
  515. T tmp = (T)1;
  516. for (unsigned int i = 0; i < v.size(); i++)
  517. tmp *= v[i];
  518. return tmp;
  519. }
  520. template <typename T>
  521. inline T mean(const Vector<T>& v)
  522. {
  523. T sum = (T)0;
  524. for (unsigned int i = 0; i < v.size(); i++)
  525. sum += v[i];
  526. return sum / v.size();
  527. }
  528. template <typename T>
  529. inline T median(const Vector<T>& v)
  530. {
  531. Vector<T> tmp = sort(v);
  532. if (v.size() % 2 == 1) // it is an odd-sized vector
  533. return tmp[v.size() / 2];
  534. else
  535. return 0.5 * (tmp[v.size() / 2 - 1] + tmp[v.size() / 2]);
  536. }
  537. template <typename T>
  538. inline T stdev(const Vector<T>& v, bool sample_correction = false)
  539. {
  540. return sqrt(var(v, sample_correction));
  541. }
  542. template <typename T>
  543. inline T var(const Vector<T>& v, bool sample_correction = false)
  544. {
  545. T sum = (T)0, ssum = (T)0;
  546. unsigned int n = v.size();
  547. for (unsigned int i = 0; i < n; i++)
  548. {
  549. sum += v[i];
  550. ssum += (v[i] * v[i]);
  551. }
  552. if (!sample_correction)
  553. return (ssum / n) - (sum / n) * (sum / n);
  554. else
  555. return n * ((ssum / n) - (sum / n) * (sum / n)) / (n - 1);
  556. }
  557. template <typename T>
  558. inline T max(const Vector<T>& v)
  559. {
  560. T value = v[0];
  561. for (unsigned int i = 1; i < v.size(); i++)
  562. value = std::max(v[i], value);
  563. return value;
  564. }
  565. template <typename T>
  566. inline T min(const Vector<T>& v)
  567. {
  568. T value = v[0];
  569. for (unsigned int i = 1; i < v.size(); i++)
  570. value = std::min(v[i], value);
  571. return value;
  572. }
  573. template <typename T>
  574. inline unsigned int index_max(const Vector<T>& v)
  575. {
  576. unsigned int max = 0;
  577. for (unsigned int i = 1; i < v.size(); i++)
  578. if (v[i] > v[max])
  579. max = i;
  580. return max;
  581. }
  582. template <typename T>
  583. inline unsigned int index_min(const Vector<T>& v)
  584. {
  585. unsigned int min = 0;
  586. for (unsigned int i = 1; i < v.size(); i++)
  587. if (v[i] < v[min])
  588. min = i;
  589. return min;
  590. }
  591. template <typename T>
  592. inline T dot_prod(const Vector<T>& a, const Vector<T>& b)
  593. {
  594. T sum = (T)0;
  595. if (a.size() != b.size())
  596. throw std::logic_error("Dotprod error: the vectors are not the same size");
  597. for (unsigned int i = 0; i < a.size(); i++)
  598. sum += a[i] * b[i];
  599. return sum;
  600. }
  601. /**
  602. Single element mathematical functions
  603. */
  604. template <typename T>
  605. inline Vector<T> exp(const Vector<T>& v)
  606. {
  607. Vector<T> tmp(v.size());
  608. for (unsigned int i = 0; i < v.size(); i++)
  609. tmp[i] = exp(v[i]);
  610. return tmp;
  611. }
  612. template <typename T>
  613. inline Vector<T> log(const Vector<T>& v)
  614. {
  615. Vector<T> tmp(v.size());
  616. for (unsigned int i = 0; i < v.size(); i++)
  617. tmp[i] = log(v[i]);
  618. return tmp;
  619. }
  620. template <typename T>
  621. inline Vector<T> sqrt(const Vector<T>& v)
  622. {
  623. Vector<T> tmp(v.size());
  624. for (unsigned int i = 0; i < v.size(); i++)
  625. tmp[i] = sqrt(v[i]);
  626. return tmp;
  627. }
  628. template <typename T>
  629. inline Vector<T> pow(const Vector<T>& v, double a)
  630. {
  631. Vector<T> tmp(v.size());
  632. for (unsigned int i = 0; i < v.size(); i++)
  633. tmp[i] = pow(v[i], a);
  634. return tmp;
  635. }
  636. template <typename T>
  637. inline Vector<T> abs(const Vector<T>& v)
  638. {
  639. Vector<T> tmp(v.size());
  640. for (unsigned int i = 0; i < v.size(); i++)
  641. tmp[i] = (T)fabs(v[i]);
  642. return tmp;
  643. }
  644. template <typename T>
  645. inline Vector<T> sign(const Vector<T>& v)
  646. {
  647. Vector<T> tmp(v.size());
  648. for (unsigned int i = 0; i < v.size(); i++)
  649. tmp[i] = v[i] > 0 ? +1 : v[i] == 0 ? 0 : -1;
  650. return tmp;
  651. }
  652. template <typename T>
  653. inline unsigned int partition(Vector<T>& v, unsigned int begin, unsigned int end)
  654. {
  655. unsigned int i = begin + 1, j = begin + 1;
  656. T pivot = v[begin];
  657. while (j <= end)
  658. {
  659. if (v[j] < pivot) {
  660. std::swap(v[i], v[j]);
  661. i++;
  662. }
  663. j++;
  664. }
  665. v[begin] = v[i - 1];
  666. v[i - 1] = pivot;
  667. return i - 2;
  668. }
  669. template <typename T>
  670. inline void quicksort(Vector<T>& v, unsigned int begin, unsigned int end)
  671. {
  672. if (end > begin)
  673. {
  674. unsigned int index = partition(v, begin, end);
  675. quicksort(v, begin, index);
  676. quicksort(v, index + 2, end);
  677. }
  678. }
  679. template <typename T>
  680. inline Vector<T> sort(const Vector<T>& v)
  681. {
  682. Vector<T> tmp(v);
  683. quicksort<T>(tmp, 0, tmp.size() - 1);
  684. return tmp;
  685. }
  686. template <typename T>
  687. inline Vector<double> rank(const Vector<T>& v)
  688. {
  689. Vector<T> tmp(v);
  690. Vector<double> tmp_rank(0.0, v.size());
  691. for (unsigned int i = 0; i < tmp.size(); i++)
  692. {
  693. unsigned int smaller = 0, equal = 0;
  694. for (unsigned int j = 0; j < tmp.size(); j++)
  695. if (i == j)
  696. continue;
  697. else
  698. if (tmp[j] < tmp[i])
  699. smaller++;
  700. else if (tmp[j] == tmp[i])
  701. equal++;
  702. tmp_rank[i] = smaller + 1;
  703. if (equal > 0)
  704. {
  705. for (unsigned int j = 1; j <= equal; j++)
  706. tmp_rank[i] += smaller + 1 + j;
  707. tmp_rank[i] /= (double)(equal + 1);
  708. }
  709. }
  710. return tmp_rank;
  711. }
  712. //enum MType { DIAG };
  713. template <typename T>
  714. class Matrix
  715. {
  716. public:
  717. Matrix(); // Default constructor
  718. Matrix(const unsigned int n, const unsigned int m); // Construct a n x m matrix
  719. Matrix(const T& a, const unsigned int n, const unsigned int m); // Initialize the content to constant a
  720. Matrix(MType t, const T& a, const T& o, const unsigned int n, const unsigned int m);
  721. Matrix(MType t, const Vector<T>& v, const T& o, const unsigned int n, const unsigned int m);
  722. Matrix(const T* a, const unsigned int n, const unsigned int m); // Initialize to array
  723. Matrix(const Matrix<T>& rhs); // Copy constructor
  724. ~Matrix(); // destructor
  725. inline T* operator[](const unsigned int& i) { return v[i]; } // Subscripting: row i
  726. inline const T* operator[](const unsigned int& i) const { return v[i]; }; // const subsctipting
  727. inline void resize(const unsigned int n, const unsigned int m);
  728. inline void resize(const T& a, const unsigned int n, const unsigned int m);
  729. inline Vector<T> extractRow(const unsigned int i) const;
  730. inline Vector<T> extractColumn(const unsigned int j) const;
  731. inline Vector<T> extractDiag() const;
  732. inline Matrix<T> extractRows(const std::set<unsigned int>& indexes) const;
  733. inline Matrix<T> extractColumns(const std::set<unsigned int>& indexes) const;
  734. inline Matrix<T> extract(const std::set<unsigned int>& r_indexes, const std::set<unsigned int>& c_indexes) const;
  735. inline void set(const T* a, unsigned int n, unsigned int m);
  736. inline void set(const std::set<unsigned int>& r_indexes, const std::set<unsigned int>& c_indexes, const Matrix<T>& m);
  737. inline void setRow(const unsigned int index, const Vector<T>& v);
  738. inline void setRow(const unsigned int index, const Matrix<T>& v);
  739. inline void setRows(const std::set<unsigned int>& indexes, const Matrix<T>& m);
  740. inline void setColumn(const unsigned int index, const Vector<T>& v);
  741. inline void setColumn(const unsigned int index, const Matrix<T>& v);
  742. inline void setColumns(const std::set<unsigned int>& indexes, const Matrix<T>& m);
  743. inline unsigned int nrows() const { return n; } // number of rows
  744. inline unsigned int ncols() const { return m; } // number of columns
  745. inline Matrix<T>& operator=(const Matrix<T>& rhs); // Assignment operator
  746. inline Matrix<T>& operator=(const T& a); // Assign to every element value a
  747. inline Matrix<T>& operator+=(const Matrix<T>& rhs);
  748. inline Matrix<T>& operator-=(const Matrix<T>& rhs);
  749. inline Matrix<T>& operator*=(const Matrix<T>& rhs);
  750. inline Matrix<T>& operator/=(const Matrix<T>& rhs);
  751. inline Matrix<T>& operator^=(const Matrix<T>& rhs);
  752. inline Matrix<T>& operator+=(const T& a);
  753. inline Matrix<T>& operator-=(const T& a);
  754. inline Matrix<T>& operator*=(const T& a);
  755. inline Matrix<T>& operator/=(const T& a);
  756. inline Matrix<T>& operator^=(const T& a);
  757. inline operator Vector<T>();
  758. private:
  759. unsigned int n; // number of rows
  760. unsigned int m; // number of columns
  761. T **v; // storage for data
  762. };
  763. template <typename T>
  764. Matrix<T>::Matrix()
  765. : n(0), m(0), v(0)
  766. {}
  767. template <typename T>
  768. Matrix<T>::Matrix(unsigned int n, unsigned int m)
  769. : v(new T*[n])
  770. {
  771. register unsigned int i;
  772. this->n = n; this->m = m;
  773. v[0] = new T[m * n];
  774. for (i = 1; i < n; i++)
  775. v[i] = v[i - 1] + m;
  776. }
  777. template <typename T>
  778. Matrix<T>::Matrix(const T& a, unsigned int n, unsigned int m)
  779. : v(new T*[n])
  780. {
  781. register unsigned int i, j;
  782. this->n = n; this->m = m;
  783. v[0] = new T[m * n];
  784. for (i = 1; i < n; i++)
  785. v[i] = v[i - 1] + m;
  786. for (i = 0; i < n; i++)
  787. for (j = 0; j < m; j++)
  788. v[i][j] = a;
  789. }
  790. template <class T>
  791. Matrix<T>::Matrix(const T* a, unsigned int n, unsigned int m)
  792. : v(new T*[n])
  793. {
  794. register unsigned int i, j;
  795. this->n = n; this->m = m;
  796. v[0] = new T[m * n];
  797. for (i = 1; i < n; i++)
  798. v[i] = v[i - 1] + m;
  799. for (i = 0; i < n; i++)
  800. for (j = 0; j < m; j++)
  801. v[i][j] = *a++;
  802. }
  803. template <class T>
  804. Matrix<T>::Matrix(MType t, const T& a, const T& o, unsigned int n, unsigned int m)
  805. : v(new T*[n])
  806. {
  807. register unsigned int i, j;
  808. this->n = n; this->m = m;
  809. v[0] = new T[m * n];
  810. for (i = 1; i < n; i++)
  811. v[i] = v[i - 1] + m;
  812. switch (t)
  813. {
  814. case DIAG:
  815. for (i = 0; i < n; i++)
  816. for (j = 0; j < m; j++)
  817. if (i != j)
  818. v[i][j] = o;
  819. else
  820. v[i][j] = a;
  821. break;
  822. default:
  823. throw std::logic_error("Matrix type not supported");
  824. }
  825. }
  826. template <class T>
  827. Matrix<T>::Matrix(MType t, const Vector<T>& a, const T& o, unsigned int n, unsigned int m)
  828. : v(new T*[n])
  829. {
  830. register unsigned int i, j;
  831. this->n = n; this->m = m;
  832. v[0] = new T[m * n];
  833. for (i = 1; i < n; i++)
  834. v[i] = v[i - 1] + m;
  835. switch (t)
  836. {
  837. case DIAG:
  838. for (i = 0; i < n; i++)
  839. for (j = 0; j < m; j++)
  840. if (i != j)
  841. v[i][j] = o;
  842. else
  843. v[i][j] = a[i];
  844. break;
  845. default:
  846. throw std::logic_error("Matrix type not supported");
  847. }
  848. }
  849. template <typename T>
  850. Matrix<T>::Matrix(const Matrix<T>& rhs)
  851. : v(new T*[rhs.n])
  852. {
  853. register unsigned int i, j;
  854. n = rhs.n; m = rhs.m;
  855. v[0] = new T[m * n];
  856. for (i = 1; i < n; i++)
  857. v[i] = v[i - 1] + m;
  858. for (i = 0; i < n; i++)
  859. for (j = 0; j < m; j++)
  860. v[i][j] = rhs[i][j];
  861. }
  862. template <typename T>
  863. Matrix<T>::~Matrix()
  864. {
  865. if (v != 0) {
  866. delete[] (v[0]);
  867. delete[] (v);
  868. }
  869. }
  870. template <typename T>
  871. inline Matrix<T>& Matrix<T>::operator=(const Matrix<T> &rhs)
  872. // postcondition: normal assignment via copying has been performed;
  873. // if matrix and rhs were different sizes, matrix
  874. // has been resized to match the size of rhs
  875. {
  876. if (this != &rhs)
  877. {
  878. register unsigned int i, j;
  879. resize(rhs.n, rhs.m);
  880. for (i = 0; i < n; i++)
  881. for (j = 0; j < m; j++)
  882. v[i][j] = rhs[i][j];
  883. }
  884. return *this;
  885. }
  886. template <typename T>
  887. inline Matrix<T>& Matrix<T>::operator=(const T& a) // assign a to every element
  888. {
  889. register unsigned int i, j;
  890. for (i = 0; i < n; i++)
  891. for (j = 0; j < m; j++)
  892. v[i][j] = a;
  893. return *this;
  894. }
  895. template <typename T>
  896. inline void Matrix<T>::resize(const unsigned int n, const unsigned int m)
  897. {
  898. register unsigned int i;
  899. if (n == this->n && m == this->m)
  900. return;
  901. if (v != 0)
  902. {
  903. delete[] (v[0]);
  904. delete[] (v);
  905. }
  906. this->n = n; this->m = m;
  907. v = new T*[n];
  908. v[0] = new T[m * n];
  909. for (i = 1; i < n; i++)
  910. v[i] = v[i - 1] + m;
  911. }
  912. template <typename T>
  913. inline void Matrix<T>::resize(const T& a, const unsigned int n, const unsigned int m)
  914. {
  915. register unsigned int i, j;
  916. resize(n, m);
  917. for (i = 0; i < n; i++)
  918. for (j = 0; j < m; j++)
  919. v[i][j] = a;
  920. }
  921. template <typename T>
  922. inline Vector<T> Matrix<T>::extractRow(const unsigned int i) const
  923. {
  924. if (i >= n)
  925. throw std::logic_error("Error in extractRow: trying to extract a row out of matrix bounds");
  926. Vector<T> tmp(v[i], m);
  927. return tmp;
  928. }
  929. template <typename T>
  930. inline Vector<T> Matrix<T>::extractColumn(const unsigned int j) const
  931. {
  932. register unsigned int i;
  933. if (j >= m)
  934. throw std::logic_error("Error in extractRow: trying to extract a row out of matrix bounds");
  935. Vector<T> tmp(n);
  936. for (i = 0; i < n; i++)
  937. tmp[i] = v[i][j];
  938. return tmp;
  939. }
  940. template <typename T>
  941. inline Vector<T> Matrix<T>::extractDiag() const
  942. {
  943. register unsigned int d = std::min(n, m), i;
  944. Vector<T> tmp(d);
  945. for (i = 0; i < d; i++)
  946. tmp[i] = v[i][i];
  947. return tmp;
  948. }
  949. template <typename T>
  950. inline Matrix<T> Matrix<T>::extractRows(const std::set<unsigned int>& indexes) const
  951. {
  952. Matrix<T> tmp(indexes.size(), m);
  953. register unsigned int i = 0, j;
  954. for (std::set<unsigned int>::const_iterator el = indexes.begin(); el != indexes.end(); el++)
  955. {
  956. for (j = 0; j < m; j++)
  957. {
  958. if (*el >= n)
  959. throw std::logic_error("Error extracting rows: the indexes are out of matrix bounds");
  960. tmp[i][j] = v[*el][j];
  961. }
  962. i++;
  963. }
  964. return tmp;
  965. }
  966. template <typename T>
  967. inline Matrix<T> Matrix<T>::extractColumns(const std::set<unsigned int>& indexes) const
  968. {
  969. Matrix<T> tmp(n, indexes.size());
  970. register unsigned int i, j = 0;
  971. for (std::set<unsigned int>::const_iterator el = indexes.begin(); el != indexes.end(); el++)
  972. {
  973. for (i = 0; i < n; i++)
  974. {
  975. if (*el >= m)
  976. throw std::logic_error("Error extracting columns: the indexes are out of matrix bounds");
  977. tmp[i][j] = v[i][*el];
  978. }
  979. j++;
  980. }
  981. return tmp;
  982. }
  983. template <typename T>
  984. inline Matrix<T> Matrix<T>::extract(const std::set<unsigned int>& r_indexes, const std::set<unsigned int>& c_indexes) const
  985. {
  986. Matrix<T> tmp(r_indexes.size(), c_indexes.size());
  987. register unsigned int i = 0, j;
  988. for (std::set<unsigned int>::const_iterator r_el = r_indexes.begin(); r_el != r_indexes.end(); r_el++)
  989. {
  990. if (*r_el >= n)
  991. throw std::logic_error("Error extracting submatrix: the indexes are out of matrix bounds");
  992. j = 0;
  993. for (std::set<unsigned int>::const_iterator c_el = c_indexes.begin(); c_el != c_indexes.end(); c_el++)
  994. {
  995. if (*c_el >= m)
  996. throw std::logic_error("Error extracting rows: the indexes are out of matrix bounds");
  997. tmp[i][j] = v[*r_el][*c_el];
  998. j++;
  999. }
  1000. i++;
  1001. }
  1002. return tmp;
  1003. }
  1004. template <typename T>
  1005. inline void Matrix<T>::setRow(unsigned int i, const Vector<T>& a)
  1006. {
  1007. if (i >= n)
  1008. throw std::logic_error("Error in setRow: trying to set a row out of matrix bounds");
  1009. if (this->m != a.size())
  1010. throw std::logic_error("Error setting matrix row: ranges are not compatible");
  1011. for (unsigned int j = 0; j < ncols(); j++)
  1012. v[i][j] = a[j];
  1013. }
  1014. template <typename T>
  1015. inline void Matrix<T>::setRow(unsigned int i, const Matrix<T>& a)
  1016. {
  1017. if (i >= n)
  1018. throw std::logic_error("Error in setRow: trying to set a row out of matrix bounds");
  1019. if (this->m != a.ncols())
  1020. throw std::logic_error("Error setting matrix column: ranges are not compatible");
  1021. if (a.nrows() != 1)
  1022. throw std::logic_error("Error setting matrix column with a non-row matrix");
  1023. for (unsigned int j = 0; j < ncols(); j++)
  1024. v[i][j] = a[0][j];
  1025. }
  1026. template <typename T>
  1027. inline void Matrix<T>::setRows(const std::set<unsigned int>& indexes, const Matrix<T>& m)
  1028. {
  1029. unsigned int i = 0;
  1030. if (indexes.size() != m.nrows() || this->m != m.ncols())
  1031. throw std::logic_error("Error setting matrix rows: ranges are not compatible");
  1032. for (std::set<unsigned int>::const_iterator el = indexes.begin(); el != indexes.end(); el++)
  1033. {
  1034. for (unsigned int j = 0; j < ncols(); j++)
  1035. {
  1036. if (*el >= n)
  1037. throw std::logic_error("Error in setRows: trying to set a row out of matrix bounds");
  1038. v[*el][j] = m[i][j];
  1039. }
  1040. i++;
  1041. }
  1042. }
  1043. template <typename T>
  1044. inline void Matrix<T>::setColumn(unsigned int j, const Vector<T>& a)
  1045. {
  1046. if (j >= m)
  1047. throw std::logic_error("Error in setColumn: trying to set a column out of matrix bounds");
  1048. if (this->n != a.size())
  1049. throw std::logic_error("Error setting matrix column: ranges are not compatible");
  1050. for (unsigned int i = 0; i < nrows(); i++)
  1051. v[i][j] = a[i];
  1052. }
  1053. template <typename T>
  1054. inline void Matrix<T>::setColumn(unsigned int j, const Matrix<T>& a)
  1055. {
  1056. if (j >= m)
  1057. throw std::logic_error("Error in setColumn: trying to set a column out of matrix bounds");
  1058. if (this->n != a.nrows())
  1059. throw std::logic_error("Error setting matrix column: ranges are not compatible");
  1060. if (a.ncols() != 1)
  1061. throw std::logic_error("Error setting matrix column with a non-column matrix");
  1062. for (unsigned int i = 0; i < nrows(); i++)
  1063. v[i][j] = a[i][0];
  1064. }
  1065. template <typename T>
  1066. inline void Matrix<T>::setColumns(const std::set<unsigned int>& indexes, const Matrix<T>& a)
  1067. {
  1068. unsigned int j = 0;
  1069. if (indexes.size() != a.ncols() || this->n != a.nrows())
  1070. throw std::logic_error("Error setting matrix columns: ranges are not compatible");
  1071. for (std::set<unsigned int>::const_iterator el = indexes.begin(); el != indexes.end(); el++)
  1072. {
  1073. for (unsigned int i = 0; i < nrows(); i++)
  1074. {
  1075. if (*el >= m)
  1076. throw std::logic_error("Error in setColumns: trying to set a column out of matrix bounds");
  1077. v[i][*el] = a[i][j];
  1078. }
  1079. j++;
  1080. }
  1081. }
  1082. template <typename T>
  1083. inline void Matrix<T>::set(const std::set<unsigned int>& r_indexes, const std::set<unsigned int>& c_indexes, const Matrix<T>& a)
  1084. {
  1085. unsigned int i = 0, j;
  1086. if (c_indexes.size() != a.ncols() || r_indexes.size() != a.nrows())
  1087. throw std::logic_error("Error setting matrix elements: ranges are not compatible");
  1088. for (std::set<unsigned int>::const_iterator r_el = r_indexes.begin(); r_el != r_indexes.end(); r_el++)
  1089. {
  1090. if (*r_el >= n)
  1091. throw std::logic_error("Error in set: trying to set a row out of matrix bounds");
  1092. j = 0;
  1093. for (std::set<unsigned int>::const_iterator c_el = c_indexes.begin(); c_el != c_indexes.end(); c_el++)
  1094. {
  1095. if (*c_el >= m)
  1096. throw std::logic_error("Error in set: trying to set a column out of matrix bounds");
  1097. v[*r_el][*c_el] = a[i][j];
  1098. j++;
  1099. }
  1100. i++;
  1101. }
  1102. }
  1103. template <typename T>
  1104. inline void Matrix<T>::set(const T* a, unsigned int n, unsigned int m)
  1105. {
  1106. if (this->n != n || this->m != m)
  1107. resize(n, m);
  1108. unsigned int k = 0;
  1109. for (unsigned int i = 0; i < n; i++)
  1110. for (unsigned int j = 0; j < m; j++)
  1111. v[i][j] = a[k++];
  1112. }
  1113. template <typename T>
  1114. Matrix<T> operator+(const Matrix<T>& rhs)
  1115. {
  1116. return rhs;
  1117. }
  1118. template <typename T>
  1119. Matrix<T> operator+(const Matrix<T>& lhs, const Matrix<T>& rhs)
  1120. {
  1121. if (lhs.ncols() != rhs.ncols() || lhs.nrows() != rhs.nrows())
  1122. throw std::logic_error("Operator+: matrices have different sizes");
  1123. Matrix<T> tmp(lhs.nrows(), lhs.ncols());
  1124. for (unsigned int i = 0; i < lhs.nrows(); i++)
  1125. for (unsigned int j = 0; j < lhs.ncols(); j++)
  1126. tmp[i][j] = lhs[i][j] + rhs[i][j];
  1127. return tmp;
  1128. }
  1129. template <typename T>
  1130. Matrix<T> operator+(const Matrix<T>& lhs, const T& a)
  1131. {
  1132. Matrix<T> tmp(lhs.nrows(), lhs.ncols());
  1133. for (unsigned int i = 0; i < lhs.nrows(); i++)
  1134. for (unsigned int j = 0; j < lhs.ncols(); j++)
  1135. tmp[i][j] = lhs[i][j] + a;
  1136. return tmp;
  1137. }
  1138. template <typename T>
  1139. Matrix<T> operator+(const T& a, const Matrix<T>& rhs)
  1140. {
  1141. Matrix<T> tmp(rhs.nrows(), rhs.ncols());
  1142. for (unsigned int i = 0; i < rhs.nrows(); i++)
  1143. for (unsigned int j = 0; j < rhs.ncols(); j++)
  1144. tmp[i][j] = a + rhs[i][j];
  1145. return tmp;
  1146. }
  1147. template <typename T>
  1148. inline Matrix<T>& Matrix<T>::operator+=(const Matrix<T>& rhs)
  1149. {
  1150. if (m != rhs.ncols() || n != rhs.nrows())
  1151. throw std::logic_error("Operator+=: matrices have different sizes");
  1152. for (unsigned int i = 0; i < n; i++)
  1153. for (unsigned int j = 0; j < m; j++)
  1154. v[i][j] += rhs[i][j];
  1155. return *this;
  1156. }
  1157. template <typename T>
  1158. inline Matrix<T>& Matrix<T>::operator+=(const T& a)
  1159. {
  1160. for (unsigned int i = 0; i < n; i++)
  1161. for (unsigned int j = 0; j < m; j++)
  1162. v[i][j] += a;
  1163. return *this;
  1164. }
  1165. template <typename T>
  1166. Matrix<T> operator-(const Matrix<T>& rhs)
  1167. {
  1168. return (T)(-1) * rhs;
  1169. }
  1170. template <typename T>
  1171. Matrix<T> operator-(const Matrix<T>& lhs, const Matrix<T>& rhs)
  1172. {
  1173. if (lhs.ncols() != rhs.ncols() || lhs.nrows() != rhs.nrows())
  1174. throw std::logic_error("Operator-: matrices have different sizes");
  1175. Matrix<T> tmp(lhs.nrows(), lhs.ncols());
  1176. for (unsigned int i = 0; i < lhs.nrows(); i++)
  1177. for (unsigned int j = 0; j < lhs.ncols(); j++)
  1178. tmp[i][j] = lhs[i][j] - rhs[i][j];
  1179. return tmp;
  1180. }
  1181. template <typename T>
  1182. Matrix<T> operator-(const Matrix<T>& lhs, const T& a)
  1183. {
  1184. Matrix<T> tmp(lhs.nrows(), lhs.ncols());
  1185. for (unsigned int i = 0; i < lhs.nrows(); i++)
  1186. for (unsigned int j = 0; j < lhs.ncols(); j++)
  1187. tmp[i][j] = lhs[i][j] - a;
  1188. return tmp;
  1189. }
  1190. template <typename T>
  1191. Matrix<T> operator-(const T& a, const Matrix<T>& rhs)
  1192. {
  1193. Matrix<T> tmp(rhs.nrows(), rhs.ncols());
  1194. for (unsigned int i = 0; i < rhs.nrows(); i++)
  1195. for (unsigned int j = 0; j < rhs.ncols(); j++)
  1196. tmp[i][j] = a - rhs[i][j];
  1197. return tmp;
  1198. }
  1199. template <typename T>
  1200. inline Matrix<T>& Matrix<T>::operator-=(const Matrix<T>& rhs)
  1201. {
  1202. if (m != rhs.ncols() || n != rhs.nrows())
  1203. throw std::logic_error("Operator-=: matrices have different sizes");
  1204. for (unsigned int i = 0; i < n; i++)
  1205. for (unsigned int j = 0; j < m; j++)
  1206. v[i][j] -= rhs[i][j];
  1207. return *this;
  1208. }
  1209. template <typename T>
  1210. inline Matrix<T>& Matrix<T>::operator-=(const T& a)
  1211. {
  1212. for (unsigned int i = 0; i < n; i++)
  1213. for (unsigned int j = 0; j < m; j++)
  1214. v[i][j] -= a;
  1215. return *this;
  1216. }
  1217. template <typename T>
  1218. Matrix<T> operator*(const Matrix<T>& lhs, const Matrix<T>& rhs)
  1219. {
  1220. if (lhs.ncols() != rhs.ncols() || lhs.nrows() != rhs.nrows())
  1221. throw std::logic_error("Operator*: matrices have different sizes");
  1222. Matrix<T> tmp(lhs.nrows(), lhs.ncols());
  1223. for (unsigned int i = 0; i < lhs.nrows(); i++)
  1224. for (unsigned int j = 0; j < lhs.ncols(); j++)
  1225. tmp[i][j] = lhs[i][j] * rhs[i][j];
  1226. return tmp;
  1227. }
  1228. template <typename T>
  1229. Matrix<T> operator*(const Matrix<T>& lhs, const T& a)
  1230. {
  1231. Matrix<T> tmp(lhs.nrows(), lhs.ncols());
  1232. for (unsigned int i = 0; i < lhs.nrows(); i++)
  1233. for (unsigned int j = 0; j < lhs.ncols(); j++)
  1234. tmp[i][j] = lhs[i][j] * a;
  1235. return tmp;
  1236. }
  1237. template <typename T>
  1238. Matrix<T> operator*(const T& a, const Matrix<T>& rhs)
  1239. {
  1240. Matrix<T> tmp(rhs.nrows(), rhs.ncols());
  1241. for (unsigned int i = 0; i < rhs.nrows(); i++)
  1242. for (unsigned int j = 0; j < rhs.ncols(); j++)
  1243. tmp[i][j] = a * rhs[i][j];
  1244. return tmp;
  1245. }
  1246. template <typename T>
  1247. inline Matrix<T>& Matrix<T>::operator*=(const Matrix<T>& rhs)
  1248. {
  1249. if (m != rhs.ncols() || n != rhs.nrows())
  1250. throw std::logic_error("Operator*=: matrices have different sizes");
  1251. for (unsigned int i = 0; i < n; i++)
  1252. for (unsigned int j = 0; j < m; j++)
  1253. v[i][j] *= rhs[i][j];
  1254. return *this;
  1255. }
  1256. template <typename T>
  1257. inline Matrix<T>& Matrix<T>::operator*=(const T& a)
  1258. {
  1259. for (unsigned int i = 0; i < n; i++)
  1260. for (unsigned int j = 0; j < m; j++)
  1261. v[i][j] *= a;
  1262. return *this;
  1263. }
  1264. template <typename T>
  1265. Matrix<T> operator/(const Matrix<T>& lhs, const Matrix<T>& rhs)
  1266. {
  1267. if (lhs.ncols() != rhs.ncols() || lhs.nrows() != rhs.nrows())
  1268. throw std::logic_error("Operator+: matrices have different sizes");
  1269. Matrix<T> tmp(lhs.nrows(), lhs.ncols());
  1270. for (unsigned int i = 0; i < lhs.nrows(); i++)
  1271. for (unsigned int j = 0; j < lhs.ncols(); j++)
  1272. tmp[i][j] = lhs[i][j] / rhs[i][j];
  1273. return tmp;
  1274. }
  1275. template <typename T>
  1276. Matrix<T> operator/(const Matrix<T>& lhs, const T& a)
  1277. {
  1278. Matrix<T> tmp(lhs.nrows(), lhs.ncols());
  1279. for (unsigned int i = 0; i < lhs.nrows(); i++)
  1280. for (unsigned int j = 0; j < lhs.ncols(); j++)
  1281. tmp[i][j] = lhs[i][j] / a;
  1282. return tmp;
  1283. }
  1284. template <typename T>
  1285. Matrix<T> operator/(const T& a, const Matrix<T>& rhs)
  1286. {
  1287. Matrix<T> tmp(rhs.nrows(), rhs.ncols());
  1288. for (unsigned int i = 0; i < rhs.nrows(); i++)
  1289. for (unsigned int j = 0; j < rhs.ncols(); j++)
  1290. tmp[i][j] = a / rhs[i][j];
  1291. return tmp;
  1292. }
  1293. template <typename T>
  1294. inline Matrix<T>& Matrix<T>::operator/=(const Matrix<T>& rhs)
  1295. {
  1296. if (m != rhs.ncols() || n != rhs.nrows())
  1297. throw std::logic_error("Operator+=: matrices have different sizes");
  1298. for (unsigned int i = 0; i < n; i++)
  1299. for (unsigned int j = 0; j < m; j++)
  1300. v[i][j] /= rhs[i][j];
  1301. return *this;
  1302. }
  1303. template <typename T>
  1304. inline Matrix<T>& Matrix<T>::operator/=(const T& a)
  1305. {
  1306. for (unsigned int i = 0; i < n; i++)
  1307. for (unsigned int j = 0; j < m; j++)
  1308. v[i][j] /= a;
  1309. return *this;
  1310. }
  1311. template <typename T>
  1312. Matrix<T> operator^(const Matrix<T>& lhs, const T& a)
  1313. {
  1314. Matrix<T> tmp(lhs.nrows(), lhs.ncols());
  1315. for (unsigned int i = 0; i < lhs.nrows(); i++)
  1316. for (unsigned int j = 0; j < lhs.ncols(); j++)
  1317. tmp[i][j] = pow(lhs[i][j], a);
  1318. return tmp;
  1319. }
  1320. template <typename T>
  1321. inline Matrix<T>& Matrix<T>::operator^=(const Matrix<T>& rhs)
  1322. {
  1323. if (m != rhs.ncols() || n != rhs.nrows())
  1324. throw std::logic_error("Operator^=: matrices have different sizes");
  1325. for (unsigned int i = 0; i < n; i++)
  1326. for (unsigned int j = 0; j < m; j++)
  1327. v[i][j] = pow(v[i][j], rhs[i][j]);
  1328. return *this;
  1329. }
  1330. template <typename T>
  1331. inline Matrix<T>& Matrix<T>::operator^=(const T& a)
  1332. {
  1333. for (unsigned int i = 0; i < n; i++)
  1334. for (unsigned int j = 0; j < m; j++)
  1335. v[i][j] = pow(v[i][j], a);
  1336. return *this;
  1337. }
  1338. template <typename T>
  1339. inline Matrix<T>::operator Vector<T>()
  1340. {
  1341. if (n > 1 && m > 1)
  1342. throw std::logic_error("Error matrix cast to vector: trying to cast a multi-dimensional matrix");
  1343. if (n == 1)
  1344. return extractRow(0);
  1345. else
  1346. return extractColumn(0);
  1347. }
  1348. template <typename T>
  1349. inline bool operator==(const Matrix<T>& a, const Matrix<T>& b)
  1350. {
  1351. if (a.nrows() != b.nrows() || a.ncols() != b.ncols())
  1352. throw std::logic_error("Matrices of different size are not confrontable");
  1353. for (unsigned i = 0; i < a.nrows(); i++)
  1354. for (unsigned j = 0; j < a.ncols(); j++)
  1355. if (a[i][j] != b[i][j])
  1356. return false;
  1357. return true;
  1358. }
  1359. template <typename T>
  1360. inline bool operator!=(const Matrix<T>& a, const Matrix<T>& b)
  1361. {
  1362. if (a.nrows() != b.nrows() || a.ncols() != b.ncols())
  1363. throw std::logic_error("Matrices of different size are not confrontable");
  1364. for (unsigned i = 0; i < a.nrows(); i++)
  1365. for (unsigned j = 0; j < a.ncols(); j++)
  1366. if (a[i][j] != b[i][j])
  1367. return true;
  1368. return false;
  1369. }
  1370. /**
  1371. Input/Output
  1372. */
  1373. template <typename T>
  1374. std::ostream& operator<<(std::ostream& os, const Matrix<T>& m)
  1375. {
  1376. os << std::endl << m.nrows() << " " << m.ncols() << std::endl;
  1377. for (unsigned int i = 0; i < m.nrows(); i++)
  1378. {
  1379. for (unsigned int j = 0; j < m.ncols() - 1; j++)
  1380. os << std::setw(20) << std::setprecision(16) << m[i][j] << ", ";
  1381. os << std::setw(20) << std::setprecision(16) << m[i][m.ncols() - 1] << std::endl;
  1382. }
  1383. return os;
  1384. }
  1385. template <typename T>
  1386. std::istream& operator>>(std::istream& is, Matrix<T>& m)
  1387. {
  1388. int rows, cols;
  1389. char comma;
  1390. is >> rows >> cols;
  1391. m.resize(rows, cols);
  1392. for (unsigned int i = 0; i < rows; i++)
  1393. for (unsigned int j = 0; j < cols; j++)
  1394. is >> m[i][j] >> comma;
  1395. return is;
  1396. }
  1397. template <typename T>
  1398. T sign(const T& v)
  1399. {
  1400. if (v >= (T)0.0)
  1401. return (T)1.0;
  1402. else
  1403. return (T)-1.0;
  1404. }
  1405. template <typename T>
  1406. T dist(const T& a, const T& b)
  1407. {
  1408. T abs_a = (T)fabs(a), abs_b = (T)fabs(b);
  1409. if (abs_a > abs_b)
  1410. return abs_a * sqrt((T)1.0 + (abs_b / abs_a) * (abs_b / abs_a));
  1411. else
  1412. return (abs_b == (T)0.0 ? (T)0.0 : abs_b * sqrt((T)1.0 + (abs_a / abs_b) * (abs_a / abs_b)));
  1413. }
  1414. template <typename T>
  1415. void svd(const Matrix<T>& A, Matrix<T>& U, Vector<T>& W, Matrix<T>& V)
  1416. {
  1417. int m = A.nrows(), n = A.ncols(), i, j, k, l, jj, nm;
  1418. const unsigned int max_its = 30;
  1419. bool flag;
  1420. Vector<T> rv1(n);
  1421. U = A;
  1422. W.resize(n);
  1423. V.resize(n, n);
  1424. T anorm, c, f, g, h, s, scale, x, y, z;
  1425. g = scale = anorm = (T)0.0;
  1426. // Householder reduction to bidiagonal form
  1427. for (i = 0; i < n; i++)
  1428. {
  1429. l = i + 1;
  1430. rv1[i] = scale * g;
  1431. g = s = scale = (T)0.0;
  1432. if (i < m)
  1433. {
  1434. for (k = i; k < m; k++)
  1435. scale += fabs(U[k][i]);
  1436. if (scale != (T)0.0)
  1437. {
  1438. for (k = i; k < m; k++)
  1439. {
  1440. U[k][i] /= scale;
  1441. s += U[k][i] * U[k][i];
  1442. }
  1443. f = U[i][i];
  1444. g = -sign(f) * sqrt(s);
  1445. h = f * g - s;
  1446. U[i][i] = f - g;
  1447. for (j = l; j < n; j++)
  1448. {
  1449. s = (T)0.0;
  1450. for (k = i; k < m; k++)
  1451. s += U[k][i] * U[k][j];
  1452. f = s / h;
  1453. for (k = i; k < m; k++)
  1454. U[k][j] += f * U[k][i];
  1455. }
  1456. for (k = i; k < m; k++)
  1457. U[k][i] *= scale;
  1458. }
  1459. }
  1460. W[i] = scale * g;
  1461. g = s = scale = (T)0.0;
  1462. if (i < m && i != n - 1)
  1463. {
  1464. for (k = l; k < n; k++)
  1465. scale += fabs(U[i][k]);
  1466. if (scale != (T)0.0)
  1467. {
  1468. for (k = l; k < n; k++)
  1469. {
  1470. U[i][k] /= scale;
  1471. s += U[i][k] * U[i][k];
  1472. }
  1473. f = U[i][l];
  1474. g = -sign(f) * sqrt(s);
  1475. h = f * g - s;
  1476. U[i][l] = f - g;
  1477. for (k = l; k <n; k++)
  1478. rv1[k] = U[i][k] / h;
  1479. for (j = l; j < m; j++)
  1480. {
  1481. s = (T)0.0;
  1482. for (k = l; k < n; k++)
  1483. s += U[j][k] * U[i][k];
  1484. for (k = l; k < n; k++)
  1485. U[j][k] += s * rv1[k];
  1486. }
  1487. for (k = l; k < n; k++)
  1488. U[i][k] *= scale;
  1489. }
  1490. }
  1491. anorm = std::max(anorm, fabs(W[i]) + fabs(rv1[i]));
  1492. }
  1493. // Accumulation of right-hand transformations
  1494. for (i = n - 1; i >= 0; i--)
  1495. {
  1496. if (i < n - 1)
  1497. {
  1498. if (g != (T)0.0)
  1499. {
  1500. for (j = l; j < n; j++)
  1501. V[j][i] = (U[i][j] / U[i][l]) / g;
  1502. for (j = l; j < n; j++)
  1503. {
  1504. s = (T)0.0;
  1505. for (k = l; k < n; k++)
  1506. s += U[i][k] * V[k][j];
  1507. for (k = l; k < n; k++)
  1508. V[k][j] += s * V[k][i];
  1509. }
  1510. }
  1511. for (j = l; j < n; j++)
  1512. V[i][j] = V[j][i] = (T)0.0;
  1513. }
  1514. V[i][i] = (T)1.0;
  1515. g = rv1[i];
  1516. l = i;
  1517. }
  1518. // Accumulation of left-hand transformations
  1519. for (i = std::min(m, n) - 1; i >= 0; i--)
  1520. {
  1521. l = i + 1;
  1522. g = W[i];
  1523. for (j = l; j < n; j++)
  1524. U[i][j] = (T)0.0;
  1525. if (g != (T)0.0)
  1526. {
  1527. g = (T)1.0 / g;
  1528. for (j = l; j < n; j++)
  1529. {
  1530. s = (T)0.0;
  1531. for (k = l; k < m; k++)
  1532. s += U[k][i] * U[k][j];
  1533. f = (s / U[i][i]) * g;
  1534. for (k = i; k < m; k++)
  1535. U[k][j] += f * U[k][i];
  1536. }
  1537. for (j = i; j < m; j++)
  1538. U[j][i] *= g;
  1539. }
  1540. else
  1541. for (j = i; j < m; j++)
  1542. U[j][i] = (T)0.0;
  1543. U[i][i]++;
  1544. }
  1545. // Diagonalization of the bidiagonal form: loop over singular values, and over allowed iterations.
  1546. for (k = n - 1; k >= 0; k--)
  1547. {
  1548. for (unsigned int its = 0; its < max_its; its++)
  1549. {
  1550. flag = true;
  1551. for (l = k; l >= 0; l--) // FIXME: in NR it was l >= 1 but there subscripts start from one
  1552. { // Test for splitting
  1553. nm = l - 1; // Note that rV[0] is always zero
  1554. if ((T)(fabs(rv1[l]) + anorm) == anorm)
  1555. {
  1556. flag = false;
  1557. break;
  1558. }
  1559. if ((T)(fabs(W[nm]) + anorm) == anorm)
  1560. break;
  1561. }
  1562. if (flag)
  1563. {
  1564. // Cancellation of rv1[l], if l > 0 FIXME: it was l > 1 in NR
  1565. c = (T)0.0;
  1566. s = (T)1.0;
  1567. for (i = l; i <= k; i++)
  1568. {
  1569. f = s * rv1[i];
  1570. rv1[i] *= c;
  1571. if ((T)(fabs(f) + anorm) == anorm)
  1572. break;
  1573. g = W[i];
  1574. h = dist(f, g);
  1575. W[i] = h;
  1576. h = (T)1.0 / h;
  1577. c = g * h;
  1578. s = -f * h;
  1579. for (j = 0; j < m; j++)
  1580. {
  1581. y = U[j][nm];
  1582. z = U[j][i];
  1583. U[j][nm] = y * c + z * s;
  1584. U[j][i] = z * c - y * s;
  1585. }
  1586. }
  1587. }
  1588. z = W[k];
  1589. if (l == k)
  1590. { // Convergence
  1591. if (z < (T)0.0)
  1592. { // Singular value is made nonnegative
  1593. W[k] = -z;
  1594. for (j = 0; j < n; j++)
  1595. V[j][k] = -V[j][k];
  1596. }
  1597. break;
  1598. }
  1599. if (its == max_its)
  1600. throw std::logic_error("Error svd: no convergence in the maximum number of iterations");
  1601. x = W[l];
  1602. nm = k - 1;
  1603. y = W[nm];
  1604. g = rv1[nm];
  1605. h = rv1[k];
  1606. f = ((y - z) * (y + z) + (g - h) * (g + h)) / (2.0 * h * y);
  1607. g = dist(f, (T)1.0);
  1608. f = ((x - z) * (x + z) + h * ((y / (f + sign(f)*fabs(g))) - h)) / x;
  1609. c = s = (T)1.0; // Next QR transformation
  1610. for (j = l; j <= nm; j++)
  1611. {
  1612. i = j + 1;
  1613. g = rv1[i];
  1614. y = W[i];
  1615. h = s * g;
  1616. g *= c;
  1617. z = dist(f, h);
  1618. rv1[j] = z;
  1619. c = f / z;
  1620. s = h / z;
  1621. f = x * c + g * s;
  1622. g = g * c - x * s;
  1623. h = y * s;
  1624. y *= c;
  1625. for (jj = 0; jj < n; jj++)
  1626. {
  1627. x = V[jj][j];
  1628. z = V[jj][i];
  1629. V[jj][j] = x * c + z * s;
  1630. V[jj][i] = z * c - x * s;
  1631. }
  1632. z = dist(f, h);
  1633. W[j] = z;
  1634. if (z != 0) // Rotation can be arbitrary if z = 0
  1635. {
  1636. z = (T)1.0 / z;
  1637. c = f * z;
  1638. s = h * z;
  1639. }
  1640. f = c * g + s * y;
  1641. x = c * y - s * g;
  1642. for (jj = 0; jj < m; jj++)
  1643. {
  1644. y = U[jj][j];
  1645. z = U[jj][i];
  1646. U[jj][j] = y * c + z * s;
  1647. U[jj][i] = z * c - y * s;
  1648. }
  1649. }
  1650. rv1[l] = (T)0.0;
  1651. rv1[k] = f;
  1652. W[k] = x;
  1653. }
  1654. }
  1655. }
  1656. template <typename T>
  1657. Matrix<T> pinv(const Matrix<T>& A)
  1658. {
  1659. Matrix<T> U, V, x, tmp(A.ncols(), A.nrows());
  1660. Vector<T> W;
  1661. CanonicalBaseVector<T> e(0, A.nrows());
  1662. svd(A, U, W, V);
  1663. for (unsigned int i = 0; i < A.nrows(); i++)
  1664. {
  1665. e.reset(i);
  1666. tmp.setColumn(i, dot_prod(dot_prod(dot_prod(V, Matrix<double>(DIAG, 1.0 / W, 0.0, W.size(), W.size())), t(U)), e));
  1667. }
  1668. return tmp;
  1669. }
  1670. template <typename T>
  1671. int lu(const Matrix<T>& A, Matrix<T>& LU, Vector<unsigned int>& index)
  1672. {
  1673. if (A.ncols() != A.nrows())
  1674. throw std::logic_error("Error in LU decomposition: matrix must be squared");
  1675. int i, p, j, k, n = A.ncols(), ex;
  1676. T val, tmp;
  1677. Vector<T> d(n);
  1678. LU = A;
  1679. index.resize(n);
  1680. ex = 1;
  1681. for (i = 0; i < n; i++)
  1682. {
  1683. index[i] = i;
  1684. val = (T)0.0;
  1685. for (j = 0; j < n; j++)
  1686. val = std::max(val, (T)fabs(LU[i][j]));
  1687. if (val == (T)0.0)
  1688. std::logic_error("Error in LU decomposition: matrix was singular");
  1689. d[i] = val;
  1690. }
  1691. for (k = 0; k < n - 1; k++)
  1692. {
  1693. p = k;
  1694. val = fabs(LU[k][k]) / d[k];
  1695. for (i = k + 1; i < n; i++)
  1696. {
  1697. tmp = fabs(LU[i][k]) / d[i];
  1698. if (tmp > val)
  1699. {
  1700. val = tmp;
  1701. p = i;
  1702. }
  1703. }
  1704. if (val == (T)0.0)
  1705. std::logic_error("Error in LU decomposition: matrix was singular");
  1706. if (p > k)
  1707. {
  1708. ex = -ex;
  1709. std::swap(index[k], index[p]);
  1710. std::swap(d[k], d[p]);
  1711. for (j = 0; j < n; j++)
  1712. std::swap(LU[k][j], LU[p][j]);
  1713. }
  1714. for (i = k + 1; i < n; i++)
  1715. {
  1716. LU[i][k] /= LU[k][k];
  1717. for (j = k + 1; j < n; j++)
  1718. LU[i][j] -= LU[i][k] * LU[k][j];
  1719. }
  1720. }
  1721. if (LU[n - 1][n - 1] == (T)0.0)
  1722. std::logic_error("Error in LU decomposition: matrix was singular");
  1723. return ex;
  1724. }
  1725. template <typename T>
  1726. Vector<T> lu_solve(const Matrix<T>& LU, const Vector<T>& b, Vector<unsigned int>& index)
  1727. {
  1728. if (LU.ncols() != LU.nrows())
  1729. throw std::logic_error("Error in LU solve: LU matrix should be squared");
  1730. unsigned int n = LU.ncols();
  1731. if (b.size() != n)
  1732. throw std::logic_error("Error in LU solve: b vector must be of the same dimensions of LU matrix");
  1733. Vector<T> x((T)0.0, n);
  1734. int i, j, p;
  1735. T sum;
  1736. p = index[0];
  1737. x[0] = b[p];
  1738. for (i = 1; i < n; i++)
  1739. {
  1740. sum = (T)0.0;
  1741. for (j = 0; j < i; j++)
  1742. sum += LU[i][j] * x[j];
  1743. p = index[i];
  1744. x[i] = b[p] - sum;
  1745. }
  1746. x[n - 1] /= LU[n - 1][n - 1];
  1747. for (i = n - 2; i >= 0; i--)
  1748. {
  1749. sum = (T)0.0;
  1750. for (j = i + 1; j < n; j++)
  1751. sum += LU[i][j] * x[j];
  1752. x[i] = (x[i] - sum) / LU[i][i];
  1753. }
  1754. return x;
  1755. }
  1756. template <typename T>
  1757. void lu_solve(const Matrix<T>& LU, Vector<T>& x, const Vector<T>& b, Vector<unsigned int>& index)
  1758. {
  1759. x = lu_solve(LU, b, index);
  1760. }
  1761. template <typename T>
  1762. Matrix<T> lu_inverse(const Matrix<T>& A)
  1763. {
  1764. if (A.ncols() != A.nrows())
  1765. throw std::logic_error("Error in LU invert: matrix must be squared");
  1766. unsigned int n = A.ncols();
  1767. Matrix<T> A1(n, n), LU;
  1768. Vector<unsigned int> index;
  1769. lu(A, LU, index);
  1770. CanonicalBaseVector<T> e(0, n);
  1771. for (unsigned i = 0; i < n; i++)
  1772. {
  1773. e.reset(i);
  1774. A1.setColumn(i, lu_solve(LU, e, index));
  1775. }
  1776. return A1;
  1777. }
  1778. template <typename T>
  1779. T lu_det(const Matrix<T>& A)
  1780. {
  1781. if (A.ncols() != A.nrows())
  1782. throw std::logic_error("Error in LU determinant: matrix must be squared");
  1783. unsigned int d;
  1784. Matrix<T> LU;
  1785. Vector<unsigned int> index;
  1786. d = lu(A, LU, index);
  1787. return d * prod(LU.extractDiag());
  1788. }
  1789. template <typename T>
  1790. void cholesky(const Matrix<T> A, Matrix<T>& LL)
  1791. {
  1792. if (A.ncols() != A.nrows())
  1793. throw std::logic_error("Error in Cholesky decomposition: matrix must be squared");
  1794. register int i, j, k, n = A.ncols();
  1795. register double sum;
  1796. LL = A;
  1797. for (i = 0; i < n; i++)
  1798. {
  1799. for (j = i; j < n; j++)
  1800. {
  1801. sum = LL[i][j];
  1802. for (k = i - 1; k >= 0; k--)
  1803. sum -= LL[i][k] * LL[j][k];
  1804. if (i == j)
  1805. {
  1806. if (sum <= 0.0)
  1807. throw std::logic_error("Error in Cholesky decomposition: matrix is not postive definite");
  1808. LL[i][i] = ::std::sqrt(sum);
  1809. }
  1810. else
  1811. LL[j][i] = sum / LL[i][i];
  1812. }
  1813. for (k = i + 1; k < n; k++)
  1814. LL[i][k] = LL[k][i];
  1815. }
  1816. }
  1817. template <typename T>
  1818. Matrix<T> cholesky(const Matrix<T> A)
  1819. {
  1820. Matrix<T> LL;
  1821. cholesky(A, LL);
  1822. return LL;
  1823. }
  1824. template <typename T>
  1825. Vector<T> cholesky_solve(const Matrix<T>& LL, const Vector<T>& b)
  1826. {
  1827. if (LL.ncols() != LL.nrows())
  1828. throw std::logic_error("Error in Cholesky solve: matrix must be squared");
  1829. unsigned int n = LL.ncols();
  1830. if (b.size() != n)
  1831. throw std::logic_error("Error in Cholesky decomposition: b vector must be of the same dimensions of LU matrix");
  1832. Vector<T> x, y;
  1833. /* Solve L * y = b */
  1834. forward_elimination(LL, y, b);
  1835. /* Solve L^T * x = y */
  1836. backward_elimination(LL, x, y);
  1837. return x;
  1838. }
  1839. template <typename T>
  1840. void cholesky_solve(const Matrix<T>& LL, Vector<T>& x, const Vector<T>& b)
  1841. {
  1842. x = cholesky_solve(LL, b);
  1843. }
  1844. template <typename T>
  1845. void forward_elimination(const Matrix<T>& L, Vector<T>& y, const Vector<T> b)
  1846. {
  1847. if (L.ncols() != L.nrows())
  1848. throw std::logic_error("Error in Forward elimination: matrix must be squared (lower triangular)");
  1849. if (b.size() != L.nrows())
  1850. throw std::logic_error("Error in Forward elimination: b vector must be of the same dimensions of L matrix");
  1851. register int i, j, n = b.size();
  1852. y.resize(n);
  1853. y[0] = b[0] / L[0][0];
  1854. for (i = 1; i < n; i++)
  1855. {
  1856. y[i] = b[i];
  1857. for (j = 0; j < i; j++)
  1858. y[i] -= L[i][j] * y[j];
  1859. y[i] = y[i] / L[i][i];
  1860. }
  1861. }
  1862. template <typename T>
  1863. Vector<T> forward_elimination(const Matrix<T>& L, const Vector<T> b)
  1864. {
  1865. Vector<T> y;
  1866. forward_elimination(L, y, b);
  1867. return y;
  1868. }
  1869. template <typename T>
  1870. void backward_elimination(const Matrix<T>& U, Vector<T>& x, const Vector<T>& y)
  1871. {
  1872. if (U.ncols() != U.nrows())
  1873. throw std::logic_error("Error in Backward elimination: matrix must be squared (upper triangular)");
  1874. if (y.size() != U.nrows())
  1875. throw std::logic_error("Error in Backward elimination: b vector must be of the same dimensions of U matrix");
  1876. register int i, j, n = y.size();
  1877. x.resize(n);
  1878. x[n - 1] = y[n - 1] / U[n - 1][n - 1];
  1879. for (i = n - 2; i >= 0; i--)
  1880. {
  1881. x[i] = y[i];
  1882. for (j = i + 1; j < n; j++)
  1883. x[i] -= U[i][j] * x[j];
  1884. x[i] = x[i] / U[i][i];
  1885. }
  1886. }
  1887. template <typename T>
  1888. Vector<T> backward_elimination(const Matrix<T>& U, const Vector<T> y)
  1889. {
  1890. Vector<T> x;
  1891. forward_elimination(U, x, y);
  1892. return x;
  1893. }
  1894. /* Setting default linear systems machinery */
  1895. #define det lu_det
  1896. #define inverse lu_inverse
  1897. #define solve lu_solve
  1898. /* Random */
  1899. template <typename T>
  1900. void random(Matrix<T>& m)
  1901. {
  1902. for (unsigned int i = 0; i < m.nrows(); i++)
  1903. for (unsigned int j = 0; j < m.ncols(); j++)
  1904. m[i][j] = (T)(rand() / double(RAND_MAX));
  1905. }
  1906. /**
  1907. Aggregate functions
  1908. */
  1909. template <typename T>
  1910. Vector<T> sum(const Matrix<T>& m)
  1911. {
  1912. Vector<T> tmp((T)0, m.ncols());
  1913. for (unsigned int j = 0; j < m.ncols(); j++)
  1914. for (unsigned int i = 0; i < m.nrows(); i++)
  1915. tmp[j] += m[i][j];
  1916. return tmp;
  1917. }
  1918. template <typename T>
  1919. Vector<T> r_sum(const Matrix<T>& m)
  1920. {
  1921. Vector<T> tmp((T)0, m.nrows());
  1922. for (unsigned int i = 0; i < m.nrows(); i++)
  1923. for (unsigned int j = 0; j < m.ncols(); j++)
  1924. tmp[i] += m[i][j];
  1925. return tmp;
  1926. }
  1927. template <typename T>
  1928. T all_sum(const Matrix<T>& m)
  1929. {
  1930. T tmp = (T)0;
  1931. for (unsigned int i = 0; i < m.nrows(); i++)
  1932. for (unsigned int j = 0; j < m.ncols(); j++)
  1933. tmp += m[i][j];
  1934. return tmp;
  1935. }
  1936. template <typename T>
  1937. Vector<T> prod(const Matrix<T>& m)
  1938. {
  1939. Vector<T> tmp((T)1, m.ncols());
  1940. for (unsigned int j = 0; j < m.ncols(); j++)
  1941. for (unsigned int i = 0; i < m.nrows(); i++)
  1942. tmp[j] *= m[i][j];
  1943. return tmp;
  1944. }
  1945. template <typename T>
  1946. Vector<T> r_prod(const Matrix<T>& m)
  1947. {
  1948. Vector<T> tmp((T)1, m.nrows());
  1949. for (unsigned int i = 0; i < m.nrows(); i++)
  1950. for (unsigned int j = 0; j < m.ncols(); j++)
  1951. tmp[i] *= m[i][j];
  1952. return tmp;
  1953. }
  1954. template <typename T>
  1955. T all_prod(const Matrix<T>& m)
  1956. {
  1957. T tmp = (T)1;
  1958. for (unsigned int i = 0; i < m.nrows(); i++)
  1959. for (unsigned int j = 0; j < m.ncols(); j++)
  1960. tmp *= m[i][j];
  1961. return tmp;
  1962. }
  1963. template <typename T>
  1964. Vector<T> mean(const Matrix<T>& m)
  1965. {
  1966. Vector<T> res((T)0, m.ncols());
  1967. for (unsigned int j = 0; j < m.ncols(); j++)
  1968. {
  1969. for (unsigned int i = 0; i < m.nrows(); i++)
  1970. res[j] += m[i][j];
  1971. res[j] /= m.nrows();
  1972. }
  1973. return res;
  1974. }
  1975. template <typename T>
  1976. Vector<T> r_mean(const Matrix<T>& m)
  1977. {
  1978. Vector<T> res((T)0, m.rows());
  1979. for (unsigned int i = 0; i < m.nrows(); i++)
  1980. {
  1981. for (unsigned int j = 0; j < m.ncols(); j++)
  1982. res[i] += m[i][j];
  1983. res[i] /= m.nrows();
  1984. }
  1985. return res;
  1986. }
  1987. template <typename T>
  1988. T all_mean(const Matrix<T>& m)
  1989. {
  1990. T tmp = (T)0;
  1991. for (unsigned int i = 0; i < m.nrows(); i++)
  1992. for (unsigned int j = 0; j < m.ncols(); j++)
  1993. tmp += m[i][j];
  1994. return tmp / (m.nrows() * m.ncols());
  1995. }
  1996. template <typename T>
  1997. Vector<T> var(const Matrix<T>& m, bool sample_correction = false)
  1998. {
  1999. Vector<T> res((T)0, m.ncols());
  2000. unsigned int n = m.nrows();
  2001. double sum, ssum;
  2002. for (unsigned int j = 0; j < m.ncols(); j++)
  2003. {
  2004. sum = (T)0.0; ssum = (T)0.0;
  2005. for (unsigned int i = 0; i < m.nrows(); i++)
  2006. {
  2007. sum += m[i][j];
  2008. ssum += (m[i][j] * m[i][j]);
  2009. }
  2010. if (!sample_correction)
  2011. res[j] = (ssum / n) - (sum / n) * (sum / n);
  2012. else
  2013. res[j] = n * ((ssum / n) - (sum / n) * (sum / n)) / (n - 1);
  2014. }
  2015. return res;
  2016. }
  2017. template <typename T>
  2018. Vector<T> stdev(const Matrix<T>& m, bool sample_correction = false)
  2019. {
  2020. return sqrt(var(m, sample_correction));
  2021. }
  2022. template <typename T>
  2023. Vector<T> r_var(const Matrix<T>& m, bool sample_correction = false)
  2024. {
  2025. Vector<T> res((T)0, m.nrows());
  2026. double sum, ssum;
  2027. unsigned int n = m.ncols();
  2028. for (unsigned int i = 0; i < m.nrows(); i++)
  2029. {
  2030. sum = 0.0; ssum = 0.0;
  2031. for (unsigned int j = 0; j < m.ncols(); j++)
  2032. {
  2033. sum += m[i][j];
  2034. ssum += (m[i][j] * m[i][j]);
  2035. }
  2036. if (!sample_correction)
  2037. res[i] = (ssum / n) - (sum / n) * (sum / n);
  2038. else
  2039. res[i] = n * ((ssum / n) - (sum / n) * (sum / n)) / (n - 1);
  2040. }
  2041. return res;
  2042. }
  2043. template <typename T>
  2044. Vector<T> r_stdev(const Matrix<T>& m, bool sample_correction = false)
  2045. {
  2046. return sqrt(r_var(m, sample_correction));
  2047. }
  2048. template <typename T>
  2049. Vector<T> max(const Matrix<T>& m)
  2050. {
  2051. Vector<T> res(m.ncols());
  2052. double value;
  2053. for (unsigned int j = 0; j < m.ncols(); j++)
  2054. {
  2055. value = m[0][j];
  2056. for (unsigned int i = 1; i < m.nrows(); i++)
  2057. value = std::max(m[i][j], value);
  2058. res[j] = value;
  2059. }
  2060. return res;
  2061. }
  2062. template <typename T>
  2063. Vector<T> r_max(const Matrix<T>& m)
  2064. {
  2065. Vector<T> res(m.nrows());
  2066. double value;
  2067. for (unsigned int i = 0; i < m.nrows(); i++)
  2068. {
  2069. value = m[i][0];
  2070. for (unsigned int j = 1; j < m.ncols(); j++)
  2071. value = std::max(m[i][j], value);
  2072. res[i] = value;
  2073. }
  2074. return res;
  2075. }
  2076. template <typename T>
  2077. Vector<T> min(const Matrix<T>& m)
  2078. {
  2079. Vector<T> res(m.ncols());
  2080. double value;
  2081. for (unsigned int j = 0; j < m.ncols(); j++)
  2082. {
  2083. value = m[0][j];
  2084. for (unsigned int i = 1; i < m.nrows(); i++)
  2085. value = std::min(m[i][j], value);
  2086. res[j] = value;
  2087. }
  2088. return res;
  2089. }
  2090. template <typename T>
  2091. Vector<T> r_min(const Matrix<T>& m)
  2092. {
  2093. Vector<T> res(m.nrows());
  2094. double value;
  2095. for (unsigned int i = 0; i < m.nrows(); i++)
  2096. {
  2097. value = m[i][0];
  2098. for (unsigned int j = 1; j < m.ncols(); j++)
  2099. value = std::min(m[i][j], value);
  2100. res[i] = value;
  2101. }
  2102. return res;
  2103. }
  2104. /**
  2105. Single element mathematical functions
  2106. */
  2107. template <typename T>
  2108. Matrix<T> exp(const Matrix<T>&m)
  2109. {
  2110. Matrix<T> tmp(m.nrows(), m.ncols());
  2111. for (unsigned int i = 0; i < m.nrows(); i++)
  2112. for (unsigned int j = 0; j < m.ncols(); j++)
  2113. tmp[i][j] = exp(m[i][j]);
  2114. return tmp;
  2115. }
  2116. template <typename T>
  2117. Matrix<T> sqrt(const Matrix<T>&m)
  2118. {
  2119. Matrix<T> tmp(m.nrows(), m.ncols());
  2120. for (unsigned int i = 0; i < m.nrows(); i++)
  2121. for (unsigned int j = 0; j < m.ncols(); j++)
  2122. tmp[i][j] = sqrt(m[i][j]);
  2123. return tmp;
  2124. }
  2125. /**
  2126. Matrix operators
  2127. */
  2128. template <typename T>
  2129. Matrix<T> kron(const Vector<T>& b, const Vector<T>& a)
  2130. {
  2131. Matrix<T> tmp(b.size(), a.size());
  2132. for (unsigned int i = 0; i < b.size(); i++)
  2133. for (unsigned int j = 0; j < a.size(); j++)
  2134. tmp[i][j] = a[j] * b[i];
  2135. return tmp;
  2136. }
  2137. template <typename T>
  2138. Matrix<T> t(const Matrix<T>& a)
  2139. {
  2140. Matrix<T> tmp(a.ncols(), a.nrows());
  2141. for (unsigned int i = 0; i < a.nrows(); i++)
  2142. for (unsigned int j = 0; j < a.ncols(); j++)
  2143. tmp[j][i] = a[i][j];
  2144. return tmp;
  2145. }
  2146. template <typename T>
  2147. Matrix<T> dot_prod(const Matrix<T>& a, const Matrix<T>& b)
  2148. {
  2149. if (a.ncols() != b.nrows())
  2150. throw std::logic_error("Error matrix dot product: dimensions of the matrices are not compatible");
  2151. Matrix<T> tmp(a.nrows(), b.ncols());
  2152. for (unsigned int i = 0; i < tmp.nrows(); i++)
  2153. for (unsigned int j = 0; j < tmp.ncols(); j++)
  2154. {
  2155. tmp[i][j] = (T)0;
  2156. for (unsigned int k = 0; k < a.ncols(); k++)
  2157. tmp[i][j] += a[i][k] * b[k][j];
  2158. }
  2159. return tmp;
  2160. }
  2161. template <typename T>
  2162. Matrix<T> dot_prod(const Matrix<T>& a, const Vector<T>& b)
  2163. {
  2164. if (a.ncols() != b.size())
  2165. throw std::logic_error("Error matrix dot product: dimensions of the matrix and the vector are not compatible");
  2166. Matrix<T> tmp(a.nrows(), 1);
  2167. for (unsigned int i = 0; i < tmp.nrows(); i++)
  2168. {
  2169. tmp[i][0] = (T)0;
  2170. for (unsigned int k = 0; k < a.ncols(); k++)
  2171. tmp[i][0] += a[i][k] * b[k];
  2172. }
  2173. return tmp;
  2174. }
  2175. template <typename T>
  2176. Matrix<T> dot_prod(const Vector<T>& a, const Matrix<T>& b)
  2177. {
  2178. if (a.size() != b.ncols())
  2179. throw std::logic_error("Error matrix dot product: dimensions of the vector and matrix are not compatible");
  2180. Matrix<T> tmp(1, b.ncols());
  2181. for (unsigned int j = 0; j < tmp.ncols(); j++)
  2182. {
  2183. tmp[0][j] = (T)0;
  2184. for (unsigned int k = 0; k < a.size(); k++)
  2185. tmp[0][j] += a[k] * b[k][j];
  2186. }
  2187. return tmp;
  2188. }
  2189. template <typename T>
  2190. inline Matrix<double> rank(const Matrix<T> m)
  2191. {
  2192. Matrix<double> tmp(m.nrows(), m.ncols());
  2193. for (unsigned int j = 0; j < m.ncols(); j++)
  2194. tmp.setColumn(j, rank<T>(m.extractColumn(j)));
  2195. return tmp;
  2196. }
  2197. template <typename T>
  2198. inline Matrix<double> r_rank(const Matrix<T> m)
  2199. {
  2200. Matrix<double> tmp(m.nrows(), m.ncols());
  2201. for (unsigned int i = 0; i < m.nrows(); i++)
  2202. tmp.setRow(i, rank<T>(m.extractRow(i)));
  2203. return tmp;
  2204. }
  2205. }
  2206. #endif // define _ARRAY_HH_