The function takes as parameters a pointer to the original matrix, the row and column who will be eliminated and the order of the minor. Since the minor will be a square matrix as well, the order represents the number of columns/rows of the minor matrix.
Minor examples |
/* * Description: * Computes the minor of a given matrix according to * a predefined @row, @column and @order. * Parameters: * mat1 - a pointer to the matrix * row - the starting row * column - the starting column * order - the size of the minor * Returns: * A pointer to the minor matrix if the conditions are valid. * Otherwise returns NULL. * Preconditions: * @mat must not be NULL * @may must be a square matrix * @column and @row must be smaller than @order * @order must be greater than 1 but smaller than * the size of the matrix */ NMatrix* NMatrix_Minor(const NMatrix *mat, integer row, integer column, integer order);The implementation of the function is:
NMatrix* NMatrix_Minor(const NMatrix *mat, integer row, integer column, integer order) { integer i=0, j=0; integer rowCount = 0, colCount = 0; NMatrix *minor = NULL; if((mat->rows==mat->columns) && ((row-1)<order) && ((column-1)< order) && (order>=1) && (order<mat->rows) ) { if(order==1) { minor = NMatrix_Create(1,1); minor->data[0][0] = mat->data[row][column]; } else { minor = NMatrix_Create(order, order); for(i=0; i<=order; i++) { if(i!=row) { colCount = 0; for(j=0;j<=order;j++) { if(j!=column) { minor->data[rowCount][colCount] = mat->data[i][j]; colCount++; } } rowCount++; } } } } return minor; }Here's an example on how to use the function:
#include<stdio.h> #include"NMatrix.h" void PrintMatrix(NMatrix *mat) { integer i = 0, j = 0; for (i = 0; i < mat->rows; i++) { for (j = 0; j < mat->columns; j++) { printf("%f ", mat->data[i][j]); } putchar('\n'); } putchar('\n'); } int main(int argc, char *argv[]) { integer i = 0, j = 0; NMatrix *mat = NULL; NMatrix *minor = NULL; /*Creating the matrix*/ mat = NMatrix_Create(5, 5); /*Assigning values to the matrix*/ for (i = 0; i < 5; i++) { for (j = 0; j < 5; j++) { mat->data[i][j] = (real) (i + j); } } /*Computing the minor*/ minor = NMatrix_Minor(mat, 1, 1, 3); /*Printing the matrices*/ puts("Original matrix: "); PrintMatrix(mat); puts("Minor: "); PrintMatrix(minor); return 0; } /*: Original matrix: 0.000000 1.000000 2.000000 3.000000 4.000000 1.000000 2.000000 3.000000 4.000000 5.000000 2.000000 3.000000 4.000000 5.000000 6.000000 3.000000 4.000000 5.000000 6.000000 7.000000 4.000000 5.000000 6.000000 7.000000 8.000000 Minor: 0.000000 2.000000 3.000000 2.000000 4.000000 5.000000 3.000000 5.000000 6.000000 */
No comments:
Post a Comment
Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!