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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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(double scalars[X])
  89. {
  90. return mult(VectorXD<X>(scalars));
  91. }
  92. tmpl VectorXD<X> &VectorXD<X>::mult(const VectorXD<X> &other)
  93. {
  94. for (unsigned i = 0; i < X; ++i) {
  95. _scalars[i] *= other._scalars[i];
  96. }
  97. return *this;
  98. }
  99. tmpl VectorXD<X> &VectorXD<X>::div(double k)
  100. {
  101. double scalars[X];
  102. for (unsigned i = 0; i < X; ++i) {
  103. scalars[i] = k;
  104. }
  105. return div(VectorXD<X>(scalars));
  106. }
  107. tmpl VectorXD<X> &VectorXD<X>::div(double scalars[X])
  108. {
  109. return div(VectorXD<X>(scalars));
  110. }
  111. tmpl VectorXD<X> &VectorXD<X>::div(const VectorXD<X> &other)
  112. {
  113. for (unsigned i = 0; i < X; ++i) {
  114. _scalars[i] /= other._scalars[i];
  115. }
  116. return *this;
  117. }
  118. tmpl double VectorXD<X>::dotProduct(const VectorXD<X> &other) const
  119. {
  120. double total = 0;
  121. for (unsigned i = 0; i < X; ++i) {
  122. total += _scalars[i] * other._scalars[i];
  123. }
  124. return total;
  125. }
  126. tmpl VectorXD<X>& VectorXD<X>::crossProduct(const VectorXD<X>& other)
  127. {
  128. VectorXD<X> t = *this;
  129. for (unsigned i = 0; i < X; ++i) {
  130. unsigned j = (i + 1) % X;
  131. unsigned k = (i + 2) % X;
  132. _scalars[i] = (t._scalars[j] * other._scalars[k]) - (t._scalars[k] * other._scalars[j]);
  133. }
  134. return *this;
  135. }
  136. tmpl VectorXD<X> VectorXD<X>::crossProduct(const VectorXD<X> &v1, const VectorXD<X> &v2)
  137. {
  138. return VectorXD<X>(v1).crossProduct(v2);
  139. }
  140. tmpl double VectorXD<X>::norm() const
  141. {
  142. double total = 0;
  143. for (unsigned i = 0; i < X; ++i) {
  144. total += _scalars[i] * _scalars[i];
  145. }
  146. return sqrt(total);
  147. }
  148. tmpl VectorXD<X> VectorXD<X>::operator+() const
  149. {
  150. return *this;
  151. }
  152. tmpl VectorXD<X> VectorXD<X>::operator+(const double &k) const
  153. {
  154. return VectorXD(*this).add(k);
  155. }
  156. tmpl VectorXD<X> &VectorXD<X>::operator+=(const double &k)
  157. {
  158. return add(k);
  159. }
  160. tmpl VectorXD<X> VectorXD<X>::operator+(const VectorXD<X> &other) const
  161. {
  162. return VectorXD<X>(*this).add(other);
  163. }
  164. tmpl VectorXD<X> &VectorXD<X>::operator+=(const VectorXD<X> &other)
  165. {
  166. return add(other);
  167. }
  168. tmpl VectorXD<X> VectorXD<X>::operator-() const
  169. {
  170. double scalars[X];
  171. for (unsigned i = 0; i < X; ++i) {
  172. scalars[i] = -_scalars[i];
  173. }
  174. return VectorXD<X>(scalars);
  175. }
  176. tmpl VectorXD<X> VectorXD<X>::operator-(const double &k) const
  177. {
  178. return VectorXD(*this).sub(k);
  179. }
  180. tmpl VectorXD<X> &VectorXD<X>::operator-=(const double &k)
  181. {
  182. return sub(k);
  183. }
  184. tmpl VectorXD<X> VectorXD<X>::operator-(const VectorXD<X> &other) const
  185. {
  186. return VectorXD(*this).sub(other);
  187. }
  188. tmpl VectorXD<X> &VectorXD<X>::operator-=(const VectorXD<X> &other)
  189. {
  190. return sub(other);
  191. }
  192. tmpl VectorXD<X> VectorXD<X>::operator*(const double &k) const
  193. {
  194. return VectorXD<X>(*this).mult(k);
  195. }
  196. tmpl VectorXD<X> &VectorXD<X>::operator*=(const double &k)
  197. {
  198. return mult(k);
  199. }
  200. tmpl VectorXD<X> VectorXD<X>::operator*(const VectorXD<X> &other) const
  201. {
  202. return VectorXD<X>(*this).mult(other);
  203. }
  204. tmpl VectorXD<X> &VectorXD<X>::operator*=(const VectorXD<X> &other)
  205. {
  206. dotProduct(other);
  207. return *this;
  208. }
  209. tmpl VectorXD<X> VectorXD<X>::operator/(const double &k) const
  210. {
  211. return VectorXD<X>(*this).div(k);
  212. }
  213. tmpl VectorXD<X> &VectorXD<X>::operator/=(const double &k)
  214. {
  215. return div(k);
  216. }
  217. tmpl bool VectorXD<X>::operator==(const VectorXD<X> &other) const
  218. {
  219. return equal(other);
  220. }
  221. tmpl bool VectorXD<X>::operator!=(const VectorXD<X> &other) const
  222. {
  223. return !equal(other);
  224. }
  225. tmpl bool VectorXD<X>::operator!() const
  226. {
  227. return isNull();
  228. }
  229. tmpl VectorXD<X>::operator bool() const
  230. {
  231. return !isNull();
  232. }
  233. tmpl QDebug operator<<(QDebug dbg, const VectorXD<X> &v)
  234. {
  235. return dbg.nospace() << "(" << v.getX() << ", " << v.getY() << ", " << v.getZ() << ")";
  236. }