Matrix在Java中返回错误的值(Matrix returning wrong values in Java)

编程入门 行业动态 更新时间:2024-10-24 12:22:46
Matrix在Java中返回错误的值(Matrix returning wrong values in Java)

我试图在java中创建一个方法,它使用整数作为参数将两个矩阵相乘,以告诉矩阵在哪里工作,并递归地将矩阵“切片”到较小的部分,并使用所谓的Divide and Conquer从这里将它们相乘(这样做是学校的任务,尽管它确实是一种难以将两个矩阵相乘的方法!)。 不出所料,在我的代码运行完毕后,matrice没有显示出我的期望。 在我的主要方法中,我有3个大小为n * n(n = 2,4,8,16 ......)的矩阵,因此它们是正方形。 我将矩阵A和B乘以矩阵D.我的主要方法是矩阵类。 这就是我的代码现在的样子:

public class indexMult { matrix mat = new matrix(); public void calc(int Ay, int Ax, int By, int Bx, int Dy, int Dx, int size){ System.out.println(Ay +" "+ Ax +" "+ By +" "+ Bx +" "+ Dy +" "+ Dx +" "+ size); if(size==2){ mat.D[Dy][Dx] = mat.A[Ay][Ax] * mat.B[By][Bx] + mat.A[Ay][Ax+1] * mat.B[By+1][Bx]; mat.D[Dy+1][Dx] = mat.A[Ay+1][Ax] * mat.B[By][Bx] + mat.A[Ay+1][Ax+1] * mat.B[By+1][Bx]; mat.D[Dy][Dx+1] = mat.A[Ay][Ax] * mat.B[By][Bx] + mat.A[Ay][Ax+1] * mat.B[By+1][Bx+1]; mat.D[Dy+1][Dx+1] = mat.A[Ay+1][Ax] * mat.B[By][Bx+1] + mat.A[Ay+1][Ax+1] * mat.B[By+1][Bx]; }else{ size = size/2; for(int i=0; i<size; i++){ for(int j=0; j<size; j++){ calc(i, j, i, j, i, j, size); calc(i, size+j, i, size+j, i, size+j, size); calc(size+i, j, size+i, j, size+i, j, size); calc(size+i, size+j, size+i, size+j, size+i, size+j, size); } } } }

}

该方法直接改变矩阵D. 为了更容易,我在矩阵A和B中的每个位置使用2个。我制定代码的方式也存在问题,因为结果不对 - 但我还没有解决这个问题。 我在这一点上的问题是,无论我设置什么,D只能填充5x5。 如果我使用n作为4或2,我得到一个超出界限的数组索引(因为我显然试图用5x5数据填充4x4矩阵)。 但那不是我想要的代码! 我的代码背后的想法是将n * n(int size)中的n除以2,并从此处对矩阵的每四分之一进行递归调用,直到大小为2x2并且我可以解决该部分。 我可能缺少将四分之一矩阵加在一起的一块,但我还没那么远 - 我在填充整个矩阵时遇到了麻烦,因为它在5x5之后停止了。 有人能够发现错误??

编辑:

在回答下面的帮助下,以及我自己的一些补救措施,这种改变似乎有效 - 不仅仅是因为我的问题,而是实际计算我需要的东西! :))

calc(Ay, Ax, By, Bx, Dy, Dx, size); calc(Ay, Ax+size, By+size, Bx, Dy, Dx, size); calc(Ay, Ax, By, Bx+size, Dy, Dx+size, size); calc(Ay, Ax+size, By+size, Bx+size, Dy, Dx+size, size); calc(Ay+size, Ax, By, Bx, Dy+size, Dx, size); calc(Ay+size, Ax+size, By+size, Bx, Dy+size, Dx, size); calc(Ay+size, Ax, By, Bx+size, Dy+size, Dx+size, size); calc(Ay+size, Ax+size, By+size, Bx+size, Dy+size, Dx+size, size);

Im trying to make a method in java that multiplies two matrices using integers as arguments to tell where in the matrices Im working, and recursively "slice up" the matrices to smaller parts and multiply them together from here using the so called Divide and Conquer (it is a school task to do so, allthough it sure is a hard way to multiply two matrices!). Unsurprisingly, after my code have finished running, the matrice does not show what I would expect. In my main method, I have 3 matrices of size n*n (n=2, 4, 8, 16...), so they are squares. Im multiplying matrice A and B into matrice D. My main method is inside matrix class. This is how my code looks now:

public class indexMult { matrix mat = new matrix(); public void calc(int Ay, int Ax, int By, int Bx, int Dy, int Dx, int size){ System.out.println(Ay +" "+ Ax +" "+ By +" "+ Bx +" "+ Dy +" "+ Dx +" "+ size); if(size==2){ mat.D[Dy][Dx] = mat.A[Ay][Ax] * mat.B[By][Bx] + mat.A[Ay][Ax+1] * mat.B[By+1][Bx]; mat.D[Dy+1][Dx] = mat.A[Ay+1][Ax] * mat.B[By][Bx] + mat.A[Ay+1][Ax+1] * mat.B[By+1][Bx]; mat.D[Dy][Dx+1] = mat.A[Ay][Ax] * mat.B[By][Bx] + mat.A[Ay][Ax+1] * mat.B[By+1][Bx+1]; mat.D[Dy+1][Dx+1] = mat.A[Ay+1][Ax] * mat.B[By][Bx+1] + mat.A[Ay+1][Ax+1] * mat.B[By+1][Bx]; }else{ size = size/2; for(int i=0; i<size; i++){ for(int j=0; j<size; j++){ calc(i, j, i, j, i, j, size); calc(i, size+j, i, size+j, i, size+j, size); calc(size+i, j, size+i, j, size+i, j, size); calc(size+i, size+j, size+i, size+j, size+i, size+j, size); } } } }

}

