Thread: Matrix vector multiplication using function

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    10

    Matrix vector multiplication using function

    Hi everybody,
    What is wrong with the program below? it is supposed to multiply a matrix by vector.

    Code:
     
    #include <iostream>
    
    using namespace std;
    
    void matris_muller(double a[][3], double x[3],double *b);
    
    int main(){
    
    double a_cap[][3] = {1,0,0,0,2,0,0,0,3};
    double x[3] = {1,1,1};
    double *b1;
    matris_muller(a_cap , x , b1);
    
    for (int i = 0 ; i <= 2; ++i)
        cout<<"  "<<b1[i]<<endl;
    
    }
    
    void matirs_muller(double a[][3], double x[3],double *b){
    
    for(int i = 0 ; i <= 2 ; ++i){
        for(int j = 0 ; j <=  2; ++j){
    
    
            b[j] += a[i][j] * x[j];
        }
    }
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try it with
    Code:
    double b1[3] = { 0, 0, 0 };
    You need to make sure you're pointing at valid memory.

    Just declaring a variable of the same type as the parameter isn't enough.

    FWIW, all these are equivalent.
    Code:
    void foo ( int arr[3] );
    void foo ( int arr[] );
    void foo ( int *arr );
    The left-most size is ignored by the compiler, so people often omit it (as you have done above).

    2D arrays are a little more complicated.

    For example, this is equivalent to your declaration above.
    Code:
    void matris_muller(double (*a)[3], double *x, double *b);
    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.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Your code, corrected and commented (with a bit of "optimizations"):
    Code:
    #include <iostream>
    
    // It helps (in terms of optimization to declare immutable
    // arguments as 'const'. And, since the first array is incomplete,
    // I've declared all of them incomplete... They must have 3 elements.
    // Again, 'static' here is for the compiler benefict.
    static void matris_muller ( const double a[][3], const double x[], double b[] );
    
    int main()
    {
      // Just to be sure I divided the values in groups of 3.
      // Nothing wrong not doing this, but, to me, is clearer.
      //
      // And... just for optimization sake, declaring as 'static const' will make
      // the compiler put those arrays in .rodata section in compile time.
      static const double a_cap[][3] = { { 1, 0, 0 }, { 0, 2, 0 }, { 0, 0, 3 } };
      static const double x[3] = { 1, 1, 1 };
    
      double b1[3];   // FIXED: We have to allocate all 3 doubles! This time, in the stack.
    
      matris_muller ( a_cap, x, b1 );
    
      for ( int i = 0 ; i < 3; ++i )
        std::cout << b1[i] << '\n';   // std::endl is used to flush the stream.
                                      // for std::cout, '\n' will do the same, but cheaper.
    }
    
    // FIXED: The name of the function was wrong!
    void matris_muller ( const double a[][3], const double x[], double b[] )
    {
      for ( int i = 0 ; i < 3 ; ++i )
        for ( int j = 0 ; j < 3; ++j )
          b[j] += a[i][j] * x[j];
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 11-30-2011, 07:54 AM
  2. Matrix multiplication
    By caysonmars in forum C Programming
    Replies: 7
    Last Post: 06-23-2011, 08:46 PM
  3. Replies: 6
    Last Post: 05-14-2011, 09:28 AM
  4. Problem in Matrix Multiplication (Using friend function)
    By wantsree in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2011, 08:03 AM

Tags for this Thread