Example #1
0
double matrix_determinant(Matrix M)
{
    assert(is_square_matrix(M));
    double temp;
    double det = 1;
    int n = M->c;

    if(n == 2) {
        return M->A[0][0] * M->A[1][1] - M->A[1][0] * M->A[0][1];
    }

    Matrix A = matrix_copy(M);
    for (int k = 0; k < n; k++) {
        int j = k;
        double max = A->A[j][j];
 
        for (int i = k + 1; i < n; i++)
            if ((temp = fabs(A->A[i][k])) > max) {
                j = i;
                max = temp;
            }
                
        matrix_swap_row(A, NULL, k, j);
        if(k != j)
            det *= -1;
 
        for (int i = k + 1; i < n; i++) {
            temp = A->A[i][k] / A->A[k][k];
            for (int j = k + 1; j < n; j++)
                A->A[i][j] -= temp * A->A[k][j];
            A->A[i][k] = 0;
        }
    }
    assert(is_square_matrix(A));
    for(int i = 0; i < n; i++)
        det *= A->A[i][i];
    free(A);
    return det;
}
Example #2
0
Matrix matrix_inverse(Matrix M)
{
    if(!is_square_matrix(M))
        return NULL;
    int n = M->r;
    Matrix N = matrix_new_empty(n, n);
    double det = matrix_determinant(M);
    if(double_equals(det, 0))
        return NULL;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++) {
            Matrix C = matrix_cofactor(M, i, j);
            if((i + j) % 2 == 0)
                N->A[i][j] = matrix_determinant(C) / det;
            else
                N->A[i][j] = -1 * matrix_determinant(C) / det;
            matrix_free(C);
        }
    Matrix I = matrix_transpose(N);
    matrix_free(N);
    assert(is_square_matrix(I));
    return I;
}
Example #3
0
void _main(void)
{
	short i_menu_choice;
	graph *p_graph = NULL;
	matrix *p_matrix = NULL;
	point v_key[8];

	ESI EsiPtr = top_estack;									//pointer en haut de la pile
	FontSetSys(F_6x8);
	clrscr();

	//Verifie si une matrice est passée en parametre
	if((GetArgType(EsiPtr) != LIST_TAG)||(GetArgType(EsiPtr-1) != LIST_TAG)){		//Ce n'est pas une matrice
		EsiPtr = open_file();		//Si ce n'est pas la cas, ouvrir une fenetre
	}

	//sym = DerefSym(SymFind(GetSymstrArg(EsiPtr)));

	if(! is_square_matrix(EsiPtr)){
		printf("\Error : The matrix must be a squared matrix (same height and width). ");
		ngetchx();
		exit(0);
	}