Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 1, 2009, 05:25 PM   #1
theAdolescent
macrumors newbie
 
Join Date: Feb 2009
Adding matrices, help anyone?

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:?
theAdolescent is offline   0 Reply With Quote
Old Feb 1, 2009, 06:04 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
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
lee1210 is offline   0 Reply With Quote
Old Feb 1, 2009, 06:14 PM   #3
theAdolescent
Thread Starter
macrumors newbie
 
Join Date: Feb 2009
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.
theAdolescent is offline   0 Reply With Quote
Old Feb 1, 2009, 06:31 PM   #4
eddietr
macrumors 6502a
 
Join Date: Oct 2006
Location: Virginia
Quote:
Originally Posted by theAdolescent View Post
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
eddietr is offline   0 Reply With Quote
Old Feb 1, 2009, 06:39 PM   #5
theAdolescent
Thread Starter
macrumors newbie
 
Join Date: Feb 2009
Ok ok I know what a matrix is now.
We might have done them before? but I really don't remember.
theAdolescent is offline   0 Reply With Quote
Old Feb 1, 2009, 07:01 PM   #6
theAdolescent
Thread Starter
macrumors newbie
 
Join Date: Feb 2009
So do you guys know how I can get started off?
Or a resource that would help?
theAdolescent is offline   0 Reply With Quote
Old Feb 1, 2009, 07:04 PM   #7
eddietr
macrumors 6502a
 
Join Date: Oct 2006
Location: Virginia
Quote:
Originally Posted by theAdolescent View Post
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.
eddietr is offline   0 Reply With Quote
Old Feb 1, 2009, 07:14 PM   #8
lazydog
macrumors 6502a
 
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
Quote:
Originally Posted by theAdolescent View Post
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
lazydog is offline   0 Reply With Quote
Old Feb 1, 2009, 07:40 PM   #9
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
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:
Code:
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
lee1210 is offline   0 Reply With Quote
Old Feb 1, 2009, 08:23 PM   #10
lazydog
macrumors 6502a
 
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
Quote:
Originally Posted by lee1210 View Post
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,

Code:
 c = Matrix.multiply( a, b ) ;
 a.multiply( b ) ;

b e n
lazydog is offline   0 Reply With Quote
Old Feb 2, 2009, 03:02 AM   #11
Sander
macrumors 6502
 
Join Date: Apr 2008
... and yet, people keep complaining that the availability of operator overloading in C++ is bad and dangerous
Sander is offline   0 Reply With Quote
Old Feb 2, 2009, 08:52 AM   #12
theAdolescent
Thread Starter
macrumors newbie
 
Join Date: Feb 2009
Ok i'm at school right now but when I get home I'll post what I got so far.
Thanks for the help.
theAdolescent is offline   0 Reply With Quote
Old Feb 2, 2009, 12:37 PM   #13
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by Sander View Post
... 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
lee1210 is offline   0 Reply With Quote
Old Feb 2, 2009, 08:39 PM   #14
theAdolescent
Thread Starter
macrumors newbie
 
Join Date: Feb 2009
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.
theAdolescent is offline   0 Reply With Quote
Old Feb 2, 2009, 09:06 PM   #15
eddietr
macrumors 6502a
 
Join Date: Oct 2006
Location: Virginia
Small tip: use code tags when you post so we can read the code.

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;
            }
    }
}
Quote:
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.)
eddietr is offline   0 Reply With Quote
Old Feb 12, 2009, 06:11 PM   #16
theAdolescent
Thread Starter
macrumors newbie
 
Join Date: Feb 2009
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?
theAdolescent is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Adding a Custom Phone Label when adding or Editing contacts Chris Kavanagh iOS 7 0 Oct 20, 2013 04:28 PM
Adding Printer Itsallkosher OS X 10.8 Mountain Lion 5 Feb 5, 2013 01:08 PM
Resolved: Adding a Printer chattahoochee MacBook 10 Jan 10, 2013 08:42 AM
Adding a line Maverick713 iPhone 1 Sep 3, 2012 06:42 PM
Adding FB Account in ML TOMIMOT OS X 10.8 Mountain Lion 3 Jul 25, 2012 11:48 PM

Forum Jump

All times are GMT -5. The time now is 08:33 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC