light_matrix.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /******************************************************************************
  2. * Copyright 2020 The Firmament Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *****************************************************************************/
  16. #ifndef LIGHT_MATRIX__
  17. #define LIGHT_MATRIX__
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #include "rtthread.h"
  22. #include "stddef.h"
  23. #define MAT_TYPE float
  24. #define MAT_MALLOC rt_malloc
  25. #define MAT_FREE rt_free
  26. #define MAT_PRINTF rt_kprintf
  27. #define MAT_ASSERT RT_ASSERT
  28. enum {
  29. MAT_EOK = 0, /**< There is no error */
  30. MAT_ERROR, /**< A generic error happens */
  31. MAT_ETIMEOUT, /**< Timed out */
  32. MAT_EFULL, /**< The resource is full */
  33. MAT_EEMPTY, /**< The resource is empty */
  34. MAT_ENOMEM, /**< No memory */
  35. MAT_ENOSYS, /**< No system */
  36. MAT_EBUSY, /**< Busy */
  37. MAT_EIO, /**< IO error */
  38. MAT_EINTR, /**< Interrupted system call */
  39. MAT_EINVAL, /**< Invalid argument */
  40. };
  41. typedef struct {
  42. size_t row, col;
  43. MAT_TYPE** element;
  44. MAT_TYPE* buffer;
  45. } Mat;
  46. /**
  47. * @brief 创建一个 mat 对象
  48. *
  49. * @param row mat 行数
  50. * @param col mat 列数
  51. * @return Mat* 返回 mat 对象指针, NULL 表示创建失败
  52. */
  53. Mat* MatNew(int row, int col);
  54. /**
  55. * @brief 释放 mat 对象
  56. *
  57. * @param mat
  58. */
  59. void MatDelete(Mat* mat);
  60. void MatInit(Mat *mat, int row, int col, MAT_TYPE *pdata);
  61. int MatSetVal(Mat* mat, MAT_TYPE* val);
  62. void MatDump(const Mat* mat);
  63. int MatZeros(Mat* mat);
  64. int MatEye(Mat* mat);
  65. int MatAdd(Mat* src1, Mat* src2, Mat* dst);
  66. int MatSub(Mat* src1, Mat* src2, Mat* dst);
  67. int MatMul(Mat* src1, Mat* src2, Mat* dst);
  68. int MatTrans(Mat* src, Mat* dst);
  69. MAT_TYPE MatDet(Mat* mat);
  70. int MatAdj(Mat* src, Mat* dst);
  71. MAT_TYPE MatNorm(Mat* mat);
  72. int MatEig(Mat* mat, MAT_TYPE* eig_val, Mat* eig_vec, MAT_TYPE eps, int njt);
  73. int MatCopy(Mat* src, Mat* dst);
  74. int MatLuDecomp(Mat* const matIn, Mat* matL, Mat* matU, Mat* matP);
  75. int MatInvByLu(Mat* const A, Mat* inv);
  76. int MatInv(Mat *src, Mat *dst);
  77. #ifdef __cplusplus
  78. }
  79. #endif
  80. #endif