Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1.  
  2. #ifndef __Matrix_CPP
  3. #define __Matrix_CPP
  4.  
  5. #include "matrix.h"
  6. #include <cstdlib>
  7. #include<cstdio>
  8. #include "Eigen/Dense"
  9. #include<time.h>
  10. #include <typeinfo>
  11. #include <iostream>
  12.  
  13. #define MAX_RANDOM 200
  14. const double eps = 1e-12;
  15. // Parameter Constructor
  16. using namespace std;
  17.  
  18.  
  19. template<typename T>
  20. MyMatrix<T>::MyMatrix(unsigned _rows, unsigned _cols, const T& _initial) {
  21. mat.resize(_rows);
  22. for (unsigned i=0; i<mat.size(); i++) {
  23. mat[i].resize(_cols, _initial);
  24. }
  25.  
  26. rows = _rows;
  27. cols = _cols;
  28. }
  29.  
  30. // Copy Constructor
  31. template<typename T>
  32. MyMatrix<T>::MyMatrix(const MyMatrix<T>& rhs) {
  33. mat = rhs.mat;
  34. rows = rhs.get_rows();
  35. cols = rhs.get_cols();
  36. }
  37.  
  38. // (Virtual) Destructor
  39. template<typename T>
  40. MyMatrix<T>::~MyMatrix() {}
  41.  
  42. // Assignment Operator
  43. template<typename T>
  44. MyMatrix<T>& MyMatrix<T>::operator=(const MyMatrix<T>& rhs) {
  45. if (&rhs == this)
  46. return *this;
  47.  
  48. unsigned new_rows = rhs.get_rows();
  49. unsigned new_cols = rhs.get_cols();
  50.  
  51. mat.resize(new_rows);
  52. for (unsigned i=0; i<mat.size(); i++) {
  53. mat[i].resize(new_cols);
  54. }
  55.  
  56. for (unsigned i=0; i<new_rows; i++) {
  57. for (unsigned j=0; j<new_cols; j++) {
  58. mat[i][j] = rhs(i, j);
  59. }
  60. }
  61. rows = new_rows;
  62. cols = new_cols;
  63.  
  64. return *this;
  65. }
  66.  
  67. // Addition of two matrices
  68. template<typename T>
  69. MyMatrix<T> MyMatrix<T>::operator+(const MyMatrix<T>& rhs) {
  70. Fraction f(1,1);
  71. MyMatrix<T> result(rows, cols, f);
  72.  
  73. for (unsigned i=0; i<rows; i++) {
  74. for (unsigned j=0; j<cols; j++) {
  75. result(i,j) = this->mat[i][j] + rhs(i,j);
  76. }
  77. }
  78.  
  79. return result;
  80. }
  81.  
  82.  
  83.  
  84.  
  85. // Left multiplication of this MyMatrix and another
  86. template<typename T>
  87. MyMatrix<T> MyMatrix<T>::operator*(const MyMatrix<T>& rhs) {
  88. unsigned rows = rhs.get_rows();
  89. unsigned cols = rhs.get_cols();
  90. Fraction f(0,1);
  91. MyMatrix<T> result(rows, cols, f);
  92.  
  93. for (unsigned i=0; i<rows; i++) {
  94. for (unsigned j=0; j<cols; j++) {
  95. for (unsigned k=0; k<rows; k++) {
  96. result(i,j) += this->mat[i][k] * rhs(k,j);
  97. }
  98. }
  99. }
  100.  
  101. return result;
  102. }
  103.  
  104. // Multiply a MyMatrix with a vector
  105. template<typename T>
  106. std::vector<T> MyMatrix<T>::operator*(const std::vector<T>& rhs) {
  107. std::vector<T> result(rhs.size());
  108.  
  109. for (unsigned i=0; i<rows; i++) {
  110. for (unsigned j=0; j<cols; j++) {
  111. result[i] += this->mat[i][j] * rhs[j];
  112. }
  113. }
  114.  
  115. return result;
  116. }
  117.  
  118.  
  119.  
  120. // Access the individual elements
  121. template<typename T>
  122. T& MyMatrix<T>::operator()(const unsigned& row, const unsigned& col) {
  123. return this->mat[row][col];
  124. }
  125.  
  126. // Access the individual elements (const)
  127. template<typename T>
  128. const T& MyMatrix<T>::operator()(const unsigned& row, const unsigned& col) const {
  129. return this->mat[row][col];
  130. }
  131.  
  132. // Get the number of rows of the MyMatrix
  133. template<typename T>
  134. unsigned MyMatrix<T>::get_rows() const {
  135. return this->rows;
  136. }
  137.  
  138. // Get the number of columns of the MyMatrix
  139. template<typename T>
  140. unsigned MyMatrix<T>::get_cols() const {
  141. return this->cols;
  142. }
  143.  
  144. template<typename T>
  145. MyMatrix<T> MyMatrix<T>::swap_rows(unsigned row1, unsigned row2) {
  146. for (int k=0; k<mat.size(); k++) {
  147. double temp = this->mat[row1][k];
  148. this->mat[row1][k] = this->mat[row2][k];
  149. this->mat[row2][k] = temp;
  150. }
  151.  
  152. return *this;
  153. }
  154.  
  155. template<typename T>
  156. void MyMatrix<T>::print() {
  157. for (int i=0; i<rows; i++) {
  158. for (int j=0; j<cols; j++) {
  159. cout << this->mat[i][j] << " ";
  160. }
  161. printf("\n");
  162. }
  163. }
  164.  
  165. template<typename T>
  166. Eigen::MatrixXd MyMatrix<T>::create_double_eigen() {
  167. Eigen::MatrixXd matE(rows, cols);
  168.  
  169. for(int i=0; i<rows; i++) {
  170. for (int j=0; j<cols; j++) {
  171. matE(i,j) = (double) this->mat[i][j];
  172. }
  173. }
  174.  
  175. return matE;
  176. }
  177.  
  178. template<typename T>
  179. Eigen::MatrixXf MyMatrix<T>::create_float_eigen() {
  180. Eigen::MatrixXf matE(rows, cols);
  181.  
  182. for(int i=0; i<rows; i++) {
  183. for (int j=0; j<cols; j++) {
  184. matE(i,j) = this->mat[i][j];
  185. }
  186. }
  187.  
  188. return matE;
  189. }
  190.  
  191. template<typename T>
  192. void MyMatrix<T>::gwp(std::vector<T> vec) {
  193. int i,j,k;
  194. double m,s,t;
  195. double X[rows], AB[rows][cols+1];
  196. Fraction f(1,1);
  197.  
  198. for(i = 0; i < rows; i++) {
  199. AB[i][rows] = (double) vec[i] ;
  200. }
  201.  
  202. for(int i=0; i<rows; i++) {
  203. for (int j=0; j<cols; j++) {
  204. AB[i][j] = (double) this->mat[i][j];
  205. }
  206. }
  207.  
  208. for(i=0; i<rows-1; i++) {
  209. for(j=i+1; j<rows; j++) {
  210. if(fabs(AB[i][i]) < eps)
  211. return;
  212. m=-AB[j][i]/AB[i][i];
  213. for(k=i+1; k<=rows; k++)
  214. AB[j][k]+= m*AB[i][k];
  215. }
  216. }
  217.  
  218. for(i=rows-1; i>=0; i--) {
  219. s = AB[i][rows];
  220. for(j=rows-1; j>=i+1; j--)
  221. s-=AB[i][j]*X[j];
  222. if(fabs(AB[i][i]) < eps)
  223. return;
  224. X[i]=s/AB[i][i];
  225. }
  226.  
  227. for(i = 0; i < rows; i++)
  228. printf("%2.4f\n",X[i]);
  229. }
  230.  
  231. template<typename T>
  232. void MyMatrix<T>::gpp(const std::vector<T>& vec) {
  233. int i,j,k;
  234. double X[rows], AB[rows][cols+1];
  235.  
  236. for(i = 0; i < rows; i++) {
  237. AB[i][rows] = (double) vec[i];
  238. }
  239.  
  240. for(int i=0; i<rows; i++) {
  241. for (int j=0; j<cols; j++) {
  242. AB[i][j] = (double) this->mat[i][j];
  243. }
  244. }
  245.  
  246. for(i=0; i<rows; i++) //Pivotisation
  247. for(k=i+1; k<rows; k++)
  248. if(abs(AB[i][i]) < abs(AB[k][i])) {
  249. for(j=0; j<=rows; j++){
  250. double temp=AB[i][j];
  251. AB[i][j]=AB[k][j];
  252. AB[k][j]=temp;
  253. }
  254. }
  255.  
  256. for(i=0; i<rows-1; i++) //loop to perform the gauss elimination
  257. for (k=i+1;k<rows;k++) {
  258. double t=AB[k][i]/AB[i][i];
  259. for (j=0;j<=rows;j++)
  260. AB[k][j]=AB[k][j]-t*AB[i][j]; //make the elements below the pivot elements equal to zero or elimnate the variables
  261. }
  262.  
  263. for (i=rows-1;i>=0;i--) //back-substitution
  264. { //x is an array whose values correspond to the values of x,y,z..
  265. X[i]=AB[i][rows]; //make the variable to be calculated equal to the rhs of the last equation
  266. for (j=i+1;j<rows;j++)
  267. if (j!=i) //then subtract all the lhs values except the coefficient of the variable whose value is being calculated
  268. X[i]=X[i]-AB[i][j]*X[j];
  269. X[i]=X[i]/AB[i][i]; //now finally divide the rhs by the coefficient of the variable to be calculated
  270. }
  271.  
  272. for(i = 0; i < rows; i++)
  273. printf("%2.4f\n",X[i]);
  274. }
  275.  
  276. template<typename T>
  277. void MyMatrix<T>::gcp(const std::vector<T>& vec) {
  278. int n = rows, MAT1 = rows;
  279. int lambda[n], omega[n], p[rows], q[rows];
  280. int i,j,k;
  281. int pi,pj,tmp;
  282. double max;
  283. double ftmp,temp;
  284. double x[rows], AB[rows][cols+1];
  285. pi=0,pj=0,max=0.0;
  286.  
  287. for(i = 0; i < rows; i++) {
  288. AB[i][rows] = vec[i];
  289. }
  290.  
  291. for(int i=0; i<rows; i++) {
  292. for (int j=0; j<cols; j++) {
  293. AB[i][j] = this->mat[i][j];
  294. }
  295. }
  296. puts("Before pivot");
  297. for(i=0;i<rows;i++) {
  298. for(j=0;j<cols+1;j++) {
  299. printf("%2.4f ",AB[i][j]);
  300. }
  301. puts("");
  302. }
  303.  
  304. for (k=0;k<n;k++){
  305. for (i=k;i<n;i++) {
  306. for (j=k;j<n;j++) {
  307. temp = fabs(AB[i][j]);
  308. if (temp>max){
  309. max = temp;
  310. pi=i;
  311. pj=j;
  312. }
  313. }
  314. }
  315.  
  316. tmp=p[k];
  317. p[k]=p[pi];
  318. p[pi]=tmp;
  319. for(i=0; i<n; i++) {
  320. ftmp = AB[k][i];
  321. AB[k][i] = AB[pi][i];
  322. AB[pi][i] = ftmp;
  323. }
  324.  
  325. //Swap Col
  326. tmp=q[k];
  327. q[k]=q[pj];
  328. q[pj]=tmp;
  329. for (i=0;i<n;i++){
  330. ftmp=AB[i][k];
  331. AB[i][k]=AB[i][pj];
  332. AB[i][pj]=ftmp;
  333. }
  334. //END PIVOT
  335.  
  336. //check pivot size and decompose
  337. if (AB[k][k]!=0){
  338. for (i=k+1;i<n;i++){
  339. //Column normalisation
  340. AB[i][k] = AB[i][k] / AB[k][k];
  341. }
  342. for (j=k+1;j<n;j++){
  343. for(i=k+1; i<n; i++){
  344. AB[i][j] = AB[i][j] - AB[i][k] * AB[k][j];
  345. }
  346. }
  347. }
  348. //END DECOMPOSE
  349. }
  350. puts("After pivot");
  351. for(i=0;i<rows;i++) {
  352. for(j=0;j<cols+1;j++) {
  353. printf("%2.4f ",AB[i][j]);
  354. }
  355. puts("");
  356. }
  357. /*
  358. int ii=0,ip;
  359. double xtmp[MAT1];
  360. double c[3];
  361. //Swap rows (x=Px)
  362. for (i=0; i<MAT1; i++){
  363. xtmp[i]=x[p[i]]; //value that should be here
  364. }
  365. //Lx=x
  366. for (i=0;i<MAT1;i++){
  367. ftmp=xtmp[i];
  368. for (j=ii-1;j<i;j++)
  369. ftmp-=AB[i][j]*xtmp[j];
  370. xtmp[i]=ftmp;
  371. }
  372.  
  373. //xtmp[MAT1-1]/=a[MAT1-1][MAT1-1];
  374. for (i=MAT1-2;i>=0;i--){
  375. ftmp=xtmp[i];
  376. for (j=i+1;j<MAT1;j++){
  377. ftmp-=AB[i][j]*xtmp[j];
  378. }
  379. xtmp[i]=(ftmp)/AB[i][i];
  380. }
  381.  
  382. for (i=0;i<MAT1;i++){
  383. x[q[i]]=xtmp[i];
  384. }
  385.  
  386. for(i = 0; i < rows; i++)
  387. printf("%2.4f\n",x[i]); */
  388. }
  389. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement