递归矩阵的行列式 [英] Determinant of matrix with recursion

查看:71
本文介绍了递归矩阵的行列式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码递归计算q阶矩阵的行列式.它适用于q = 3和q = 2,但是对于q = 4,它输出的垃圾值在每次运行程序时都会更改:这里出了什么问题?

The code below calculates the determinant of a matrix of order q recursively. It works for q=3 and q=2 but for q=4 it outputs garbage values which change every time I run the program: What is going wrong here?

#include <stdio.h>
#include <math.h>

int det(int q, int arr[q][q]);
int main(void)
{
    int arr[4][4] = {
            {2,4,9,8},
            {6,3,4,5},
            {5,7,8,6},
            {3,2,5,7}
            };
    printf("value of determinant is %d\n", det(4, arr));
}

int det(int q, int arr[q][q])
{   
    if(q>2)
    {
    int i, j, k, m, n, s[q-1][q-1], d=0, cof;
    for(k=-1,i=0,j=0;k<q-1;k++)
    {
        i=0;j=0;
        for(m=1;m<q;m++,i++)
        {
            n=0;j=0;
            for(n,j;n<k+1;n++,j++)
            {
                s[i][j] = arr[m][n];
            }
            n=q-1+k;
            for(n;n<q;n++,j++)
            {
                s[i][j] = (arr[m][n]);
            }
        }
        cof = (arr[0][k+1])*(pow(-1,k+1));
        d += cof*det(q-1, s);
    }
    return d;
    }
    else if(q==2)
    {
        int d = ((arr[0][0])*(arr[1][1])-(arr[0][1])*(arr[1][0]));      
        return d;
    }
}

推荐答案

在您的代码中,问题的主要根源是算法.建议您刷新行列式概念.您可以查看以下代码,以检查错误.您使用的算法.

In your code, the main root of the problem is the algorithm.I suggest you refresh the determinant concepts.You can review the code below to check mistakes in the algorithm you used.

#include <stdio.h>
#include <math.h>

int det(int q, int arr[q][q]);
int main(void)
{
    int arr[5][5] = {
            {2,4,9,1,2},
            {6,3,4,2,6},
            {5,7,8,6,9},
            {9,1,5,3,3},
            {3,2,5,3,9}
            };
    printf("value of determinant is %d\n", det(5, arr));
    return 0;
}

int det(int q, int arr[q][q])
{
    if(q>2)
    {
        int resultOfSubStep = 0 ; //Final result that will be returned
        int smallerMatrix[q-1][q-1] ; //Matrix in which smaller matrix are to be stored which will be given as arguments to det function in current recursion 
        int i = 0 ; 
        for(;i<q;i++){
            int j = 0 ;

            for (;j<q-1;j++) { //This for loop copies the element required from arr into smallerMatrix
                int counter = 0 ;
                int k = 0 ;
                for (;k<q;k++){
                    if(k == i)continue;
                    smallerMatrix[j][counter] = arr[j+1][k];
                    counter++;
                }
            }
            int k1 = arr[0][i]*det(q-1, smallerMatrix); //This is the result of determinant of q-1*q-1 matrix
            resultOfSubStep += pow(-1,i)*k1; //multiplied by -1 or +1 according to the position of root element
        }
        return resultOfSubStep;
    }
    else if(q==2)
    {
        int d = ((arr[0][0])*(arr[1][1])-(arr[0][1])*(arr[1][0]));
        return d;
    }
    else return arr[0][0];
}

在注释部分,您要求其他方法.我认为,LU分解是最简单的方法.您可以在 LU分解中查看其详细信息.在这种方法中,您必须将给定矩阵简化为上或下三角矩阵,然后仅取对角线元素的乘积,因此无需递归.希望这会有所帮助.

In the comments section, you asked for other methods.In my opinion, LU decomposition is the easiest one. you can check details for it on LU Decomposition.In this method, you have to reduce the given matrix to upper or lower triangular matrix and then just take the product of diagonal elements.So no need for recursion. Hope this helps.

这篇关于递归矩阵的行列式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