Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #include "matrix.h"
  2.  
  3.  
  4. Matrix::Matrix(ifstream& input) {
  5. string inputRow;
  6. string iString;
  7. std::stringstream ss;
  8. while(!input.eof()) {
  9. vector<long double> newVec;
  10.  
  11. getline(input,inputRow);
  12. std::stringstream ss;
  13. ss << inputRow;
  14. while(!ss.eof()) {
  15. ss >> iString;
  16. if(iString != "") {
  17. newVec.push_back(stold(iString));
  18. }
  19. ss.flush();
  20. iString = "";
  21. }
  22.  
  23. if(!newVec.empty()) {
  24. mat.push_back(newVec);
  25. }
  26. }
  27. }
  28.  
  29. void Matrix::transpose(){
  30. vector<vector<long double> > newVecR;
  31. vector<long double> newVecC;
  32. for(uint i = 0; i < mat.size(); ++i) {
  33. newVecC.push_back(0);
  34. }
  35. for(uint i = 0; i < mat[0].size(); ++i) {
  36. newVecR.push_back(newVecC);
  37. }
  38. for(uint i = 0; i < mat.size(); ++i) {
  39. for(uint j = 0; j < mat[i].size(); ++j) {
  40. newVecR[j][i] = mat[i][j];
  41. }
  42. }
  43. setMatrix(newVecR);
  44. }
  45.  
  46. Matrix Matrix::operator/(long double denom) {
  47. vector<long double> newVec;
  48. Matrix m;
  49.  
  50. for (uint i = 0; i < mat.size(); ++i) {
  51. for(uint j = 0; j < mat[i].size(); ++j){
  52.  
  53. newVec.push_back((mat[i][j]/denom));
  54. }
  55. m.mat.push_back(newVec);
  56. newVec.clear();
  57. }
  58. return m;
  59. }
  60.  
  61. Matrix Matrix::operator*(Matrix& lhs) {
  62. if(mat.size() > 0) {
  63. if(mat[0].size() != lhs.getMatrix().size()) {
  64. cerr << "Matrix size mismatch." << endl;
  65. return Matrix();
  66. }
  67. }
  68.  
  69. long double sum = 0;
  70. vector<long double> newVec;
  71. vector<vector<long double> > lhsMat = lhs.getMatrix();
  72. Matrix m;
  73. for (uint i = 0; i < mat.size(); ++i) {
  74. for(uint j = 0; j < lhsMat[0].size(); ++j){
  75. for(uint k = 0; k < mat[i].size(); ++k){
  76. sum += mat[i][k]*lhsMat[k][j];
  77. }
  78. newVec.push_back(sum);
  79. sum = 0;
  80. }
  81. m.mat.push_back(newVec);
  82. newVec.clear();
  83. }
  84. return m;
  85. }
  86.  
  87. long double Matrix::sum(){
  88. long double s = 0;
  89. for(auto r: mat) {
  90. for (auto c: r) {
  91. s += c;
  92. }
  93. }
  94. return s;
  95. }
  96.  
  97. vector<vector<long double> > Matrix::getMatrix() {
  98. return mat;
  99. }
  100.  
  101. void Matrix::setMatrix(vector<vector<long double> > matrix) {
  102. mat.clear();
  103. mat = matrix;
  104. }
  105. void Matrix::setMatrix(Matrix matrix) {
  106. mat.clear();
  107. mat = matrix.getMatrix();
  108. }
  109. void Matrix::printMatrix() {
  110. for (uint i = 0; i < mat.size(); ++i) {
  111. for(vector<long double>::iterator it = mat[i].begin(); it != mat[i].end(); ++it){
  112. cout << setprecision(2)<< setw(7) << *it << " ";
  113. }
  114. cout << endl;
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement