matrix_op.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "RTE_Components.h"
  2. #include CMSIS_device_header
  3. #if defined(MPS3)
  4. #include "cmsis_driver_config.h"
  5. #include "stdout_USART.h"
  6. #endif
  7. #include <iostream>
  8. #include <dsppp/memory_pool.hpp>
  9. #include <dsppp/fixed_point.hpp>
  10. #include <dsppp/matrix.hpp>
  11. using namespace arm_cmsis_dsp;
  12. int main(void)
  13. {
  14. #if defined(MPS3)
  15. stdout_init();
  16. #endif
  17. std::cout << "Matrix operation examples\r\n";
  18. constexpr int ROWS = 8;
  19. constexpr int COLS = 8;
  20. Matrix<float32_t,ROWS,COLS> a;
  21. Matrix<float32_t,ROWS,COLS> b;
  22. for(std::size_t i=0;i<ROWS*COLS;i++)
  23. {
  24. a[i] = float32_t(i);
  25. }
  26. for(std::size_t row=0; row<ROWS; row++)
  27. {
  28. for(std::size_t col=0; col<COLS; col++)
  29. {
  30. b(row,col) = float32_t(row*col);
  31. }
  32. }
  33. Matrix<float32_t,ROWS,COLS> result = a * a + b;
  34. std::cout << "Result = " << std::endl << result ;
  35. // Vector views
  36. // Rows
  37. result.row(1) = 0.0f;
  38. std::cout << "Result = " << std::endl << result ;
  39. // Row with stride
  40. // setting odd elements of 3rd row to 0
  41. result.row<2>(2,1) = 0.0f;
  42. std::cout << "Result = " << std::endl << result ;
  43. // Column with stride
  44. result.col<2>(2,1) = 5.0f;
  45. std::cout << "Result = " << std::endl << result ;
  46. // Matrix view
  47. result.sub(4,8,4,8) = result.sub(4,8,4,8) + result.sub(4,8,4,8);
  48. std::cout << "Result = " << std::endl << result ;
  49. // operators
  50. // dot
  51. result = dot(a,b);
  52. std::cout << "Result = " << std::endl << result ;
  53. // diagonal
  54. Vector<float32_t,ROWS> c;
  55. for(int i = 0;i< ROWS;i++)
  56. {
  57. c[i] = i;
  58. }
  59. result = Matrix<float32_t,ROWS,COLS>::diagonal(c);
  60. std::cout << "Result = " << std::endl << result ;
  61. // identity matrix
  62. result = Matrix<float32_t,ROWS,COLS>::identity();
  63. std::cout << "Result = " << std::endl << result ;
  64. // transpose matrix
  65. result = a.transpose();
  66. std::cout << "Result = " << std::endl << result ;
  67. transposeTo(result,a);
  68. std::cout << "Result = " << std::endl << result ;
  69. // outer product
  70. result = outer(c,c);
  71. std::cout << "Result = " << std::endl << result ;
  72. #if defined(MPS3)
  73. while(1);
  74. #endif
  75. }