Matrix Multiplication

Started by
4 comments, last by jedis1000 18 years, 9 months ago
First of all, let me say that I realize that this exact same topic is listed here already, but I'm doing something a little different. (If it's not different, sorry!) Here's the deal: I'm writing a Matrix class in Java, and I've set it up so that the class holds the dimensions of the matrix in ints, and holds a 2D array of doubles for the data that the matrix could hold. I'v already implemented adding, subtracting, and scalar multiplication, but I'm a little stuck on multiplying a matrix by another matrix. I know how these calculations would work on paper, but I can't for the life of me figure out how to convert this method to code. (I don't know how to do those nifty code boxes yet, so I'll just post my stuff below.) public class Matrix { double [][] data; //Holds elements in a 2D array of doubles int width, height; //Holds dimension of array ...Ctors/methods con't below... public Matrix multiply(Matrix mult) { //As you can see, you pass in a Matrix object //And it returns a new Matrix with the answer } } I have other methods like add and subtract, which work like this: public void add(Matrix mat) { //Does the addition, and places the answer in the Matrix the func was called from } Subtract and scalar multiplication work the same as add (return-wise) Other than the hard part of multiplying matrices, the class is finished. Can anyone help? -Thanks
Advertisement
I take it since you have separate width and height variables that the matrices can be non-square? I've never had to write such a function, but I'll give it a go:

void Mult(const Matrix& m0, const Matrix& m1, Matrix& result){    int M0 = m0.height;    int N0 = m0.width;    int M1 = m1.height;    int N1 = m1.width;    if (N0 != M1)    {        // Return zero matrix, throw an error, or whatever    }    result.SetSize(M0, N1);    for (int i = 0; i < M0; ++i)    {        for (int j = 0; j < N1; ++j)        {            result(i, j) = 0;            for (int k = 0; k < N0; ++k)                result(i, j) += m0(i, k) * m1(k, j);        }    }}


Well, I gotta run, and there's probably only about a 50% chance that I got that right. But maybe it'll get you moving in the right direction.
Okay, thanks. I'll see if it works in a sec...

This is something that you've never had to do though? Maybe I should just write subclasses for things like QuadMatrix and 3x3 and things like that which ovverride the parent class multiplication method...
Does that sound like something useful/working?

Also, I just tried the method listed... with no luck. It was a nice try though, and it was able to get 1 or 2 of the elements correct each time. That was probably just by chance, though...

I don't think I'll continue working on this right now, but I'll inherit new classes from this one and write multiplication methods for same-dimension Matrix multiplicaton.

[Edited by - jedis1000 on July 18, 2005 1:04:47 PM]
Quote:Also, I just tried the method listed... with no luck. It was a nice try though, and it was able to get 1 or 2 of the elements correct each time. That was probably just by chance, though...
Hm. Now that I've had a chance to look at it, I'd up the chances of that code being correct to about 90%. Can you post your implementation? I'm curious as to why it didn't work...

That said, for many purposes you only need small (2x2, 3x3, 4x4) square matrices, in which case the mult function is simpler. You can do it with loops, or (as is often done in practice) just write it out in place.
Quote:Original post by jedis1000
This is something that you've never had to do though? Maybe I should just write subclasses for things like QuadMatrix and 3x3 and things like that which ovverride the parent class multiplication method...
Does that sound like something useful/working?


Please don't do that... you will be re-writing the same functions for no reason. Matrix multiplication is something that can be generalised for any size matrices... I checked over Jyk's code, i think its fine... just in case here is my code written in C++ which i know works...

// This function multiplies the left matrix with the right matrixinline CMatrix &CMatrix::EqualsMultiple(const CMatrix &LeftMatrix, const CMatrix &RightMatrix){	Initialise(LeftMatrix.m_iHeight, RightMatrix.m_iWidth);	int i, j, k;	for (i=0; i<m_iHeight; ++i)	{		for (j=0; j<m_iWidth; ++j)		{			for  (k=0; k<LeftMatrix.m_iWidth; ++k)			{				(*this)(i, j) += LeftMatrix(i, k) * RightMatrix(k, j);			}		}	}	return *this;}
Hmm... I' guess I'll try it again.

I must've fudged something when I converted it to Java. Sorry jyk...

double M0 = getWidth(); //Switched these... dammit.
double N0 = getHeight();
double M1 = mult.getWidth();
double N1 = mult.getHeight();
Matrix returner = null;

if(getWidth() == mult.getHeight())
{
returner = new Matrix(getHeight(), mult.getWidth());

for (int i = 0; i < M0; ++i)
{
for (int j = 0; j < N1; ++j)
{
returner.setElement(i, j, 0);
for (int k = 0; k < N0; ++k)
returner.setElement(i, j, returner.getElement(i, j) + getElement(i, k) * mult.getElement(k, j));
}
}
}
else
{
System.out.println("DIMENSION ERROR!!");
}
return returner;

This topic is closed to new replies.

Advertisement