Mindstorms 3rd Party ROBOTC Drivers RobotC
[Home] [Download] [Submit a bug/suggestion] [ROBOTC Forums] [Blog] [Support this project]

math-matrix.h

Go to the documentation of this file.
00001 /*!@addtogroup other
00002  * @{
00003  * @defgroup matrixfloat Matrix Library
00004  * matrix Math Library
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: math-matrix.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __MATH_MATRIX_FLOAT_H__
00013 #define __MATH_MATRIX_FLOAT_H__
00014 /** \file math-matrix.h
00015  * \brief Matrix library
00016  *
00017  * math-matrix.h provides a number of frequently used functions that are useful for
00018  * doing math with matrices.
00019  * Taken from http://playground.arduino.cc/Code/MatrixMath and ported to ROBOTC.
00020  * License: You may use this code as you wish, provided you give credit where its due.
00021  *
00022  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00023 
00024  *
00025  * Changelog:
00026  * - 0.1: Initial release
00027  *
00028  * \author Charlie Matlack
00029  * \author RobH45345
00030  * \author Xander Soldaat (xander_at_botbench.com)
00031  * \date 2 March 2013
00032  * \version 0.1
00033  */
00034 
00035 #pragma systemFile
00036 
00037 #define NR_END 1
00038 
00039 #define MATRIX_MAX_SIZE 10
00040 
00041 
00042 /**
00043  * Prints a nicely formatted version of the matrix to the debugstream
00044  *
00045  * This function is for floats
00046  * @param matrix the matrix to be printed
00047  * @param numRows the number of rows in the matrix
00048  * @param numCols the number of columns in the matrix
00049  * @param label the label to use when printing the matrix
00050  */
00051 void matrixPrintF(float* matrix, int numRows, int numCols, char* label){
00052   // matrixA = input matrix (numRowsA x n)
00053   int i,j;
00054   writeDebugStreamLine(label);
00055   for (i=0; i<numRows; i++){
00056     for (j=0;j<numCols;j++){
00057       writeDebugStream("%f", matrix[numCols*i+j]);
00058       writeDebugStream("\t");
00059     }
00060     writeDebugStreamLine("");
00061   }
00062 }
00063 
00064 
00065 /**
00066  * Prints a nicely formatted version of the matrix to the debugstream
00067  *
00068  * This function is for longs
00069  * @param matrix the matrix to be printed
00070  * @param numRows the number of rows in the matrix
00071  * @param numCols the number of columns in the matrix
00072  * @param label the label to use when printing the matrix
00073  */
00074 void matrixPrintL(long* matrix, int numRows, int numCols, char* label){
00075   // matrixA = input matrix (numRowsA x n)
00076   int i,j;
00077   writeDebugStreamLine(label);
00078   for (i=0; i<numRows; i++){
00079     for (j=0;j<numCols;j++){
00080       writeDebugStream("%d", matrix[numCols*i+j]);
00081       writeDebugStream("\t");
00082     }
00083     writeDebugStreamLine("");
00084   }
00085 }
00086 
00087 
00088 /**
00089  * Copies all the values from one matrix into another
00090  *
00091  * This function is for floats
00092  * @param source the matrix which is to be copied
00093  * @param numRows the number of rows in the source matrix
00094  * @param numCols the number of columns in the source matrix
00095  * @param destination the matrix to copy to
00096  */
00097 void matrixCopyF(float* source, int numRows, int numCols, float* destination)
00098 {
00099   memcpy(destination, source, numRows * numCols * sizeof(float));
00100 }
00101 
00102 
00103 /**
00104  * Copies all the values from one matrix into another
00105  *
00106  * This function is for longs
00107  * @param source the matrix which is to be copied
00108  * @param numRows the number of rows in the source matrix
00109  * @param numCols the number of columns in the source matrix
00110  * @param destination the matrix to copy to
00111  */
00112 void matrixCopyL(long* source, int numRows, int numCols, long* destination)
00113 {
00114   memcpy(destination, source, numRows * numCols * sizeof(long));
00115 }
00116 
00117 
00118 /**
00119  * Copies all the values from one matrix into another
00120  *
00121  * This function is for floats
00122  * @param matrixA the first matrix to be multiplied
00123  * @param matrixB the second matrix to be multiplied
00124  * @param numRowsA the number of rows in the first matrix
00125  * @param numColsA the number of columns in the first matrix
00126  * @param numColsB the number of columns in the second matrix
00127  * @param matrixC the resulting matrix
00128  */
00129 void matrixMultF(float* matrixA, float* matrixB, int numRowsA, int numColsA, int numColsB, float* matrixC)
00130 {
00131   int i, j, k;
00132   for (i = 0; i < numRowsA; i++)
00133     for(j = 0; j < numColsB; j++)
00134   {
00135     matrixC[numColsB * i + j] = 0;
00136     for (k = 0; k < numColsA; k++)
00137       matrixC[numColsB*i+j]= matrixC[numColsB*i+j]+matrixA[numColsA*i+k]*matrixB[numColsB*k+j];
00138   }
00139 }
00140 
00141 
00142 /**
00143  * Copies all the values from one matrix into another
00144  *
00145  * This function is for longs
00146  * @param matrixA the first matrix to be multiplied
00147  * @param matrixB the second matrix to be multiplied
00148  * @param numRowsA the number of rows in the first matrix
00149  * @param numColsA the number of columns in the first matrix
00150  * @param numColsB the number of columns in the second matrix
00151  * @param matrixC the resulting matrix
00152  */
00153 void matrixMultL(long* matrixA, long* matrixB, int numRowsA, int numColsA, int numColsB, long* matrixC)
00154 {
00155   int i, j, k;
00156   for (i = 0; i < numRowsA; i++)
00157     for(j = 0; j < numColsB; j++)
00158   {
00159     matrixC[numColsB * i + j] = 0;
00160     for (k = 0; k < numColsA; k++)
00161       matrixC[numColsB*i+j]= matrixC[numColsB*i+j]+matrixA[numColsA*i+k]*matrixB[numColsB*k+j];
00162   }
00163 }
00164 
00165 
00166 /**
00167  * Adds values of two matrices
00168  *
00169  * This function is for floats
00170  * @param matrixA the first matrix to be added
00171  * @param matrixB the second matrix to be added
00172  * @param numRowsA the number of rows of both matrices
00173  * @param numColsA the number of columns of both matrices
00174  * @param matrixC the resulting matrix
00175  */
00176 void matrixAddF(float* matrixA, float* matrixB, int numRowsA, int numColsA, float* matrixC)
00177 {
00178   int i, j;
00179   for (i = 0; i < numRowsA; i++)
00180     for(j = 0; j < numColsA; j++)
00181     matrixC[numColsA * i + j] = matrixA[numColsA * i + j] + matrixB[numColsA * i + j];
00182 }
00183 
00184 
00185 /**
00186  * Adds values of two matrices
00187  *
00188  * This function is for longs
00189  * @param matrixA the first matrix to be added
00190  * @param matrixB the second matrix to be added
00191  * @param numRowsA the number of rows of both matrices
00192  * @param numColsA the number of columns of both matrices
00193  * @param matrixC the resulting matrix
00194  */
00195 void matrixAddL(long* matrixA, long* matrixB, int numRowsA, int numColsA, long* matrixC)
00196 {
00197   int i, j;
00198   for (i = 0; i < numRowsA; i++)
00199     for(j = 0; j < numColsA; j++)
00200     matrixC[numColsA * i + j] = matrixA[numColsA * i + j] + matrixB[numColsA * i + j];
00201 }
00202 
00203 
00204 /**
00205  * Adds values of two matrices
00206  *
00207  * This function is for floats
00208  * @param matrixA the first matrix to be added
00209  * @param matrixB the second matrix to be added
00210  * @param numRowsA the number of rows of both matrices
00211  * @param numColsA the number of columns of both matrices
00212  * @param matrixC the resulting matrix
00213  */
00214 void matrixSubtractF(float* matrixA, float* matrixB, int numRowsA, int numColsA, float* matrixC)
00215 {
00216   int i, j;
00217   for (i = 0; i < numRowsA; i++)
00218     for(j = 0; j < numColsA; j++)
00219     matrixC[numColsA * i + j] = matrixA[numColsA * i +j ] - matrixB[numColsA * i + j];
00220 }
00221 
00222 
00223 /**
00224  * Adds values of two matrices
00225  *
00226  * This function is for longs
00227  * @param matrixA the first matrix to be added
00228  * @param matrixB the second matrix to be added
00229  * @param numRowsA the number of rows of both matrices
00230  * @param numColsA the number of columns of both matrices
00231  * @param matrixC the resulting matrix
00232  */
00233 void matrixSubtractL(long* matrixA, long* matrixB, int numRowsA, int numColsA, long* matrixC)
00234 {
00235   int i, j;
00236   for (i = 0; i < numRowsA; i++)
00237     for(j = 0; j < numColsA; j++)
00238     matrixC[numColsA * i + j] = matrixA[numColsA * i +j ] - matrixB[numColsA * i + j];
00239 }
00240 
00241 
00242 /**
00243  * Transpose a matrix
00244  *
00245  * This function is for floats
00246  * @param matrixA the first matrix to be transposed
00247  * @param numRowsA the number of rows of matrixA
00248  * @param numColsA the number of columns matrixA
00249  * @param matrixC the resulting matrix
00250  */
00251 void matrixTransposeF(float* matrixA, int numRowsA, int numColsA, float* matrixC)
00252 {
00253   int i, j;
00254   for (i = 0;i < numRowsA; i++)
00255     for(j = 0; j < numColsA; j++)
00256     matrixC[numRowsA * j + i] = matrixA[numColsA * i + j];
00257 }
00258 
00259 
00260 /**
00261  * Transpose a matrix
00262  *
00263  * This function is for longs
00264  * @param matrixA the first matrix to be transposed
00265  * @param numRowsA the number of rows of matrixA
00266  * @param numColsA the number of columns matrixA
00267  * @param matrixC the resulting matrix
00268  */
00269 void matrixTransposeL(long* matrixA, int numRowsA, int numColsA, long* matrixC)
00270 {
00271   int i, j;
00272   for (i = 0;i < numRowsA; i++)
00273     for(j = 0; j < numColsA; j++)
00274     matrixC[numRowsA * j + i] = matrixA[numColsA * i + j];
00275 }
00276 
00277 
00278 #endif // __MATH_MATRIX_FLOAT_H__
00279 /*
00280  * $Id: math-matrix.h 133 2013-03-10 15:15:38Z xander $
00281  */
00282 /* @} */
00283 /* @} */