This method changes matrice D directly. To make it easier, Im using 2 in every spot in matrice A and B. There is also an issue in the way I have formulated my code, as the results are not right - but I have not come to this issue yet. My issue at this point is that no matter what I set n to, D only fills 5x5. If I use n as 4 or 2, I get an Array Index Out of Bounds Exception (because Im obviously trying to fill a 4x4 matrice with 5x5 data). But thats not what I want my code to do! The idea behind my code is to divide the n in n*n (int size) by 2, and from here do a recursive call with each quarter of the matrice until the size is 2x2 and I can solve that part. Im probably lacking a piece to add together the quarter matrices, but Im not that far yet - Im having trouble filling the whole matrice as it stops after 5x5. Are anyone able to spot the error??

EDIT:

With help from answer bellow, and some fixies myself, this change seems to work - not only for my issue, but to actually calculate what I need! :))

calc(Ay, Ax, By, Bx, Dy, Dx, size); calc(Ay, Ax+size, By+size, Bx, Dy, Dx, size); calc(Ay, Ax, By, Bx+size, Dy, Dx+size, size); calc(Ay, Ax+size, By+size, Bx+size, Dy, Dx+size, size); calc(Ay+size, Ax, By, Bx, Dy+size, Dx, size); calc(Ay+size, Ax+size, By+size, Bx, Dy+size, Dx, size); calc(Ay+size, Ax, By, Bx+size, Dy+size, Dx+size, size); calc(Ay+size, Ax+size, By+size, Bx+size, Dy+size, Dx+size, size);

最满意答案

沿着这些方向可能会有所作为:

public class indexMult { matrix mat = new matrix(); public void calc(int Ay, int Ax, int By, int Bx, int Dy, int Dx, int size){ System.out.println(Ay +" "+ Ax +" "+ By +" "+ Bx +" "+ Dy +" "+ Dx +" "+ size); if(size==2){ mat.D[Dy][Dx] = mat.A[Ay][Ax] * mat.B[By][Bx] + mat.A[Ay][Ax+1] * mat.B[By+1][Bx]; mat.D[Dy+1][Dx] = mat.A[Ay+1][Ax] * mat.B[By][Bx] + mat.A[Ay+1][Ax+1] * mat.B[By+1][Bx]; mat.D[Dy][Dx+1] = mat.A[Ay][Ax] * mat.B[By][Bx] + mat.A[Ay][Ax+1] * mat.B[By+1][Bx+1]; mat.D[Dy+1][Dx+1] = mat.A[Ay+1][Ax] * mat.B[By][Bx+1] + mat.A[Ay+1][Ax+1] * mat.B[By+1][Bx]; }else{ size = size/2; calc(Ay, Ax, By, Bx, Dy, Dx, size); calc(Ay, Ax+size, By, Bx+size, Dy, Dx+size, size); calc(Ay+size, Ax, By+size, Bx, Dy+size, Dx, size); calc(Ay+size, Ax+size, By+size, Bx+size, Dy+size, Dx+size, size); } }

我刚刚用递归替换了循环,只是简单地递归。

Probably something along these lines would work:

public class indexMult { matrix mat = new matrix(); public void calc(int Ay, int Ax, int By, int Bx, int Dy, int Dx, int size){ System.out.println(Ay +" "+ Ax +" "+ By +" "+ Bx +" "+ Dy +" "+ Dx +" "+ size); if(size==2){ mat.D[Dy][Dx] = mat.A[Ay][Ax] * mat.B[By][Bx] + mat.A[Ay][Ax+1] * mat.B[By+1][Bx]; mat.D[Dy+1][Dx] = mat.A[Ay+1][Ax] * mat.B[By][Bx] + mat.A[Ay+1][Ax+1] * mat.B[By+1][Bx]; mat.D[Dy][Dx+1] = mat.A[Ay][Ax] * mat.B[By][Bx] + mat.A[Ay][Ax+1] * mat.B[By+1][Bx+1]; mat.D[Dy+1][Dx+1] = mat.A[Ay+1][Ax] * mat.B[By][Bx+1] + mat.A[Ay+1][Ax+1] * mat.B[By+1][Bx]; }else{ size = size/2; calc(Ay, Ax, By, Bx, Dy, Dx, size); calc(Ay, Ax+size, By, Bx+size, Dy, Dx+size, size); calc(Ay+size, Ax, By+size, Bx, Dy+size, Dx, size); calc(Ay+size, Ax+size, By+size, Bx+size, Dy+size, Dx+size, size); } }

I have just replaced the loop with recursion to simply just recursion.

更多推荐

本文发布于:2023-08-05 00:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424271.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Java   Matrix   values   wrong

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!