Ejemplo n.º 1
0
int inverse_define(Matrix *matrix, Matrix *inverse_matri) {
	size_t i, j;
	size_t row = matrix->row_len;
	float *arr = matrix->arr;
	float determ = determinant(matrix);    //求整个矩阵的行列式
    float *arr_inverse = inverse_matri->arr;
    if (determ != 0)
    {
        fprintf(stderr, "The determinant of the matrix is %f\n", determ);
        for (i = 0; i < row; i++)    //求逆矩阵
        {
            for (j = 0; j < row; j++)
            {
                *(arr_inverse + j * row + i) = Creat_M(arr, i, j, row)/determ;
            }
        }
    }
    else
    {
        fprintf(stderr, "The determinant is 0, and there is no inverse matrix!\n");
    }
	return 0;
}
Ejemplo n.º 2
0
int mArrayInvert(void)
{
	#if 0
	float *buffer, *p;            
    int row, num;                
    int i, j;
    float determ;                
    float a[N][N], b[N][N];
    int n;
	int mRet = 0;
    
	printf("Test invert matrix using invert matrix defination!\n");
	printf("Please input matrix Row number:\t");
	//mFlush();
	scanf("%d", &row);
	//mFlush();
    num = 2 * row * row;
    buffer = (float *)malloc(num*sizeof(float));        
    p = buffer;
    if (NULL != p)
    {
        for (i = 0; i < row; i++)
        {
			printf("Please input the value of %d row: \t", i+1);
            for (j = 0; j < row; j++)
            {
				//mFlush();
				scanf("%f", p++);
				//mFlush();
            }
        }
    }
    else
    {
	   printf("Can't distribute memory\n");
    }
    printf("The original matrix : \n");
    print(buffer, row);                
    
    determ = MatDet(buffer, row);    
    p = buffer + row * row;
    if (determ != 0)
    {
		printf("The determinant of the matrix is ", determ);
        for (i = 0; i < row; i++)    
        {
            for (j = 0; j < row; j++)
            {
                *(p+j*row+i) = Creat_M(buffer, i, j, row)/determ;
            }
        }
        printf("The inverse matrix is:\n"); 
        print(p, row);                
    }
    else
    {
        printf("The determinant is 0, and there is no inverse matrix!\n");
    }
    free(buffer);   
	
	#endif

	          
                   
    int i, j;
    float determ;                
    float a[N][N], b[N][N];
    int n = 3;
	int mRet = 0;     
	printf("\nTo get convert matrix using mGauss!\n");
   
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
			//scanf("%f", &a[i][j]);
			a[i][j] = mCCMatrix[i][j];
        }
    }
	printf("Orignal Matrix:\n");
	for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
			printf("%10.6f\t", a[i][j]);
        }
		printf("\n");
    }
   
    if (!mGauss(a, b, n))
    {
		printf("Inverse matrix:\n");
        for (i = 0; i < n; i++)
        {
            
            for (j = 0; j < n; j++)
            {
                printf("%10.6f\t", b[i][j]);
            }
           printf("\n");
        }

		printf("Copy Inverse Matrix:\n");
		for (i=0; i<n; i++)
		{
			for (j=0; j<n; j++)
			{
				mCCMatrixInvert[i][j] = b[i][j];
				printf("%10.6f\t", mCCMatrixInvert[i][j]);
			}
			printf("\n");
		}
    } 

	mRet = mVerifyMatrix(a, b, n);
	
	return mRet;
}