You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vectorxd.hxx 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include <math.h>
  2. #define tmpl template<unsigned X>
  3. tmpl VectorXD<X>::VectorXD()
  4. {
  5. for (unsigned i = 0; i < X; ++i) {
  6. _scalars[i] = 0;
  7. }
  8. }
  9. tmpl VectorXD<X>::VectorXD(const double scalars[X])
  10. {
  11. for (unsigned i = 0; i < X; ++i) {
  12. _scalars[i] = scalars[i];
  13. }
  14. }
  15. tmpl VectorXD<X>::VectorXD(const VectorXD<X>& other)
  16. {
  17. for (unsigned i = 0; i < X; ++i) {
  18. _scalars[i] = other._scalars[i];
  19. }
  20. }
  21. tmpl VectorXD<X>& VectorXD<X>::setScalar(unsigned i, double value)
  22. {
  23. _scalars[i] = value;
  24. return *this;
  25. }
  26. tmpl double VectorXD<X>::getScalar(unsigned i)
  27. {
  28. return _scalars[i];
  29. }
  30. tmpl bool VectorXD<X>::isNull() const
  31. {
  32. return equal(VectorXD<X>());
  33. }
  34. tmpl bool VectorXD<X>::equal(const VectorXD<X> &other) const
  35. {
  36. for (unsigned i = 0; i < X; ++i) {
  37. if (_scalars[i] != other._scalars[i]) {
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. tmpl VectorXD<X> &VectorXD<X>::add(double k)
  44. {
  45. double scalars[X];
  46. for (unsigned i = 0; i < X; ++i) {
  47. scalars[i] = k;
  48. }
  49. return add(VectorXD<X>(scalars));
  50. }
  51. tmpl VectorXD<X> &VectorXD<X>::add(double scalars[X])
  52. {
  53. return add(VectorXD<X>(scalars));
  54. }
  55. tmpl VectorXD<X>& VectorXD<X>::add(const VectorXD<X> &other)
  56. {
  57. for (unsigned i = 0; i < X; ++i) {
  58. _scalars[i] += other._scalars[i];
  59. }
  60. return *this;
  61. }
  62. tmpl VectorXD<X> &VectorXD<X>::sub(double k)
  63. {
  64. double scalars[X];
  65. for (unsigned i = 0; i < X; ++i) {
  66. scalars[i] = k;
  67. }
  68. return sub(VectorXD<X>(scalars));
  69. }
  70. tmpl VectorXD<X> &VectorXD<X>::sub(double scalars[X])
  71. {
  72. return sub(VectorXD<X>(scalars));
  73. }
  74. tmpl VectorXD<X>& VectorXD<X>::sub(const VectorXD<X> &other)
  75. {
  76. for (unsigned i = 0; i < X; ++i) {
  77. _scalars[i] -= other._scalars[i];
  78. }
  79. return *this;
  80. }
  81. tmpl VectorXD<X> &VectorXD<X>::mult(double k)
  82. {
  83. for (unsigned i = 0; i < X; ++i) {
  84. _scalars[i] *= k;
  85. }
  86. return *this;
  87. }
  88. tmpl VectorXD<X> &VectorXD<X>::mult(const VectorXD<X> &other)
  89. {
  90. for (unsigned i = 0; i < X; ++i) {
  91. _scalars[i] *= other._scalars[i];
  92. }
  93. return *this;
  94. }
  95. tmpl VectorXD<X> &VectorXD<X>::div(double k)
  96. {
  97. for (unsigned i = 0; i < X; ++i) {
  98. _scalars[i] /= k;
  99. }
  100. return *this;
  101. }
  102. tmpl double VectorXD<X>::dotProduct(const VectorXD<X> &other) const
  103. {
  104. double total = 0;
  105. for (unsigned i = 0; i < X; ++i) {
  106. total += _scalars[i] * other._scalars[i];
  107. }
  108. return total;
  109. }
  110. tmpl VectorXD<X>& VectorXD<X>::crossProduct(const VectorXD<X>& other)
  111. {
  112. VectorXD<X> t = *this;
  113. for (unsigned i = 0; i < X; ++i) {
  114. unsigned j = (i + 1) % X;
  115. unsigned k = (i + 2) % X;
  116. _scalars[i] = (t._scalars[j] * other._scalars[k]) - (t._scalars[k] * other._scalars[j]);
  117. }
  118. return *this;
  119. }
  120. tmpl double VectorXD<X>::norm() const
  121. {
  122. double total = 0;
  123. for (unsigned i = 0; i < X; ++i) {
  124. total += _scalars[i] * _scalars[i];
  125. }
  126. return sqrt(total);
  127. }
  128. tmpl VectorXD<X> VectorXD<X>::operator+() const
  129. {
  130. return *this;
  131. }
  132. tmpl VectorXD<X> VectorXD<X>::operator+(const double &k) const
  133. {
  134. return VectorXD(*this).add(k);
  135. }
  136. tmpl VectorXD<X> &VectorXD<X>::operator+=(const double &k)
  137. {
  138. return add(k);
  139. }
  140. tmpl VectorXD<X> VectorXD<X>::operator+(const VectorXD<X> &other) const
  141. {
  142. return VectorXD<X>(*this).add(other);
  143. }
  144. tmpl VectorXD<X> &VectorXD<X>::operator+=(const VectorXD<X> &other)
  145. {
  146. return add(other);
  147. }
  148. tmpl VectorXD<X> VectorXD<X>::operator-() const
  149. {
  150. double scalars[X];
  151. for (unsigned i = 0; i < X; ++i) {
  152. scalars[i] = -_scalars[i];
  153. }
  154. return VectorXD<X>(scalars);
  155. }
  156. tmpl VectorXD<X> VectorXD<X>::operator-(const double &k) const
  157. {
  158. return VectorXD(*this).sub(k);
  159. }
  160. tmpl VectorXD<X> &VectorXD<X>::operator-=(const double &k)
  161. {
  162. return sub(k);
  163. }
  164. tmpl VectorXD<X> VectorXD<X>::operator-(const VectorXD<X> &other) const
  165. {
  166. return VectorXD(*this).sub(other);
  167. }
  168. tmpl VectorXD<X> &VectorXD<X>::operator-=(const VectorXD<X> &other)
  169. {
  170. return sub(other);
  171. }
  172. tmpl VectorXD<X> VectorXD<X>::operator*(const double &k) const
  173. {
  174. return VectorXD<X>(*this).mult(k);
  175. }
  176. tmpl VectorXD<X> &VectorXD<X>::operator*=(const double &k)
  177. {
  178. return mult(k);
  179. }
  180. tmpl VectorXD<X> VectorXD<X>::operator*(const VectorXD<X> &other) const
  181. {
  182. return VectorXD<X>(*this).mult(other);
  183. }
  184. tmpl VectorXD<X> &VectorXD<X>::operator*=(const VectorXD<X> &other)
  185. {
  186. dotProduct(other);
  187. return *this;
  188. }
  189. tmpl VectorXD<X> VectorXD<X>::operator/(const double &k) const
  190. {
  191. return VectorXD<X>(*this).div(k);
  192. }
  193. tmpl VectorXD<X> &VectorXD<X>::operator/=(const double &k)
  194. {
  195. return div(k);
  196. }
  197. tmpl bool VectorXD<X>::operator==(const VectorXD<X> &other) const
  198. {
  199. return equal(other);
  200. }
  201. tmpl bool VectorXD<X>::operator!=(const VectorXD<X> &other) const
  202. {
  203. return !equal(other);
  204. }
  205. tmpl bool VectorXD<X>::operator!() const
  206. {
  207. return isNull();
  208. }
  209. tmpl VectorXD<X>::operator bool() const
  210. {
  211. return !isNull();
  212. }
  213. tmpl QDebug operator<<(QDebug dbg, const VectorXD<X> &v)
  214. {
  215. return dbg.nospace() << "(" << v.getX() << ", " << v.getY() << ", " << v.getZ() << ")";
  216. }