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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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>::div(double k)
  89. {
  90. for (unsigned i = 0; i < X; ++i) {
  91. _scalars[i] /= k;
  92. }
  93. return *this;
  94. }
  95. tmpl double VectorXD<X>::dotProduct(const VectorXD<X> &other) const
  96. {
  97. double total = 0;
  98. for (unsigned i = 0; i < X; ++i) {
  99. total += _scalars[i] * other._scalars[i];
  100. }
  101. return total;
  102. }
  103. tmpl double VectorXD<X>::norm() const
  104. {
  105. double total = 0;
  106. for (unsigned i = 0; i < X; ++i) {
  107. total += _scalars[i] * _scalars[i];
  108. }
  109. return sqrt(total);
  110. }
  111. tmpl VectorXD<X> VectorXD<X>::operator+() const
  112. {
  113. return *this;
  114. }
  115. tmpl VectorXD<X> VectorXD<X>::operator+(const double &k) const
  116. {
  117. return VectorXD(*this).add(k);
  118. }
  119. tmpl VectorXD<X> &VectorXD<X>::operator+=(const double &k)
  120. {
  121. return add(k);
  122. }
  123. tmpl VectorXD<X> VectorXD<X>::operator+(const VectorXD<X> &other) const
  124. {
  125. return VectorXD<X>(*this).add(other);
  126. }
  127. tmpl VectorXD<X> &VectorXD<X>::operator+=(const VectorXD<X> &other)
  128. {
  129. return add(other);
  130. }
  131. tmpl VectorXD<X> VectorXD<X>::operator-() const
  132. {
  133. double scalars[X];
  134. for (unsigned i = 0; i < X; ++i) {
  135. scalars[i] = -_scalars[i];
  136. }
  137. return VectorXD<X>(scalars);
  138. }
  139. tmpl VectorXD<X> VectorXD<X>::operator-(const double &k) const
  140. {
  141. return VectorXD(*this).sub(k);
  142. }
  143. tmpl VectorXD<X> &VectorXD<X>::operator-=(const double &k)
  144. {
  145. return sub(k);
  146. }
  147. tmpl VectorXD<X> VectorXD<X>::operator-(const VectorXD<X> &other) const
  148. {
  149. return VectorXD(*this).sub(other);
  150. }
  151. tmpl VectorXD<X> &VectorXD<X>::operator-=(const VectorXD<X> &other)
  152. {
  153. return sub(other);
  154. }
  155. tmpl VectorXD<X> VectorXD<X>::operator*(const double &k) const
  156. {
  157. return VectorXD<X>(*this).mult(k);
  158. }
  159. tmpl VectorXD<X> &VectorXD<X>::operator*=(const double &k)
  160. {
  161. return mult(k);
  162. }
  163. tmpl double VectorXD<X>::operator*(const VectorXD<X> &other) const
  164. {
  165. return VectorXD<X>(*this).dotProduct(other);
  166. }
  167. tmpl VectorXD<X> &VectorXD<X>::operator*=(const VectorXD<X> &other)
  168. {
  169. dotProduct(other);
  170. return *this;
  171. }
  172. tmpl VectorXD<X> VectorXD<X>::operator/(const double &k) const
  173. {
  174. return VectorXD<X>(*this).div(k);
  175. }
  176. tmpl VectorXD<X> &VectorXD<X>::operator/=(const double &k)
  177. {
  178. return div(k);
  179. }
  180. tmpl bool VectorXD<X>::operator==(const VectorXD<X> &other) const
  181. {
  182. return equal(other);
  183. }
  184. tmpl bool VectorXD<X>::operator!=(const VectorXD<X> &other) const
  185. {
  186. return !equal(other);
  187. }
  188. tmpl bool VectorXD<X>::operator!() const
  189. {
  190. return isNull();
  191. }
  192. tmpl VectorXD<X>::operator bool() const
  193. {
  194. return !isNull();
  195. }
  196. tmpl QDebug operator<<(QDebug dbg, const VectorXD<X> &v)
  197. {
  198. return dbg.nospace() << "(" << v.getX() << ", " << v.getY() << ", " << v.getZ() << ")";
  199. }