ivectorl.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #ifndef _IVectorl__h_
  2. #define _IVectorl__h_
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <iostream.h>
  6. class IVectorl
  7. {
  8. friend class IMatrix;
  9. friend ostream& operator<<(ostream&, const IVectorl&);
  10. friend IVectorl operator*(int, const IVectorl&);
  11. public:
  12. IVectorl(int = 0);
  13. IVectorl(int, int);
  14. IVectorl(const IVectorl&);
  15. ~IVectorl() { if(v) delete []v; }
  16. int dim() const { return d; }
  17. void flip();
  18. IVectorl& resize(int nn);
  19. IVectorl& operator=(const IVectorl&);
  20. IVectorl& operator=(int);
  21. int& operator[](int);
  22. int operator[](int) const;
  23. IVectorl operator+(const IVectorl&) const;
  24. IVectorl operator-(const IVectorl&) const;
  25. IVectorl& operator+=(const IVectorl&);
  26. IVectorl& operator-=(const IVectorl&);
  27. IVectorl operator*(int) const;
  28. IVectorl operator/(int) const;
  29. int operator*(const IVectorl&) const;
  30. IVectorl operator-() const;
  31. int operator==(const IVectorl&) const;
  32. int operator!=(const IVectorl& w) const { return !(*this == w); }
  33. private:
  34. int* v; // v(0:d-1)
  35. int d;
  36. };
  37. inline int IVectorl::operator[](int i) const {
  38. if (i < 0 || i > d - 1) {
  39. cerr << "IVectorl: index " << i << " out of range" << endl;
  40. exit(1);
  41. }
  42. return (v[i]);
  43. }
  44. inline int& IVectorl::operator[](int i) {
  45. if (i < 0 || i > d - 1) {
  46. cerr << "IVectorl: index " << i << " out of range" << endl;
  47. exit(1);
  48. }
  49. return (v[i]);
  50. }
  51. inline IVectorl IVectorl::operator+(const IVectorl& vec) const {
  52. IVectorl result(d);
  53. int n = d;
  54. if (d != vec.d) {
  55. cerr << "IVectorl+: IVectorls have different length" << endl;
  56. exit(1);
  57. }
  58. while (n--) result.v[n] = v[n] + vec.v[n];
  59. return (result);
  60. }
  61. inline IVectorl IVectorl::operator-(const IVectorl& vec) const {
  62. IVectorl result(d);
  63. int n = d;
  64. if (d != vec.d) {
  65. cerr << "IVectorl-: IVectorls have different length" << endl;
  66. exit(1);
  67. }
  68. while (n--) result.v[n] = v[n] - vec.v[n];
  69. return (result);
  70. }
  71. inline IVectorl IVectorl::operator*(int x) const {
  72. IVectorl result(d);
  73. int n = d;
  74. while (n--) result.v[n] = v[n] * x;
  75. return (result);
  76. }
  77. inline IVectorl IVectorl::operator/(int x) const {
  78. IVectorl result(d);
  79. int n = d;
  80. while (n--) result.v[n] = v[n] / x;
  81. return (result);
  82. }
  83. inline IVectorl& IVectorl::operator+=(const IVectorl& vec) {
  84. int n = d;
  85. if (d != vec.d) {
  86. cerr << "IVectorl+=: IVectorls have different length" << endl;
  87. exit(1);
  88. }
  89. while (n--) v[n] += vec.v[n];
  90. return (*this);
  91. }
  92. inline IVectorl& IVectorl::operator-=(const IVectorl& vec) {
  93. int n = d;
  94. if (d != vec.d) {
  95. cerr << "IVectorl-=: IVectorls have different length" << endl;
  96. exit(1);
  97. }
  98. while (n--) v[n] -= vec.v[n];
  99. return (*this);
  100. }
  101. inline IVectorl IVectorl::operator-() const {
  102. IVectorl result(d);
  103. int n = d;
  104. while (n--) result.v[n] = -v[n];
  105. return (result);
  106. }
  107. inline int IVectorl::operator*(const IVectorl& vec) const {
  108. if (d != vec.d) {
  109. cerr << "IVectorl*: IVectorls have different length" << endl;
  110. exit(1);
  111. }
  112. int result = 0;
  113. int n = d;
  114. while (n--) result = result + v[n] * vec.v[n];
  115. return (result);
  116. }
  117. inline IVectorl& IVectorl::operator=(const IVectorl& vec) {
  118. //if (this != &vec) {
  119. //int n = vec.d;
  120. //if (d != vec.d) {
  121. //cerr << "IVectorl=: IVectorls have different length" << endl;
  122. //exit(1);
  123. //}
  124. //while (n--) v[n] = vec.v[n];
  125. //}
  126. if (this != &vec) {
  127. int n = vec.d;
  128. if (n != d) {
  129. delete v;
  130. v = new int [n];
  131. d = n;
  132. }
  133. while (n--) v[n] = vec.v[n];
  134. }
  135. return (*this);
  136. }
  137. inline IVectorl& IVectorl::operator=(int a) {
  138. int n = d;
  139. while (n--) v[n] = a;
  140. return (*this);
  141. }
  142. inline int IVectorl::operator==(const IVectorl& vec) const {
  143. if (vec.d != d) return (0);
  144. int i = 0;
  145. while ((i < d) && (v[i]==vec.v[i])) i++;
  146. return ((i == d) ? 1: 0);
  147. }
  148. inline int MAX_L(int x,int y) { return (x > y ? x : y); }
  149. int norm(const IVectorl& v);
  150. IVectorl operator^(const IVectorl& v, const IVectorl& w);
  151. #endif