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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 VectorXD<X> VectorXD<X>::crossProduct(const VectorXD<X> &v1, const VectorXD<X> &v2)
  121. {
  122. return VectorXD<X>(v1).crossProduct(v2);
  123. }
  124. tmpl double VectorXD<X>::norm() const
  125. {
  126. double total = 0;
  127. for (unsigned i = 0; i < X; ++i) {
  128. total += _scalars[i] * _scalars[i];
  129. }
  130. return sqrt(total);
  131. }
  132. tmpl VectorXD<X> VectorXD<X>::operator+() const
  133. {
  134. return *this;
  135. }
  136. tmpl VectorXD<X> VectorXD<X>::operator+(const double &k) const
  137. {
  138. return VectorXD(*this).add(k);
  139. }
  140. tmpl VectorXD<X> &VectorXD<X>::operator+=(const double &k)
  141. {
  142. return add(k);
  143. }
  144. tmpl VectorXD<X> VectorXD<X>::operator+(const VectorXD<X> &other) const
  145. {
  146. return VectorXD<X>(*this).add(other);
  147. }
  148. tmpl VectorXD<X> &VectorXD<X>::operator+=(const VectorXD<X> &other)
  149. {
  150. return add(other);
  151. }
  152. tmpl VectorXD<X> VectorXD<X>::operator-() const
  153. {
  154. double scalars[X];
  155. for (unsigned i = 0; i < X; ++i) {
  156. scalars[i] = -_scalars[i];
  157. }
  158. return VectorXD<X>(scalars);
  159. }
  160. tmpl VectorXD<X> VectorXD<X>::operator-(const double &k) const
  161. {
  162. return VectorXD(*this).sub(k);
  163. }
  164. tmpl VectorXD<X> &VectorXD<X>::operator-=(const double &k)
  165. {
  166. return sub(k);
  167. }
  168. tmpl VectorXD<X> VectorXD<X>::operator-(const VectorXD<X> &other) const
  169. {
  170. return VectorXD(*this).sub(other);
  171. }
  172. tmpl VectorXD<X> &VectorXD<X>::operator-=(const VectorXD<X> &other)
  173. {
  174. return sub(other);
  175. }
  176. tmpl VectorXD<X> VectorXD<X>::operator*(const double &k) const
  177. {
  178. return VectorXD<X>(*this).mult(k);
  179. }
  180. tmpl VectorXD<X> &VectorXD<X>::operator*=(const double &k)
  181. {
  182. return mult(k);
  183. }
  184. tmpl VectorXD<X> VectorXD<X>::operator*(const VectorXD<X> &other) const
  185. {
  186. return VectorXD<X>(*this).mult(other);
  187. }
  188. tmpl VectorXD<X> &VectorXD<X>::operator*=(const VectorXD<X> &other)
  189. {
  190. dotProduct(other);
  191. return *this;
  192. }
  193. tmpl VectorXD<X> VectorXD<X>::operator/(const double &k) const
  194. {
  195. return VectorXD<X>(*this).div(k);
  196. }
  197. tmpl VectorXD<X> &VectorXD<X>::operator/=(const double &k)
  198. {
  199. return div(k);
  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 VectorXD<X> &other) const
  206. {
  207. return !equal(other);
  208. }
  209. tmpl bool VectorXD<X>::operator!() const
  210. {
  211. return isNull();
  212. }
  213. tmpl VectorXD<X>::operator bool() const
  214. {
  215. return !isNull();
  216. }
  217. tmpl QDebug operator<<(QDebug dbg, const VectorXD<X> &v)
  218. {
  219. return dbg.nospace() << "(" << v.getX() << ", " << v.getY() << ", " << v.getZ() << ")";
  220. }