Adding matrices, help anyone?

Discussion in 'Mac Programming' started by theAdolescent, Feb 1, 2009.

  1. macrumors newbie

    Joined:
    Feb 1, 2009
    #1
    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:?
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. thread starter macrumors newbie

    Joined:
    Feb 1, 2009
    #3
    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.
     
  4. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #4
    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
     
  5. thread starter macrumors newbie

    Joined:
    Feb 1, 2009
    #5
    Ok ok I know what a matrix is now.
    We might have done them before? but I really don't remember.
     
  6. thread starter macrumors newbie

    Joined:
    Feb 1, 2009
    #6
    So do you guys know how I can get started off?
    Or a resource that would help?
     
  7. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #7
    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.
     
  8. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #8
    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
     
  9. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    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
     
  10. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #10
    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
     
  11. macrumors 6502

    Joined:
    Apr 24, 2008
    #11
    ... and yet, people keep complaining that the availability of operator overloading in C++ is bad and dangerous :)
     
  12. thread starter macrumors newbie

    Joined:
    Feb 1, 2009
    #12
    Ok i'm at school right now but when I get home I'll post what I got so far.
    Thanks for the help.
     
  13. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #13
    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
     
  14. thread starter macrumors newbie

    Joined:
    Feb 1, 2009
    #14
    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. :)
     
  15. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #15
    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;
                }
        }
    }
    
    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.)
     
  16. thread starter macrumors newbie

    Joined:
    Feb 1, 2009
    #16
    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?
     

Share This Page