[英]Recursive method not reaching the base case, however, I'm not sure why. Java
这是一个奇怪的项目,所以没有非常奇怪的代码。 我遇到的真正问题是为什么这种递归方法永远不会达到基本情况。
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;
}
因此,行列式调用minor,它返回一个大小为1 的新方阵。 我从一个 5x5 矩阵开始,并在其上调用行列式方法。 然后,该方法采用 5x5 矩阵(4x4 矩阵)的次要行列式。 这一直持续到我们到达基本情况,其中它是一个 2x2 矩阵并且计算行列式是微不足道的。
然而,问题在于代码永远不会超过它是一个 4x4 矩阵的点。 每次调用该次要时(在每次递归调用时都应该这样做),由于次要中的最后一行代码,minor.size--;,它应该输出更小的矩阵。
通过一些错误测试,似乎矩阵的大小在第一次递减,但在随后的递归调用中不会递减。 我对 Java 有点陌生,Java 是否对方法调用中的方法调用有一些奇怪的规则,导致它没有按照应有的方式递减? 除此之外,我一无所获。
提前致谢。
对于那些问:
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 只是 SparseMatrix 类的一个数据成员,它表示我正在建模的奇怪的不存在矩阵是 SizexSize。 有关更多详细信息,我在下面的评论中写了相关内容。
public class SparseMatrix implements SparseInterface {
public LinkedList<Index> matrix;
public int size;
public SparseMatrix() {
matrix = new LinkedList<Index>();
size = 5;
}
代码对我来说有点混乱。 我认为您正试图在minor()
方法中保持SparseMatrix对象的状态。
我认为这样的事情可能是您要寻找的?
minor.determinant()
有点令人困惑。 SparseMatrix 中是否有不同的determinant()
方法? 您之前返回了在 minor 方法中创建的SparseMatrix对象,并且之前有minor(getSize(),1).determinant()
。 看起来您正在SparseMatrix类中调用determinant()
方法,但我在您的代码中没有在该类中看到该方法。 这些方法都在SparseMatrix类中吗?
我还建议将对象的变量名称更改为与您使用的方法名称不同的名称。 它降低了代码的可读性。
不管怎样,我认为这可以让你继续前进。
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.