Matrices Library
mikroC PRO for ARM includes a library for operating and working with matrices. Routines with 16 as their suffix work with 16-bit data (fractional Q15 format) and routines with 32 as their suffix work with 32-bit data (fractional Q31 format).
  Note :
When using Cortex M4 MCUs, please have in mind that arrays of 16-bit (half-word) type that represent matrices must be allocated on the word-aligned addresses.
Library Routines
- Matrix_Transpose16
 - Matrix_Transpose32
 - Matrix_Subtract16
 - Matrix_Subtract32
 - Matrix_Scale16
 - Matrix_Scale32
 - Matrix_Multiply16
 - Matrix_Multiply32
 - Matrix_Add16
 - Matrix_Add32
 
Matrix_Transpose16
| Prototype | 
 void Matrix_Transpose16(int *src, int *dest, unsigned numRows, unsigned numCols);  | 
|---|---|
| Description | 
 Function does matrix transposition. dest[i][j] = src[j][i]  | 
| Parameters | 
  | 
| Returns | 
 Nothing.  | 
| Requires | 
 Nothing.  | 
| Notes | 
 None.  | 
Matrix_Transpose32
| Prototype | 
 void Matrix_Transpose32(long *src, long *dest, unsigned numRows, unsigned numCols);  | 
|---|---|
| Description | 
 Function does matrix transposition. dest[i][j] = src[j][i]  | 
| Parameters | 
  | 
| Returns | 
 Nothing.  | 
| Requires | 
 Nothing.  | 
| Notes | 
 None.  | 
Matrix_Subtract16
| Prototype | 
 void Matrix_Subtract16(int *src1, int *src2, int *dest, unsigned numRows, unsigned numCols);  | 
|---|---|
| Description | 
 Function does matrix subtraction. dest[i][j] = src1[i][j] - src2[i][j]  | 
| Parameters | 
  | 
| Returns | 
 Nothing.  | 
| Requires | 
 Nothing.  | 
| Notes | 
 None.  | 
Matrix_Subtract32
| Prototype | 
 void Matrix_Subtract32(long *src1, long *src2, long *dest, unsigned numRows, unsigned numCols);  | 
|---|---|
| Description | 
 Function does matrix subtraction. dest[i][j] = src1[i][j] - src2[i][j]  | 
| Parameters | 
  | 
| Returns | 
 Nothing.  | 
| Requires | 
 Nothing.  | 
| Notes | 
 None.  | 
Matrix_Scale16
| Prototype | 
 void Matrix_Scale16(int ScaleValue, int *src, int *dest, unsigned numRows, unsigned numCols);  | 
|---|---|
| Description | 
 Function does matrix scale. dest[i][j] = ScaleValue * src[i][j]  | 
| Parameters | 
  | 
| Returns | 
 Nothing.  | 
| Requires | 
 Nothing.  | 
| Notes | 
 None.  | 
Matrix_Scale32
| Prototype | 
 void Matrix_Scale32(long ScaleValue, long *src, long *dest, unsigned numRows, unsigned numCols);  | 
|---|---|
| Description | 
 Function does matrix scale. dest[i][j] = ScaleValue * src[i][j]  | 
| Parameters | 
  | 
| Returns | 
 Nothing.  | 
| Requires | 
 Nothing.  | 
| Notes | 
 None.  | 
Matrix_Multiply16
| Prototype | 
 void Matrix_Multiply16(int *src1, int *src2, int *dest, unsigned numRows1, unsigned numCols2, unsigned numCols1Rows2);  | 
|---|---|
| Description | 
 Function does matrix multiplication. 
  | 
| Parameters | 
  | 
| Returns | 
 Nothing.  | 
| Requires | 
 Nothing.  | 
| Notes | 
 None.  | 
Matrix_Multiply32
| Prototype | 
 void Matrix_Multiply32(long *src1, long *src2, long *dest, unsigned numRows1, unsigned numCols2, unsigned numCols1Rows2);  | 
|---|---|
| Description | 
 Function does matrix multiplication. 
  | 
| Parameters | 
  | 
| Returns | 
 Nothing.  | 
| Requires | 
 Nothing.  | 
| Notes | 
 None.  | 
Matrix_Add16
| Prototype | 
 void Matrix_Add16(int *src1, int *src2, int *dest, unsigned numRows, unsigned numCols);  | 
|---|---|
| Description | 
 Function does matrix addition. dest[i][j] = src1[i][j] + src2[i][j]  | 
| Parameters | 
  | 
| Returns | 
 Nothing.  | 
| Requires | 
 Nothing.  | 
| Notes | 
 None.  | 
Matrix_Add32
| Prototype | 
 void Matrix_Add32(long *src1, long *src2, long *dest, long numRows, unsigned numCols);  | 
|---|---|
| Description | 
 Function does matrix addition. dest[i][j] = src1[i][j] + src2[i][j]  | 
| Parameters | 
  | 
| Returns | 
 Nothing.  | 
| Requires | 
 Nothing.  | 
| Notes | 
 None.  | 
Library Example
int Q15_1[3][3] = { {0x1000, 0x2000, 0x3000},
                    {0x4000, 0x5000, 0x6000},
                    {0x7000, 0x8000, 0x9000} };
                  
int Q15_2[3][3] = { {0x9000, 0x8000, 0x7000},
                    {0x6000, 0x5000, 0x4000},
                    {0x3000, 0x2000, 0x1000} };
                    
int Q15_Out[3][3];
void main() {
  Matrix_Transpose16(Q15_1, Q15_Out, 3, 3);  
  // Q15_Out = ( 0x1000, 0x4000, 0x7000,
  //             0x2000, 0x5000, 0x8000,
  //             0x3000, 0x6000, 0x9000 )
  
  Matrix_Subtract16(Q15_1, Q15_2, Q15_Out, 3, 3);
  // Q15_Out = ( 0x8000, 0xA000, 0xC000,
  //             0xE000, 0x0000, 0x2000,
  //             0x4000, 0x6000, 0x8000 )
  // Overflow : Q15_Out[0][0], Q15[0][1]...
  
  Matrix_Add16(Q15_1, Q15_2, Q15_Out, 3, 3);
  // Q15_Out = ( 0xA000, 0xA000, 0xA000,
  //             0xA000, 0xA000, 0xA000,
  //             0xA000, 0xA000, 0xA000 )
  // Overflow : Q15_Out[0][2], Q15[1][0]...
  
  Matrix_Scale16(0x4000, Q15_1, Q15_Out, 3, 3);
  // Q15_Out = ( 0x0800, 0x1000, 0x1800,
  //             0x2000, 0x2800, 0x3000,
  //             0x3800, 0xC000, 0xC800 )
  
  Matrix_Multiply16(Q15_1, Q15_2, Q15_Out, 3, 3, 3);
  // Q15_Out = ( 0x1C00, 0x1000, 0x2400,
  //             0x2800, 0x0A00, 0x6C00,  `
  //             0x1400, 0x2400, 0x1400 )
  // Overflow : Q15_Out[2][0], Q15[2][1]
  
}
What do you think about this topic ? Send us feedback!




