39 return( (MatA.
rows == MatA.
cols) ? 1 : 0 );
58 printf(
"MatAlloc: rows < 1\n");
66 printf(
"MatAlloc: cols < 1\n");
121 for (i=0;i<matF.
rows;i++)
131 printf(
"MatFree: attempt to free null matF row: %d\n", i);
141 printf(
"MatFree: attempt to free null matF\n");
155 float *f = (
float *) V;
164 MatA.
data[r][c] = f[k++];
192 for(r=0;r<matrix.
rows;++r)
194 for(c=0;c<matrix.
cols;++c)
217 int rows = MatA.
rows;
218 int cols = MatA.
cols;
224 printf(
"DeleteRowCol cols:%d < 2 error\n",cols);
232 printf(
"DeleteRowCol rows:%d < 2 error\n",rows);
238 for(r=0;r<MatA.
rows;++r)
244 for(c=0;c<MatA.
cols;++c)
249 MatM.
data[rM][cM] = MatA.
data[r][c];
269 for (r = 0; r < MatA.
rows; r++)
272 for( c = 0 ; c < MatA.
cols ; c++ )
312 float D =
Minor(MatA, row, col);;
314 D *= ( ((row+col) & 1) ? -1.0 : 1.0);
331 for(r = 0; r< MatA.
rows;++r)
333 for(c = 0; c < MatA.
cols;++c)
359 printf(
"Determinate: Matrix MUST be square!\n");
367 printf(
"Determinate: Matrix size MUST be > 0!\n");
384 for (n=0;n<MatA.
size;++n)
417 printf(
"Determinant(MatA) = 0!\n\n");
423 printf(
"Determinant(MatA):\n%e\n\n", (
double) D);
428 printf(
"Adjugate(MatA)\n");
432 for (r=0;r<MatAdj.
rows;++r)
435 for (c=0;c<MatAdj.
cols;++c)
437 MatAdj.
data[r][c] /= D;
441 printf(
"Adjugate(MatA)/Determinant(MatA)\n");
499 printf(
"error MatA cols(%d) != MatB rows(%d)\n", MatA.
cols, MatB.
rows);
503 for (rA = 0; rA < MatA.
rows; ++rA)
506 for (cB = 0; cB < MatB.
cols; ++cB)
509 for (rB = 0; rB < MatB.
rows; ++rB)
511 sum += (MatA.
data[rA][rB] * MatB.
data[rB][cB]);
544 fp =
fopen(name,
"rb");
551 ptr =
fgets(tmp,253,fp);
559 cnt =
sscanf(ptr,
"Matrix R:%d C:%d", (
int *) &rows, (
int *) &cols);
560 if(rows < 1 || cols < 1)
562 printf(
"sscanf: %d\n",cnt);
563 printf(
"MatRead expected header: Matrix R:%d C:%d\n",tmp);
572 printf(
"MatRead(%s: &d,&d) could not alloc memory\n", name, rows,cols);
580 ptr =
fgets(tmp,253,fp);
611 fp =
fopen(name,
"wb");
618 for(r=0;r<MatW.
rows;++r)
620 for(c=0;c<MatW.
cols;++c)
739 int main(
int argc,
char *argv[])
741 mat_t MatA,MatX,MatY;
751 printf(
"==========================================\n");
756 printf(
"Adjugate(MatA)\n");
760 printf(
"==========================================\n");
765 printf(
"==========================================\n");
778 printf(
"==========================================\n");
786 printf(
"==========================================\n");
787 printf(
"Set of three display positions\n");
796 printf(
"Correponding touch screen positions, differing scale and skew\n");
807 printf(
"Solution Matrix to translate touch screen to screen positions\n");
823 printf(
"==========================================\n");
832 printf(
"==========================================\n");
833 printf(
"Set of five display positions\n");
842 printf(
"Correponding touch screen positions, differing scale and skew\n");
847 printf(
"Compute pseudo-inverse matrix, 1/(AT × A) × AT\n");
849 printf(
"PI = Pseudo Invert(A)\n");
853 printf(
"Solution Matrix to translate touch screen to screen positions\n");
855 MatR =
MatMul(MatPI,MatX);
862 MatR =
MatMul(MatPI,MatY);
870 printf(
"==========================================\n");
MEMSPACE int TestSquare(mat_t MatA)
Credits: https://www.cs.rochester.edu/~brown/Crypto/assts/projects/adj.html.
Master include file for project Includes all project includes and defines here.
MEMSPACE void MatPrint(mat_t matrix)
Print a matrix.
MEMSPACE mat_t Transpose(mat_t MatA)
Transpose matrix.
MEMSPACE char * fgets(char *str, int size, FILE *stream)
get a string from stdin See fdevopen() sets stream->put get for TTY devices
MEMSPACE FILE * fopen(const char *path, const char *mode)
POSIX Open a file with path name and ascii file mode string.
MEMSPACE int fprintf(FILE *fp, const char *format,...)
fprintf function Example user defined printf function using fputc for I/O This method allows I/O to d...
MEMSPACE float Cofactor(mat_t MatA, int row, int col)
Cofactor is determinate of minor submatrix * (-1)exp(row+col) Minor submatrix has one less row and co...
MEMSPACE mat_t MatAlloc(int rows, int cols)
Allocate a matrix.
MEMSPACE uint16_t sum(char *name)
sum of a file with 16bit hex and integer results
MEMSPACE float Minor(mat_t MatA, int row, int col)
Compute determinate of the minor submatrix Minor submatrix has one less row and column as a result...
MEMSPACE double strtod(const char *nptr, char **endptr)
MEMSPACE mat_t DeleteRowCol(mat_t MatA, int row, int col)
Create smaller matrix by deleatting specified row and colume Used by Minor and Cofactor.
int main(int argc, char *argv[])
MEMSPACE int fclose(FILE *stream)
POSIX close a file stream.
int sscanf(const char *strp, const char *fmt,...)
MEMSPACE mat_t MatRead(char *name)
Read a matrix.
MEMSPACE mat_t Adjugate(mat_t MatA)
Adjugate is transpose of cofactor matrix of A.
MEMSPACE mat_t MatLoad(void *V, int rows, int cols)
Load a matrix.
MEMSPACE void MatFree(mat_t matF)
Free a matrix.
MEMSPACE int printf(const char *format,...)
MEMSPACE mat_t MatMul(mat_t MatA, mat_t MatB)
Multiply two matrix.
MEMSPACE int MatWrite(char *name, mat_t MatW)
Write a matrix.
MEMSPACE mat_t MatLoadSQ(void *V, int size)
Load a square matrix.
MEMSPACE void safefree(void *p)
Safe free - Only free a pointer if it is in malloc memory range. We want to try to catch frees of sta...
MEMSPACE float Determinant(mat_t MatA)
Determinant by recursion using Cofactors.
MEMSPACE mat_t MatAllocSQ(int size)
Allocate a matrix.
MEMSPACE mat_t Invert(mat_t MatA)
Calculate Matrix Inverse.
MEMSPACE void * safecalloc(size_t nmemb, size_t size)
Safe Calloc - Display Error message if Calloc fails.
MEMSPACE mat_t PseudoInvert(mat_t MatA)
Calculate Pseudo Matrix Inverse Used for least square fitting of non square matrix with excess soluti...