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.

matrixmxn.hxx 6.5KB

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