matrix.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include <stdlib.h>
  2. #include <math.h>
  3. #include "matrix.h"
  4. #include "vector.h"
  5. //#include "stdafx.h"
  6. #define EPS 1.0e-12
  7. /********************** constructors ****************************/
  8. Matrix::Matrix(const Matrix& p) {
  9. d_i = p.d_i;
  10. d_j = p.d_j;
  11. if (d_i > 0) {
  12. v = new Vector*[d_i];
  13. for (int i = 0; i < d_i; i++)
  14. v[i] = new Vector(*p.v[i]);
  15. }
  16. else v = NULL;
  17. }
  18. Matrix::Matrix(const Vector& vec) {
  19. d_i = vec.d;
  20. d_j = 1;
  21. v = new Vector*[d_i];
  22. for(int i = 0; i < d_i; i++) {
  23. v[i] = new Vector(1);
  24. elem(i,0) = vec[i];
  25. }
  26. }
  27. Matrix::Matrix(int dim1, int dim2, double a) {
  28. d_i = dim1;
  29. d_j = dim2;
  30. v = new Vector*[d_i];
  31. for(int i = 0; i < d_i; i++) {
  32. v[i] = new Vector(d_j);
  33. for(int j = 0; j < d_j; j++) elem(i,j) = a;
  34. }
  35. }
  36. Matrix::~Matrix() {
  37. if (v) {
  38. while(d_i--) delete v[d_i];
  39. delete v;
  40. }
  41. }
  42. /************************ members *******************************/
  43. void Matrix::flip_rows(int i,int j)
  44. { Vector* p = v[i];
  45. v[i] = v[j];
  46. v[j] = p;
  47. }
  48. void Matrix::free() {
  49. if (v) {
  50. while(d_i--) delete v[d_i];
  51. delete v;
  52. }
  53. }
  54. void Matrix::check_dimensions(const Matrix& mat) const {
  55. if (d_i != mat.d_i || d_j != mat.d_j) {
  56. cerr << "Matrix:incompatible matrix types." << endl;
  57. exit(1);
  58. }
  59. }
  60. Matrix Matrix::trans() const {
  61. Matrix result(d_j,d_i);
  62. for(int i = 0; i < d_j; i++)
  63. for(int j = 0; j < d_i; j++)
  64. result.elem(i,j) = elem(j,i);
  65. return (result);
  66. }
  67. Matrix& Matrix::resize(int new_d_i, int new_d_j) {
  68. int i, j;
  69. if (d_i != new_d_i || d_j != new_d_j) {
  70. for(i = 0; i < d_i; i++) delete v[i];
  71. delete v;
  72. d_i = new_d_i;
  73. d_j = new_d_j;
  74. v = new Vector*[d_i];
  75. for(i = 0; i < d_i; i++) v[i] = new Vector(d_j);
  76. }
  77. for(i = 0; i < d_i; i++)
  78. for(j = 0; j < d_j; j++) elem(i,j) = 0.0;
  79. return (*this);
  80. }
  81. /***************************** friends **************************/
  82. ostream& operator<<(ostream& s, const Matrix& M) {
  83. //int i;
  84. //for (i = 0; i < M.d_i; i++) s << M[i];
  85. int i,j;
  86. for(j=0; j < M.d_j; j++)
  87. for(i=0; i < M.d_i; i++)
  88. s << " [" << i << "][" << j << "] = " << M[i][j]<< endl;
  89. return (s);
  90. }
  91. istream& operator>>(istream& s, Matrix& M) {
  92. int i = 0;
  93. while (i < M.d_i && s >> M[i++]);
  94. return (s);
  95. }
  96. Matrix operator*(double a, const Matrix& mat) {
  97. return(mat*a);
  98. }
  99. double max(const Matrix& M) {
  100. int n = M.dim_j();
  101. Vector c(n);
  102. while (n--) c[n] = max(M.col(n));
  103. return max(c);
  104. }
  105. double min(const Matrix& M) {
  106. int n = M.dim_j();
  107. Vector c(n);
  108. while (n--) c[n] = min(M.col(n));
  109. return(min(c));
  110. }
  111. double norm(const Matrix& mat) {
  112. register double result = 0.0;
  113. for(int i = 0; i < mat.dim_i(); i++)
  114. for(int j = 0; j < mat.dim_j(); j++)
  115. result += mat.elem(i,j);
  116. return (result);
  117. }
  118. double norm1(const Matrix& mat) {
  119. return (norm(mat.abs()));
  120. }
  121. double norm2(const Matrix& mat) {
  122. register double result = 0.0;
  123. for(int i = 0; i < mat.dim_i(); i++)
  124. for(int j = 0; j < mat.dim_j(); j++)
  125. result += mat.elem(i,j)*mat.elem(i,j);
  126. return (sqrt(result));
  127. }
  128. double norm2_sqv(const Matrix& mat) {
  129. register double result = 0.0;
  130. for(int i = 0; i < mat.dim_i(); i++)
  131. for(int j = 0; j < mat.dim_j(); j++)
  132. result += mat.elem(i,j)*mat.elem(i,j);
  133. return (result);
  134. }
  135. void Matrix::swap_rows(int m, int n) {
  136. flip_rows(m,n);
  137. }
  138. void del(Matrix& mat) {
  139. mat.resize(0,0);
  140. }