ivectorl.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include <stdlib.h>
  2. #include <math.h>
  3. #include "ivectorl.h"
  4. //#include "stdafx.h"
  5. /*************************** constructors **************************/
  6. IVectorl::IVectorl(int n) {
  7. if (n < 0) {
  8. cerr << "IVectorl: negative dimension." << endl;
  9. exit(1);
  10. }
  11. d = n;
  12. if (d > 0) {
  13. v = new int[d];
  14. while (n--) v[n] = 0;
  15. }
  16. else v = NULL;
  17. }
  18. IVectorl::IVectorl(int n, int v0) {
  19. if (n <= 0) {
  20. cerr << "IVectorl: undefined dimension." << endl;
  21. exit(1);
  22. }
  23. d = n;
  24. v = new int[d];
  25. while (n--) v[n] = v0;
  26. }
  27. IVectorl::IVectorl(const IVectorl& v0) {
  28. d = v0.d;
  29. if (d > 0) {
  30. v = new int[d];
  31. for(int i = 0; i < d; i++) v[i] = v0.v[i];
  32. }
  33. else v = NULL;
  34. }
  35. /************************* members ****************************/
  36. /************************************************
  37. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. IVectorl& IVectorl::operator=(int a) {
  138. int n = d;
  139. while (n--) v[n] = a;
  140. return (*this);
  141. }
  142. 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. ****************************************************/
  149. void IVectorl::flip() {
  150. int el;
  151. int half;
  152. half = d / 2;
  153. for (int i = 0; i < half; i++) {
  154. el = v[i]; v[i] = v[d - 1 - i]; v[d - 1 - i] = el;
  155. }
  156. }
  157. IVectorl& IVectorl::resize(int nn) {
  158. if (d != nn) {
  159. delete []v;
  160. d = nn;
  161. v = new int[d];
  162. while (nn--) v[nn] = 0;
  163. }
  164. return (*this);
  165. }
  166. /************** friends *********************/
  167. ostream& operator<<(ostream& out, const IVectorl& v) {
  168. for (int i = 0; i < v.d; i++)
  169. //out << v.v[i] << ' ' << endl;
  170. out << " [" << i << "] = " << v.v[i] << ' ' << endl;
  171. return (out);
  172. }
  173. /************** nonfriends *********************/
  174. int norm(const IVectorl& v) {
  175. int result = 0;
  176. int n = v.dim();
  177. while (n--) result = result + v[n];
  178. return (result);
  179. }
  180. IVectorl operator^(const IVectorl& v,const IVectorl& w) {
  181. if (v.dim() != w.dim()) {
  182. cerr << "IVectorl=: IVectorls have different length" << endl;
  183. exit(1);
  184. }
  185. int n = v.dim();
  186. IVectorl result(n, 0.0);
  187. while (n--) result[n] = v[n] * w[n];
  188. return (result);
  189. }