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

Prototype

void Matrix_Transpose16(int *src, int *dest, unsigned numRows, unsigned numCols);

Description

Function does matrix transposition.

dest[i][j] = src[j][i]

Parameters
  • src: pointer to original matrix
  • dest: pointer to result matrix
  • numRows: number of rows in the source matrix
  • numCols: number of cols in the source matrix
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
  • src: pointer to original matrix
  • dest: pointer to result matrix
  • numRows: number of rows in the source matrix
  • numCols: number of cols in the source matrix
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
  • src1: pointer to the first matrix
  • src2: pointer to the second matrix
  • dest: pointer to the result matrix
  • numRows: number of rows in the source matrix
  • numCols: number of cols in the source matrix
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
  • src1: pointer to the first matrix
  • src2: pointer to the second matrix
  • dest: pointer to the result matrix
  • numRows: number of rows in the source matrix
  • numCols: number of cols in the source matrix
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
  • ScaleValue: scale value
  • src: pointer to the original matrix
  • dest: pointer to the result matrix
  • numRows: number of rows in the source matrix
  • numCols: number of cols in the source matrix
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
  • ScaleValue: scale value
  • src: pointer to the original matrix
  • dest: pointer to the result matrix
  • numRows: number of rows in the source matrix
  • numCols: number of cols in the source matrix
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.


with :
i Î [0, numRows1-1]
j Î [0, numCols2-1]
k Î [0, numCols1Rows2-1]

Parameters
  • src1: pointer to the first matrix
  • src2: pointer to the second matrix
  • dest: pointer to result matrix
  • numRows1: number of rows in the first matrix
  • numCols2: number of columns in the second matrix
  • numCols1Rows2: number of columns in the first matrix and rows in the second matrix
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.


with :
i Î [0, numRows1-1]
j Î [0, numCols2-1]
k Î [0, numCols1Rows2-1]

Parameters
  • src1: pointer to the first matrix
  • src2: pointer to the second matrix
  • dest: pointer to result matrix
  • numRows1: number of rows in the first matrix
  • numCols2: number of columns in the second matrix
  • numCols1Rows2: number of columns in the first matrix and rows in the second matrix
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
  • src1: pointer to the first matrix
  • src2: pointer to the second matrix
  • dest: pointer to the result matrix
  • numRows1: number of rows in the first matrix
  • numCols2: number of columns in the second matrix
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
  • src1: pointer to the first matrix
  • src2: pointer to the second matrix
  • dest: pointer to the result matrix
  • numRows1: number of rows in the first matrix
  • numCols2: number of columns in the second matrix
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]
  
}
Copyright (c) 2002-2019 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code