Matrices Library

mikroC PRO for dsPIC30/33 and PIC24 includes a library for operating and working with matrices. All routines work with fractional Q15 format.

Library Routines

Matrix_Transpose

Prototype

void Matrix_Transpose(unsigned *src, unsigned *dest, unsigned numRows, unsigned numCols);

Description

Function does matrix transposition.

dstM[i][j] = srcM[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.

Example
int mx1[6] = {1,2,3,4,5,6};
int mxDest[9];

Matrix_Transpose(mx1, mxDest, 2,3);
Notes [W0..W5] used, not restored

Matrix_Subtract

Prototype

void Matrix_Subtract(unsigned *src1, unsigned *src2, unsigned *dest, unsigned num_rows, unsigned num_cols);

Description

Function does matrix subtraction.

dstM[i][j] = srcM1[i][j] - srcM2[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.

Example
int mx1[6] = {1,2,3,4,5,6};
int mx2[6] = {2,2,2,2,2,2};
int mxDest[9];

Matrix_Subtract(mx1, mx2, mxDest, 2, 3);
Notes
  • [W0..W4] used, not restored
  • AccuA used, not restored
  • AccuB used, not restored
  • CORCON saved, used, restored

Matrix_Scale

Prototype

void Matrix_Scale(unsigned ScaleValue, unsigned *src1, unsigned *dest, unsigned numRows, unsigned numCols);

Description

Function does matrix scale.

dstM[i][j] = sclVal * srcM[i][j]

Parameters
  • ScaleValue: scale value
  • src1: 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.

Example
int mx1[6] = {0xA000,0xC000,0xE000,           // -0.75 -0.5 -0.25
              0x2000,0x4000,0x6000};          //  0.25  0.5  0.75
int mxDest[9];

Matrix_Scale(0x4000, mx1, mxDest, 2,3);
Notes
  • [W0..W5] used, not restored
  • AccuA used, not restored
  • CORCON saved, used, restored
  • numRows*numCols < 214
  • Matix members are in Radix Q15 number format.

Matrix_Multiply

Prototype

void Matrix_Multiply(unsigned *src1, unsigned *src2, unsigned *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.

Example
int mx1[6] = {0x4000,0x4000,0x4000,           // 0.5
              0x4000,0x4000,0x4000};
int mxDest[9];

Matrix_Multiply(mx1,mx1,mxDest,2,2,3);
Notes
  • [W0..W7] used, not restored
  • [W8..W13] used, and restored
  • AccuA used, not restored
  • CORCON saved, used, restored
  • Matix members are in Radix Q15 number format.

Matrix_Add

Prototype

void Matrix_Add(unsigned *src1, unsigned *src2, unsigned *dest, unsigned numRows, unsigned numCols);

Description

Function does matrix addition.

dstM[i][j] = srcM1[i][j] + srcM2[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.

Example
int mx1[6] = {1,2,3,4,5,6};
int mx2[6] = {2,2,2,2,2,2};
int mxDest[9];

Matrix_Add(mx1,mx2, mxDest,2,3);
Notes
  • [W0..W4] used, not restored
  • AccuA used, not restored.
  • CORCON saved, used, restored.
  • numRows1*numCols2 < 214
Copyright (c) 2002-2018 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