Matrices Library

mikroBasic PRO for PIC32 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).

Library Routines

Matrix_Transpose16

Prototype

sub procedure Matrix_Transpose16(dim src as ^integer, dim dest as ^integer, dim numRows as word, dim numCols as word)

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

sub procedure Matrix_Transpose32(dim src as ^longint, dim dest as ^longint, dim numRows as word, dim numCols as word)

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

sub procedure Matrix_Subtract16(dim src1 as ^integer, dim src2 as ^integer, dim dest as ^integer, dim numRows as word, dim numCols as word)

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

sub procedure Matrix_Subtract32(dim src1 as ^longint, dim src2 as ^longint, dim dest as ^longint, dim numRows as word, dim numCols as word)

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

sub procedure Matrix_Scale16(dim ScaleValue as integer, dim src as ^integer, dim dest as ^integer, dim numRows as word, dim numCols as word)

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

sub procedure Matrix_Scale32(dim ScaleValue as longint, dim src as ^longint, dim dest as longint, dim numRows as word, dim numCols as word)

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

sub procedure Matrix_Multiply16(dim src1 as ^integer, dim src2 as ^integer, dim dest as ^integer, dim numRows1 as word, dim numCols2 as word, dim numCols1Rows2 as word)

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

sub procedure Matrix_Multiply32(dim src1 as ^longint, dim src2 as ^longint, dim dest as ^longint, dim numRows1 as word, dim numCols2 as word, dim numCols1Rows2 as word)

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

sub procedure Matrix_Add16(dim src1 as ^integer, dim src2 as ^integer, dim dest as ^integer, dim numRows as word, dim numCols as word)

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

sub procedure Matrix_Add32(dim src1 as ^longint, dim src2 as ^longint, dim dest as ^longint, dim numRows as word, dim numCols as word)

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

program Matrix

dim Q15_1, Q15_2, Q15_Out as integer[3][3]

sub procedure Init()
    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 sub

main:

  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]
  asm nop end asm
end.
Copyright (c) 2002-2012 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