Matrices Library
mikroPascal 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).
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 |
procedure Matrix_Transpose16(src : ^integer; dest : ^integer; numRows : word; numCols : word); |
---|---|
Description |
Function does matrix transposition. dest[i][j] = src[j][i] |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Notes |
None. |
Matrix_Transpose32
Prototype |
procedure Matrix_Transpose32(src : ^longint; dest : ^longint; numRows : word; numCols : word); |
---|---|
Description |
Function does matrix transposition. dest[i][j] = src[j][i] |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Notes |
None. |
Matrix_Subtract16
Prototype |
procedure Matrix_Subtract16(src1 : ^integer; src2 : ^integer; dest : ^integer; numRows : word; numCols : word); |
---|---|
Description |
Function does matrix subtraction. dest[i][j] = src1[i][j] - src2[i][j] |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Notes |
None. |
Matrix_Subtract32
Prototype |
procedure Matrix_Subtract32(src1 : ^longint; src2 : ^longint; dest : ^longint; numRows : word; numCols : word); |
---|---|
Description |
Function does matrix subtraction. dest[i][j] = src1[i][j] - src2[i][j] |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Notes |
None. |
Matrix_Scale16
Prototype |
procedure Matrix_Scale16(ScaleValue : integer; src : ^integer; dest : ^integer; numRows : word; numCols : word); |
---|---|
Description |
Function does matrix scale. dest[i][j] = ScaleValue * src[i][j] |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Notes |
None. |
Matrix_Scale32
Prototype |
procedure Matrix_Scale32(ScaleValue : longint; src : ^longint; dest : ^longint; numRows : word; numCols : word); |
---|---|
Description |
Function does matrix scale. dest[i][j] = ScaleValue * src[i][j] |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Notes |
None. |
Matrix_Multiply16
Prototype |
procedure Matrix_Multiply16(src1 : ^integer; src2 : ^integer; dest : ^integer; numRows1 : word; numCols2 : word; numCols1Rows2 : word); |
---|---|
Description |
Function does matrix multiplication.
|
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Notes |
None. |
Matrix_Multiply32
Prototype |
procedure Matrix_Multiply32(src1 : ^integer; src2 : ^integer; dest : ^integer; numRows1 : word; numCols2 : word; numCols1Rows2 : word); |
---|---|
Description |
Function does matrix multiplication.
|
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Notes |
None. |
Matrix_Add16
Prototype |
procedure Matrix_Add16(src1 : ^integer; src2 : ^integer; dest : ^integer; numRows : word; numCols : word); |
---|---|
Description |
Function does matrix addition. dest[i][j] = src1[i][j] + src2[i][j] |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Notes |
None. |
Matrix_Add32
Prototype |
procedure Matrix_Add32(src1 : ^longint; src2 : ^longint; dest : ^longint; numRows : word; numCols : word); |
---|---|
Description |
Function does matrix addition. dest[i][j] = src1[i][j] + src2[i][j] |
Parameters |
|
Returns |
Nothing. |
Requires |
Nothing. |
Notes |
None. |
Library Example
program Matrices_Test; var Q15_1, Q15_2, Q15_Out : array[3] of array[3] of integer; procedure Init(); begin Q15_1[0][0] := 0x1000; Q15_1[0][1] := 0x2000; Q15_1[0][2] := 0x3000; Q15_1[1][0] := 0x4000; Q15_1[1][1] := 0x5000; Q15_1[1][2] := 0x6000; Q15_1[2][0] := 0x7000; Q15_1[2][1] := integer(0x8000); Q15_1[2][2] := integer(0x9000); Q15_2[0][0] := integer(0x9000); Q15_2[0][1] := integer(0x8000); Q15_2[0][2] := 0x7000; Q15_2[1][0] := 0x6000; Q15_2[1][1] := 0x5000; Q15_2[1][2] := 0x4000; Q15_2[2][0] := 0x3000; Q15_2[2][1] := 0x2000; Q15_2[2][2] := 0x1000; end; begin Init(); Matrix_Transpose16(@Q15_1[0], @Q15_Out[0], 3, 3); // Q15_Out = ( 0x1000, 0x4000, 0x7000, // 0x2000, 0x5000, 0x8000, // 0x3000, 0x6000, 0x9000 ) Matrix_Subtract16(@Q15_1[0], @Q15_2[0], @Q15_Out[0], 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[0], @Q15_2[0], @Q15_Out[0], 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[0], @Q15_Out[0], 3, 3); // Q15_Out = ( 0x0800, 0x1000, 0x1800, // 0x2000, 0x2800, 0x3000, // 0x3800, 0xC000, 0xC800 ) Matrix_Multiply16(@Q15_1[0][0], @Q15_2[0][0], @Q15_Out[0][0], 3, 3, 3); // Q15_Out = ( 0x1C00, 0x1000, 0x2400, // 0x2800, 0x0A00, 0x6C00, ` // 0x1400, 0x2400, 0x1400 ) // Overflow : Q15_Out[2][0], Q15[2][1] end.