Thread: beginner; need help with Vectors & Matrices

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    24

    beginner; need help with Vectors & Matrices

    I have to write a C program for a class; due at 9 pm tonight.
    I have absolutely no idea how to get started on this.
    I've worked with arrays and matrices to some degree but I don't have much experience.
    I'm not really sure what it is asking me to do or how to do it. Please help!

    This is the prompt:


    /*

    In this homework you will write three functions: one to multiply a
    matrix by a vector, another to compute the Euclidean norm of a
    vector, and a function to copy one vector into another. Then you
    will use these functions to compute the values of the iteration:

    x(k + 1) = A x(k), k = 0, 1, 2, ...

    where x(k) are n-dimensional vectors and A is an n-by-n matrix.

    In your program (hw8.c) write a function called mat_vec_mult whose
    prototype is:

    void
    mat_vec_mult(double A[], int m, int n,
    double x[], int nx,
    double y[], int ny);

    This function will take three arrays (A, x, y) as arguments as well
    as a number of integers. The function mat_vec_mult should then
    perform the matrix-vector product:

    y = Ax.

    If A is m-by-n then x should be an n-dimensional vector and y should
    be m-dimensional. The vectors x and y should be stored directly as
    n- and m-dimensional C arrays (not sparse!). The matrix A however
    will not be stored as a two-dimensional C array. Instead, we will
    stack up all the columns in a single one-dimensional array (note
    that double A[] is the first parameter of mat_vec_mult!) For
    example, if A is the 3-by-4 matrix

    A = [1 2 3 4
    5 6 7 8
    9 10 11 12]

    the corresponding array A is

    {1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12}.

    That is, the one-dimensional array A contains all elements of the
    matrix A stored column by column.

    In mat_vec_mult, the integers m and n correspond to the dimensions
    of the matrix A and nx and ny to the dimensions of the vectors x and
    y, respectively. The function mat_vec_mult must check if all
    dimensions are compatible and issue a warning but not interrrupt
    your program in case they are not compatible.

    As a simple example, if

    A = [1 2 3 4
    5 6 7 8
    9 10 11 12]

    x = {1, -1, 0, 1}

    then

    y = {3, 7, 11}

    Write also a function with prototype:

    double
    vec_copy(double x[], int nx, double y[], int ny);

    that checks whether nx and ny are the same and issue a warning if
    they are not, then copies the nx-dimensional vector stored in the
    array x into the nx- dimensional vector stored in the array y.

    Finally write a function with prototype:

    double
    vec_norm(double x[], int nx);

    to compute the Euclidean norm of a nx-dimensional vector stored in
    the array x.

    Now in the main() function of your program compute the iteration

    x(k + 1) = A x(k), k = 0, 1, 2, ...

    Use the function mat_vec_mult() you wrote to compute the product of
    the matrix A by the vector x(k) on each iteration. A single
    one-dimensional C array x should be used to hold the values of x(k)
    as the iteration progresses. During an interation, use other arrays
    if necessary to perform the computations and the function vec_copy()
    to transfer the results of the computation to the array x at the end
    of the iteration.

    Use the function vec_norm() to compute the Euclidean norm of x(k) at
    each iteration. If this norm falls below 1E-4 you should print a
    message indicating that and exit the program.

    You should also interrupt your program if after k reaches 100 the
    norm of x(k) is not yet below 1E-4. Print a warning indicating that
    on exit.

    Print the value of the initial vector x(0) and each subsequent x(k)
    on the screen. Print also the Euclidean norm of x(k) at each
    iteration.

    Use as data the 3-by-3 matrix

    A = [ 1 1 0
    1 -1 2
    -1 -1 0 ]

    and 3-dimensional vector

    x(0) = {1, -1, 2}

    For the above data your program should produce output that looks
    like:

    .................................................. ......................

    iacs5.ucsd.edu% ./a.out
    x(0) = {1, -1, 2}
    ||x(0)|| = 2.44949
    x(1) = {0, 6, 0}
    ||x(1)|| = 6
    x(2) = {6, -6, -6}
    ||x(2)|| = 10.3923
    x(3) = {0, 0, 0}
    ||x(3)|| = 0

    ||x(3)|| < 1e-4.
    Exiting...
    iacs5.ucsd.edu%

    .................................................. ......................

    */



    This is what I have so far:


    Code:
    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    
    void main()
    {
        mat_vec_mult(double A[], int m, int n,
                     double x[], int nx,
                     double y[], int ny);
    
    }

    Thank you so much!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sam... View Post
    I have to write a C program for a class; due at 9 pm tonight.
    I have absolutely no idea how to get started on this.
    Doh!

    Less beer-bong, more study.

    Actually...
    Quote Originally Posted by sam... View Post
    This is what I have so far:


    Code:
    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    
    void main()
    {
        mat_vec_mult(double A[], int m, int n,
                     double x[], int nx,
                     double y[], int ny);
    
    }

    Thank you so much!
    It does look like you know how to get started. Well, except for the void main part.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    Thank you. So I don't need the "void" part?
    But the prompt says that the prototype of the function called mat_vec_mult has to look like:

    Code:
        void
        mat_vec_mult(double A[], int m, int n,
                     double x[], int nx,
                     double y[], int ny);
    But what do I do next?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Quote Originally Posted by quzah View Post

    Well, except for the void main part.

    Quzah.
    Quote Originally Posted by sam... View Post
    So I don't need the "void" part?
    What this means is that you should always use int main().

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by sam...
    But what do I do next?
    I would probably write the mac_vec_mult() function next.

    No one here is going to do your homework for you.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    I'm not asking anyone to do my work for me...I just need some guidance.
    I prefer to do my own work anyway.
    I just don't understand what mat_vec_mult even is...and how it would perform the product y=Ax

    I think i understand the general logic behind it but is there an example I can look at?
    I'm just not sure what I'm supposed to be doing.

    Thanks anyway.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    mat_vec_mult() is a function you're supposed to implement so that when it's called with proper arguments it computes vector product Ax and stores the result in y.

    Assuming nx and ny are the dimensions of vectors x and y, here is a sample which should do what you need:

    Code:
    void mat_vec_mult(double A[], int m, int n, double x[], int nx, double y[], int ny) {
    
        if (n != ny) {
            printf("Dimensions of A and y do not match. Bye!");
            return;
        }
        if (m != nx) {
            printf("Dimensions of A and x do not match. Bye!");
            return;
        }
        int i;
        for (i = 0; i < ny; i++) {
            int j;
            double sum = 0.0;
            for (j = 0; j < nx; j++) {
                sum = sum + (A[(j*m)+i]*x[j]);
            }
            y[i] = sum;
        }
    }
    What it does is it goes through the numbers in matrix A and computes the elements of vector y.

    Now you should implement the other 2 functions in similar way. It actually contains very little programming, biggest part is knowing the math.
    Last edited by Yawney; 11-23-2010 at 05:53 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your first post explains every single function you need to make. Why is it so hard for you to figure out? You've got a paragraph describing what each function is supposed to do.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    1
    i have the some homework and i dont understand the idea of using the vec_copy function.
    what is the purpose of copying the array_x into array_y?

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    Thank you so much! I figured out how to write the 3 functions...now I'm just having trouble with the main() part. Please let me know if you have any suggestions; meanwhile, I'll keep working on it.

    If I understand correctly, mat_vec_mult is the one that performs the matrix-vector product (y=Ax), vec_copy is the one that copies the vector (that initially stored in x) to the vector stored in y, and vec_norm is of course the one that computes the Euc. norm of the vector stored in x. we have to remember that "x" and "y" are referring to arrays. So basically, this homework just combines what we learned about arrays and functions. Let me know if I'm horribly confused about this (which is a possibility).

    I'm still having trouble with main() though...but the rest of it is beginning to make sense. Thanks again!

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This is the second time you've played the "9PM TONIGHT" card

    My suggestion is
    a) show up a lot sooner
    b) show up with more effort.

    With both of these things, there will be time to explain all the bits you're really stuck on.

    We're not here to give you a complete answer at short notice because you can't be bothered to plan ahead and put some effort in.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    Just because I asked at the last minute doesn't mean I didn't have work done beforehand.
    I was simply asking for suggestions and clarifications; I didn't want to post my work online because of the potential copying that could occur.
    I am not expecting anyone to do my work, which is why I'm asking for help/suggestions, NOT direct answers.
    I apologize if it seemed differently.
    And FYI, I turned in the assignment on time both times with the HELPFUL suggestions that I was given.
    Thank you for your considerate (however misguided) comment in any case.

    Everyone: thank you for the help.
    Last edited by sam...; 11-24-2010 at 12:09 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 09:20 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM