matrix.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #pragma once
  2. #include "float.h"
  3. #include "helpler_funtions.h"
  4. #include "math.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. /**
  9. * @brief 向量求模
  10. *
  11. * @param V 向量
  12. * @param n V的维度
  13. * @return float V的模值
  14. */
  15. static inline float Vector_GetNorm(const float *V, int n) {
  16. float norm = 0.0f;
  17. for (int i = 0; i < n; i++)
  18. norm += V[i] * V[i];
  19. norm = sqrtf(norm);
  20. return norm;
  21. }
  22. /**
  23. * @brief 向量 V 归一化
  24. * @param V 向量
  25. * @param n V的维度
  26. */
  27. static inline void Vector_Normalize(float *V, int n) {
  28. float norm = 0.0f;
  29. for (int i = 0; i < n; i++)
  30. norm += V[i] * V[i];
  31. norm = sqrtf(norm);
  32. if (!is_zerof(norm)) {
  33. for (int i = 0; i < n; ++i) {
  34. V[i] /= norm;
  35. }
  36. }
  37. }
  38. /**
  39. * @brief 向量约束模值
  40. * @param V 向量
  41. * @param n 向量维度
  42. * #param norm_max 最大模值
  43. */
  44. static inline void Vector_ConstrainNorm(float *V, int n, float norm_max) {
  45. if (norm_max > 0) {
  46. float norm = Vector_GetNorm(V, n);
  47. if (norm > norm_max) {
  48. float ratio = norm_max / norm;
  49. for (int i = 0; i < n; ++i) {
  50. V[i] *= ratio;
  51. }
  52. }
  53. }
  54. }
  55. /**
  56. * @brief 向量点乘
  57. *
  58. * @param V1 向量
  59. * @param V2 向量
  60. * @param n V1 V1 的维数
  61. * @return float V1 * V2 的结果
  62. */
  63. static inline float Vector_DotProduct(const float *V1, const float *V2, int n) {
  64. float dotProduct = 0.0f;
  65. for (int i = 0; i < n; ++i)
  66. dotProduct += V1[i] * V2[i];
  67. return dotProduct;
  68. }
  69. /**
  70. * @brief 向量和实数乘
  71. *
  72. * @param V 向量
  73. * @param k 实数
  74. * @param n 向量维数
  75. */
  76. static inline void Vector_Scale(float *V, float k, int n) {
  77. for (int i = 0; i < n; ++i) {
  78. V[i] *= k;
  79. }
  80. }
  81. /**
  82. * @brief 3 纬向量叉乘
  83. *
  84. * @param V1
  85. * @param V2
  86. * @param V V1 x V2
  87. */
  88. static inline void Vector_CrossProduct_3D(const float V1[3], const float V2[3],
  89. float *V) {
  90. float_t temp_v[3];
  91. temp_v[0] = V1[1] * V2[2] - V1[2] * V2[1];
  92. temp_v[1] = V1[2] * V2[0] - V1[0] * V2[2];
  93. temp_v[2] = V1[0] * V2[1] - V1[1] * V2[0];
  94. for (int i = 0; i < 3; i++) {
  95. V[i] = temp_v[i];
  96. }
  97. }
  98. /**
  99. * @brief 矩阵初始化
  100. *
  101. * @param P 矩阵
  102. * @param datap
  103. * @param m
  104. * @param n
  105. */
  106. static inline void Matrix_Init(float *P, const float *datap, int m, int n) {
  107. for (int i = 0; i < m; i++) {
  108. for (int j = 0; j < n; j++) {
  109. if (datap == 0) {
  110. P[i * n + j] = 0.0f;
  111. } else {
  112. P[i * n + j] = datap[i * n + j];
  113. }
  114. }
  115. }
  116. }
  117. /**
  118. * @brief 矩阵转置
  119. *
  120. * @param M 要转置的矩阵
  121. * @param M_t 转置后的矩阵
  122. * @param m 矩阵行数
  123. * @param n 矩阵列数
  124. */
  125. static inline void Matrix_Transpose(const float *M, float *M_t, int m, int n) {
  126. for (int i = 0; i < n; i++) {
  127. for (int j = 0; j < m; j++) {
  128. M_t[i * m + j] = M[j * n + i];
  129. }
  130. }
  131. }
  132. /**
  133. * @brief 矩阵乘法
  134. *
  135. * @param P 矩阵
  136. * @param Q 矩阵
  137. * @param R P * Q
  138. * @param l P的行数
  139. * @param m P的列数 M的行数
  140. * @param n M的列数
  141. */
  142. static inline void Matrix_Multiplication(const float *P, const float *Q,
  143. float *R, int l, int m, int n) {
  144. // 将 R 矩阵零化
  145. for (int i = 0; i < l; i++) {
  146. for (int j = 0; j < n; j++) {
  147. R[i * n + j] = 0.0f;
  148. }
  149. }
  150. // 计算 P * Q = R
  151. for (int i = 0; i < l; i++) {
  152. for (int j = 0; j < n; j++) {
  153. for (int k = 0; k < m; k++) {
  154. R[i * n + j] += P[i * m + k] * Q[k * n + j];
  155. }
  156. }
  157. }
  158. }
  159. /**
  160. * @brief 矩阵和实数乘
  161. *
  162. * @param P 矩阵
  163. * @param k 实数
  164. * @param m P的行数
  165. * @param n P的列数
  166. */
  167. static inline void Matrix_Multiplication_RealNum(float *P, float k, int m,
  168. int n) {
  169. for (int i = 0; i < m; i++) {
  170. for (int j = 0; j < n; j++) {
  171. P[i * n + j] *= k;
  172. }
  173. }
  174. }
  175. /**
  176. * @brief 方阵的迹
  177. *
  178. * @param P 方阵
  179. * @param m P的行列维度
  180. * @return float
  181. */
  182. static inline float Matrix_GetTrace(const float *P, int m) {
  183. float trace = 0.0f;
  184. for (int i = 0; i < m; ++i)
  185. trace += P[i * (m + 1)];
  186. return trace;
  187. }
  188. /**
  189. * @brief 矩阵按元素加
  190. *
  191. */
  192. static inline void Matrix_AdditionByElement(const float *P, const float *Q,
  193. float *R, int m, int n) {
  194. for (int i = 0; i < m; ++i) {
  195. for (int j = 0; j < n; ++j) {
  196. R[i * n + j] = P[i * n + j] + Q[i * n + j];
  197. }
  198. }
  199. }
  200. /**
  201. * @brief 矩阵按元数减
  202. *
  203. */
  204. static inline void Matrix_SubtractionByElement(const float *P, const float *Q,
  205. float *R, int m, int n) {
  206. for (int i = 0; i < m; ++i) {
  207. for (int j = 0; j < n; ++j) {
  208. R[i * n + j] = P[i * n + j] - Q[i * n + j];
  209. }
  210. }
  211. }
  212. int Matrix_Inv_3D(const float P[3][3], float inv[3][3]);
  213. int Matrix_Brinv(float *a, int n);
  214. #ifdef __cplusplus
  215. }
  216. #endif