简体   繁体   English

递归方法没有达到基本情况,但是,我不确定为什么。 爪哇

[英]Recursive method not reaching the base case, however, I'm not sure why. Java

This is kind of a weird project, so bare with the pretty weird code.这是一个奇怪的项目,所以没有非常奇怪的代码。 The real issue I'm having is why exactly this recursive method is never reaching the base case.我遇到的真正问题是为什么这种递归方法永远不会达到基本情况。

public int determinant() {
    int determinant = 0;
    if(getSize() == 2)
    {
        return (getElement(1, 1) * getElement (2, 2)) - (getElement(1,2) * getElement(2,1));
    }

    determinant += Math.pow(-1, getSize()+1) * getElement(getSize(), 1) * minor(getSize(),1).determinant();
    return determinant;
}

public SparseInterface minor(int row, int col) {
    SparseMatrix minor = new SparseMatrix();
    minor.matrix = matrix;
    for(int i = 0; i < matrix.size(); i++)
    {
        if(row == minor.matrix.get(i).getRow() || col == minor.matrix.get(i).getCol())
        {
            minor.matrix.remove(i);
        }
    }
    minor.size--;
    return minor;
}

So, determinant calls minor, which returns a new square matrix that's size is 1 smaller.因此,行列式调用minor,它返回一个大小为1 的新方阵。 I start with a 5x5 matrix, and I call the determinant method on it.我从一个 5x5 矩阵开始,并在其上调用行列式方法。 That method then takes the determinant of the minor of the 5x5 matrix (a 4x4 matrix).然后,该方法采用 5x5 矩阵(4x4 矩阵)的次要行列式。 This goes on until we reach the base case, where it is a 2x2 matrix and calculating the determinant is trivial.这一直持续到我们到达基本情况,其中它是一个 2x2 矩阵并且计算行列式是微不足道的。

However, the issue lies in the fact that the code never gets past the point of it being a 4x4 matrix.然而,问题在于代码永远不会超过它是一个 4x4 矩阵的点。 Every time that minor gets called (which it should on every recursive call) it should put out smaller matrix, due to the last line of code in minor, minor.size--;.每次调用该次要时(在每次递归调用时都应该这样做),由于次要中的最后一行代码,minor.size--;,它应该输出更小的矩阵。

With some bug testing, it seems that the size of the matrix gets decremented the first time, but not on subsequent recursive calls.通过一些错误测试,似乎矩阵的大小在第一次递减,但在随后的递归调用中不会递减。 I'm somewhat new to Java, does Java have some weird rules in regards to method calls within method calls that is causing it to not get decremented the way it should be?我对 Java 有点陌生,Java 是否对方法调用中的方法调用有一些奇怪的规则,导致它没有按照应有的方式递减? Other than that I am at a loss.除此之外,我一无所获。

Thanks in advance.提前致谢。

For those who asked:对于那些问:

public int getElement(int row, int col) {
    int element = 0;
    if(row > getSize() || col > getSize())
    {
        System.out.println("Column/row combination is out of bounds.");
        return element;
    }

    for(int i = 0; i < matrix.size(); i++)
    {
        if(matrix.get(i).getRow() == row && matrix.get(i).getCol() == col)
        {
            element = matrix.get(i).getData();
        }
    }
    return element;
}

public int getSize() {
    return size;
}

Size is is just a data member of the class SparseMatrix, that says the weird nonexistent matrix I'm modeling is SizexSize. Size 只是 SparseMatrix 类的一个数据成员,它表示我正在建模的奇怪的不存在矩阵是 SizexSize。 For more details I wrote about it in the comments below.有关更多详细信息,我在下面的评论中写了相关内容。

public class SparseMatrix implements SparseInterface {
public LinkedList<Index> matrix;
public int size;

public SparseMatrix() {
    matrix = new LinkedList<Index>();
    size = 5;

}

The code is a bit confusing to me.代码对我来说有点混乱。 I think you're trying to keep the state of the SparseMatrix object in the minor() method.我认为您正试图在minor()方法中保持SparseMatrix对象的状态。

I think something like this is what you would be looking for maybe?我认为这样的事情可能是您要寻找的?

The minor.determinant() is a tad bit confusing. minor.determinant()有点令人困惑。 Is there a different determinant() method in SparseMatrix ? SparseMatrix 中是否有不同的determinant()方法? You were previously returning the SparseMatrix object created in the minor method, and previously had minor(getSize(),1).determinant() .您之前返回了在 minor 方法中创建的SparseMatrix对象,并且之前有minor(getSize(),1).determinant() It looks like you're calling the determinant() method inside the SparseMatrix class, but I don't see that method in that class in your code.看起来您正在SparseMatrix类中调用determinant()方法,但我在您的代码中没有在该类中看到该方法。 Are these methods all in the SparseMatrix class?这些方法都在SparseMatrix类中吗?

I would also suggest changing the variable names of your objects to something different than the method names you are using.我还建议将对象的变量名称更改为与您使用的方法名称不同的名称。 It reduces readability of your code.它降低了代码的可读性。

Either way, I think this can get you going.不管怎样,我认为这可以让你继续前进。

private SparseMatrix minor = new SparseMatrix();

public int determinant() {
    int determinant = 0;

    if(getSize() == 2)
    {
        return (getElement(1, 1) * getElement (2, 2)) - (getElement(1,2) * getElement(2,1));
    }

    minor(getSize(),1);

    determinant += Math.pow(-1, getSize()+1) * getElement(getSize(), 1) * this.minor.determinant();

    return determinant;
}

public void minor(int row, int col) {
    //in your previous code, I'm not sure where you're getting Matrix from, 
    //so I'll leave this in here - I think you're also wanting to use the 
    //matrix that is now stored in your minor object, so I changed that in
    //the for loop, but there are likely more elegant solutions for this if
    //I saw all the code
    minor.matrix = matrix;

    for(int i = 0; i < this.minor.matrix.size(); i++)
    {
        if(row == this.minor.matrix.get(i).getRow() || col == this.minor.matrix.get(i).getCol())
        {
            this.minor.matrix.remove(i);
        }
    }

    this.minor.size--;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM