View Full Version : Adding matrices, help anyone?

theAdolescent

Feb 1, 2009, 05:25 PM

I have an assignment that states:

2. When you are adding two matrices together, you add similar elements together and place them in a new matrix. Because of this, you need to have matrices with identical sizes. Create a Matrix class, with methods such as add(Matrix a), subtract(Matrix a) and multiplyScalar(double n).

I have no idea where to start, would anyone be able to help:?

lee1210

Feb 1, 2009, 06:04 PM

By assignment, do you mean for school? We need to know so we don't do homework for you.

What language are you using?

Have you created a class before?

Do you know what a matrix is in mathematical terms? Is this of integers? Real numbers? Do you know what primitive to use for each element?

Are the functions supposed to return a new matrix, or modify the matrix they are called on?

Let us know and we'll see what we can do?

-Lee

theAdolescent

Feb 1, 2009, 06:14 PM

Yes it is for school.

It's in Java.

I could use a refreshing on what a matrix is and yes I've created a class before.

I don't know if it's supposed to return new or modify the matrix. What I posted is all I got.

eddietr

Feb 1, 2009, 06:31 PM

Yes it is for school.

It's in Java.

I could use a refreshing on what a matrix is and yes I've created a class before.

I don't know if it's supposed to return new or modify the matrix. What I posted is all I got.

I would think you would want to return a new matrix. Might want to confirm that with your instructor, but if you want to get started I would assume a new matrix.

So was there an earlier assignment to create a matrix class? It seems odd that he/she/they would have you build add and subtract methods without first covering how to model a matrix in a class.

If I were doing the assignment, I would also assume each element is as integer for now. Once you have that working, you can always go back and generalize it. I always like to start simple with some basic assumptions and then build from there.

Here is what you need to know about matrix addition and subtraction summarized in one page:

http://www.purplemath.com/modules/mtrxadd.htm

theAdolescent

Feb 1, 2009, 06:39 PM

Ok ok I know what a matrix is now.

We might have done them before? but I really don't remember.

theAdolescent

Feb 1, 2009, 07:01 PM

So do you guys know how I can get started off?

Or a resource that would help?

eddietr

Feb 1, 2009, 07:04 PM

Ok ok I know what a matrix is now.

We might have done them before? but I really don't remember.

Well, I would take the time to go through your notes and past assignments and see if you did cover how to model a matrix in a class. Maybe in your textbook or lecture notes? I doubt this assignment would be given without some background in that.

If not, then you need to start with a basic matrix class (java class, I mean) before you even bother with addition and multiplication.

Once you work out a basic class that can store a matrix, post it up here and I'm sure then people can help you make it better. (or help you make it work if for some reason it doesn't work for you.)

Just think about ways you can store integers where you have m rows of them and n columns.

lazydog

Feb 1, 2009, 07:14 PM

Yes it is for school.

It's in Java.

I could use a refreshing on what a matrix is and yes I've created a class before.

I don't know if it's supposed to return new or modify the matrix. What I posted is all I got.

Hi there are plenty of sites which explain matrices better than I can, but at the risk of confusing you:-

A matrix is basically a 2-dimensional array of numbers characterised by its numbers of rows and columns. You can also think of a matrix as a list of either row vectors or column vectors. For example, the matrix

0 1 2 3

4 5 6 7

8 9 0 1

can be thought of as the list of row vectors

[ [0 1 2 3], [4 5 6 7], [8 9 0 1] ]

or the list of column vectors

[ [0 4 8], [1 5 9], [2 6 0], [3 7 1] ]

When thinking through operations between matrices you might find the row/column interpretation easier to follow. For example, to multiply two matricies C = A*B, you multiple the row vectors of A by the column vectors of B. This puts the condition that the number of columns in A must equal the number of rows in B, and the resulting matrix C will have the property of the number of rows equal to the number of rows in A, and the number of columns will equal the number of columns of B.

When you come to implement your Matrix class though, it will be far easier to store the matrix as a 2-dimensional array of floats, rather than an array of row or column vectors. Also you'll need to save the dimensions of the matrix in your Matrix class. That way you will be able to check that the number of columns and rows in the operands make sense for the particular operation you are implementing, eg as in multiplication above.

My vote would be to have multiply, add etc return new matrices. This makes it far easier to write expressions which involve the same matrix more than once, eg if you wanted to calculate D = C *( C + B ).

b e n

lee1210

Feb 1, 2009, 07:40 PM

People have been pretty clear on what you have in front of you. Just to make something clear, the way i read it, you just have to multiply every element of the matrix by the scaler, keeping them in the same positions. I just looked again, and it looks like multiplyScaler takes a double, so i guess the assumption is that the matrix should be of doubles.

So essentially, i would represent the matrix as:

double myMatrix[][];

I don't know which constructors you'd like to build... maybe just accept such an array? If you're going to allow a constructor that just takes the dimensions, then you can just assign new double[m][n]; to myMatrix.

Your operations just become nested loops, either adding/subtracting the elements at the same positions, or multiplying each element by the scaler in the case of your multiply.

I guess returning a new Object makes sense... but you should verify this, because i could imagine both ways working. Since there's no operator overloading, there's not a lot of expressions you can build with objects that will "look" like standard mathematical expressions, so returning a new object isn't that big of a boon.

-Lee

lazydog

Feb 1, 2009, 08:23 PM

I guess returning a new Object makes sense... but you should verify this, because i could imagine both ways working. Since there's no operator overloading, there's not a lot of expressions you can build with objects that will "look" like standard mathematical expressions, so returning a new object isn't that big of a boon.

-Lee

One way to resolve these types of situations is to adopt a convetion that implements methods that return new objects as class/static methods , and operations that operate on the object as instance methods. So for example,

c = Matrix.multiply( a, b ) ;

a.multiply( b ) ;

b e n

Sander

Feb 2, 2009, 03:02 AM

... and yet, people keep complaining that the availability of operator overloading in C++ is bad and dangerous :)

theAdolescent

Feb 2, 2009, 08:52 AM

Ok i'm at school right now but when I get home I'll post what I got so far.

Thanks for the help.

lee1210

Feb 2, 2009, 12:37 PM

... and yet, people keep complaining that the availability of operator overloading in C++ is bad and dangerous :)

I don't curse the availability, but it can be a bit misleading and lead beginners astray. My issue is a simple looking postfix increment can have order N complexity, instantiate a new object, and transmit GPS coordinates to the KGB. Once you are familiar with the concept it is fine to seek API docs, look at the definition if it's available, etc, but this can lead to confusion for a beginner.

I guess I like the idea of operators for primitives and explicit method calls for objects, but that's just personal preference.

-Lee

theAdolescent

Feb 2, 2009, 08:39 PM

import java.util.Scanner;

public class tenpointtwo {

public static void main(String[] args) {

String input;

Scanner in = new Scanner(System.in);

final int ROWS = 3;

final int COLUMNS = 3;

String[][] myMatx = new String[ROWS][COLUMNS];

for (int r = 0; r < ROWS; r++)

for (int c = 0; c < COLUMNS; c++) {

input = in.next();

myMatx[r][c] = input;

}

}

}

Ok, I've got the class done. Seems like I've got to add another matrix to it now. I really have no clue how to do that; I think the teacher taught us how the first day of class but I was sick. I could ask him tomorrow but I really wanna see if I can get this thing done tonight since I've still got 2 other programs for this chaper to do! I'm going to check the book and other sites but if someone could respond that would be super sweet. :)

eddietr

Feb 2, 2009, 09:06 PM

Small tip: use code tags when you post so we can read the code. :)

import java.util.Scanner;

public class tenpointtwo {

public static void main(String[] args) {

String input;

Scanner in = new Scanner(System.in);

final int ROWS = 3;

final int COLUMNS = 3;

String[][] myMatx = new String[ROWS][COLUMNS];

for (int r = 0; r < ROWS; r++)

for (int c = 0; c < COLUMNS; c++) {

input = in.next();

myMatx[r][c] = input;

}

}

}

Ok, I've got the class done.

Actually, you haven't made a matrix class yet.

You've made a class with a main() that you can use to run your program. But the next step is to make a Matrix class that stores the matrix. Right now you're just storing your matrix in a 2D array.

(Your Matrix class might be little more than a wrapper around a 2D array if you choose to do it that way.)

theAdolescent

Feb 12, 2009, 06:11 PM

Ok sorry it took so long to respond again I had some stuff going on.

So how would I store it in a matrix class?