diff -r 000000000000 -r 2c8ba1964db7 MatrixMath.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MatrixMath.cpp Sat Nov 07 13:23:07 2015 +0100 @@ -0,0 +1,206 @@ +/* + * MatrixMath.cpp Library for MatrixMath + * + * Created by Charlie Matlack on 12/18/10. + * Modified from code by RobH45345 on Arduino Forums, taken from unknown source. + * MatrixMath.cpp + */ + +#include "Marlin.h" +#include "MatrixMath.h" + +#define NR_END 1 + +MatrixMath::MatrixMath() +{ +} + +// Matrix Printing Routine +// Uses tabs to separate numbers under assumption printed float width won't cause problems +void MatrixMath::MatrixPrint(float* A, int m, int n, String label){ + // A = input matrix (m x n) + int i,j; + SERIAL_ECHOLN(' '); + SERIAL_ECHOLN(label); + for (i=0; i= tmp) // 'Avoid using other functions inside abs()?' + { + tmp = abs(A[i*n+k]); + pivrow = i; + } + } + + // check for singular matrix + if (A[pivrow*n+k] == 0.0f) + { + SERIAL_ECHOLNPGM("Inversion failed due to singular matrix"); + return 0; + } + + // Execute pivot (row swap) if needed + if (pivrow != k) + { + // swap row k with pivrow + for (j = 0; j < n; j++) + { + tmp = A[k*n+j]; + A[k*n+j] = A[pivrow*n+j]; + A[pivrow*n+j] = tmp; + } + } + pivrows[k] = pivrow; // record row swap (even if no swap happened) + + tmp = 1.0f/A[k*n+k]; // invert pivot element + A[k*n+k] = 1.0f; // This element of input matrix becomes result matrix + + // Perform row reduction (divide every element by pivot) + for (j = 0; j < n; j++) + { + A[k*n+j] = A[k*n+j]*tmp; + } + + // Now eliminate all other entries in this column + for (i = 0; i < n; i++) + { + if (i != k) + { + tmp = A[i*n+k]; + A[i*n+k] = 0.0f; // The other place where in matrix becomes result mat + for (j = 0; j < n; j++) + { + A[i*n+j] = A[i*n+j] - A[k*n+j]*tmp; + } + } + } + } + + // Done, now need to undo pivot row swaps by doing column swaps in reverse order + for (k = n-1; k >= 0; k--) + { + if (pivrows[k] != k) + { + for (i = 0; i < n; i++) + { + tmp = A[i*n+k]; + A[i*n+k] = A[i*n+pivrows[k]]; + A[i*n+pivrows[k]] = tmp; + } + } + } + return 1; +} + +void MatrixMath::MatrixIdentity(float* A, int m, int n) +{ + int i, j; + for (i=0;i