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

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